summaryrefslogtreecommitdiffstats
path: root/chrome/browser/media_galleries/media_galleries_scan_result_dialog_controller_unittest.cc
blob: 45d092f48701c4880ec7d2c801b15a4ddfd196bc (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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
// Copyright 2014 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.

#include "base/bind.h"
#include "base/command_line.h"
#include "base/files/file_path.h"
#include "base/memory/scoped_ptr.h"
#include "base/memory/weak_ptr.h"
#include "base/run_loop.h"
#include "base/strings/string16.h"
#include "base/strings/utf_string_conversions.h"
#include "base/time/time.h"
#include "chrome/browser/extensions/test_extension_system.h"
#include "chrome/browser/media_galleries/media_galleries_preferences.h"
#include "chrome/browser/media_galleries/media_galleries_scan_result_dialog_controller.h"
#include "chrome/browser/media_galleries/media_galleries_test_util.h"
#include "chrome/browser/storage_monitor/test_storage_monitor.h"
#include "chrome/common/extensions/permissions/media_galleries_permission.h"
#include "chrome/test/base/testing_profile.h"
#include "content/public/test/test_browser_thread_bundle.h"
#include "extensions/browser/extension_system.h"
#include "extensions/common/extension.h"
#include "testing/gtest/include/gtest/gtest.h"

#if defined(OS_CHROMEOS)
#include "chrome/browser/chromeos/login/user_manager.h"
#include "chrome/browser/chromeos/settings/cros_settings.h"
#include "chrome/browser/chromeos/settings/device_settings_service.h"
#endif

namespace {

base::FilePath MakePath(std::string dir) {
#if defined(OS_WIN)
  return base::FilePath(FILE_PATH_LITERAL("C:\\")).AppendASCII(dir);
#elif defined(OS_POSIX)
  return base::FilePath(FILE_PATH_LITERAL("/")).Append(dir);
#else
  NOTIMPLEMENTED();
#endif
}

MediaGalleryPrefId AddScanResult(MediaGalleriesPreferences* gallery_prefs,
                                 const std::string& path, int image_count,
                                 int music_count, int video_count) {
  MediaGalleryPrefInfo gallery_info;
  gallery_prefs->LookUpGalleryByPath(MakePath(path), &gallery_info);
  return gallery_prefs->AddGallery(
      gallery_info.device_id,
      gallery_info.path,
      MediaGalleryPrefInfo::kScanResult,
      gallery_info.volume_label,
      gallery_info.vendor_name,
      gallery_info.model_name,
      gallery_info.total_size_in_bytes,
      gallery_info.last_attach_time,
      image_count, music_count, video_count);
}

class MockMediaGalleriesScanResultDialog
    : public MediaGalleriesScanResultDialog {
 public:
  typedef base::Callback<void(int update_count)> DialogDestroyedCallback;

  explicit MockMediaGalleriesScanResultDialog(
      const DialogDestroyedCallback& callback)
      : update_count_(0),
        dialog_destroyed_callback_(callback) {
  }

  virtual ~MockMediaGalleriesScanResultDialog() {
    dialog_destroyed_callback_.Run(update_count_);
  }

  // MockMediaGalleriesScanResultDialog implementation.
  virtual void UpdateResults() OVERRIDE {
    update_count_++;
  }

  // Number up times UpdateResults has been called.
  int update_count() {
    return update_count_;
  }

 private:
  int update_count_;

  DialogDestroyedCallback dialog_destroyed_callback_;

  DISALLOW_COPY_AND_ASSIGN(MockMediaGalleriesScanResultDialog);
};

}  // namespace

class MediaGalleriesScanResultDialogControllerTest : public testing::Test {
 public:
  MediaGalleriesScanResultDialogControllerTest()
      : dialog_(NULL),
        dialog_update_count_at_destruction_(0),
        controller_(NULL),
        profile_(new TestingProfile()),
        weak_factory_(this) {
  }

  virtual ~MediaGalleriesScanResultDialogControllerTest() {
    EXPECT_FALSE(controller_);
    EXPECT_FALSE(dialog_);
  }

  virtual void SetUp() OVERRIDE {
    ASSERT_TRUE(TestStorageMonitor::CreateAndInstall());

    extensions::TestExtensionSystem* extension_system(
        static_cast<extensions::TestExtensionSystem*>(
            extensions::ExtensionSystem::Get(profile_.get())));
    extension_system->CreateExtensionService(
        CommandLine::ForCurrentProcess(), base::FilePath(), false);

    gallery_prefs_.reset(new MediaGalleriesPreferences(profile_.get()));
    base::RunLoop loop;
    gallery_prefs_->EnsureInitialized(loop.QuitClosure());
    loop.Run();

    std::vector<std::string> read_permissions;
    read_permissions.push_back(
        extensions::MediaGalleriesPermission::kReadPermission);
    extension_ = AddMediaGalleriesApp("read", read_permissions, profile_.get());
  }

  virtual void TearDown() OVERRIDE {
    TestStorageMonitor::RemoveSingleton();
  }

  void StartDialog() {
    ASSERT_FALSE(controller_);
    controller_ = new MediaGalleriesScanResultDialogController(
        *extension_.get(),
        gallery_prefs_.get(),
        base::Bind(
            &MediaGalleriesScanResultDialogControllerTest::CreateMockDialog,
            base::Unretained(this)),
        base::Bind(
            &MediaGalleriesScanResultDialogControllerTest::OnControllerDone,
            base::Unretained(this)));
  }

  MediaGalleriesScanResultDialogController* controller() {
    return controller_;
  }

  MockMediaGalleriesScanResultDialog* dialog() {
    return dialog_;
  }

  int dialog_update_count_at_destruction() {
    EXPECT_FALSE(dialog_);
    return dialog_update_count_at_destruction_;
  }

  extensions::Extension* extension() {
    return extension_.get();
  }

  MediaGalleriesPreferences* gallery_prefs() {
    return gallery_prefs_.get();
  }

 private:
  MediaGalleriesScanResultDialog* CreateMockDialog(
      MediaGalleriesScanResultDialogController* controller) {
    EXPECT_FALSE(dialog_);
    dialog_update_count_at_destruction_ = 0;
    dialog_ = new MockMediaGalleriesScanResultDialog(base::Bind(
        &MediaGalleriesScanResultDialogControllerTest::OnDialogDestroyed,
        weak_factory_.GetWeakPtr()));
    return dialog_;
  }

  void OnDialogDestroyed(int update_count) {
    EXPECT_TRUE(dialog_);
    dialog_update_count_at_destruction_ = update_count;
    dialog_ = NULL;
  }

  void OnControllerDone() {
    controller_ = NULL;
  }

  // Needed for extension service & friends to work.
  content::TestBrowserThreadBundle thread_bundle_;

  // The dialog is owned by the controller, but this pointer should only be
  // valid while the dialog is live within the controller.
  MockMediaGalleriesScanResultDialog* dialog_;
  int dialog_update_count_at_destruction_;

  // The controller owns itself.
  MediaGalleriesScanResultDialogController* controller_;

  scoped_refptr<extensions::Extension> extension_;

  EnsureMediaDirectoriesExists mock_gallery_locations_;

#if defined OS_CHROMEOS
  chromeos::ScopedTestDeviceSettingsService test_device_settings_service_;
  chromeos::ScopedTestCrosSettings test_cros_settings_;
  chromeos::ScopedTestUserManager test_user_manager_;
#endif

  TestStorageMonitor monitor_;
  scoped_ptr<TestingProfile> profile_;
  scoped_ptr<MediaGalleriesPreferences> gallery_prefs_;

  base::WeakPtrFactory<MediaGalleriesScanResultDialogControllerTest>
      weak_factory_;

  DISALLOW_COPY_AND_ASSIGN(MediaGalleriesScanResultDialogControllerTest);
};

TEST_F(MediaGalleriesScanResultDialogControllerTest, EmptyDialog) {
  StartDialog();
  EXPECT_TRUE(controller());
  EXPECT_TRUE(dialog());
  EXPECT_EQ(0U, controller()->GetGalleryList().size());

  controller()->DialogFinished(true);
  EXPECT_FALSE(controller());
  EXPECT_FALSE(dialog());
  EXPECT_EQ(0, dialog_update_count_at_destruction());
}

TEST_F(MediaGalleriesScanResultDialogControllerTest, AddScanResults) {
  // Start with two scan results.
  MediaGalleryPrefId scan1 =
      gallery_prefs()->AddGalleryByPath(MakePath("scan1"),
                                        MediaGalleryPrefInfo::kScanResult);
  MediaGalleryPrefId scan2 =
      gallery_prefs()->AddGalleryByPath(MakePath("scan2"),
                                        MediaGalleryPrefInfo::kScanResult);
  EXPECT_EQ(0U, gallery_prefs()->GalleriesForExtension(*extension()).size());

  // Show the dialog, but cancel it.
  StartDialog();
  EXPECT_EQ(2U, controller()->GetGalleryList().size());
  controller()->DialogFinished(false);
  EXPECT_EQ(0U, gallery_prefs()->GalleriesForExtension(*extension()).size());

  // Show the dialog, unselect both and accept it.
  StartDialog();
  EXPECT_EQ(2U, controller()->GetGalleryList().size());
  controller()->DidToggleGalleryId(scan1, false);
  controller()->DidToggleGalleryId(scan2, false);
  controller()->DialogFinished(true);
  EXPECT_EQ(0U, gallery_prefs()->GalleriesForExtension(*extension()).size());

  // Show the dialog, leave one selected and accept it.
  StartDialog();
  EXPECT_EQ(2U, controller()->GetGalleryList().size());
  controller()->DidToggleGalleryId(scan1, false);
  controller()->DialogFinished(true);
  MediaGalleryPrefIdSet permitted =
      gallery_prefs()->GalleriesForExtension(*extension());
  ASSERT_EQ(1U, permitted.size());
  EXPECT_EQ(scan2, *permitted.begin());

  // Show the dialog, toggle the remaining entry twice and then accept it.
  StartDialog();
  EXPECT_EQ(1U, controller()->GetGalleryList().size());
  controller()->DidToggleGalleryId(scan1, false);
  controller()->DidToggleGalleryId(scan1, true);
  controller()->DialogFinished(true);
  EXPECT_EQ(2U, gallery_prefs()->GalleriesForExtension(*extension()).size());
}

TEST_F(MediaGalleriesScanResultDialogControllerTest, Blacklisted) {
  // Start with two scan results.
  MediaGalleryPrefId scan1 =
      gallery_prefs()->AddGalleryByPath(MakePath("scan1"),
                                        MediaGalleryPrefInfo::kScanResult);
  MediaGalleryPrefId scan2 =
      gallery_prefs()->AddGalleryByPath(MakePath("scan2"),
                                        MediaGalleryPrefInfo::kScanResult);
  EXPECT_EQ(0U, gallery_prefs()->GalleriesForExtension(*extension()).size());

  // Show the dialog, but cancel it.
  StartDialog();
  EXPECT_EQ(2U, controller()->GetGalleryList().size());
  controller()->DialogFinished(false);
  EXPECT_EQ(0U, gallery_prefs()->GalleriesForExtension(*extension()).size());

  // Blacklist one and try again.
  gallery_prefs()->ForgetGalleryById(scan2);
  StartDialog();
  EXPECT_EQ(1U, controller()->GetGalleryList().size());
  controller()->DialogFinished(false);

  // Adding it as a user gallery should change its type.
  gallery_prefs()->AddGalleryByPath(MakePath("scan2"),
                                    MediaGalleryPrefInfo::kUserAdded);
  StartDialog();
  EXPECT_EQ(1U, controller()->GetGalleryList().size());

  // Blacklisting the other while the dialog is open should remove it.
  gallery_prefs()->ForgetGalleryById(scan1);
  EXPECT_EQ(0U, controller()->GetGalleryList().size());
  controller()->DialogFinished(false);
  EXPECT_EQ(0U, gallery_prefs()->GalleriesForExtension(*extension()).size());
  EXPECT_EQ(1, dialog_update_count_at_destruction());
}

TEST_F(MediaGalleriesScanResultDialogControllerTest, PrefUpdates) {
  MediaGalleryPrefId selected =
      gallery_prefs()->AddGalleryByPath(MakePath("selected"),
                                        MediaGalleryPrefInfo::kScanResult);
  MediaGalleryPrefId unselected =
      gallery_prefs()->AddGalleryByPath(MakePath("unselected"),
                                        MediaGalleryPrefInfo::kScanResult);
  MediaGalleryPrefId selected_add_permission =
      gallery_prefs()->AddGalleryByPath(MakePath("selected_add_permission"),
                                        MediaGalleryPrefInfo::kScanResult);
  MediaGalleryPrefId unselected_add_permission =
      gallery_prefs()->AddGalleryByPath(MakePath("unselected_add_permission"),
                                        MediaGalleryPrefInfo::kScanResult);
  MediaGalleryPrefId selected_removed =
      gallery_prefs()->AddGalleryByPath(MakePath("selected_removed"),
                                        MediaGalleryPrefInfo::kScanResult);
  MediaGalleryPrefId unselected_removed =
      gallery_prefs()->AddGalleryByPath(MakePath("unselected_removed"),
                                        MediaGalleryPrefInfo::kScanResult);
  MediaGalleryPrefId selected_update =
      gallery_prefs()->AddGalleryByPath(MakePath("selected_update"),
                                        MediaGalleryPrefInfo::kScanResult);
  MediaGalleryPrefId unselected_update =
      gallery_prefs()->AddGalleryByPath(MakePath("unselected_update"),
                                        MediaGalleryPrefInfo::kScanResult);

  gallery_prefs()->AddGalleryByPath(MakePath("user"),
                                    MediaGalleryPrefInfo::kUserAdded);
  gallery_prefs()->AddGalleryByPath(MakePath("auto_detected"),
                                    MediaGalleryPrefInfo::kAutoDetected);
  MediaGalleryPrefId blacklisted =
      gallery_prefs()->AddGalleryByPath(MakePath("blacklisted"),
                                        MediaGalleryPrefInfo::kAutoDetected);
  gallery_prefs()->ForgetGalleryById(blacklisted);
  EXPECT_EQ(0U, gallery_prefs()->GalleriesForExtension(*extension()).size());

  StartDialog();
  EXPECT_EQ(8U, controller()->GetGalleryList().size());
  controller()->DidToggleGalleryId(unselected, false);
  controller()->DidToggleGalleryId(unselected_add_permission, false);
  controller()->DidToggleGalleryId(unselected_removed, false);
  controller()->DidToggleGalleryId(unselected_update, false);
  EXPECT_EQ(0, dialog()->update_count());
  EXPECT_EQ(8U, controller()->GetGalleryList().size());

  // Add permission.
  gallery_prefs()->SetGalleryPermissionForExtension(*extension(),
                                                    unselected_add_permission,
                                                    true);
  EXPECT_EQ(1, dialog()->update_count());
  EXPECT_EQ(7U, controller()->GetGalleryList().size());
  gallery_prefs()->SetGalleryPermissionForExtension(*extension(),
                                                    selected_add_permission,
                                                    true);
  EXPECT_EQ(2, dialog()->update_count());
  EXPECT_EQ(6U, controller()->GetGalleryList().size());

  // Blacklist scan results.
  gallery_prefs()->ForgetGalleryById(unselected_removed);
  EXPECT_EQ(3, dialog()->update_count());
  EXPECT_EQ(5U, controller()->GetGalleryList().size());
  gallery_prefs()->ForgetGalleryById(selected_removed);
  EXPECT_EQ(4, dialog()->update_count());
  EXPECT_EQ(4U, controller()->GetGalleryList().size());

  // Update names.
  const MediaGalleryPrefInfo& unselected_update_info =
      gallery_prefs()->known_galleries().find(unselected_update)->second;
  gallery_prefs()->AddGallery(
      unselected_update_info.device_id, base::FilePath(),
      MediaGalleryPrefInfo::kScanResult,
      base::ASCIIToUTF16("Updated & Unselected"),
      base::string16(), base::string16(), 0, base::Time(), 0, 0, 0);
  EXPECT_EQ(5, dialog()->update_count());
  EXPECT_EQ(4U, controller()->GetGalleryList().size());
  const MediaGalleryPrefInfo& selected_update_info =
      gallery_prefs()->known_galleries().find(selected_update)->second;
  gallery_prefs()->AddGallery(
      selected_update_info.device_id, base::FilePath(),
      MediaGalleryPrefInfo::kScanResult,
      base::ASCIIToUTF16("Updated & Selected"),
      base::string16(), base::string16(), 0, base::Time(), 0, 0, 0);
  EXPECT_EQ(6, dialog()->update_count());
  ASSERT_EQ(4U, controller()->GetGalleryList().size());

  MediaGalleriesScanResultDialogController::OrderedScanResults results =
      controller()->GetGalleryList();
  EXPECT_EQ(selected, results[0].pref_info.pref_id);
  EXPECT_TRUE(results[0].selected);
  EXPECT_EQ(selected_update, results[1].pref_info.pref_id);
  EXPECT_TRUE(results[1].selected);
  EXPECT_EQ(base::ASCIIToUTF16("Updated & Selected"),
            results[1].pref_info.volume_label);
  EXPECT_EQ(unselected, results[2].pref_info.pref_id);
  EXPECT_FALSE(results[2].selected);
  EXPECT_EQ(unselected_update, results[3].pref_info.pref_id);
  EXPECT_FALSE(results[3].selected);
  EXPECT_EQ(base::ASCIIToUTF16("Updated & Unselected"),
            results[3].pref_info.volume_label);

  controller()->DialogFinished(true);
  EXPECT_EQ(4U, gallery_prefs()->GalleriesForExtension(*extension()).size());
  StartDialog();
  EXPECT_EQ(2U, controller()->GetGalleryList().size());
  controller()->DialogFinished(false);
}

TEST_F(MediaGalleriesScanResultDialogControllerTest, ForgetGallery) {
  // Start with two scan results.
  MediaGalleryPrefId scan1 =
      gallery_prefs()->AddGalleryByPath(MakePath("scan1"),
                                        MediaGalleryPrefInfo::kScanResult);
  MediaGalleryPrefId scan2 =
      gallery_prefs()->AddGalleryByPath(MakePath("scan2"),
                                        MediaGalleryPrefInfo::kScanResult);
  EXPECT_EQ(0U, gallery_prefs()->GalleriesForExtension(*extension()).size());

  // Remove one and then cancel.
  StartDialog();
  EXPECT_EQ(2U, controller()->GetGalleryList().size());
  controller()->DidForgetGallery(scan1);
  controller()->DialogFinished(false);
  EXPECT_EQ(0U, gallery_prefs()->GalleriesForExtension(*extension()).size());

  // Remove one and then have it blacklisted from prefs.
  StartDialog();
  EXPECT_EQ(2U, controller()->GetGalleryList().size());
  controller()->DidForgetGallery(scan1);
  EXPECT_EQ(1, dialog()->update_count());
  controller()->DidToggleGalleryId(scan2, false);  // Uncheck the second.
  gallery_prefs()->ForgetGalleryById(scan1);
  controller()->DialogFinished(true);
  EXPECT_EQ(0U, gallery_prefs()->GalleriesForExtension(*extension()).size());
  EXPECT_EQ(2, dialog_update_count_at_destruction());

  // Remove the other.
  StartDialog();
  EXPECT_EQ(1U, controller()->GetGalleryList().size());
  controller()->DidForgetGallery(scan2);
  controller()->DialogFinished(true);
  EXPECT_EQ(0U, gallery_prefs()->GalleriesForExtension(*extension()).size());

  // Check that nothing shows up.
  StartDialog();
  EXPECT_EQ(0U, controller()->GetGalleryList().size());
  controller()->DialogFinished(false);
}

TEST_F(MediaGalleriesScanResultDialogControllerTest, SortOrder) {
  // Intentionally our of order numerically and alphabetically.
  MediaGalleryPrefId third = AddScanResult(gallery_prefs(), "third", 2, 2, 2);
  MediaGalleryPrefId second = AddScanResult(gallery_prefs(), "second", 9, 0, 0);
  MediaGalleryPrefId first = AddScanResult(gallery_prefs(), "first", 8, 2, 3);
  MediaGalleryPrefId fifth = AddScanResult(gallery_prefs(), "abb", 3, 0, 0);
  MediaGalleryPrefId fourth = AddScanResult(gallery_prefs(), "aaa", 3, 0, 0);

  StartDialog();
  MediaGalleriesScanResultDialogController::OrderedScanResults results =
      controller()->GetGalleryList();
  ASSERT_EQ(5U, results.size());
  EXPECT_EQ(first, results[0].pref_info.pref_id);
  EXPECT_EQ(second, results[1].pref_info.pref_id);
  EXPECT_EQ(third, results[2].pref_info.pref_id);
  EXPECT_EQ(fourth, results[3].pref_info.pref_id);
  EXPECT_EQ(fifth, results[4].pref_info.pref_id);
  controller()->DialogFinished(false);
}