summaryrefslogtreecommitdiffstats
path: root/chrome/browser/extensions/api/media_galleries_private/media_galleries_watch_apitest.cc
blob: 9f83c21206d3a86c9babb282d63b4c73f2c1ce1b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// MediaGalleriesPrivate gallery watch API browser tests.

#include "base/file_util.h"
#include "base/files/file_path.h"
#include "base/path_service.h"
#include "base/strings/utf_string_conversions.h"
#include "build/build_config.h"
#include "chrome/browser/extensions/extension_apitest.h"
#include "chrome/browser/extensions/extension_service.h"
#include "chrome/browser/extensions/extension_system.h"
#include "chrome/browser/extensions/extension_test_message_listener.h"
#include "chrome/browser/media_galleries/media_galleries_test_util.h"
#include "chrome/common/chrome_paths.h"
#include "chrome/common/chrome_switches.h"
#include "content/public/browser/render_view_host.h"
#include "extensions/common/extension.h"

namespace {

// Id of test extension from
// chrome/test/data/extensions/api_test/|kTestExtensionPath|
const char kTestExtensionId[] = "gceegfkgibmgpfopknlcgleimclbknie";
const char kTestExtensionPath[] = "media_galleries_private/gallerywatch";

#if !defined(OS_CHROMEOS)
// JS commands.
const char kGetAllWatchedGalleryIdsCmd[] = "getAllWatchedGalleryIds()";
const char kGetMediaFileSystemsCmd[] = "getMediaFileSystems()";
const char kSetupWatchOnValidGalleriesCmd[] = "setupWatchOnValidGalleries()";
#if defined(OS_WIN)
const char kAddGalleryChangedListenerCmd[] = "addGalleryChangedListener()";
const char kRemoveAllGalleryWatchCmd[] = "removeAllGalleryWatch()";
const char kRemoveGalleryChangedListenerCmd[] =
    "removeGalleryChangedListener()";
const char kRemoveGalleryWatchCmd[] = "removeGalleryWatch()";
const char kSetupWatchOnInvalidGalleryCmd[] = "setupWatchOnInvalidGallery()";
#endif  // defined(OS_WIN)

// And JS reply messages.
const char kAddGalleryWatchOK[] = "add_gallery_watch_ok";
const char kGetAllGalleryWatchOK[] = "get_all_gallery_watch_ok";
const char kGetMediaFileSystemsCallbackOK[] =
    "get_media_file_systems_callback_ok";
const char kGetMediaFileSystemsOK[] = "get_media_file_systems_ok";
#if defined(OS_WIN)
const char kAddGalleryChangedListenerOK[] = "add_gallery_changed_listener_ok";
const char kRemoveAllGalleryWatchOK[] = "remove_all_gallery_watch_ok";
const char kRemoveGalleryChangedListenerOK[] =
    "remove_gallery_changed_listener_ok";
const char kRemoveGalleryWatchOK[] = "remove_gallery_watch_ok";
#endif  // defined(OS_WIN)

// Test reply messages.
const char kGetAllGalleryWatchResultA[] = "gallery_watchers_does_not_exists";
const char kAddGalleryWatchRequestFailed[] = "add_watch_request_failed";
#if defined(OS_WIN)
const char kAddGalleryWatchRequestSucceeded[] = "add_watch_request_succeeded";
const char kGalleryChangedEventReceived[] = "gallery_changed_event_received";
const char kGetAllGalleryWatchResultB[] =
    "watchers_for_galleries_{1, 2, 3}_found";
#endif  // defined(OS_WIN)
#endif  // !defined(OS_CHROMEOS)

}  // namespace


///////////////////////////////////////////////////////////////////////////////
//                 MediaGalleriesPrivateGalleryWatchApiTest                  //
///////////////////////////////////////////////////////////////////////////////

class MediaGalleriesPrivateGalleryWatchApiTest : public ExtensionApiTest {
 public:
  MediaGalleriesPrivateGalleryWatchApiTest() {}
  virtual ~MediaGalleriesPrivateGalleryWatchApiTest() {}

