1 // Copyright 2014 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.
5 // MediaGalleries gallery watch API browser tests.
7 #include "base/files/file_path.h"
8 #include "base/files/file_path_watcher.h"
9 #include "base/files/file_util.h"
10 #include "base/run_loop.h"
11 #include "base/strings/utf_string_conversions.h"
12 #include "build/build_config.h"
13 #include "chrome/browser/browser_process.h"
14 #include "chrome/browser/extensions/extension_apitest.h"
15 #include "chrome/browser/media_galleries/media_file_system_registry.h"
16 #include "chrome/browser/media_galleries/media_galleries_preferences.h"
17 #include "chrome/browser/media_galleries/media_galleries_test_util.h"
18 #include "chrome/common/chrome_paths.h"
19 #include "content/public/browser/render_frame_host.h"
20 #include "content/public/browser/render_view_host.h"
21 #include "extensions/browser/process_manager.h"
22 #include "extensions/common/extension.h"
23 #include "extensions/common/switches.h"
24 #include "extensions/test/extension_test_message_listener.h"
28 // Id of test extension from
29 // chrome/test/data/extensions/api_test/|kTestExtensionPath|
30 const char kTestExtensionId
[] = "gceegfkgibmgpfopknlcgleimclbknie";
31 const char kTestExtensionPath
[] = "media_galleries/gallerywatch";
34 const char kGetAllWatchedGalleryIdsCmd
[] = "getAllWatchedGalleryIds()";
35 const char kGetMediaFileSystemsCmd
[] = "getMediaFileSystems()";
36 const char kSetupWatchOnValidGalleriesCmd
[] = "setupWatchOnValidGalleries()";
37 const char kSetupWatchOnUnlistenedValidGalleriesCmd
[] =
38 "setupWatchOnUnlistenedValidGalleries()";
39 const char kAddGalleryChangedListenerCmd
[] = "addGalleryChangedListener()";
40 const char kAddCheckingGalleryChangedListenerCmd
[] =
41 "addCheckingGalleryChangedListener()";
42 const char kRemoveAllGalleryWatchCmd
[] = "removeAllGalleryWatch()";
43 const char kRemoveGalleryChangedListenerCmd
[] =
44 "removeGalleryChangedListener()";
45 const char kRemoveGalleryWatchCmd
[] = "removeGalleryWatch()";
46 const char kSetupWatchOnInvalidGalleryCmd
[] = "setupWatchOnInvalidGallery()";
48 // And JS reply messages.
49 const char kAddGalleryWatchOK
[] = "add_gallery_watch_ok";
50 const char kGetAllGalleryWatchOK
[] = "get_all_gallery_watch_ok";
51 const char kGetMediaFileSystemsCallbackOK
[] =
52 "get_media_file_systems_callback_ok";
53 const char kGetMediaFileSystemsOK
[] = "get_media_file_systems_ok";
54 const char kAddGalleryChangedListenerOK
[] = "add_gallery_changed_listener_ok";
55 const char kRemoveAllGalleryWatchOK
[] = "remove_all_gallery_watch_ok";
56 const char kRemoveGalleryChangedListenerOK
[] =
57 "remove_gallery_changed_listener_ok";
58 const char kRemoveGalleryWatchOK
[] = "remove_gallery_watch_ok";
59 const char kOnGalleryChangedCheckingOK
[] = "on_gallery_changed_checking_ok";
61 // Test reply messages.
62 const char kNoGalleryWatchesInstalled
[] = "gallery_watchers_does_not_exists";
63 const char kAddGalleryWatchRequestFailed
[] = "add_watch_request_failed";
64 const char kAddGalleryWatchRequestRuntimeError
[] =
65 "add_watch_request_runtime_error";
66 const char kAddGalleryWatchRequestSucceeded
[] = "add_watch_request_succeeded";
67 const char kGalleryChangedEventReceived
[] = "gallery_changed_event_received";
68 const char kGalleryWatchesCheck
[] = "gallery_watcher_checks";
72 ///////////////////////////////////////////////////////////////////////////////
73 // MediaGalleriesGalleryWatchApiTest //
74 ///////////////////////////////////////////////////////////////////////////////
76 class MediaGalleriesGalleryWatchApiTest
: public ExtensionApiTest
{
78 MediaGalleriesGalleryWatchApiTest()
79 : extension_(NULL
), background_host_(NULL
) {}
80 ~MediaGalleriesGalleryWatchApiTest() override
{}
83 // ExtensionApiTest overrides.
84 void SetUpCommandLine(base::CommandLine
* command_line
) override
{
85 ExtensionApiTest::SetUpCommandLine(command_line
);
86 command_line
->AppendSwitchASCII(
87 extensions::switches::kWhitelistedExtensionID
, kTestExtensionId
);
89 void SetUpOnMainThread() override
{
90 ExtensionApiTest::SetUpOnMainThread();
91 ensure_media_directories_exists_
.reset(new EnsureMediaDirectoriesExists
);
92 extension_
= LoadExtension(test_data_dir_
.AppendASCII(kTestExtensionPath
));
93 GetBackgroundHostForTestExtension();
95 FetchMediaGalleriesList();
97 void TearDownOnMainThread() override
{
99 background_host_
= NULL
;
100 ensure_media_directories_exists_
.reset();
101 ExtensionApiTest::TearDownOnMainThread();
104 bool GalleryWatchesSupported() {
105 return base::FilePathWatcher::RecursiveWatchAvailable();
108 void ExecuteCmdAndCheckReply(const std::string
& js_command
,
109 const std::string
& ok_message
) {
110 ExtensionTestMessageListener
listener(ok_message
, false);
111 background_host_
->GetMainFrame()->ExecuteJavaScriptForTests(
112 base::ASCIIToUTF16(js_command
));
113 EXPECT_TRUE(listener
.WaitUntilSatisfied());
116 bool AddNewFileInTestGallery() {
117 base::FilePath gallery_file
=
118 test_gallery_
.path().Append(FILE_PATH_LITERAL("test1.txt"));
119 std::string
content("new content");
121 base::WriteFile(gallery_file
, content
.c_str(), content
.length());
122 return (write_size
== static_cast<int>(content
.length()));
125 void SetupGalleryWatches() {
126 std::string expected_result
= GalleryWatchesSupported()
127 ? kAddGalleryWatchRequestSucceeded
128 : kAddGalleryWatchRequestFailed
;
130 ExtensionTestMessageListener
add_gallery_watch_finished(
131 expected_result
, false /* no reply */);
132 ExecuteCmdAndCheckReply(kSetupWatchOnValidGalleriesCmd
, kAddGalleryWatchOK
);
133 EXPECT_TRUE(add_gallery_watch_finished
.WaitUntilSatisfied());
137 void GetBackgroundHostForTestExtension() {
138 ASSERT_TRUE(extension_
);
139 background_host_
= extensions::ProcessManager::Get(browser()->profile())
140 ->GetBackgroundHostForExtension(extension_
->id())
141 ->render_view_host();
142 ASSERT_TRUE(background_host_
);
145 void CreateTestGallery() {
146 MediaGalleriesPreferences
* preferences
=
147 g_browser_process
->media_file_system_registry()->GetPreferences(
148 browser()->profile());
149 base::RunLoop runloop
;
150 preferences
->EnsureInitialized(runloop
.QuitClosure());
153 ASSERT_TRUE(test_gallery_
.CreateUniqueTempDir());
154 MediaGalleryPrefInfo gallery_info
;
156 preferences
->LookUpGalleryByPath(test_gallery_
.path(), &gallery_info
));
157 MediaGalleryPrefId id
=
158 preferences
->AddGallery(gallery_info
.device_id
,
160 MediaGalleryPrefInfo::kAutoDetected
,
161 gallery_info
.volume_label
,
162 gallery_info
.vendor_name
,
163 gallery_info
.model_name
,
164 gallery_info
.total_size_in_bytes
,
165 gallery_info
.last_attach_time
,
170 preferences
->SetGalleryPermissionForExtension(*extension_
, id
, true);
173 void FetchMediaGalleriesList() {
174 ExtensionTestMessageListener
get_media_systems_finished(
175 kGetMediaFileSystemsCallbackOK
, false /* no reply */);
176 ExecuteCmdAndCheckReply(kGetMediaFileSystemsCmd
, kGetMediaFileSystemsOK
);
177 EXPECT_TRUE(get_media_systems_finished
.WaitUntilSatisfied());
180 scoped_ptr
<EnsureMediaDirectoriesExists
> ensure_media_directories_exists_
;
182 base::ScopedTempDir test_gallery_
;
184 const extensions::Extension
* extension_
;
186 content::RenderViewHost
* background_host_
;
188 DISALLOW_COPY_AND_ASSIGN(MediaGalleriesGalleryWatchApiTest
);
191 IN_PROC_BROWSER_TEST_F(MediaGalleriesGalleryWatchApiTest
, BasicGalleryWatch
) {
192 // Add gallery watch listener.
193 ExecuteCmdAndCheckReply(kAddGalleryChangedListenerCmd
,
194 kAddGalleryChangedListenerOK
);
196 SetupGalleryWatches();
198 // Modify gallery contents.
199 ExtensionTestMessageListener
gallery_change_event_received(
200 kGalleryChangedEventReceived
, false /* no reply */);
202 ASSERT_TRUE(AddNewFileInTestGallery());
203 if (GalleryWatchesSupported())
204 EXPECT_TRUE(gallery_change_event_received
.WaitUntilSatisfied());
206 // Remove gallery watch listener.
207 ExecuteCmdAndCheckReply(kRemoveGalleryChangedListenerCmd
,
208 kRemoveGalleryChangedListenerOK
);
210 // Remove gallery watch request.
211 if (GalleryWatchesSupported())
212 ExecuteCmdAndCheckReply(kRemoveGalleryWatchCmd
, kRemoveGalleryWatchOK
);
215 IN_PROC_BROWSER_TEST_F(MediaGalleriesGalleryWatchApiTest
,
216 CorrectResponseOnModifyingWatchedGallery
) {
217 if (!GalleryWatchesSupported())
220 // Add gallery watch listener.
221 ExecuteCmdAndCheckReply(kAddCheckingGalleryChangedListenerCmd
,
222 kAddGalleryChangedListenerOK
);
223 SetupGalleryWatches();
226 // Modify gallery contents; expect correct details.
227 ExtensionTestMessageListener
got_correct_details(
228 kOnGalleryChangedCheckingOK
, false);
229 ASSERT_TRUE(AddNewFileInTestGallery());
230 EXPECT_TRUE(got_correct_details
.WaitUntilSatisfied());
233 IN_PROC_BROWSER_TEST_F(MediaGalleriesGalleryWatchApiTest
,
234 RemoveListenerAndModifyGallery
) {
235 if (!GalleryWatchesSupported())
238 // Add a gallery watch listener.
239 ExecuteCmdAndCheckReply(kAddGalleryChangedListenerCmd
,
240 kAddGalleryChangedListenerOK
);
241 SetupGalleryWatches();
243 // Modify gallery contents.
244 ExtensionTestMessageListener
gallery_change_event_received(
245 kGalleryChangedEventReceived
, false /* no reply */);
246 ASSERT_TRUE(AddNewFileInTestGallery());
247 EXPECT_TRUE(gallery_change_event_received
.WaitUntilSatisfied());
249 // Remove gallery watch listener.
250 ExecuteCmdAndCheckReply(kRemoveGalleryChangedListenerCmd
,
251 kRemoveGalleryChangedListenerOK
);
253 // No listener, modify gallery contents.
254 ASSERT_TRUE(AddNewFileInTestGallery());
256 // Remove gallery watch.
257 ExecuteCmdAndCheckReply(kRemoveGalleryWatchCmd
, kRemoveGalleryWatchOK
);
260 IN_PROC_BROWSER_TEST_F(MediaGalleriesGalleryWatchApiTest
,
261 SetupGalleryWatchWithoutListeners
) {
262 if (!GalleryWatchesSupported())
265 ExecuteCmdAndCheckReply(kSetupWatchOnUnlistenedValidGalleriesCmd
,
266 kAddGalleryWatchRequestRuntimeError
);
269 IN_PROC_BROWSER_TEST_F(MediaGalleriesGalleryWatchApiTest
,
270 SetupGalleryChangedListenerWithoutWatchers
) {
271 // Add gallery watch listener.
272 ExecuteCmdAndCheckReply(kAddGalleryChangedListenerCmd
,
273 kAddGalleryChangedListenerOK
);
275 // Modify gallery contents. Listener should not get called because add watch
276 // request was not called.
277 ExtensionTestMessageListener
gallery_change_event_received(
278 kGalleryChangedEventReceived
, false /* no reply */);
279 ASSERT_TRUE(AddNewFileInTestGallery());
281 // Remove gallery watch listener.
282 ExecuteCmdAndCheckReply(kRemoveGalleryChangedListenerCmd
,
283 kRemoveGalleryChangedListenerOK
);
286 IN_PROC_BROWSER_TEST_F(MediaGalleriesGalleryWatchApiTest
,
287 SetupWatchOnInvalidGallery
) {
288 // Add gallery watch listener.
289 ExecuteCmdAndCheckReply(kAddGalleryChangedListenerCmd
,
290 kAddGalleryChangedListenerOK
);
291 // Set up a invalid gallery watch.
292 ExecuteCmdAndCheckReply(
293 kSetupWatchOnInvalidGalleryCmd
, kAddGalleryWatchRequestFailed
);
296 IN_PROC_BROWSER_TEST_F(MediaGalleriesGalleryWatchApiTest
, GetAllGalleryWatch
) {
297 // Add gallery watch listener.
298 ExecuteCmdAndCheckReply(kAddGalleryChangedListenerCmd
,
299 kAddGalleryChangedListenerOK
);
301 // Gallery watchers are not yet added.
302 // chrome.mediaGalleries.getAllGalleryWatch should return an empty
304 ExtensionTestMessageListener
initial_get_all_check_finished(
305 kNoGalleryWatchesInstalled
, false /* no reply */);
306 ExecuteCmdAndCheckReply(kGetAllWatchedGalleryIdsCmd
, kGetAllGalleryWatchOK
);
307 EXPECT_TRUE(initial_get_all_check_finished
.WaitUntilSatisfied());
309 if (!GalleryWatchesSupported())
312 SetupGalleryWatches();
314 // chrome.mediaGalleries.getAllGalleryWatch should return the
315 // gallery identifiers.
316 ExtensionTestMessageListener
get_all_watched_galleries_finished(
317 kGalleryWatchesCheck
, false /* no reply */);
318 ExecuteCmdAndCheckReply(kGetAllWatchedGalleryIdsCmd
, kGetAllGalleryWatchOK
);
319 EXPECT_TRUE(get_all_watched_galleries_finished
.WaitUntilSatisfied());
321 // Remove gallery watch request.
322 ExecuteCmdAndCheckReply(kRemoveGalleryWatchCmd
, kRemoveGalleryWatchOK
);
324 // Gallery watchers removed.
325 // chrome.mediaGalleries.getAllGalleryWatch() should return an empty
327 ExtensionTestMessageListener
final_get_all_check_finished(
328 kNoGalleryWatchesInstalled
, false /* no reply */);
329 ExecuteCmdAndCheckReply(kGetAllWatchedGalleryIdsCmd
, kGetAllGalleryWatchOK
);
330 EXPECT_TRUE(final_get_all_check_finished
.WaitUntilSatisfied());
333 IN_PROC_BROWSER_TEST_F(MediaGalleriesGalleryWatchApiTest
,
334 RemoveAllGalleryWatch
) {
335 // Add gallery watch listener.
336 ExecuteCmdAndCheckReply(kAddGalleryChangedListenerCmd
,
337 kAddGalleryChangedListenerOK
);
339 if (!GalleryWatchesSupported())
342 SetupGalleryWatches();
344 // chrome.mediaGalleries.getAllGalleryWatch should return the watched
345 // gallery identifiers.
346 ExtensionTestMessageListener
get_all_watched_galleries_finished(
347 kGalleryWatchesCheck
, false /* no reply */);
348 ExecuteCmdAndCheckReply(kGetAllWatchedGalleryIdsCmd
, kGetAllGalleryWatchOK
);
349 EXPECT_TRUE(get_all_watched_galleries_finished
.WaitUntilSatisfied());
351 // Remove all gallery watchers.
352 ExecuteCmdAndCheckReply(kRemoveAllGalleryWatchCmd
, kRemoveAllGalleryWatchOK
);
354 // Gallery watchers removed. chrome.mediaGalleries.getAllGalleryWatch
355 // should return an empty list.
356 ExtensionTestMessageListener
final_get_all_check_finished(
357 kNoGalleryWatchesInstalled
, false /* no reply */);
358 ExecuteCmdAndCheckReply(kGetAllWatchedGalleryIdsCmd
, kGetAllGalleryWatchOK
);
359 EXPECT_TRUE(final_get_all_check_finished
.WaitUntilSatisfied());