OLD | NEW |
---|---|
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/shell_integration_linux.h" | 5 #include "chrome/browser/shell_integration_linux.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <cstdlib> | 8 #include <cstdlib> |
9 #include <map> | 9 #include <map> |
10 #include <vector> | 10 #include <vector> |
11 | 11 |
12 #include "base/base_paths.h" | 12 #include "base/base_paths.h" |
13 #include "base/command_line.h" | 13 #include "base/command_line.h" |
14 #include "base/environment.h" | 14 #include "base/environment.h" |
15 #include "base/files/file_path.h" | 15 #include "base/files/file_path.h" |
16 #include "base/files/file_util.h" | 16 #include "base/files/file_util.h" |
17 #include "base/files/scoped_temp_dir.h" | 17 #include "base/files/scoped_temp_dir.h" |
18 #include "base/message_loop/message_loop.h" | 18 #include "base/message_loop/message_loop.h" |
19 #include "base/stl_util.h" | 19 #include "base/stl_util.h" |
20 #include "base/strings/string_util.h" | 20 #include "base/strings/string_util.h" |
21 #include "base/strings/utf_string_conversions.h" | 21 #include "base/strings/utf_string_conversions.h" |
22 #include "base/test/scoped_path_override.h" | 22 #include "base/test/scoped_path_override.h" |
23 #include "chrome/common/chrome_constants.h" | 23 #include "chrome/common/chrome_constants.h" |
24 #include "content/public/test/test_browser_thread.h" | 24 #include "content/public/test/test_browser_thread.h" |
25 #include "testing/gmock/include/gmock/gmock.h" | 25 #include "testing/gmock/include/gmock/gmock.h" |
26 #include "testing/gtest/include/gtest/gtest.h" | 26 #include "testing/gtest/include/gtest/gtest.h" |
27 #include "url/gurl.h" | 27 #include "url/gurl.h" |
28 | 28 |
29 using content::BrowserThread; | 29 using content::BrowserThread; |
30 using ::testing::Contains; | |
30 using ::testing::ElementsAre; | 31 using ::testing::ElementsAre; |
31 | 32 |
32 namespace shell_integration_linux { | 33 namespace shell_integration_linux { |
33 | 34 |
34 namespace { | 35 namespace { |
35 | 36 |
36 // Provides mock environment variables values based on a stored map. | 37 // Provides mock environment variables values based on a stored map. |
37 class MockEnvironment : public base::Environment { | 38 class MockEnvironment : public base::Environment { |
38 public: | 39 public: |
39 MockEnvironment() {} | 40 MockEnvironment() {} |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
92 | 93 |
93 } // namespace | 94 } // namespace |
94 | 95 |
95 TEST(ShellIntegrationTest, GetDataWriteLocation) { | 96 TEST(ShellIntegrationTest, GetDataWriteLocation) { |
96 base::MessageLoop message_loop; | 97 base::MessageLoop message_loop; |
97 content::TestBrowserThread file_thread(BrowserThread::FILE, &message_loop); | 98 content::TestBrowserThread file_thread(BrowserThread::FILE, &message_loop); |
98 | 99 |
99 // Test that it returns $XDG_DATA_HOME. | 100 // Test that it returns $XDG_DATA_HOME. |
100 { | 101 { |
101 MockEnvironment env; | 102 MockEnvironment env; |
102 env.Set("HOME", "/home/user"); | 103 base::ScopedPathOverride home_override(base::DIR_HOME, |
104 base::FilePath("/home/user"), | |
105 true /* absolute? */, | |
106 false /* create? */); | |
103 env.Set("XDG_DATA_HOME", "/user/path"); | 107 env.Set("XDG_DATA_HOME", "/user/path"); |
104 base::FilePath path; | 108 base::FilePath path = GetDataWriteLocation(&env); |
105 ASSERT_TRUE(GetDataWriteLocation(&env, &path)); | |
106 EXPECT_EQ("/user/path", path.value()); | 109 EXPECT_EQ("/user/path", path.value()); |
107 } | 110 } |
108 | 111 |
109 // Test that $XDG_DATA_HOME falls back to $HOME/.local/share. | 112 // Test that $XDG_DATA_HOME falls back to $HOME/.local/share. |
110 { | 113 { |
111 MockEnvironment env; | 114 MockEnvironment env; |
112 env.Set("HOME", "/home/user"); | 115 base::ScopedPathOverride home_override(base::DIR_HOME, |
113 base::FilePath path; | 116 base::FilePath("/home/user"), |
114 ASSERT_TRUE(GetDataWriteLocation(&env, &path)); | 117 true /* absolute? */, |
118 false /* create? */); | |
119 base::FilePath path = GetDataWriteLocation(&env); | |
115 EXPECT_EQ("/home/user/.local/share", path.value()); | 120 EXPECT_EQ("/home/user/.local/share", path.value()); |
116 } | 121 } |
117 | |
118 // Test that if neither $XDG_DATA_HOME nor $HOME are specified, it fails. | |
119 { | |
120 MockEnvironment env; | |
121 base::FilePath path; | |
122 ASSERT_FALSE(GetDataWriteLocation(&env, &path)); | |
123 } | |
124 } | 122 } |
125 | 123 |
126 TEST(ShellIntegrationTest, GetDataSearchLocations) { | 124 TEST(ShellIntegrationTest, GetDataSearchLocations) { |
127 base::MessageLoop message_loop; | 125 base::MessageLoop message_loop; |
128 content::TestBrowserThread file_thread(BrowserThread::FILE, &message_loop); | 126 content::TestBrowserThread file_thread(BrowserThread::FILE, &message_loop); |
129 | 127 |
130 // Test that it returns $XDG_DATA_HOME + $XDG_DATA_DIRS. | 128 // Test that it returns $XDG_DATA_HOME + $XDG_DATA_DIRS. |
131 { | 129 { |
132 MockEnvironment env; | 130 MockEnvironment env; |
133 env.Set("HOME", "/home/user"); | 131 base::ScopedPathOverride home_override(base::DIR_HOME, |
132 base::FilePath("/home/user"), | |
133 true /* absolute? */, | |
134 false /* create? */); | |
134 env.Set("XDG_DATA_HOME", "/user/path"); | 135 env.Set("XDG_DATA_HOME", "/user/path"); |
135 env.Set("XDG_DATA_DIRS", "/system/path/1:/system/path/2"); | 136 env.Set("XDG_DATA_DIRS", "/system/path/1:/system/path/2"); |
136 EXPECT_THAT( | 137 EXPECT_THAT( |
137 FilePathsToStrings(GetDataSearchLocations(&env)), | 138 FilePathsToStrings(GetDataSearchLocations(&env)), |
138 ElementsAre("/user/path", | 139 ElementsAre("/user/path", |
139 "/system/path/1", | 140 "/system/path/1", |
140 "/system/path/2")); | 141 "/system/path/2")); |
141 } | 142 } |
142 | 143 |
143 // Test that $XDG_DATA_HOME falls back to $HOME/.local/share. | 144 // Test that $XDG_DATA_HOME falls back to $HOME/.local/share. |
144 { | 145 { |
145 MockEnvironment env; | 146 MockEnvironment env; |
146 env.Set("HOME", "/home/user"); | 147 base::ScopedPathOverride home_override(base::DIR_HOME, |
148 base::FilePath("/home/user"), | |
149 true /* absolute? */, | |
150 false /* create? */); | |
147 env.Set("XDG_DATA_DIRS", "/system/path/1:/system/path/2"); | 151 env.Set("XDG_DATA_DIRS", "/system/path/1:/system/path/2"); |
148 EXPECT_THAT( | 152 EXPECT_THAT( |
149 FilePathsToStrings(GetDataSearchLocations(&env)), | 153 FilePathsToStrings(GetDataSearchLocations(&env)), |
150 ElementsAre("/home/user/.local/share", | 154 ElementsAre("/home/user/.local/share", |
151 "/system/path/1", | 155 "/system/path/1", |
152 "/system/path/2")); | 156 "/system/path/2")); |
153 } | 157 } |
154 | 158 |
155 // Test that if neither $XDG_DATA_HOME nor $HOME are specified, it still | 159 // Test that if neither $XDG_DATA_HOME nor $HOME are specified, it still |
156 // succeeds. | 160 // succeeds. |
157 { | 161 { |
158 MockEnvironment env; | 162 MockEnvironment env; |
159 env.Set("XDG_DATA_DIRS", "/system/path/1:/system/path/2"); | 163 env.Set("XDG_DATA_DIRS", "/system/path/1:/system/path/2"); |
160 EXPECT_THAT( | 164 std::vector<std::string> results = |
161 FilePathsToStrings(GetDataSearchLocations(&env)), | 165 FilePathsToStrings(GetDataSearchLocations(&env)); |
162 ElementsAre("/system/path/1", | 166 ASSERT_EQ(3U, results.size()); |
Matt Giuca
2015/01/18 23:52:10
I'm not sure why you changed this from a nice Elem
| |
163 "/system/path/2")); | 167 EXPECT_THAT(results, Contains("/system/path/1")); |
168 EXPECT_THAT(results, Contains("/system/path/2")); | |
164 } | 169 } |
165 | 170 |
166 // Test that $XDG_DATA_DIRS falls back to the two default paths. | 171 // Test that $XDG_DATA_DIRS falls back to the two default paths. |
167 { | 172 { |
168 MockEnvironment env; | 173 MockEnvironment env; |
169 env.Set("HOME", "/home/user"); | 174 base::ScopedPathOverride home_override(base::DIR_HOME, |
175 base::FilePath("/home/user"), | |
176 true /* absolute? */, | |
177 false /* create? */); | |
170 env.Set("XDG_DATA_HOME", "/user/path"); | 178 env.Set("XDG_DATA_HOME", "/user/path"); |
171 EXPECT_THAT( | 179 EXPECT_THAT( |
172 FilePathsToStrings(GetDataSearchLocations(&env)), | 180 FilePathsToStrings(GetDataSearchLocations(&env)), |
173 ElementsAre("/user/path", | 181 ElementsAre("/user/path", |
174 "/usr/local/share", | 182 "/usr/local/share", |
175 "/usr/share")); | 183 "/usr/share")); |
176 } | 184 } |
177 } | 185 } |
178 | 186 |
179 TEST(ShellIntegrationTest, GetExistingShortcutLocations) { | 187 TEST(ShellIntegrationTest, GetExistingShortcutLocations) { |
(...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
309 GetExistingShortcutContents(&env, kTemplateFilepath, &contents)); | 317 GetExistingShortcutContents(&env, kTemplateFilepath, &contents)); |
310 EXPECT_EQ(kTestData1, contents); | 318 EXPECT_EQ(kTestData1, contents); |
311 } | 319 } |
312 | 320 |
313 // Test that it falls back to $HOME/.local/share/applications. | 321 // Test that it falls back to $HOME/.local/share/applications. |
314 { | 322 { |
315 base::ScopedTempDir temp_dir; | 323 base::ScopedTempDir temp_dir; |
316 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 324 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
317 | 325 |
318 MockEnvironment env; | 326 MockEnvironment env; |
319 env.Set("HOME", temp_dir.path().value()); | 327 base::ScopedPathOverride home_override(base::DIR_HOME, |
328 temp_dir.path(), | |
329 true /* absolute? */, | |
330 false /* create? */); | |
320 ASSERT_TRUE(base::CreateDirectory( | 331 ASSERT_TRUE(base::CreateDirectory( |
321 temp_dir.path().Append(".local/share/applications"))); | 332 temp_dir.path().Append(".local/share/applications"))); |
322 ASSERT_TRUE(WriteString( | 333 ASSERT_TRUE(WriteString( |
323 temp_dir.path().Append(".local/share/applications") | 334 temp_dir.path().Append(".local/share/applications") |
324 .Append(kTemplateFilename), | 335 .Append(kTemplateFilename), |
325 kTestData1)); | 336 kTestData1)); |
326 std::string contents; | 337 std::string contents; |
327 ASSERT_TRUE( | 338 ASSERT_TRUE( |
328 GetExistingShortcutContents(&env, kTemplateFilepath, &contents)); | 339 GetExistingShortcutContents(&env, kTemplateFilepath, &contents)); |
329 EXPECT_EQ(kTestData1, contents); | 340 EXPECT_EQ(kTestData1, contents); |
(...skipping 304 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
634 | 645 |
635 for (size_t i = 0; i < arraysize(test_cases); i++) { | 646 for (size_t i = 0; i < arraysize(test_cases); i++) { |
636 SCOPED_TRACE(i); | 647 SCOPED_TRACE(i); |
637 EXPECT_EQ(test_cases[i].expected_output, | 648 EXPECT_EQ(test_cases[i].expected_output, |
638 GetDirectoryFileContents(base::ASCIIToUTF16(test_cases[i].title), | 649 GetDirectoryFileContents(base::ASCIIToUTF16(test_cases[i].title), |
639 test_cases[i].icon_name)); | 650 test_cases[i].icon_name)); |
640 } | 651 } |
641 } | 652 } |
642 | 653 |
643 } // namespace shell_integration_linux | 654 } // namespace shell_integration_linux |
OLD | NEW |