 protected:
  // ExtensionApiTest overrides.
  virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
    ExtensionApiTest::SetUpCommandLine(command_line);
    command_line->AppendSwitchASCII(switches::kWhitelistedExtensionID,
                                    kTestExtensionId);
  }

  void ExecuteCmdAndCheckReply(content::RenderViewHost* host,
                              const std::string& js_command,
                              const std::string& ok_message) {
    ExtensionTestMessageListener listener(ok_message, false);
    host->ExecuteJavascriptInWebFrame(base::string16(),
                                      base::ASCIIToUTF16(js_command));
    EXPECT_TRUE(listener.WaitUntilSatisfied());
  }

  bool AddNewFileInGallery(int gallery_directory_key) {
    if ((gallery_directory_key != chrome::DIR_USER_MUSIC) &&
        (gallery_directory_key != chrome::DIR_USER_PICTURES) &&
        (gallery_directory_key != chrome::DIR_USER_VIDEOS))
      return false;

    base::FilePath gallery_dir;
    if (!PathService::Get(gallery_directory_key, &gallery_dir))
      return false;
    base::FilePath gallery_file =
        gallery_dir.Append(FILE_PATH_LITERAL("test1.txt"));
    std::string content("new content");
    int write_size = file_util::WriteFile(gallery_file, content.c_str(),
                                          content.length());
    return (write_size == static_cast<int>(content.length()));
  }

  // Loads the test extension and returns the RenderViewHost of the extension.
  // Returns NULL if the extension load operation failed.
  content::RenderViewHost* GetBackgroundHostForTestExtension() {
    const extensions::Extension* extension =
        LoadExtension(test_data_dir_.AppendASCII(kTestExtensionPath));
    if (!extension)
      return NULL;
    return extensions::ExtensionSystem::Get(browser()->profile())->
        process_manager()->GetBackgroundHostForExtension(extension->id())->
           render_view_host();
  }

 private:
  DISALLOW_COPY_AND_ASSIGN(MediaGalleriesPrivateGalleryWatchApiTest);
};


///////////////////////////////////////////////////////////////////////////////
//                               TESTS                                       //
///////////////////////////////////////////////////////////////////////////////
#if defined(OS_WIN)
IN_PROC_BROWSER_TEST_F(MediaGalleriesPrivateGalleryWatchApiTest,
                       BasicGalleryWatch) {
  EnsureMediaDirectoriesExists media_directories;
  content::RenderViewHost* host = GetBackgroundHostForTestExtension();
  ASSERT_TRUE(host);

  // Get media file systems.
  ExtensionTestMessageListener get_media_systems_finished(
      kGetMediaFileSystemsCallbackOK, false  /* no reply */);
  ExecuteCmdAndCheckReply(host, kGetMediaFileSystemsCmd,
                          kGetMediaFileSystemsOK);
  EXPECT_TRUE(get_media_systems_finished.WaitUntilSatisfied());

  // Set up gallery watch.
  ExtensionTestMessageListener add_gallery_watch_finished(
      kAddGalleryWatchRequestSucceeded, false  /* no reply */);
  ExecuteCmdAndCheckReply(host, kSetupWatchOnValidGalleriesCmd,
                          kAddGalleryWatchOK);
  EXPECT_TRUE(add_gallery_watch_finished.WaitUntilSatisfied());

  // Add gallery watch listener.
  ExecuteCmdAndCheckReply(host, kAddGalleryChangedListenerCmd,
                          kAddGalleryChangedListenerOK);

  // Modify gallery contents.
  ExtensionTestMessageListener music_gallery_change_event_received(
      kGalleryChangedEventReceived, false  /* no reply */);
  ASSERT_TRUE(AddNewFileInGallery(chrome::DIR_USER_MUSIC));
  EXPECT_TRUE(music_gallery_change_event_received.WaitUntilSatisfied());

  ExtensionTestMessageListener pictures_gallery_change_event_received(
      kGalleryChangedEventReceived, false  /* no reply */);
  ASSERT_TRUE(AddNewFileInGallery(chrome::DIR_USER_PICTURES));
  EXPECT_TRUE(pictures_gallery_change_event_received.WaitUntilSatisfied());

  ExtensionTestMessageListener videos_gallery_change_event_received(
      kGalleryChangedEventReceived, false  /* no reply */);
  ASSERT_TRUE(AddNewFileInGallery(chrome::DIR_USER_VIDEOS));
  EXPECT_TRUE(videos_gallery_change_event_received.WaitUntilSatisfied());

  // Remove gallery watch listener.
  ExecuteCmdAndCheckReply(host, kRemoveGalleryChangedListenerCmd,
                          kRemoveGalleryChangedListenerOK);

  // Remove gallery watch request.
  ExecuteCmdAndCheckReply(host, kRemoveGalleryWatchCmd, kRemoveGalleryWatchOK);
}

