Avi Drissman | 4a8573c | 2022-09-09 19:35:54 | [diff] [blame] | 1 | // Copyright 2013 The Chromium Authors |
[email protected] | b96aa93 | 2009-08-12 21:34:49 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | e7f7987 | 2013-09-13 01:39:31 | [diff] [blame] | 5 | #include "chrome/browser/shell_integration_linux.h" |
[email protected] | b96aa93 | 2009-08-12 21:34:49 | [diff] [blame] | 6 | |
avi | 664c07b | 2015-12-26 02:18:31 | [diff] [blame] | 7 | #include <stddef.h> |
| 8 | |
[email protected] | 111f028 | 2013-08-05 10:09:29 | [diff] [blame] | 9 | #include <algorithm> |
[email protected] | 07753f0f | 2013-02-05 07:52:59 | [diff] [blame] | 10 | #include <cstdlib> |
[email protected] | af71d64 | 2010-03-12 10:29:04 | [diff] [blame] | 11 | #include <map> |
thestig | d0b66234 | 2015-01-16 08:21:19 | [diff] [blame] | 12 | #include <vector> |
[email protected] | af71d64 | 2010-03-12 10:29:04 | [diff] [blame] | 13 | |
[email protected] | 14fbaed | 2013-05-02 07:54:02 | [diff] [blame] | 14 | #include "base/base_paths.h" |
[email protected] | 2164e51 | 2014-01-22 09:32:10 | [diff] [blame] | 15 | #include "base/command_line.h" |
Jan Wilken Dörrie | bf97d44 | 2020-12-09 19:01:58 | [diff] [blame] | 16 | #include "base/containers/contains.h" |
[email protected] | e7f7987 | 2013-09-13 01:39:31 | [diff] [blame] | 17 | #include "base/environment.h" |
[email protected] | 5799981 | 2013-02-24 05:40:52 | [diff] [blame] | 18 | #include "base/files/file_path.h" |
thestig | 18dfb7a5 | 2014-08-26 10:44:04 | [diff] [blame] | 19 | #include "base/files/file_util.h" |
[email protected] | ea1a3f6 | 2012-11-16 20:34:23 | [diff] [blame] | 20 | #include "base/files/scoped_temp_dir.h" |
Peter Kasting | 9eb10b0 | 2021-04-29 03:10:48 | [diff] [blame] | 21 | #include "base/strings/string_piece.h" |
[email protected] | 24a555b6 | 2013-06-10 22:01:17 | [diff] [blame] | 22 | #include "base/strings/string_util.h" |
[email protected] | e309f31 | 2013-06-07 21:50:08 | [diff] [blame] | 23 | #include "base/strings/utf_string_conversions.h" |
[email protected] | 14fbaed | 2013-05-02 07:54:02 | [diff] [blame] | 24 | #include "base/test/scoped_path_override.h" |
Nico Weber | eaa0841 | 2019-08-14 01:24:37 | [diff] [blame] | 25 | #include "build/branding_buildflags.h" |
Daniel Murphy | 2ddeccc4 | 2022-02-17 02:14:22 | [diff] [blame] | 26 | #include "chrome/browser/web_applications/os_integration/web_app_shortcut.h" |
Song Fangzhen | 49468b2 | 2021-09-02 15:41:59 | [diff] [blame] | 27 | #include "chrome/browser/web_applications/web_app_helpers.h" |
[email protected] | 4289680 | 2009-08-28 23:39:44 | [diff] [blame] | 28 | #include "chrome/common/chrome_constants.h" |
Robert Woods | 954a218 | 2020-03-20 06:08:27 | [diff] [blame] | 29 | #include "components/services/app_service/public/cpp/file_handler.h" |
Glenn Hartmann | 37987f5 | 2023-09-26 21:06:17 | [diff] [blame] | 30 | #include "components/webapps/common/web_app_id.h" |
Gabriel Charette | c710874 | 2019-08-23 03:31:40 | [diff] [blame] | 31 | #include "content/public/test/browser_task_environment.h" |
[email protected] | 111f028 | 2013-08-05 10:09:29 | [diff] [blame] | 32 | #include "testing/gmock/include/gmock/gmock.h" |
[email protected] | b96aa93 | 2009-08-12 21:34:49 | [diff] [blame] | 33 | #include "testing/gtest/include/gtest/gtest.h" |
Alexander Dunaev | 66b50ba | 2021-08-10 04:11:46 | [diff] [blame] | 34 | #include "ui/ozone/public/ozone_platform.h" |
Eric Willigers | 05e0865 | 2022-02-08 01:59:09 | [diff] [blame] | 35 | #include "url/gurl.h" |
Alexander Dunaev | 66b50ba | 2021-08-10 04:11:46 | [diff] [blame] | 36 | |
[email protected] | 111f028 | 2013-08-05 10:09:29 | [diff] [blame] | 37 | using ::testing::ElementsAre; |
[email protected] | 631bb74 | 2011-11-02 11:29:39 | [diff] [blame] | 38 | |
[email protected] | 06bfeb1 | 2014-05-27 14:00:09 | [diff] [blame] | 39 | namespace shell_integration_linux { |
| 40 | |
[email protected] | af71d64 | 2010-03-12 10:29:04 | [diff] [blame] | 41 | namespace { |
| 42 | |
| 43 | // Provides mock environment variables values based on a stored map. |
[email protected] | 76b90d31 | 2010-08-03 03:00:50 | [diff] [blame] | 44 | class MockEnvironment : public base::Environment { |
[email protected] | af71d64 | 2010-03-12 10:29:04 | [diff] [blame] | 45 | public: |
[email protected] | 76b90d31 | 2010-08-03 03:00:50 | [diff] [blame] | 46 | MockEnvironment() {} |
[email protected] | af71d64 | 2010-03-12 10:29:04 | [diff] [blame] | 47 | |
Peter Boström | fadb175 | 2021-09-30 19:17:01 | [diff] [blame] | 48 | MockEnvironment(const MockEnvironment&) = delete; |
| 49 | MockEnvironment& operator=(const MockEnvironment&) = delete; |
| 50 | |
thestig | 0c412e85 | 2016-06-30 08:04:40 | [diff] [blame] | 51 | void Set(base::StringPiece name, const std::string& value) { |
Peter Kasting | 9eb10b0 | 2021-04-29 03:10:48 | [diff] [blame] | 52 | variables_[std::string(name)] = value; |
[email protected] | af71d64 | 2010-03-12 10:29:04 | [diff] [blame] | 53 | } |
| 54 | |
thestig | 0c412e85 | 2016-06-30 08:04:40 | [diff] [blame] | 55 | bool GetVar(base::StringPiece variable_name, std::string* result) override { |
Peter Kasting | 9eb10b0 | 2021-04-29 03:10:48 | [diff] [blame] | 56 | if (base::Contains(variables_, std::string(variable_name))) { |
| 57 | *result = variables_[std::string(variable_name)]; |
[email protected] | af71d64 | 2010-03-12 10:29:04 | [diff] [blame] | 58 | return true; |
| 59 | } |
| 60 | |
| 61 | return false; |
| 62 | } |
| 63 | |
thestig | 0c412e85 | 2016-06-30 08:04:40 | [diff] [blame] | 64 | bool SetVar(base::StringPiece variable_name, |
dcheng | e1bc798 | 2014-10-30 00:32:40 | [diff] [blame] | 65 | const std::string& new_value) override { |
[email protected] | fc586c7 | 2010-07-31 16:55:40 | [diff] [blame] | 66 | ADD_FAILURE(); |
| 67 | return false; |
| 68 | } |
| 69 | |
thestig | 0c412e85 | 2016-06-30 08:04:40 | [diff] [blame] | 70 | bool UnSetVar(base::StringPiece variable_name) override { |
[email protected] | fc586c7 | 2010-07-31 16:55:40 | [diff] [blame] | 71 | ADD_FAILURE(); |
[email protected] | e9032c6 | 2010-07-16 03:34:25 | [diff] [blame] | 72 | return false; |
[email protected] | ac7264c | 2010-07-08 13:32:51 | [diff] [blame] | 73 | } |
| 74 | |
[email protected] | af71d64 | 2010-03-12 10:29:04 | [diff] [blame] | 75 | private: |
| 76 | std::map<std::string, std::string> variables_; |
[email protected] | af71d64 | 2010-03-12 10:29:04 | [diff] [blame] | 77 | }; |
| 78 | |
[email protected] | af71d64 | 2010-03-12 10:29:04 | [diff] [blame] | 79 | } // namespace |
| 80 | |
[email protected] | d81a63c0 | 2013-03-07 08:49:04 | [diff] [blame] | 81 | TEST(ShellIntegrationTest, GetExistingShortcutContents) { |
| 82 | const char kTemplateFilename[] = "shortcut-test.desktop"; |
| 83 | base::FilePath kTemplateFilepath(kTemplateFilename); |
[email protected] | af71d64 | 2010-03-12 10:29:04 | [diff] [blame] | 84 | const char kTestData1[] = "a magical testing string"; |
| 85 | const char kTestData2[] = "a different testing string"; |
| 86 | |
Gabriel Charette | 798fde7 | 2019-08-20 22:24:04 | [diff] [blame] | 87 | content::BrowserTaskEnvironment task_environment; |
[email protected] | af71d64 | 2010-03-12 10:29:04 | [diff] [blame] | 88 | |
[email protected] | 58bf925 | 2013-03-06 04:12:36 | [diff] [blame] | 89 | // Test that it searches $XDG_DATA_HOME/applications. |
[email protected] | af71d64 | 2010-03-12 10:29:04 | [diff] [blame] | 90 | { |
[email protected] | ea1a3f6 | 2012-11-16 20:34:23 | [diff] [blame] | 91 | base::ScopedTempDir temp_dir; |
[email protected] | af71d64 | 2010-03-12 10:29:04 | [diff] [blame] | 92 | ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 93 | |
[email protected] | 76b90d31 | 2010-08-03 03:00:50 | [diff] [blame] | 94 | MockEnvironment env; |
vabr | 8c498ea4 | 2016-09-15 12:41:58 | [diff] [blame] | 95 | env.Set("XDG_DATA_HOME", temp_dir.GetPath().value()); |
[email protected] | 58bf925 | 2013-03-06 04:12:36 | [diff] [blame] | 96 | // Create a file in a non-applications directory. This should be ignored. |
Lei Zhang | eebbef6 | 2020-05-05 20:16:05 | [diff] [blame] | 97 | ASSERT_TRUE(base::WriteFile(temp_dir.GetPath().Append(kTemplateFilename), |
| 98 | kTestData2)); |
vabr | 8c498ea4 | 2016-09-15 12:41:58 | [diff] [blame] | 99 | ASSERT_TRUE( |
| 100 | base::CreateDirectory(temp_dir.GetPath().Append("applications"))); |
Lei Zhang | eebbef6 | 2020-05-05 20:16:05 | [diff] [blame] | 101 | ASSERT_TRUE(base::WriteFile( |
vabr | 8c498ea4 | 2016-09-15 12:41:58 | [diff] [blame] | 102 | temp_dir.GetPath().Append("applications").Append(kTemplateFilename), |
thestig | d0b66234 | 2015-01-16 08:21:19 | [diff] [blame] | 103 | kTestData1)); |
[email protected] | af71d64 | 2010-03-12 10:29:04 | [diff] [blame] | 104 | std::string contents; |
[email protected] | d81a63c0 | 2013-03-07 08:49:04 | [diff] [blame] | 105 | ASSERT_TRUE( |
[email protected] | 06bfeb1 | 2014-05-27 14:00:09 | [diff] [blame] | 106 | GetExistingShortcutContents(&env, kTemplateFilepath, &contents)); |
[email protected] | af71d64 | 2010-03-12 10:29:04 | [diff] [blame] | 107 | EXPECT_EQ(kTestData1, contents); |
| 108 | } |
| 109 | |
[email protected] | 58bf925 | 2013-03-06 04:12:36 | [diff] [blame] | 110 | // Test that it falls back to $HOME/.local/share/applications. |
| 111 | { |
| 112 | base::ScopedTempDir temp_dir; |
| 113 | ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 114 | |
| 115 | MockEnvironment env; |
vabr | 8c498ea4 | 2016-09-15 12:41:58 | [diff] [blame] | 116 | base::ScopedPathOverride home_override(base::DIR_HOME, temp_dir.GetPath(), |
thestig | d2b1fcf | 2015-01-21 22:11:49 | [diff] [blame] | 117 | true /* absolute? */, |
| 118 | false /* create? */); |
[email protected] | 426d1c9 | 2013-12-03 20:08:54 | [diff] [blame] | 119 | ASSERT_TRUE(base::CreateDirectory( |
vabr | 8c498ea4 | 2016-09-15 12:41:58 | [diff] [blame] | 120 | temp_dir.GetPath().Append(".local/share/applications"))); |
Lei Zhang | eebbef6 | 2020-05-05 20:16:05 | [diff] [blame] | 121 | ASSERT_TRUE(base::WriteFile(temp_dir.GetPath() |
| 122 | .Append(".local/share/applications") |
| 123 | .Append(kTemplateFilename), |
| 124 | kTestData1)); |
[email protected] | 58bf925 | 2013-03-06 04:12:36 | [diff] [blame] | 125 | std::string contents; |
[email protected] | d81a63c0 | 2013-03-07 08:49:04 | [diff] [blame] | 126 | ASSERT_TRUE( |
[email protected] | 06bfeb1 | 2014-05-27 14:00:09 | [diff] [blame] | 127 | GetExistingShortcutContents(&env, kTemplateFilepath, &contents)); |
[email protected] | 58bf925 | 2013-03-06 04:12:36 | [diff] [blame] | 128 | EXPECT_EQ(kTestData1, contents); |
| 129 | } |
| 130 | |
| 131 | // Test that it searches $XDG_DATA_DIRS/applications. |
[email protected] | af71d64 | 2010-03-12 10:29:04 | [diff] [blame] | 132 | { |
[email protected] | ea1a3f6 | 2012-11-16 20:34:23 | [diff] [blame] | 133 | base::ScopedTempDir temp_dir; |
[email protected] | af71d64 | 2010-03-12 10:29:04 | [diff] [blame] | 134 | ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 135 | |
[email protected] | 76b90d31 | 2010-08-03 03:00:50 | [diff] [blame] | 136 | MockEnvironment env; |
vabr | 8c498ea4 | 2016-09-15 12:41:58 | [diff] [blame] | 137 | env.Set("XDG_DATA_DIRS", temp_dir.GetPath().value()); |
| 138 | ASSERT_TRUE( |
| 139 | base::CreateDirectory(temp_dir.GetPath().Append("applications"))); |
Lei Zhang | eebbef6 | 2020-05-05 20:16:05 | [diff] [blame] | 140 | ASSERT_TRUE(base::WriteFile( |
vabr | 8c498ea4 | 2016-09-15 12:41:58 | [diff] [blame] | 141 | temp_dir.GetPath().Append("applications").Append(kTemplateFilename), |
thestig | d0b66234 | 2015-01-16 08:21:19 | [diff] [blame] | 142 | kTestData2)); |
[email protected] | af71d64 | 2010-03-12 10:29:04 | [diff] [blame] | 143 | std::string contents; |
[email protected] | d81a63c0 | 2013-03-07 08:49:04 | [diff] [blame] | 144 | ASSERT_TRUE( |
[email protected] | 06bfeb1 | 2014-05-27 14:00:09 | [diff] [blame] | 145 | GetExistingShortcutContents(&env, kTemplateFilepath, &contents)); |
[email protected] | af71d64 | 2010-03-12 10:29:04 | [diff] [blame] | 146 | EXPECT_EQ(kTestData2, contents); |
| 147 | } |
| 148 | |
[email protected] | 58bf925 | 2013-03-06 04:12:36 | [diff] [blame] | 149 | // Test that it searches $X/applications for each X in $XDG_DATA_DIRS. |
[email protected] | af71d64 | 2010-03-12 10:29:04 | [diff] [blame] | 150 | { |
[email protected] | 58bf925 | 2013-03-06 04:12:36 | [diff] [blame] | 151 | base::ScopedTempDir temp_dir1; |
| 152 | ASSERT_TRUE(temp_dir1.CreateUniqueTempDir()); |
| 153 | base::ScopedTempDir temp_dir2; |
| 154 | ASSERT_TRUE(temp_dir2.CreateUniqueTempDir()); |
[email protected] | af71d64 | 2010-03-12 10:29:04 | [diff] [blame] | 155 | |
[email protected] | 76b90d31 | 2010-08-03 03:00:50 | [diff] [blame] | 156 | MockEnvironment env; |
vabr | 8c498ea4 | 2016-09-15 12:41:58 | [diff] [blame] | 157 | env.Set("XDG_DATA_DIRS", |
| 158 | temp_dir1.GetPath().value() + ":" + temp_dir2.GetPath().value()); |
[email protected] | 58bf925 | 2013-03-06 04:12:36 | [diff] [blame] | 159 | // Create a file in a non-applications directory. This should be ignored. |
Lei Zhang | eebbef6 | 2020-05-05 20:16:05 | [diff] [blame] | 160 | ASSERT_TRUE(base::WriteFile(temp_dir1.GetPath().Append(kTemplateFilename), |
| 161 | kTestData1)); |
[email protected] | 58bf925 | 2013-03-06 04:12:36 | [diff] [blame] | 162 | // Only create a findable desktop file in the second path. |
vabr | 8c498ea4 | 2016-09-15 12:41:58 | [diff] [blame] | 163 | ASSERT_TRUE( |
| 164 | base::CreateDirectory(temp_dir2.GetPath().Append("applications"))); |
Lei Zhang | eebbef6 | 2020-05-05 20:16:05 | [diff] [blame] | 165 | ASSERT_TRUE(base::WriteFile( |
vabr | 8c498ea4 | 2016-09-15 12:41:58 | [diff] [blame] | 166 | temp_dir2.GetPath().Append("applications").Append(kTemplateFilename), |
thestig | d0b66234 | 2015-01-16 08:21:19 | [diff] [blame] | 167 | kTestData2)); |
[email protected] | af71d64 | 2010-03-12 10:29:04 | [diff] [blame] | 168 | std::string contents; |
[email protected] | d81a63c0 | 2013-03-07 08:49:04 | [diff] [blame] | 169 | ASSERT_TRUE( |
[email protected] | 06bfeb1 | 2014-05-27 14:00:09 | [diff] [blame] | 170 | GetExistingShortcutContents(&env, kTemplateFilepath, &contents)); |
[email protected] | d81a63c0 | 2013-03-07 08:49:04 | [diff] [blame] | 171 | EXPECT_EQ(kTestData2, contents); |
| 172 | } |
| 173 | } |
| 174 | |
[email protected] | 111f028 | 2013-08-05 10:09:29 | [diff] [blame] | 175 | TEST(ShellIntegrationTest, GetExistingProfileShortcutFilenames) { |
| 176 | base::FilePath kProfilePath("a/b/c/Profile Name?"); |
| 177 | const char kApp1Filename[] = "chrome-extension1-Profile_Name_.desktop"; |
| 178 | const char kApp2Filename[] = "chrome-extension2-Profile_Name_.desktop"; |
| 179 | const char kUnrelatedAppFilename[] = "chrome-extension-Other_Profile.desktop"; |
| 180 | |
Gabriel Charette | 798fde7 | 2019-08-20 22:24:04 | [diff] [blame] | 181 | content::BrowserTaskEnvironment task_environment; |
[email protected] | 111f028 | 2013-08-05 10:09:29 | [diff] [blame] | 182 | |
| 183 | base::ScopedTempDir temp_dir; |
| 184 | ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
Lei Zhang | eebbef6 | 2020-05-05 20:16:05 | [diff] [blame] | 185 | ASSERT_TRUE(base::WriteFile(temp_dir.GetPath().Append(kApp1Filename), "")); |
| 186 | ASSERT_TRUE(base::WriteFile(temp_dir.GetPath().Append(kApp2Filename), "")); |
[email protected] | 111f028 | 2013-08-05 10:09:29 | [diff] [blame] | 187 | // This file should not be returned in the results. |
Lei Zhang | eebbef6 | 2020-05-05 20:16:05 | [diff] [blame] | 188 | ASSERT_TRUE( |
| 189 | base::WriteFile(temp_dir.GetPath().Append(kUnrelatedAppFilename), "")); |
[email protected] | 111f028 | 2013-08-05 10:09:29 | [diff] [blame] | 190 | std::vector<base::FilePath> paths = |
vabr | 8c498ea4 | 2016-09-15 12:41:58 | [diff] [blame] | 191 | GetExistingProfileShortcutFilenames(kProfilePath, temp_dir.GetPath()); |
[email protected] | 111f028 | 2013-08-05 10:09:29 | [diff] [blame] | 192 | // Path order is arbitrary. Sort the output for consistency. |
| 193 | std::sort(paths.begin(), paths.end()); |
| 194 | EXPECT_THAT(paths, |
| 195 | ElementsAre(base::FilePath(kApp1Filename), |
| 196 | base::FilePath(kApp2Filename))); |
| 197 | } |
| 198 | |
[email protected] | 0b7df36d | 2012-07-11 09:50:47 | [diff] [blame] | 199 | TEST(ShellIntegrationTest, GetWebShortcutFilename) { |
[email protected] | b96aa93 | 2009-08-12 21:34:49 | [diff] [blame] | 200 | const struct { |
thestig | d0b66234 | 2015-01-16 08:21:19 | [diff] [blame] | 201 | const char* const path; |
| 202 | const char* const url; |
[email protected] | b96aa93 | 2009-08-12 21:34:49 | [diff] [blame] | 203 | } test_cases[] = { |
thestig | d0b66234 | 2015-01-16 08:21:19 | [diff] [blame] | 204 | { "http___foo_.desktop", "http://foo" }, |
| 205 | { "http___foo_bar_.desktop", "http://foo/bar/" }, |
| 206 | { "http___foo_bar_a=b&c=d.desktop", "http://foo/bar?a=b&c=d" }, |
[email protected] | b96aa93 | 2009-08-12 21:34:49 | [diff] [blame] | 207 | |
| 208 | // Now we're starting to be more evil... |
thestig | d0b66234 | 2015-01-16 08:21:19 | [diff] [blame] | 209 | { "http___foo_.desktop", "http://foo/bar/baz/../../../../../" }, |
| 210 | { "http___foo_.desktop", "http://foo/bar/././../baz/././../" }, |
| 211 | { "http___.._.desktop", "http://../../../../" }, |
[email protected] | b96aa93 | 2009-08-12 21:34:49 | [diff] [blame] | 212 | }; |
Daniel Cheng | 7d9e3d5 | 2022-02-26 09:03:24 | [diff] [blame] | 213 | for (size_t i = 0; i < std::size(test_cases); i++) { |
[email protected] | 4f260d0 | 2010-12-23 18:35:42 | [diff] [blame] | 214 | EXPECT_EQ(std::string(chrome::kBrowserProcessExecutableName) + "-" + |
[email protected] | 4289680 | 2009-08-28 23:39:44 | [diff] [blame] | 215 | test_cases[i].path, |
[email protected] | 06bfeb1 | 2014-05-27 14:00:09 | [diff] [blame] | 216 | GetWebShortcutFilename(GURL(test_cases[i].url)).value()) << |
[email protected] | 4289680 | 2009-08-28 23:39:44 | [diff] [blame] | 217 | " while testing " << test_cases[i].url; |
[email protected] | b96aa93 | 2009-08-12 21:34:49 | [diff] [blame] | 218 | } |
| 219 | } |
| 220 | |
[email protected] | a2778d3 | 2011-12-01 07:49:34 | [diff] [blame] | 221 | TEST(ShellIntegrationTest, GetDesktopFileContents) { |
[email protected] | 14fbaed | 2013-05-02 07:54:02 | [diff] [blame] | 222 | const base::FilePath kChromeExePath("/opt/google/chrome/google-chrome"); |
[email protected] | b96aa93 | 2009-08-12 21:34:49 | [diff] [blame] | 223 | const struct { |
thestig | d0b66234 | 2015-01-16 08:21:19 | [diff] [blame] | 224 | const char* const url; |
| 225 | const char* const title; |
| 226 | const char* const icon_name; |
| 227 | const char* const categories; |
Jay Harris | 05b50bd | 2019-10-30 01:48:38 | [diff] [blame] | 228 | const char* const mime_type; |
[email protected] | d81a63c0 | 2013-03-07 08:49:04 | [diff] [blame] | 229 | bool nodisplay; |
thestig | d0b66234 | 2015-01-16 08:21:19 | [diff] [blame] | 230 | const char* const expected_output; |
[email protected] | b96aa93 | 2009-08-12 21:34:49 | [diff] [blame] | 231 | } test_cases[] = { |
Nico Weber | eaa0841 | 2019-08-14 01:24:37 | [diff] [blame] | 232 | // Real-world case. |
Jay Harris | 05b50bd | 2019-10-30 01:48:38 | [diff] [blame] | 233 | {"http://gmail.com", "GMail", "chrome-http__gmail.com", "", "", false, |
[email protected] | b96aa93 | 2009-08-12 21:34:49 | [diff] [blame] | 234 | |
Nico Weber | eaa0841 | 2019-08-14 01:24:37 | [diff] [blame] | 235 | "#!/usr/bin/env xdg-open\n" |
| 236 | "[Desktop Entry]\n" |
| 237 | "Version=1.0\n" |
| 238 | "Terminal=false\n" |
| 239 | "Type=Application\n" |
| 240 | "Name=GMail\n" |
| 241 | "Exec=/opt/google/chrome/google-chrome --app=http://gmail.com/\n" |
| 242 | "Icon=chrome-http__gmail.com\n" |
| 243 | "StartupWMClass=gmail.com\n"}, |
[email protected] | 82810fe1 | 2009-09-25 16:21:57 | [diff] [blame] | 244 | |
Nico Weber | eaa0841 | 2019-08-14 01:24:37 | [diff] [blame] | 245 | // Make sure that empty icons are replaced by the chrome icon. |
Jay Harris | 05b50bd | 2019-10-30 01:48:38 | [diff] [blame] | 246 | {"http://gmail.com", "GMail", "", "", "", false, |
[email protected] | 0b303cc | 2009-09-28 22:35:15 | [diff] [blame] | 247 | |
Nico Weber | eaa0841 | 2019-08-14 01:24:37 | [diff] [blame] | 248 | "#!/usr/bin/env xdg-open\n" |
| 249 | "[Desktop Entry]\n" |
| 250 | "Version=1.0\n" |
| 251 | "Terminal=false\n" |
| 252 | "Type=Application\n" |
| 253 | "Name=GMail\n" |
| 254 | "Exec=/opt/google/chrome/google-chrome --app=http://gmail.com/\n" |
| 255 | #if BUILDFLAG(GOOGLE_CHROME_BRANDING) |
| 256 | "Icon=google-chrome\n" |
[email protected] | a7e18f4 | 2013-12-19 02:10:37 | [diff] [blame] | 257 | #else |
Nico Weber | eaa0841 | 2019-08-14 01:24:37 | [diff] [blame] | 258 | "Icon=chromium-browser\n" |
[email protected] | a7e18f4 | 2013-12-19 02:10:37 | [diff] [blame] | 259 | #endif |
Nico Weber | eaa0841 | 2019-08-14 01:24:37 | [diff] [blame] | 260 | "StartupWMClass=gmail.com\n"}, |
[email protected] | 0b303cc | 2009-09-28 22:35:15 | [diff] [blame] | 261 | |
Nico Weber | eaa0841 | 2019-08-14 01:24:37 | [diff] [blame] | 262 | // Test adding categories and NoDisplay=true. |
| 263 | {"http://gmail.com", "GMail", "chrome-http__gmail.com", |
Jay Harris | 05b50bd | 2019-10-30 01:48:38 | [diff] [blame] | 264 | "Graphics;Education;", "", true, |
[email protected] | d81a63c0 | 2013-03-07 08:49:04 | [diff] [blame] | 265 | |
Nico Weber | eaa0841 | 2019-08-14 01:24:37 | [diff] [blame] | 266 | "#!/usr/bin/env xdg-open\n" |
| 267 | "[Desktop Entry]\n" |
| 268 | "Version=1.0\n" |
| 269 | "Terminal=false\n" |
| 270 | "Type=Application\n" |
| 271 | "Name=GMail\n" |
| 272 | "Exec=/opt/google/chrome/google-chrome --app=http://gmail.com/\n" |
| 273 | "Icon=chrome-http__gmail.com\n" |
| 274 | "Categories=Graphics;Education;\n" |
| 275 | "NoDisplay=true\n" |
| 276 | "StartupWMClass=gmail.com\n"}, |
[email protected] | d81a63c0 | 2013-03-07 08:49:04 | [diff] [blame] | 277 | |
Nico Weber | eaa0841 | 2019-08-14 01:24:37 | [diff] [blame] | 278 | // Now we're starting to be more evil... |
| 279 | {"http://evil.com/evil --join-the-b0tnet", "Ownz0red\nExec=rm -rf /", |
Jay Harris | 05b50bd | 2019-10-30 01:48:38 | [diff] [blame] | 280 | "chrome-http__evil.com_evil", "", "", false, |
[email protected] | b96aa93 | 2009-08-12 21:34:49 | [diff] [blame] | 281 | |
Nico Weber | eaa0841 | 2019-08-14 01:24:37 | [diff] [blame] | 282 | "#!/usr/bin/env xdg-open\n" |
| 283 | "[Desktop Entry]\n" |
| 284 | "Version=1.0\n" |
| 285 | "Terminal=false\n" |
| 286 | "Type=Application\n" |
| 287 | "Name=http://evil.com/evil%20--join-the-b0tnet\n" |
| 288 | "Exec=/opt/google/chrome/google-chrome " |
| 289 | "--app=http://evil.com/evil%20--join-the-b0tnet\n" |
| 290 | "Icon=chrome-http__evil.com_evil\n" |
| 291 | "StartupWMClass=evil.com__evil%20--join-the-b0tnet\n"}, |
| 292 | {"http://evil.com/evil; rm -rf /; \"; rm -rf $HOME >ownz0red", |
Jay Harris | 05b50bd | 2019-10-30 01:48:38 | [diff] [blame] | 293 | "Innocent Title", "chrome-http__evil.com_evil", "", "", false, |
[email protected] | 82810fe1 | 2009-09-25 16:21:57 | [diff] [blame] | 294 | |
Nico Weber | eaa0841 | 2019-08-14 01:24:37 | [diff] [blame] | 295 | "#!/usr/bin/env xdg-open\n" |
| 296 | "[Desktop Entry]\n" |
| 297 | "Version=1.0\n" |
| 298 | "Terminal=false\n" |
| 299 | "Type=Application\n" |
| 300 | "Name=Innocent Title\n" |
| 301 | "Exec=/opt/google/chrome/google-chrome " |
| 302 | "\"--app=http://evil.com/evil;%20rm%20-rf%20/;%20%22;%20rm%20" |
| 303 | // Note: $ is escaped as \$ within an arg to Exec, and then |
| 304 | // the \ is escaped as \\ as all strings in a Desktop file should |
| 305 | // be; finally, \\ becomes \\\\ when represented in a C++ string! |
| 306 | "-rf%20\\\\$HOME%20%3Eownz0red\"\n" |
| 307 | "Icon=chrome-http__evil.com_evil\n" |
| 308 | "StartupWMClass=evil.com__evil;%20rm%20-rf%20_;%20%22;%20" |
| 309 | "rm%20-rf%20$HOME%20%3Eownz0red\n"}, |
| 310 | {"http://evil.com/evil | cat `echo ownz0red` >/dev/null", |
Jay Harris | 05b50bd | 2019-10-30 01:48:38 | [diff] [blame] | 311 | "Innocent Title", "chrome-http__evil.com_evil", "", "", false, |
[email protected] | 82810fe1 | 2009-09-25 16:21:57 | [diff] [blame] | 312 | |
Nico Weber | eaa0841 | 2019-08-14 01:24:37 | [diff] [blame] | 313 | "#!/usr/bin/env xdg-open\n" |
| 314 | "[Desktop Entry]\n" |
| 315 | "Version=1.0\n" |
| 316 | "Terminal=false\n" |
| 317 | "Type=Application\n" |
| 318 | "Name=Innocent Title\n" |
| 319 | "Exec=/opt/google/chrome/google-chrome " |
| 320 | "--app=http://evil.com/evil%20%7C%20cat%20%60echo%20ownz0red" |
| 321 | "%60%20%3E/dev/null\n" |
| 322 | "Icon=chrome-http__evil.com_evil\n" |
| 323 | "StartupWMClass=evil.com__evil%20%7C%20cat%20%60echo%20ownz0red" |
| 324 | "%60%20%3E_dev_null\n"}, |
Jay Harris | 05b50bd | 2019-10-30 01:48:38 | [diff] [blame] | 325 | // Test setting mime type |
| 326 | {"https://paint.app", "Paint", "chrome-https__paint.app", "Image", |
| 327 | "image/png;image/jpg", false, |
| 328 | |
| 329 | "#!/usr/bin/env xdg-open\n" |
| 330 | "[Desktop Entry]\n" |
| 331 | "Version=1.0\n" |
| 332 | "Terminal=false\n" |
| 333 | "Type=Application\n" |
| 334 | "Name=Paint\n" |
Jay Harris | f730b6e | 2019-11-25 23:49:06 | [diff] [blame] | 335 | "MimeType=image/png;image/jpg\n" |
Mike Jackson | cfb1bc1 | 2021-03-24 01:22:22 | [diff] [blame] | 336 | "Exec=/opt/google/chrome/google-chrome --app=https://paint.app/ %U\n" |
Jay Harris | 05b50bd | 2019-10-30 01:48:38 | [diff] [blame] | 337 | "Icon=chrome-https__paint.app\n" |
| 338 | "Categories=Image\n" |
Jay Harris | 05b50bd | 2019-10-30 01:48:38 | [diff] [blame] | 339 | "StartupWMClass=paint.app\n"}, |
| 340 | |
| 341 | // Test evil mime type. |
| 342 | {"https://paint.app", "Evil Paint", "chrome-https__paint.app", "Image", |
| 343 | "image/png\nExec=rm -rf /", false, |
| 344 | |
| 345 | "#!/usr/bin/env xdg-open\n" |
| 346 | "[Desktop Entry]\n" |
| 347 | "Version=1.0\n" |
| 348 | "Terminal=false\n" |
| 349 | "Type=Application\n" |
| 350 | "Name=Evil Paint\n" |
| 351 | "Exec=/opt/google/chrome/google-chrome --app=https://paint.app/\n" |
| 352 | "Icon=chrome-https__paint.app\n" |
| 353 | "Categories=Image\n" |
| 354 | "StartupWMClass=paint.app\n"}}; |
[email protected] | 07753f0f | 2013-02-05 07:52:59 | [diff] [blame] | 355 | |
Daniel Cheng | 7d9e3d5 | 2022-02-26 09:03:24 | [diff] [blame] | 356 | for (size_t i = 0; i < std::size(test_cases); i++) { |
[email protected] | b1039293 | 2011-03-08 21:28:14 | [diff] [blame] | 357 | SCOPED_TRACE(i); |
[email protected] | a0b60cfd | 2011-04-06 18:02:41 | [diff] [blame] | 358 | EXPECT_EQ( |
| 359 | test_cases[i].expected_output, |
[email protected] | 06bfeb1 | 2014-05-27 14:00:09 | [diff] [blame] | 360 | GetDesktopFileContents( |
[email protected] | 14fbaed | 2013-05-02 07:54:02 | [diff] [blame] | 361 | kChromeExePath, |
[email protected] | a0b60cfd | 2011-04-06 18:02:41 | [diff] [blame] | 362 | web_app::GenerateApplicationNameFromURL(GURL(test_cases[i].url)), |
Jay Harris | 05b50bd | 2019-10-30 01:48:38 | [diff] [blame] | 363 | GURL(test_cases[i].url), std::string(), |
| 364 | base::ASCIIToUTF16(test_cases[i].title), test_cases[i].icon_name, |
| 365 | base::FilePath(), test_cases[i].categories, test_cases[i].mime_type, |
Mandy Chen | d57606f | 2021-08-31 17:49:10 | [diff] [blame] | 366 | test_cases[i].nodisplay, "", {})); |
| 367 | } |
| 368 | } |
| 369 | |
| 370 | TEST(ShellIntegrationTest, GetDesktopFileContentsForApps) { |
| 371 | const base::FilePath kChromeExePath("/opt/google/chrome/google-chrome"); |
| 372 | const struct { |
| 373 | const char* const url; |
| 374 | const char* const title; |
| 375 | const char* const icon_name; |
| 376 | bool nodisplay; |
| 377 | std::set<web_app::DesktopActionInfo> action_info; |
| 378 | const char* const expected_output; |
| 379 | } test_cases[] = { |
| 380 | // Test Shortcut Menu actions. |
| 381 | {"https://example.app", |
| 382 | "Lawful example", |
| 383 | "IconName", |
| 384 | false, |
| 385 | { |
| 386 | web_app::DesktopActionInfo("action1", "Action 1", |
| 387 | GURL("https://example.com/action1")), |
| 388 | web_app::DesktopActionInfo("action2", "Action 2", |
| 389 | GURL("https://example.com/action2")), |
| 390 | web_app::DesktopActionInfo("action3", "Action 3", |
| 391 | GURL("https://example.com/action3")), |
| 392 | web_app::DesktopActionInfo("action4", "Action 4", |
| 393 | GURL("https://example.com/action4")), |
Mandy Chen | 5de2f24 | 2022-01-12 00:30:21 | [diff] [blame] | 394 | web_app::DesktopActionInfo("action5", "Action 5", |
| 395 | GURL("https://example.com/action%205")), |
Mandy Chen | d57606f | 2021-08-31 17:49:10 | [diff] [blame] | 396 | }, |
| 397 | |
| 398 | "#!/usr/bin/env xdg-open\n" |
| 399 | "[Desktop Entry]\n" |
| 400 | "Version=1.0\n" |
| 401 | "Terminal=false\n" |
| 402 | "Type=Application\n" |
| 403 | "Name=Lawful example\n" |
| 404 | "Exec=/opt/google/chrome/google-chrome --app-id=TestAppId\n" |
| 405 | "Icon=IconName\n" |
| 406 | "StartupWMClass=example.app\n" |
Mandy Chen | 5de2f24 | 2022-01-12 00:30:21 | [diff] [blame] | 407 | "Actions=action1;action2;action3;action4;action5\n\n" |
Mandy Chen | d57606f | 2021-08-31 17:49:10 | [diff] [blame] | 408 | "[Desktop Action action1]\n" |
| 409 | "Name=Action 1\n" |
| 410 | "Exec=/opt/google/chrome/google-chrome --app-id=TestAppId " |
| 411 | "--app-launch-url-for-shortcuts-menu-item=https://example.com/" |
| 412 | "action1\n\n" |
| 413 | "[Desktop Action action2]\n" |
| 414 | "Name=Action 2\n" |
| 415 | "Exec=/opt/google/chrome/google-chrome --app-id=TestAppId " |
| 416 | "--app-launch-url-for-shortcuts-menu-item=https://example.com/" |
| 417 | "action2\n\n" |
| 418 | "[Desktop Action action3]\n" |
| 419 | "Name=Action 3\n" |
| 420 | "Exec=/opt/google/chrome/google-chrome --app-id=TestAppId " |
| 421 | "--app-launch-url-for-shortcuts-menu-item=https://example.com/" |
| 422 | "action3\n\n" |
| 423 | "[Desktop Action action4]\n" |
| 424 | "Name=Action 4\n" |
| 425 | "Exec=/opt/google/chrome/google-chrome --app-id=TestAppId " |
| 426 | "--app-launch-url-for-shortcuts-menu-item=https://example.com/" |
Mandy Chen | 5de2f24 | 2022-01-12 00:30:21 | [diff] [blame] | 427 | "action4\n\n" |
| 428 | "[Desktop Action action5]\n" |
| 429 | "Name=Action 5\n" |
| 430 | "Exec=/opt/google/chrome/google-chrome --app-id=TestAppId " |
| 431 | "--app-launch-url-for-shortcuts-menu-item=https://example.com/" |
| 432 | "action%%205\n"}, |
Mandy Chen | d57606f | 2021-08-31 17:49:10 | [diff] [blame] | 433 | }; |
| 434 | |
Daniel Cheng | 7d9e3d5 | 2022-02-26 09:03:24 | [diff] [blame] | 435 | for (size_t i = 0; i < std::size(test_cases); i++) { |
Mandy Chen | d57606f | 2021-08-31 17:49:10 | [diff] [blame] | 436 | SCOPED_TRACE(i); |
| 437 | EXPECT_EQ( |
| 438 | test_cases[i].expected_output, |
| 439 | GetDesktopFileContents( |
| 440 | kChromeExePath, |
| 441 | web_app::GenerateApplicationNameFromURL(GURL(test_cases[i].url)), |
| 442 | GURL(test_cases[i].url), "TestAppId", |
| 443 | base::ASCIIToUTF16(test_cases[i].title), test_cases[i].icon_name, |
| 444 | base::FilePath(), "", "", test_cases[i].nodisplay, "", |
| 445 | test_cases[i].action_info)); |
[email protected] | b96aa93 | 2009-08-12 21:34:49 | [diff] [blame] | 446 | } |
| 447 | } |
[email protected] | d81a63c0 | 2013-03-07 08:49:04 | [diff] [blame] | 448 | |
[email protected] | a3c2595 | 2013-05-02 13:16:06 | [diff] [blame] | 449 | TEST(ShellIntegrationTest, GetDirectoryFileContents) { |
| 450 | const struct { |
thestig | d0b66234 | 2015-01-16 08:21:19 | [diff] [blame] | 451 | const char* const title; |
| 452 | const char* const icon_name; |
| 453 | const char* const expected_output; |
[email protected] | a3c2595 | 2013-05-02 13:16:06 | [diff] [blame] | 454 | } test_cases[] = { |
Nico Weber | eaa0841 | 2019-08-14 01:24:37 | [diff] [blame] | 455 | // Real-world case. |
| 456 | {"Chrome Apps", "chrome-apps", |
[email protected] | a3c2595 | 2013-05-02 13:16:06 | [diff] [blame] | 457 | |
Nico Weber | eaa0841 | 2019-08-14 01:24:37 | [diff] [blame] | 458 | "[Desktop Entry]\n" |
| 459 | "Version=1.0\n" |
| 460 | "Type=Directory\n" |
| 461 | "Name=Chrome Apps\n" |
| 462 | "Icon=chrome-apps\n"}, |
[email protected] | a3c2595 | 2013-05-02 13:16:06 | [diff] [blame] | 463 | |
Nico Weber | eaa0841 | 2019-08-14 01:24:37 | [diff] [blame] | 464 | // Make sure that empty icons are replaced by the chrome icon. |
| 465 | {"Chrome Apps", "", |
[email protected] | a3c2595 | 2013-05-02 13:16:06 | [diff] [blame] | 466 | |
Nico Weber | eaa0841 | 2019-08-14 01:24:37 | [diff] [blame] | 467 | "[Desktop Entry]\n" |
| 468 | "Version=1.0\n" |
| 469 | "Type=Directory\n" |
| 470 | "Name=Chrome Apps\n" |
| 471 | #if BUILDFLAG(GOOGLE_CHROME_BRANDING) |
| 472 | "Icon=google-chrome\n" |
[email protected] | a7e18f4 | 2013-12-19 02:10:37 | [diff] [blame] | 473 | #else |
Nico Weber | eaa0841 | 2019-08-14 01:24:37 | [diff] [blame] | 474 | "Icon=chromium-browser\n" |
[email protected] | a7e18f4 | 2013-12-19 02:10:37 | [diff] [blame] | 475 | #endif |
Nico Weber | eaa0841 | 2019-08-14 01:24:37 | [diff] [blame] | 476 | }, |
[email protected] | a3c2595 | 2013-05-02 13:16:06 | [diff] [blame] | 477 | }; |
| 478 | |
Daniel Cheng | 7d9e3d5 | 2022-02-26 09:03:24 | [diff] [blame] | 479 | for (size_t i = 0; i < std::size(test_cases); i++) { |
[email protected] | a3c2595 | 2013-05-02 13:16:06 | [diff] [blame] | 480 | SCOPED_TRACE(i); |
[email protected] | 06bfeb1 | 2014-05-27 14:00:09 | [diff] [blame] | 481 | EXPECT_EQ(test_cases[i].expected_output, |
| 482 | GetDirectoryFileContents(base::ASCIIToUTF16(test_cases[i].title), |
| 483 | test_cases[i].icon_name)); |
[email protected] | a3c2595 | 2013-05-02 13:16:06 | [diff] [blame] | 484 | } |
| 485 | } |
[email protected] | 06bfeb1 | 2014-05-27 14:00:09 | [diff] [blame] | 486 | |
Robert Woods | 954a218 | 2020-03-20 06:08:27 | [diff] [blame] | 487 | TEST(ShellIntegrationTest, GetMimeTypesRegistrationFilename) { |
| 488 | const struct { |
| 489 | const char* const profile_path; |
| 490 | const char* const app_id; |
| 491 | const char* const expected_filename; |
| 492 | } test_cases[] = { |
| 493 | {"Default", "app-id", "-app-id-Default.xml"}, |
| 494 | {"Default Profile", "app-id", "-app-id-Default_Profile.xml"}, |
| 495 | {"foo/Default", "app-id", "-app-id-Default.xml"}, |
| 496 | {"Default*Profile", "app-id", "-app-id-Default_Profile.xml"}}; |
| 497 | std::string browser_name(chrome::kBrowserProcessExecutableName); |
| 498 | |
| 499 | for (const auto& test_case : test_cases) { |
| 500 | const base::FilePath filename = |
| 501 | GetMimeTypesRegistrationFilename(base::FilePath(test_case.profile_path), |
Glenn Hartmann | 37987f5 | 2023-09-26 21:06:17 | [diff] [blame] | 502 | webapps::AppId(test_case.app_id)); |
Robert Woods | 954a218 | 2020-03-20 06:08:27 | [diff] [blame] | 503 | EXPECT_EQ(browser_name + test_case.expected_filename, filename.value()); |
| 504 | } |
| 505 | } |
| 506 | |
| 507 | TEST(ShellIntegrationTest, GetMimeTypesRegistrationFileContents) { |
| 508 | apps::FileHandlers file_handlers; |
| 509 | { |
| 510 | apps::FileHandler file_handler; |
| 511 | { |
| 512 | apps::FileHandler::AcceptEntry accept_entry; |
| 513 | accept_entry.mime_type = "application/foo"; |
| 514 | accept_entry.file_extensions.insert(".foo"); |
| 515 | file_handler.accept.push_back(accept_entry); |
| 516 | } |
Evan Stade | b8f43a4 | 2021-09-14 16:25:14 | [diff] [blame] | 517 | file_handler.display_name = u"FoO"; |
Robert Woods | 954a218 | 2020-03-20 06:08:27 | [diff] [blame] | 518 | file_handlers.push_back(file_handler); |
| 519 | } |
| 520 | { |
| 521 | apps::FileHandler file_handler; |
| 522 | { |
| 523 | apps::FileHandler::AcceptEntry accept_entry; |
| 524 | accept_entry.mime_type = "application/foobar"; |
| 525 | accept_entry.file_extensions.insert(".foobar"); |
| 526 | file_handler.accept.push_back(accept_entry); |
| 527 | } |
| 528 | file_handlers.push_back(file_handler); |
| 529 | } |
| 530 | { |
| 531 | apps::FileHandler file_handler; |
| 532 | { |
| 533 | apps::FileHandler::AcceptEntry accept_entry; |
| 534 | accept_entry.mime_type = "application/bar"; |
Evan Stade | b8f43a4 | 2021-09-14 16:25:14 | [diff] [blame] | 535 | // A name that has a reserved XML character. |
| 536 | file_handler.display_name = u"ba<r"; |
Robert Woods | 954a218 | 2020-03-20 06:08:27 | [diff] [blame] | 537 | accept_entry.file_extensions.insert(".bar"); |
| 538 | accept_entry.file_extensions.insert(".baz"); |
| 539 | file_handler.accept.push_back(accept_entry); |
| 540 | } |
| 541 | file_handlers.push_back(file_handler); |
| 542 | } |
| 543 | |
| 544 | const std::string file_contents = |
| 545 | GetMimeTypesRegistrationFileContents(file_handlers); |
| 546 | const std::string expected_file_contents = |
Evan Stade | b8f43a4 | 2021-09-14 16:25:14 | [diff] [blame] | 547 | "<?xml version=\"1.0\"?>\n" |
Robert Woods | 954a218 | 2020-03-20 06:08:27 | [diff] [blame] | 548 | "<mime-info " |
| 549 | "xmlns=\"http://www.freedesktop.org/standards/shared-mime-info\">\n" |
Evan Stade | b8f43a4 | 2021-09-14 16:25:14 | [diff] [blame] | 550 | " <mime-type type=\"application/foo\">\n" |
| 551 | " <comment>FoO</comment>\n" |
| 552 | " <glob pattern=\"*.foo\"/>\n" |
| 553 | " </mime-type>\n" |
| 554 | " <mime-type type=\"application/foobar\">\n" |
| 555 | " <glob pattern=\"*.foobar\"/>\n" |
| 556 | " </mime-type>\n" |
| 557 | " <mime-type type=\"application/bar\">\n" |
| 558 | " <comment>ba<r</comment>\n" |
| 559 | " <glob pattern=\"*.bar\"/>\n" |
| 560 | " <glob pattern=\"*.baz\"/>\n" |
| 561 | " </mime-type>\n" |
Robert Woods | 954a218 | 2020-03-20 06:08:27 | [diff] [blame] | 562 | "</mime-info>\n"; |
| 563 | |
| 564 | EXPECT_EQ(file_contents, expected_file_contents); |
| 565 | } |
| 566 | |
Alexander Dunaev | 1016e63 | 2021-08-10 15:13:34 | [diff] [blame] | 567 | // The WM class name may be either capitalised or not, depending on the |
| 568 | // platform. |
| 569 | void CheckProgramClassClass(const std::string& class_name) { |
Maksim Sisov | 213ddfa | 2021-09-30 08:18:56 | [diff] [blame] | 570 | if (ui::OzonePlatform::GetPlatformNameForTest() == "x11") { |
Alexander Dunaev | 1016e63 | 2021-08-10 15:13:34 | [diff] [blame] | 571 | EXPECT_EQ("Foo", class_name); |
Maksim Sisov | 213ddfa | 2021-09-30 08:18:56 | [diff] [blame] | 572 | } else { |
| 573 | EXPECT_EQ("foo", class_name); |
Alexander Dunaev | 1016e63 | 2021-08-10 15:13:34 | [diff] [blame] | 574 | } |
Alexander Dunaev | 1016e63 | 2021-08-10 15:13:34 | [diff] [blame] | 575 | } |
| 576 | |
thomasanderson | 12d8758 | 2016-07-29 21:17:41 | [diff] [blame] | 577 | TEST(ShellIntegrationTest, WmClass) { |
| 578 | base::CommandLine command_line((base::FilePath())); |
| 579 | EXPECT_EQ("foo", internal::GetProgramClassName(command_line, "foo.desktop")); |
Alexander Dunaev | 1016e63 | 2021-08-10 15:13:34 | [diff] [blame] | 580 | CheckProgramClassClass( |
| 581 | internal::GetProgramClassClass(command_line, "foo.desktop")); |
thomasanderson | 12d8758 | 2016-07-29 21:17:41 | [diff] [blame] | 582 | |
| 583 | command_line.AppendSwitchASCII("class", "baR"); |
| 584 | EXPECT_EQ("foo", internal::GetProgramClassName(command_line, "foo.desktop")); |
| 585 | EXPECT_EQ("baR", internal::GetProgramClassClass(command_line, "foo.desktop")); |
| 586 | |
| 587 | command_line = base::CommandLine(base::FilePath()); |
| 588 | command_line.AppendSwitchASCII("user-data-dir", "/tmp/baz"); |
| 589 | EXPECT_EQ("foo (/tmp/baz)", |
| 590 | internal::GetProgramClassName(command_line, "foo.desktop")); |
Alexander Dunaev | 1016e63 | 2021-08-10 15:13:34 | [diff] [blame] | 591 | CheckProgramClassClass( |
| 592 | internal::GetProgramClassClass(command_line, "foo.desktop")); |
thomasanderson | 12d8758 | 2016-07-29 21:17:41 | [diff] [blame] | 593 | } |
| 594 | |
Mike Jackson | 0cb0ba7 | 2022-09-26 23:22:21 | [diff] [blame] | 595 | TEST(ShellIntegrationTest, GetDesktopEntryStringValueFromFromDesktopFile) { |
| 596 | const char* const kDesktopFileContents = |
| 597 | "#!/usr/bin/env xdg-open\n" |
| 598 | "[Desktop Entry]\n" |
| 599 | "Version=1.0\n" |
| 600 | "Terminal=false\n" |
| 601 | "Type=Application\n" |
| 602 | "Name=Lawful example\n" |
| 603 | "Exec=/opt/google/chrome/google-chrome --app-id=TestAppId\n" |
| 604 | "Icon=IconName\n" |
| 605 | "StartupWMClass=example.app\n" |
| 606 | "Actions=action1\n\n" |
| 607 | "[Desktop Action action1]\n" |
| 608 | "Name=Action 1\n" |
| 609 | "Exec=/opt/google/chrome/google-chrome --app-id=TestAppId --Test" |
| 610 | "Action1=Value"; |
| 611 | |
| 612 | // Verify basic strings return the right value. |
| 613 | EXPECT_EQ("Lawful example", |
| 614 | shell_integration_linux::internal:: |
| 615 | GetDesktopEntryStringValueFromFromDesktopFileForTest( |
| 616 | "Name", kDesktopFileContents)); |
| 617 | EXPECT_EQ("example.app", |
| 618 | shell_integration_linux::internal:: |
| 619 | GetDesktopEntryStringValueFromFromDesktopFileForTest( |
| 620 | "StartupWMClass", kDesktopFileContents)); |
| 621 | // Verify that booleans are returned correctly. |
| 622 | EXPECT_EQ("false", shell_integration_linux::internal:: |
| 623 | GetDesktopEntryStringValueFromFromDesktopFileForTest( |
| 624 | "Terminal", kDesktopFileContents)); |
| 625 | // Verify that numbers are returned correctly. |
| 626 | EXPECT_EQ("1.0", shell_integration_linux::internal:: |
| 627 | GetDesktopEntryStringValueFromFromDesktopFileForTest( |
| 628 | "Version", kDesktopFileContents)); |
| 629 | // Verify that a non-existent key returns an empty string. |
| 630 | EXPECT_EQ("", shell_integration_linux::internal:: |
| 631 | GetDesktopEntryStringValueFromFromDesktopFileForTest( |
| 632 | "DoesNotExistKey", kDesktopFileContents)); |
| 633 | // Verify that a non-existent key in [Desktop Entry] section returns an empty |
| 634 | // string. |
| 635 | EXPECT_EQ("", shell_integration_linux::internal:: |
| 636 | GetDesktopEntryStringValueFromFromDesktopFileForTest( |
| 637 | "Action1", kDesktopFileContents)); |
| 638 | } |
| 639 | |
[email protected] | 06bfeb1 | 2014-05-27 14:00:09 | [diff] [blame] | 640 | } // namespace shell_integration_linux |