Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / media_galleries / media_galleries_dialog_controller_unittest.cc
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
3 // found in the LICENSE file.
4
5 #include "base/bind.h"
6 #include "base/command_line.h"
7 #include "base/files/file_path.h"
8 #include "base/run_loop.h"
9 #include "base/strings/string16.h"
10 #include "base/strings/string_util.h"
11 #include "base/strings/utf_string_conversions.h"
12 #include "chrome/browser/extensions/test_extension_system.h"
13 #include "chrome/browser/media_galleries/media_galleries_dialog_controller.h"
14 #include "chrome/browser/media_galleries/media_galleries_preferences.h"
15 #include "chrome/browser/media_galleries/media_galleries_test_util.h"
16 #include "chrome/common/extensions/permissions/media_galleries_permission.h"
17 #include "chrome/test/base/testing_profile.h"
18 #include "components/storage_monitor/storage_info.h"
19 #include "components/storage_monitor/test_storage_monitor.h"
20 #include "content/public/test/test_browser_thread_bundle.h"
21 #include "testing/gtest/include/gtest/gtest.h"
22
23 #if defined(OS_CHROMEOS)
24 #include "chrome/browser/chromeos/login/user_manager.h"
25 #include "chrome/browser/chromeos/settings/cros_settings.h"
26 #include "chrome/browser/chromeos/settings/device_settings_service.h"
27 #endif
28
29 namespace {
30
31 std::string GalleryName(const MediaGalleryPrefInfo& gallery) {
32   base::string16 name = gallery.GetGalleryDisplayName();
33   return UTF16ToASCII(name);
34 }
35
36 class MockMediaGalleriesDialog
37     : public MediaGalleriesDialog {
38  public:
39   typedef base::Callback<void(int update_count)> DialogDestroyedCallback;
40
41   explicit MockMediaGalleriesDialog(const DialogDestroyedCallback& callback)
42       : update_count_(0),
43         dialog_destroyed_callback_(callback) {
44   }
45
46   virtual ~MockMediaGalleriesDialog() {
47     dialog_destroyed_callback_.Run(update_count_);
48   }
49
50   // MockMediaGalleriesDialog implementation.
51   virtual void UpdateGalleries() OVERRIDE {
52     update_count_++;
53   }
54
55   // Number of times UpdateResults has been called.
56   int update_count() {
57     return update_count_;
58   }
59
60  private:
61   int update_count_;
62
63   DialogDestroyedCallback dialog_destroyed_callback_;
64
65   DISALLOW_COPY_AND_ASSIGN(MockMediaGalleriesDialog);
66 };
67
68 }  // namespace
69
70 class MediaGalleriesDialogControllerTest : public ::testing::Test {
71  public:
72   MediaGalleriesDialogControllerTest()
73       : dialog_(NULL),
74         dialog_update_count_at_destruction_(0),
75         controller_(NULL),
76         profile_(new TestingProfile()),
77         weak_factory_(this) {
78   }
79
80   virtual ~MediaGalleriesDialogControllerTest() {
81     EXPECT_FALSE(controller_);
82     EXPECT_FALSE(dialog_);
83   }
84
85   virtual void SetUp() OVERRIDE {
86     ASSERT_TRUE(TestStorageMonitor::CreateAndInstall());
87
88     extensions::TestExtensionSystem* extension_system(
89         static_cast<extensions::TestExtensionSystem*>(
90             extensions::ExtensionSystem::Get(profile_.get())));
91     extension_system->CreateExtensionService(
92         CommandLine::ForCurrentProcess(), base::FilePath(), false);
93
94     gallery_prefs_.reset(new MediaGalleriesPreferences(profile_.get()));
95     base::RunLoop loop;
96     gallery_prefs_->EnsureInitialized(loop.QuitClosure());
97     loop.Run();
98
99     std::vector<std::string> read_permissions;
100     read_permissions.push_back(
101         extensions::MediaGalleriesPermission::kReadPermission);
102     extension_ = AddMediaGalleriesApp("read", read_permissions, profile_.get());
103   }
104
105   virtual void TearDown() OVERRIDE {
106     TestStorageMonitor::Destroy();
107   }
108
109   void StartDialog() {
110     ASSERT_FALSE(controller_);
111     controller_ = new MediaGalleriesDialogController(
112         *extension_.get(),
113         gallery_prefs_.get(),
114         base::Bind(&MediaGalleriesDialogControllerTest::CreateMockDialog,
115                    base::Unretained(this)),
116         base::Bind(
117             &MediaGalleriesDialogControllerTest::OnControllerDone,
118             base::Unretained(this)));
119   }
120
121   MediaGalleriesDialogController* controller() {
122     return controller_;
123   }
124
125   MockMediaGalleriesDialog* dialog() {
126     return dialog_;
127   }
128
129   int dialog_update_count_at_destruction() {
130     EXPECT_FALSE(dialog_);
131     return dialog_update_count_at_destruction_;
132   }
133
134   extensions::Extension* extension() {
135     return extension_.get();
136   }
137
138   MediaGalleriesPreferences* gallery_prefs() {
139     return gallery_prefs_.get();
140   }
141
142   void TestForgottenType(MediaGalleryPrefInfo::Type type);
143
144  protected:
145   EnsureMediaDirectoriesExists mock_gallery_locations_;
146
147  private:
148   MediaGalleriesDialog* CreateMockDialog(
149       MediaGalleriesDialogController* controller) {
150     EXPECT_FALSE(dialog_);
151     dialog_update_count_at_destruction_ = 0;
152     dialog_ = new MockMediaGalleriesDialog(base::Bind(
153         &MediaGalleriesDialogControllerTest::OnDialogDestroyed,
154         weak_factory_.GetWeakPtr()));
155     return dialog_;
156   }
157
158   void OnDialogDestroyed(int update_count) {
159     EXPECT_TRUE(dialog_);
160     dialog_update_count_at_destruction_ = update_count;
161     dialog_ = NULL;
162   }
163
164   void OnControllerDone() {
165     controller_ = NULL;
166   }
167
168   // Needed for extension service & friends to work.
169   content::TestBrowserThreadBundle thread_bundle_;
170
171   // The dialog is owned by the controller, but this pointer should only be
172   // valid while the dialog is live within the controller.
173   MockMediaGalleriesDialog* dialog_;
174   int dialog_update_count_at_destruction_;
175
176   // The controller owns itself.
177   MediaGalleriesDialogController* controller_;
178
179   scoped_refptr<extensions::Extension> extension_;
180
181 #if defined OS_CHROMEOS
182   chromeos::ScopedTestDeviceSettingsService test_device_settings_service_;
183   chromeos::ScopedTestCrosSettings test_cros_settings_;
184   chromeos::ScopedTestUserManager test_user_manager_;
185 #endif
186
187   TestStorageMonitor monitor_;
188   scoped_ptr<TestingProfile> profile_;
189   scoped_ptr<MediaGalleriesPreferences> gallery_prefs_;
190
191   base::WeakPtrFactory<MediaGalleriesDialogControllerTest>
192       weak_factory_;
193
194   DISALLOW_COPY_AND_ASSIGN(MediaGalleriesDialogControllerTest);
195 };
196
197 void MediaGalleriesDialogControllerTest::TestForgottenType(
198     MediaGalleryPrefInfo::Type type) {
199   EXPECT_EQ(0U, gallery_prefs()->GalleriesForExtension(*extension()).size());
200
201   MediaGalleryPrefId forgotten1 = gallery_prefs()->AddGalleryByPath(
202       MakeMediaGalleriesTestingPath("forgotten1"), type);
203   MediaGalleryPrefId forgotten2 = gallery_prefs()->AddGalleryByPath(
204       MakeMediaGalleriesTestingPath("forgotten2"), type);
205   // Show dialog and accept to verify 2 entries
206   StartDialog();
207   EXPECT_EQ(mock_gallery_locations_.num_galleries() + 2U,
208             controller()->AttachedPermissions().size());
209   EXPECT_EQ(0U, controller()->UnattachedPermissions().size());
210   controller()->DidToggleGalleryId(forgotten1, true);
211   controller()->DidToggleGalleryId(forgotten2, true);
212   controller()->DialogFinished(true);
213   EXPECT_EQ(2U, gallery_prefs()->GalleriesForExtension(*extension()).size());
214
215   // Forget one and cancel to see that it's still there.
216   StartDialog();
217   controller()->DidForgetGallery(forgotten1);
218   EXPECT_EQ(mock_gallery_locations_.num_galleries() + 1U,
219             controller()->AttachedPermissions().size());
220   controller()->DialogFinished(false);
221   EXPECT_EQ(2U, gallery_prefs()->GalleriesForExtension(*extension()).size());
222
223   // Forget one and confirm to see that it's gone.
224   StartDialog();
225   controller()->DidForgetGallery(forgotten1);
226   EXPECT_EQ(mock_gallery_locations_.num_galleries() + 1U,
227             controller()->AttachedPermissions().size());
228   controller()->DialogFinished(true);
229   EXPECT_EQ(1U, gallery_prefs()->GalleriesForExtension(*extension()).size());
230
231   // Add a new one and forget it & see that it's gone.
232   MediaGalleryPrefId forgotten3 = gallery_prefs()->AddGalleryByPath(
233       MakeMediaGalleriesTestingPath("forgotten3"), type);
234   StartDialog();
235   EXPECT_EQ(mock_gallery_locations_.num_galleries() + 2U,
236             controller()->AttachedPermissions().size());
237   EXPECT_EQ(0U, controller()->UnattachedPermissions().size());
238   controller()->DidToggleGalleryId(forgotten3, true);
239   controller()->DidForgetGallery(forgotten3);
240   EXPECT_EQ(mock_gallery_locations_.num_galleries() + 1U,
241             controller()->AttachedPermissions().size());
242   controller()->DialogFinished(true);
243   EXPECT_EQ(1U, gallery_prefs()->GalleriesForExtension(*extension()).size());
244 }
245
246 TEST_F(MediaGalleriesDialogControllerTest, TestForgottenUserAdded) {
247   TestForgottenType(MediaGalleryPrefInfo::kUserAdded);
248 }
249
250 TEST_F(MediaGalleriesDialogControllerTest, TestForgottenAutoDetected) {
251   TestForgottenType(MediaGalleryPrefInfo::kAutoDetected);
252 }
253
254 TEST_F(MediaGalleriesDialogControllerTest, TestForgottenScanResult) {
255   TestForgottenType(MediaGalleryPrefInfo::kScanResult);
256 }
257
258 TEST_F(MediaGalleriesDialogControllerTest, TestNameGeneration) {
259   MediaGalleryPrefInfo gallery;
260   gallery.pref_id = 1;
261   gallery.device_id = StorageInfo::MakeDeviceId(
262       StorageInfo::FIXED_MASS_STORAGE, "/path/to/gallery");
263   gallery.type = MediaGalleryPrefInfo::kAutoDetected;
264   std::string galleryName("/path/to/gallery");
265 #if defined(OS_CHROMEOS)
266   galleryName = "gallery";
267 #endif
268   EXPECT_EQ(galleryName, GalleryName(gallery));
269
270   gallery.display_name = base::ASCIIToUTF16("override");
271   EXPECT_EQ("override", GalleryName(gallery));
272
273   gallery.display_name = base::string16();
274   gallery.volume_label = base::ASCIIToUTF16("label");
275   EXPECT_EQ(galleryName, GalleryName(gallery));
276
277   gallery.path = base::FilePath(FILE_PATH_LITERAL("sub/gallery2"));
278   galleryName = "/path/to/gallery/sub/gallery2";
279 #if defined(OS_CHROMEOS)
280   galleryName = "gallery2";
281 #endif
282 #if defined(OS_WIN)
283   galleryName = base::FilePath(FILE_PATH_LITERAL("/path/to/gallery"))
284                     .Append(gallery.path).MaybeAsASCII();
285 #endif
286   EXPECT_EQ(galleryName, GalleryName(gallery));
287
288   gallery.path = base::FilePath();
289   gallery.device_id = StorageInfo::MakeDeviceId(
290       StorageInfo::REMOVABLE_MASS_STORAGE_WITH_DCIM,
291       "/path/to/dcim");
292   gallery.display_name = base::ASCIIToUTF16("override");
293   EXPECT_EQ("override", GalleryName(gallery));
294
295   gallery.volume_label = base::ASCIIToUTF16("volume");
296   gallery.vendor_name = base::ASCIIToUTF16("vendor");
297   gallery.model_name = base::ASCIIToUTF16("model");
298   EXPECT_EQ("override", GalleryName(gallery));
299
300   gallery.display_name = base::string16();
301   EXPECT_EQ("volume", GalleryName(gallery));
302
303   gallery.volume_label = base::string16();
304   EXPECT_EQ("vendor, model", GalleryName(gallery));
305
306   gallery.total_size_in_bytes = 1000000;
307   EXPECT_EQ("977 KB vendor, model", GalleryName(gallery));
308
309   gallery.path = base::FilePath(FILE_PATH_LITERAL("sub/path"));
310   EXPECT_EQ("path - 977 KB vendor, model", GalleryName(gallery));
311 }