IN_PROC_BROWSER_TEST_F(MediaGalleriesPrivateGalleryWatchApiTest,
                       RemoveListenerAndModifyGallery) {
  EnsureMediaDirectoriesExists media_directories;
  content::RenderViewHost* host = GetBackgroundHostForTestExtension();
  ASSERT_TRUE(host);

  // Get media file systems.
  ExtensionTestMessageListener get_media_systems_finished(
      kGetMediaFileSystemsCallbackOK, false  /* no reply */);
  ExecuteCmdAndCheckReply(host, kGetMediaFileSystemsCmd,
                          kGetMediaFileSystemsOK);
  EXPECT_TRUE(get_media_systems_finished.WaitUntilSatisfied());

  // Set up gallery watch.
  ExtensionTestMessageListener add_gallery_watch_finished(
      kAddGalleryWatchRequestSucceeded, false  /* no reply */);
  ExecuteCmdAndCheckReply(host, kSetupWatchOnValidGalleriesCmd,
                         kAddGalleryWatchOK);
  EXPECT_TRUE(add_gallery_watch_finished.WaitUntilSatisfied());

  // Add a gallery watch listener.
  ExecuteCmdAndCheckReply(host, kAddGalleryChangedListenerCmd,
                          kAddGalleryChangedListenerOK);
  // Modify gallery contents.
  ExtensionTestMessageListener music_gallery_change_event_received(
      kGalleryChangedEventReceived, false  /* no reply */);
  ASSERT_TRUE(AddNewFileInGallery(chrome::DIR_USER_MUSIC));
  EXPECT_TRUE(music_gallery_change_event_received.WaitUntilSatisfied());

  // Remove gallery watch listener.
  ExecuteCmdAndCheckReply(host, kRemoveGalleryChangedListenerCmd,
                          kRemoveGalleryChangedListenerOK);

  // No listener, modify gallery contents.
  ASSERT_TRUE(AddNewFileInGallery(chrome::DIR_USER_MUSIC));

  // Remove gallery watch.
  ExecuteCmdAndCheckReply(host, kRemoveGalleryWatchCmd, kRemoveGalleryWatchOK);
}

IN_PROC_BROWSER_TEST_F(MediaGalleriesPrivateGalleryWatchApiTest,
                       SetupGalleryWatchWithoutListeners) {
  EnsureMediaDirectoriesExists media_directories;
  content::RenderViewHost* host = GetBackgroundHostForTestExtension();
  ASSERT_TRUE(host);

  // Get media file systems.
  ExtensionTestMessageListener get_media_systems_finished(
      kGetMediaFileSystemsCallbackOK, false  /* no reply */);
  ExecuteCmdAndCheckReply(host, kGetMediaFileSystemsCmd,
                          kGetMediaFileSystemsOK);
  EXPECT_TRUE(get_media_systems_finished.WaitUntilSatisfied());

  // Set up gallery watch.
  ExecuteCmdAndCheckReply(host, kSetupWatchOnValidGalleriesCmd,
                          kAddGalleryWatchOK);

  // No listeners, modify gallery contents.
  ExtensionTestMessageListener music_gallery_change_event_received(
      kGalleryChangedEventReceived, false  /* no reply */);
  ASSERT_TRUE(AddNewFileInGallery(chrome::DIR_USER_MUSIC));

  // Remove gallery watch.
  ExecuteCmdAndCheckReply(host, kRemoveGalleryWatchCmd, kRemoveGalleryWatchOK);
}

IN_PROC_BROWSER_TEST_F(MediaGalleriesPrivateGalleryWatchApiTest,
                       SetupGalleryChangedListenerWithoutWatchers) {
  EnsureMediaDirectoriesExists media_directories;
  content::RenderViewHost* host = GetBackgroundHostForTestExtension();
  ASSERT_TRUE(host);

  // Get media file systems.
  ExtensionTestMessageListener get_media_systems_finished(
      kGetMediaFileSystemsCallbackOK, false  /* no reply */);
  ExecuteCmdAndCheckReply(host, kGetMediaFileSystemsCmd,
                          kGetMediaFileSystemsOK);
  EXPECT_TRUE(get_media_systems_finished.WaitUntilSatisfied());

  // Add gallery watch listener.
  ExecuteCmdAndCheckReply(host, kAddGalleryChangedListenerCmd,
                          kAddGalleryChangedListenerOK);

  // Modify gallery contents. Listener should not get called because add watch
  // request was not called.
  ExtensionTestMessageListener music_gallery_change_event_received(
      kGalleryChangedEventReceived, false  /* no reply */);
  ASSERT_TRUE(AddNewFileInGallery(chrome::DIR_USER_MUSIC));

  // Remove gallery watch listener.
  ExecuteCmdAndCheckReply(host, kRemoveGalleryChangedListenerCmd,
                          kRemoveGalleryChangedListenerOK);
}

