OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 // MediaGalleriesPreferences unit tests. | |
6 | |
7 #include "chrome/browser/media_gallery/media_galleries_preferences.h" | |
8 | |
9 #include "base/command_line.h" | |
10 #include "base/file_util.h" | |
11 #include "base/memory/ref_counted.h" | |
12 #include "base/memory/scoped_ptr.h" | |
13 #include "base/message_loop.h" | |
14 #include "base/utf_string_conversions.h" | |
15 #include "base/values.h" | |
16 #include "chrome/browser/extensions/extension_service.h" | |
17 #include "chrome/browser/extensions/extension_system.h" | |
18 #include "chrome/browser/extensions/test_extension_system.h" | |
19 #include "chrome/browser/media_gallery/media_file_system_registry.h" | |
20 #include "chrome/common/chrome_switches.h" | |
21 #include "chrome/common/extensions/extension.h" | |
22 #include "chrome/common/extensions/extension_manifest_constants.h" | |
23 #include "chrome/test/base/testing_profile.h" | |
24 #include "content/public/test/test_browser_thread.h" | |
25 #include "testing/gtest/include/gtest/gtest.h" | |
26 | |
27 namespace chrome { | |
28 | |
29 namespace { | |
30 | |
31 class TestMediaGalleriesPreferences : public MediaGalleriesPreferences { | |
32 public: | |
33 static string16 GetDisplayNameForPath(const FilePath& path) { | |
34 return MediaGalleriesPreferences::ComputeDisplayName(path); | |
35 } | |
36 }; | |
37 | |
38 class MediaGalleriesPreferencesTest : public testing::Test { | |
39 public: | |
40 MediaGalleriesPreferencesTest() | |
41 : ui_thread_(content::BrowserThread::UI, &loop_), | |
42 file_thread_(content::BrowserThread::FILE, &loop_), | |
43 profile_(new TestingProfile()), | |
44 extension_service_(NULL) { | |
45 } | |
46 | |
47 virtual ~MediaGalleriesPreferencesTest() {} | |
48 | |
49 virtual void SetUp() OVERRIDE { | |
50 CommandLine::ForCurrentProcess()->AppendSwitch( | |
51 switches::kEnableMediaGalleryUI); | |
52 extensions_dir_ = profile_->GetPath().AppendASCII("Extensions"); | |
53 ASSERT_TRUE(file_util::CreateDirectory(extensions_dir_)); | |
54 | |
55 extensions::TestExtensionSystem* extension_system( | |
56 static_cast<extensions::TestExtensionSystem*>( | |
57 extensions::ExtensionSystem::Get(profile_.get()))); | |
58 extension_service_ = extension_system->CreateExtensionService( | |
59 CommandLine::ForCurrentProcess(), extensions_dir_, false); | |
60 | |
61 MediaGalleriesPreferences::RegisterUserPrefs(profile_->GetPrefs()); | |
62 gallery_prefs_.reset(new MediaGalleriesPreferences(profile_.get())); | |
63 | |
64 // Load the default galleries into the expectations. | |
65 if (gallery_prefs_->known_galleries().size()) { | |
66 const MediaGalleriesPrefInfoMap& known_galleries = | |
67 gallery_prefs_->known_galleries(); | |
68 ASSERT_EQ(1U, known_galleries.size()); | |
69 MediaGalleriesPrefInfoMap::const_iterator it = known_galleries.begin(); | |
70 expected_galleries_[it->first] = it->second; | |
71 } | |
72 | |
73 std::vector<std::string> read_permissions; | |
74 read_permissions.push_back("mediaGalleriesRead"); | |
75 std::vector<std::string> all_permissions; | |
76 read_permissions.push_back("mediaGalleriesAllGalleries"); | |
77 read_permissions.push_back("mediaGalleriesRead"); | |
78 | |
79 all_permission_extension = AddExtension("all", all_permissions); | |
80 regular_permission_extension = AddExtension("regular", read_permissions); | |
81 no_permissions_extension = AddExtension("no", read_permissions); | |
82 } | |
83 | |
84 virtual void TearDown() OVERRIDE { | |
85 Verify(); | |
86 } | |
87 | |
88 void Verify() { | |
89 const MediaGalleriesPrefInfoMap& known_galleries = | |
90 gallery_prefs_->known_galleries(); | |
91 EXPECT_EQ(expected_galleries_.size(), known_galleries.size()); | |
92 for (MediaGalleriesPrefInfoMap::const_iterator it = known_galleries.begin(); | |
93 it != known_galleries.end(); | |
94 ++it) { | |
95 VerifyGalleryInfo(&it->second, it->first); | |
96 } | |
97 | |
98 std::set<MediaGalleryPrefId> galleries_for_all = | |
99 gallery_prefs_->GalleriesForExtension(*all_permission_extension.get()); | |
100 EXPECT_EQ(expected_galleries_for_all, galleries_for_all); | |
101 printf("All: %zu\n", galleries_for_all.size()); | |
Lei Zhang
2012/08/03 19:22:04
debug code?
vandebo (ex-Chrome)
2012/08/03 20:27:27
Yea, tests are still coming along. Removed.
| |
102 | |
103 std::set<MediaGalleryPrefId> galleries_for_regular = | |
104 gallery_prefs_->GalleriesForExtension( | |
105 *regular_permission_extension.get()); | |
106 EXPECT_EQ(expected_galleries_for_regular, galleries_for_regular); | |
107 | |
108 std::set<MediaGalleryPrefId> galleries_for_no = | |
109 gallery_prefs_->GalleriesForExtension(*no_permissions_extension.get()); | |
110 EXPECT_EQ(0U, galleries_for_no.size()); | |
111 } | |
112 | |
113 void VerifyGalleryInfo(const MediaGalleryPrefInfo* actual, | |
114 MediaGalleryPrefId expected_id) const { | |
115 MediaGalleriesPrefInfoMap::const_iterator in_expectation = | |
116 expected_galleries_.find(expected_id); | |
117 EXPECT_NE(in_expectation, expected_galleries_.end()); | |
118 EXPECT_EQ(in_expectation->second.pref_id, actual->pref_id); | |
119 EXPECT_EQ(in_expectation->second.display_name, actual->display_name); | |
120 EXPECT_EQ(in_expectation->second.device_id, actual->device_id); | |
121 EXPECT_EQ(in_expectation->second.path.value(), actual->path.value()); | |
122 EXPECT_EQ(in_expectation->second.type, actual->type); | |
123 } | |
124 | |
125 MediaGalleriesPreferences* gallery_prefs() { | |
126 return gallery_prefs_.get(); | |
127 } | |
128 | |
129 void AddGalleryExpectation(MediaGalleryPrefId id, std::string display_name, | |
130 std::string device_id, FilePath::StringType path, | |
131 MediaGalleryPrefInfo::Type type) { | |
132 expected_galleries_[id].pref_id = id; | |
133 expected_galleries_[id].display_name = ASCIIToUTF16(display_name); | |
134 expected_galleries_[id].device_id = device_id; | |
135 expected_galleries_[id].path = FilePath(path); | |
136 expected_galleries_[id].type = type; | |
137 | |
138 if (type == MediaGalleryPrefInfo::kAutoDetected) | |
139 expected_galleries_for_all.insert(id); | |
140 } | |
141 | |
142 scoped_refptr<extensions::Extension> all_permission_extension; | |
143 scoped_refptr<extensions::Extension> regular_permission_extension; | |
144 scoped_refptr<extensions::Extension> no_permissions_extension; | |
145 | |
146 std::set<MediaGalleryPrefId> expected_galleries_for_all; | |
147 std::set<MediaGalleryPrefId> expected_galleries_for_regular; | |
148 | |
149 MediaGalleriesPrefInfoMap expected_galleries_; | |
150 | |
151 private: | |
152 scoped_refptr<extensions::Extension> AddExtension( | |
153 std::string name, | |
154 std::vector<std::string> permissions) { | |
155 scoped_ptr<DictionaryValue> manifest(new DictionaryValue); | |
156 manifest->SetString(extension_manifest_keys::kName, name); | |
157 manifest->SetString(extension_manifest_keys::kVersion, "0.1"); | |
158 ListValue* permission_list = new ListValue;; | |
159 for (size_t i = 0; i < permissions.size(); i++) | |
160 permission_list->Append(Value::CreateStringValue(permissions[i])); | |
161 manifest->Set(extension_manifest_keys::kPermissions, permission_list); | |
162 | |
163 FilePath path = extensions_dir_.AppendASCII(name); | |
164 std::string errors; | |
165 scoped_refptr<extensions::Extension> extension = | |
166 extensions::Extension::Create(path, | |
167 extensions::Extension::INTERNAL, | |
168 *manifest.get(), | |
169 extensions::Extension::NO_FLAGS, | |
170 &errors); | |
171 EXPECT_TRUE(extension.get() != NULL); | |
172 EXPECT_TRUE(extensions::Extension::IdIsValid(extension->id())); | |
173 if (!extension.get() || | |
174 !extensions::Extension::IdIsValid(extension->id())) { | |
175 return NULL; | |
176 } | |
177 | |
178 extension_service_->extension_prefs()->OnExtensionInstalled( | |
179 extension, extensions::Extension::ENABLED, false, | |
180 StringOrdinal::CreateInitialOrdinal()); | |
181 | |
182 return extension; | |
183 } | |
184 | |
185 // Needed for extension service & friends to work. | |
186 MessageLoop loop_; | |
187 content::TestBrowserThread ui_thread_; | |
188 content::TestBrowserThread file_thread_; | |
189 | |
190 scoped_ptr<TestingProfile> profile_; | |
191 scoped_ptr<MediaGalleriesPreferences> gallery_prefs_; | |
192 FilePath extensions_dir_; | |
193 ExtensionService* extension_service_; | |
194 | |
195 DISALLOW_COPY_AND_ASSIGN(MediaGalleriesPreferencesTest); | |
196 }; | |
197 | |
198 FilePath MakePath(std::string dir) { | |
199 #if defined(OS_WINDOWS) | |
200 return FilePath(FILE_PATH_LITERAL("C:")).Append(UTF8ToWide(dir)); | |
201 #elif defined(OS_POSIX) | |
202 return FilePath(FILE_PATH_LITERAL("/")).Append(dir); | |
203 #else | |
204 NOTREACHED(); | |
205 #endif | |
206 } | |
207 | |
208 TEST_F(MediaGalleriesPreferencesTest, DISABLED_GalleryManagement) { | |
209 MediaGalleryPrefId auto_id, user_added_id, id; | |
210 FilePath path; | |
211 std::string device_id; | |
212 ASSERT_EQ(1UL, gallery_prefs()->known_galleries().begin()->first); | |
213 Verify(); | |
214 | |
215 // Add a new auto detected gallery. | |
216 path = MakePath("new_auto"); | |
217 device_id = MediaFileSystemRegistry::GetInstance()->GetDeviceIdFromPath(path); | |
218 id = gallery_prefs()->AddGallery(device_id, ASCIIToUTF16("NewAutoGallery"), | |
219 path, false /*auto*/); | |
220 EXPECT_EQ(2UL, id); | |
221 auto_id = id; | |
222 AddGalleryExpectation(id, "NewAutoGallery", device_id, | |
223 FILE_PATH_LITERAL("new_auto"), | |
224 MediaGalleryPrefInfo::kAutoDetected); | |
225 Verify(); | |
226 | |
227 // Add it again (as user), nothing should happen. | |
228 id = gallery_prefs()->AddGallery(device_id, ASCIIToUTF16("NewAutoGallery"), | |
229 path, true /*user*/); | |
230 EXPECT_EQ(auto_id, id); | |
231 Verify(); | |
232 | |
233 // Add a new user added gallery. | |
234 path = MakePath("new_user"); | |
235 device_id = MediaFileSystemRegistry::GetInstance()->GetDeviceIdFromPath(path); | |
236 id = gallery_prefs()->AddGallery(device_id, ASCIIToUTF16("NewUserGallery"), | |
237 path, true /*user*/); | |
238 EXPECT_EQ(3UL, id); | |
239 user_added_id = id; | |
240 AddGalleryExpectation(id, "NewUserGallery", device_id, | |
241 FILE_PATH_LITERAL("new_user"), | |
242 MediaGalleryPrefInfo::kUserAdded); | |
243 Verify(); | |
244 | |
245 // Lookup some galleries. | |
246 EXPECT_TRUE(gallery_prefs()->LookUpGalleryByPath(MakePath("new_auto"), NULL)); | |
247 EXPECT_TRUE(gallery_prefs()->LookUpGalleryByPath(MakePath("new_user"), NULL)); | |
248 EXPECT_FALSE(gallery_prefs()->LookUpGalleryByPath(MakePath("other"), NULL)); | |
249 | |
250 // Check that we always get the gallery info. | |
251 MediaGalleryPrefInfo gallery_info; | |
252 EXPECT_TRUE(gallery_prefs()->LookUpGalleryByPath(MakePath("new_auto"), | |
253 &gallery_info)); | |
254 VerifyGalleryInfo(&gallery_info, auto_id); | |
255 EXPECT_TRUE(gallery_prefs()->LookUpGalleryByPath(MakePath("new_user"), | |
256 &gallery_info)); | |
257 VerifyGalleryInfo(&gallery_info, user_added_id); | |
258 | |
259 path = MakePath("other"); | |
260 EXPECT_FALSE(gallery_prefs()->LookUpGalleryByPath(path, &gallery_info)); | |
261 EXPECT_EQ(kInvalidMediaGalleryPrefId, gallery_info.pref_id); | |
262 EXPECT_EQ(TestMediaGalleriesPreferences::GetDisplayNameForPath(path), | |
263 gallery_info.display_name); | |
264 EXPECT_EQ(MediaFileSystemRegistry::GetInstance()->GetDeviceIdFromPath(path), | |
265 gallery_info.device_id); | |
266 EXPECT_EQ(FilePath(FILE_PATH_LITERAL("other")).value(), | |
267 gallery_info.path.value()); | |
268 | |
269 // Remove an auto added gallery (i.e. make it blacklisted). | |
270 gallery_prefs()->ForgetGalleryById(auto_id); | |
271 expected_galleries_[auto_id].type = MediaGalleryPrefInfo::kBlackListed; | |
272 Verify(); | |
273 | |
274 // Remove a user added gallery and it should go away. | |
275 gallery_prefs()->ForgetGalleryById(user_added_id); | |
276 expected_galleries_.erase(user_added_id); | |
277 Verify(); | |
278 } | |
279 | |
280 TEST_F(MediaGalleriesPreferencesTest, DISABLED_GalleryPermissions) { | |
281 MediaGalleryPrefId auto_id, user_added_id, to_blacklist_id, id; | |
282 FilePath path; | |
283 std::string device_id; | |
284 ASSERT_EQ(1UL, gallery_prefs()->known_galleries().begin()->first); | |
285 Verify(); | |
286 | |
287 // Add some galleries to test with. | |
288 path = MakePath("new_user"); | |
289 device_id = MediaFileSystemRegistry::GetInstance()->GetDeviceIdFromPath(path); | |
290 id = gallery_prefs()->AddGallery(device_id, ASCIIToUTF16("NewUserGallery"), | |
291 path, true /*user*/); | |
292 EXPECT_EQ(2UL, id); | |
293 user_added_id = id; | |
294 AddGalleryExpectation(id, "NewUserGallery", device_id, | |
295 FILE_PATH_LITERAL("new_user"), | |
296 MediaGalleryPrefInfo::kUserAdded); | |
297 Verify(); | |
298 | |
299 path = MakePath("new_auto"); | |
300 device_id = MediaFileSystemRegistry::GetInstance()->GetDeviceIdFromPath(path); | |
301 id = gallery_prefs()->AddGallery(device_id, ASCIIToUTF16("NewAutoGallery"), | |
302 path, false /*auto*/); | |
303 EXPECT_EQ(3UL, id); | |
304 auto_id = id; | |
305 AddGalleryExpectation(id, "NewAutoGallery", device_id, | |
306 FILE_PATH_LITERAL("new_auto"), | |
307 MediaGalleryPrefInfo::kAutoDetected); | |
308 Verify(); | |
309 | |
310 path = MakePath("to_blacklist"); | |
311 device_id = MediaFileSystemRegistry::GetInstance()->GetDeviceIdFromPath(path); | |
312 id = gallery_prefs()->AddGallery(device_id, | |
313 ASCIIToUTF16("ToBlacklistGallery"), path, | |
314 false /*auto*/); | |
315 EXPECT_EQ(4UL, id); | |
316 to_blacklist_id = id; | |
317 AddGalleryExpectation(id, "ToBlacklistGallery", device_id, | |
318 FILE_PATH_LITERAL("to_blacklist"), | |
319 MediaGalleryPrefInfo::kAutoDetected); | |
320 Verify(); | |
321 } | |
322 | |
323 } // namespace | |
324 | |
325 } // namespace chrome | |
OLD | NEW |