IN_PROC_BROWSER_TEST_F(MediaGalleriesPrivateGalleryWatchApiTest,
                       SetupWatchOnInvalidGallery) {
  content::RenderViewHost* host = GetBackgroundHostForTestExtension();
  ASSERT_TRUE(host);

  // Set up a invalid gallery watch.
  ExtensionTestMessageListener invalid_gallery_watch_request_finished(
      kAddGalleryWatchRequestFailed, false  /* no reply */);
  ExecuteCmdAndCheckReply(host, kSetupWatchOnInvalidGalleryCmd,
                          kAddGalleryWatchOK);
  EXPECT_TRUE(invalid_gallery_watch_request_finished.WaitUntilSatisfied());
}

IN_PROC_BROWSER_TEST_F(MediaGalleriesPrivateGalleryWatchApiTest,
                       GetAllGalleryWatch) {
  EnsureMediaDirectoriesExists media_directories;
  content::RenderViewHost* host = GetBackgroundHostForTestExtension();
  ASSERT_TRUE(host);

  // Get media file systems.
  ExtensionTestMessageListener get_media_systems_finished(
      kGetMediaFileSystemsCallbackOK, false  /* no reply */);
  ExecuteCmdAndCheckReply(host, kGetMediaFileSystemsCmd,
                          kGetMediaFileSystemsOK);
  EXPECT_TRUE(get_media_systems_finished.WaitUntilSatisfied());

  // Gallery watchers are not yet added.
  // chrome.mediaGalleriesPrivate.getAllGalleryWatch should return an empty
  // list.
  ExtensionTestMessageListener initial_get_all_check_finished(
      kGetAllGalleryWatchResultA, false  /* no reply */);
  ExecuteCmdAndCheckReply(host, kGetAllWatchedGalleryIdsCmd,
                          kGetAllGalleryWatchOK);
  EXPECT_TRUE(initial_get_all_check_finished.WaitUntilSatisfied());

  // Set up gallery watchers.
  ExtensionTestMessageListener add_gallery_watch_finished(
      kAddGalleryWatchRequestSucceeded, false  /* no reply */);
  ExecuteCmdAndCheckReply(host, kSetupWatchOnValidGalleriesCmd,
                          kAddGalleryWatchOK);
  EXPECT_TRUE(add_gallery_watch_finished.WaitUntilSatisfied());

  // chrome.mediaGalleriesPrivate.getAllGalleryWatch should return the
  // gallery identifiers.
  ExtensionTestMessageListener get_all_watched_galleries_finished(
      kGetAllGalleryWatchResultB, false  /* no reply */);
  ExecuteCmdAndCheckReply(host, kGetAllWatchedGalleryIdsCmd,
                          kGetAllGalleryWatchOK);
  EXPECT_TRUE(get_all_watched_galleries_finished.WaitUntilSatisfied());

  // Remove gallery watch request.
  ExecuteCmdAndCheckReply(host, kRemoveGalleryWatchCmd, kRemoveGalleryWatchOK);

  // Gallery watchers removed.
  // chrome.mediaGalleriesPrivate.getAllGalleryWatch() should return an empty
  // list.
  ExtensionTestMessageListener final_get_all_check_finished(
      kGetAllGalleryWatchResultA, false  /* no reply */);
  ExecuteCmdAndCheckReply(host, kGetAllWatchedGalleryIdsCmd,
                          kGetAllGalleryWatchOK);
  EXPECT_TRUE(final_get_all_check_finished.WaitUntilSatisfied());
}

IN_PROC_BROWSER_TEST_F(MediaGalleriesPrivateGalleryWatchApiTest,
                       RemoveAllGalleryWatch) {
  EnsureMediaDirectoriesExists media_directories;
  content::RenderViewHost* host = GetBackgroundHostForTestExtension();
  ASSERT_TRUE(host);

  // Get media file systems.
  ExtensionTestMessageListener get_media_systems_finished(
      kGetMediaFileSystemsCallbackOK, false  /* no reply */);
  ExecuteCmdAndCheckReply(host, kGetMediaFileSystemsCmd,
                          kGetMediaFileSystemsOK);
  EXPECT_TRUE(get_media_systems_finished.WaitUntilSatisfied());

  // Set up gallery watchers.
  ExtensionTestMessageListener add_gallery_watch_finished(
      kAddGalleryWatchRequestSucceeded, false  /* no reply */);
  ExecuteCmdAndCheckReply(host, kSetupWatchOnValidGalleriesCmd,
                          kAddGalleryWatchOK);
  EXPECT_TRUE(add_gallery_watch_finished.WaitUntilSatisfied());

  // chrome.mediaGalleriesPrivate.getAllGalleryWatch should return the watched
  // gallery identifiers.
  ExtensionTestMessageListener get_all_watched_galleries_finished(
      kGetAllGalleryWatchResultB, false  /* no reply */);
  ExecuteCmdAndCheckReply(host, kGetAllWatchedGalleryIdsCmd,
                          kGetAllGalleryWatchOK);
  EXPECT_TRUE(get_all_watched_galleries_finished.WaitUntilSatisfied());

  // Remove all gallery watchers.
  ExecuteCmdAndCheckReply(host, kRemoveAllGalleryWatchCmd,
                          kRemoveAllGalleryWatchOK);

  // Gallery watchers removed. chrome.mediaGalleriesPrivate.getAllGalleryWatch
  // should return an empty list.
  ExtensionTestMessageListener final_get_all_check_finished(
      kGetAllGalleryWatchResultA, false  /* no reply */);
  ExecuteCmdAndCheckReply(host, kGetAllWatchedGalleryIdsCmd,
                          kGetAllGalleryWatchOK);
  EXPECT_TRUE(final_get_all_check_finished.WaitUntilSatisfied());
}
#endif

#if !defined(OS_WIN) && !defined(OS_CHROMEOS)
// Gallery watch request is not enabled on non-windows platforms.
// Please refer to crbug.com/144491.
IN_PROC_BROWSER_TEST_F(MediaGalleriesPrivateGalleryWatchApiTest,
                       SetupGalleryWatch) {
  EnsureMediaDirectoriesExists media_directories;
  content::RenderViewHost* host = GetBackgroundHostForTestExtension();
  ASSERT_TRUE(host);

  // Get media file systems.
  ExtensionTestMessageListener get_media_systems_finished(
      kGetMediaFileSystemsCallbackOK, false  /* no reply */);
  ExecuteCmdAndCheckReply(host, kGetMediaFileSystemsCmd,
                          kGetMediaFileSystemsOK);
  EXPECT_TRUE(get_media_systems_finished.WaitUntilSatisfied());

  // Set up a invalid gallery watch.
  ExtensionTestMessageListener gallery_watch_request_finished(
      kAddGalleryWatchRequestFailed, false  /* no reply */);
  // Set up gallery watch.
  ExecuteCmdAndCheckReply(host, kSetupWatchOnValidGalleriesCmd,
                          kAddGalleryWatchOK);
  EXPECT_TRUE(gallery_watch_request_finished.WaitUntilSatisfied());
}

// Gallery watch request is not enabled on non-windows platforms.
// Please refer to crbug.com/144491.
IN_PROC_BROWSER_TEST_F(MediaGalleriesPrivateGalleryWatchApiTest,
                       GetAllGalleryWatch) {
  EnsureMediaDirectoriesExists media_directories;
  content::RenderViewHost* host = GetBackgroundHostForTestExtension();
  ASSERT_TRUE(host);

  // Get media file systems.
  ExtensionTestMessageListener get_media_systems_finished(
      kGetMediaFileSystemsCallbackOK, false  /* no reply */);
  ExecuteCmdAndCheckReply(host, kGetMediaFileSystemsCmd,
                          kGetMediaFileSystemsOK);
  EXPECT_TRUE(get_media_systems_finished.WaitUntilSatisfied());

  // Set up gallery watch.
  ExecuteCmdAndCheckReply(host, kSetupWatchOnValidGalleriesCmd,
                          kAddGalleryWatchOK);

  // Gallery watchers does not exists.
  // chrome.mediaGalleriesPrivate.getAllGalleryWatch should return an empty
  // list.
  ExtensionTestMessageListener get_all_gallery_watch_finished(
      kGetAllGalleryWatchResultA, false  /* no reply */);
  ExecuteCmdAndCheckReply(host, kGetAllWatchedGalleryIdsCmd,
                          kGetAllGalleryWatchOK);
  EXPECT_TRUE(get_all_gallery_watch_finished.WaitUntilSatisfied());
}
#endif