Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / ui / cocoa / extensions / media_galleries_dialog_cocoa_unittest.mm
blob9015cccd45afe2cbc7665fadacbefe2bc59d92f4
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.
5 #include "base/strings/string_number_conversions.h"
6 #include "base/strings/utf_string_conversions.h"
7 #include "chrome/browser/media_galleries/media_galleries_dialog_controller_mock.h"
8 #include "chrome/browser/storage_monitor/storage_info.h"
9 #include "chrome/browser/ui/cocoa/extensions/media_galleries_dialog_cocoa.h"
10 #include "extensions/common/extension.h"
11 #include "extensions/common/test_util.h"
12 #include "testing/gtest/include/gtest/gtest.h"
14 using ::testing::_;
15 using ::testing::NiceMock;
16 using ::testing::Return;
17 using ::testing::ReturnPointee;
18 using ::testing::ReturnRef;
20 MediaGalleryPrefInfo MakePrefInfoForTesting(MediaGalleryPrefId pref_id) {
21   MediaGalleryPrefInfo gallery;
22   gallery.pref_id = pref_id;
23   gallery.device_id =
24       StorageInfo::MakeDeviceId(StorageInfo::FIXED_MASS_STORAGE,
25                                 base::Int64ToString(pref_id));
26   gallery.display_name = base::ASCIIToUTF16("name");
27   return gallery;
30 class MediaGalleriesDialogTest : public testing::Test {
31  public:
32   MediaGalleriesDialogTest() {}
33   virtual ~MediaGalleriesDialogTest() {}
34   virtual void SetUp() OVERRIDE {
35     dummy_extension_ = extensions::test_util::CreateExtensionWithID("dummy");
36   }
37   virtual void TearDown() OVERRIDE {
38     dummy_extension_ = NULL;
39   }
41   const extensions::Extension& dummy_extension() const {
42     return *dummy_extension_;
43   }
45  private:
46   scoped_refptr<extensions::Extension> dummy_extension_;
48   DISALLOW_COPY_AND_ASSIGN(MediaGalleriesDialogTest);
51 // Tests that checkboxes are initialized according to the contents of
52 // permissions().
53 TEST_F(MediaGalleriesDialogTest, InitializeCheckboxes) {
54   NiceMock<MediaGalleriesDialogControllerMock> controller(dummy_extension());
56   MediaGalleriesDialogController::GalleryPermissionsVector attached_permissions;
57   attached_permissions.push_back(
58       MediaGalleriesDialogController::GalleryPermission(
59           MakePrefInfoForTesting(1), true));
60   attached_permissions.push_back(
61       MediaGalleriesDialogController::GalleryPermission(
62           MakePrefInfoForTesting(2), false));
63   EXPECT_CALL(controller, AttachedPermissions()).
64       WillRepeatedly(Return(attached_permissions));
66   MediaGalleriesDialogController::GalleryPermissionsVector
67       unattached_permissions;
68   EXPECT_CALL(controller, UnattachedPermissions()).
69       WillRepeatedly(Return(unattached_permissions));
71   // Initializing checkboxes should not cause them to be toggled.
72   EXPECT_CALL(controller, DidToggleGalleryId(_, _)).
73       Times(0);
75   scoped_ptr<MediaGalleriesDialogCocoa> dialog(
76       static_cast<MediaGalleriesDialogCocoa*>(
77           MediaGalleriesDialog::Create(&controller)));
78   EXPECT_EQ(2U, [dialog->checkboxes_ count]);
80   NSButton* checkbox1 = [dialog->checkboxes_ objectAtIndex:0];
81   EXPECT_EQ([checkbox1 state], NSOnState);
83   NSButton* checkbox2 = [dialog->checkboxes_ objectAtIndex:1];
84   EXPECT_EQ([checkbox2 state], NSOffState);
87 // Tests that toggling checkboxes updates the controller.
88 TEST_F(MediaGalleriesDialogTest, ToggleCheckboxes) {
89   NiceMock<MediaGalleriesDialogControllerMock> controller(dummy_extension());
91   MediaGalleriesDialogController::GalleryPermissionsVector attached_permissions;
92   attached_permissions.push_back(
93       MediaGalleriesDialogController::GalleryPermission(
94           MakePrefInfoForTesting(1), true));
95   EXPECT_CALL(controller, AttachedPermissions()).
96       WillRepeatedly(Return(attached_permissions));
98   MediaGalleriesDialogController::GalleryPermissionsVector
99       unattached_permissions;
100   EXPECT_CALL(controller, UnattachedPermissions()).
101       WillRepeatedly(Return(unattached_permissions));
103   scoped_ptr<MediaGalleriesDialogCocoa> dialog(
104       static_cast<MediaGalleriesDialogCocoa*>(
105           MediaGalleriesDialog::Create(&controller)));
106   EXPECT_EQ(1U, [dialog->checkboxes_ count]);
108   NSButton* checkbox = [dialog->checkboxes_ objectAtIndex:0];
109   EXPECT_EQ([checkbox state], NSOnState);
111   EXPECT_CALL(controller, DidToggleGalleryId(1, false));
112   [checkbox performClick:nil];
113   EXPECT_EQ([checkbox state], NSOffState);
115   EXPECT_CALL(controller, DidToggleGalleryId(1, true));
116   [checkbox performClick:nil];
117   EXPECT_EQ([checkbox state], NSOnState);
120 // Tests that UpdateGalleries will add a new checkbox, but only if it refers to
121 // a gallery that the dialog hasn't seen before.
122 TEST_F(MediaGalleriesDialogTest, UpdateAdds) {
123   NiceMock<MediaGalleriesDialogControllerMock> controller(dummy_extension());
125   MediaGalleriesDialogController::GalleryPermissionsVector attached_permissions;
126   EXPECT_CALL(controller, AttachedPermissions()).
127       WillRepeatedly(ReturnPointee(&attached_permissions));
129   MediaGalleriesDialogController::GalleryPermissionsVector
130       unattached_permissions;
131   EXPECT_CALL(controller, UnattachedPermissions()).
132       WillRepeatedly(Return(unattached_permissions));
134   scoped_ptr<MediaGalleriesDialogCocoa> dialog(
135       static_cast<MediaGalleriesDialogCocoa*>(
136           MediaGalleriesDialog::Create(&controller)));
138   EXPECT_EQ(0U, [dialog->checkboxes_ count]);
139   CGFloat old_container_height = NSHeight([dialog->checkbox_container_ frame]);
141   attached_permissions.push_back(
142       MediaGalleriesDialogController::GalleryPermission(
143           MakePrefInfoForTesting(1), true));
144   dialog->UpdateGalleries();
145   EXPECT_EQ(1U, [dialog->checkboxes_ count]);
147   // The checkbox container should be taller.
148   CGFloat new_container_height = NSHeight([dialog->checkbox_container_ frame]);
149   EXPECT_GT(new_container_height, old_container_height);
150   old_container_height = new_container_height;
152   attached_permissions.push_back(
153       MediaGalleriesDialogController::GalleryPermission(
154           MakePrefInfoForTesting(2), true));
155   dialog->UpdateGalleries();
156   EXPECT_EQ(2U, [dialog->checkboxes_ count]);
158   // The checkbox container should be taller.
159   new_container_height = NSHeight([dialog->checkbox_container_ frame]);
160   EXPECT_GT(new_container_height, old_container_height);
161   old_container_height = new_container_height;
163   attached_permissions[1].allowed = false;
164   dialog->UpdateGalleries();
165   EXPECT_EQ(2U, [dialog->checkboxes_ count]);
167   // The checkbox container height should not have changed.
168   new_container_height = NSHeight([dialog->checkbox_container_ frame]);
169   EXPECT_EQ(new_container_height, old_container_height);
172 TEST_F(MediaGalleriesDialogTest, ForgetDeletes) {
173   NiceMock<MediaGalleriesDialogControllerMock> controller(dummy_extension());
175   MediaGalleriesDialogController::GalleryPermissionsVector attached_permissions;
176   EXPECT_CALL(controller, AttachedPermissions()).
177       WillRepeatedly(ReturnPointee(&attached_permissions));
179   MediaGalleriesDialogController::GalleryPermissionsVector
180       unattached_permissions;
181   EXPECT_CALL(controller, UnattachedPermissions()).
182       WillRepeatedly(Return(unattached_permissions));
184   scoped_ptr<MediaGalleriesDialogCocoa> dialog(
185       static_cast<MediaGalleriesDialogCocoa*>(
186           MediaGalleriesDialog::Create(&controller)));
188   // Add a couple of galleries.
189   attached_permissions.push_back(
190       MediaGalleriesDialogController::GalleryPermission(
191           MakePrefInfoForTesting(1), true));
192   dialog->UpdateGalleries();
193   attached_permissions.push_back(
194       MediaGalleriesDialogController::GalleryPermission(
195           MakePrefInfoForTesting(2), true));
196   dialog->UpdateGalleries();
197   EXPECT_EQ(2U, [dialog->checkboxes_ count]);
198   CGFloat old_container_height = NSHeight([dialog->checkbox_container_ frame]);
200   // Remove a gallery.
201   attached_permissions.erase(attached_permissions.begin());
202   dialog->UpdateGalleries();
203   EXPECT_EQ(1U, [dialog->checkboxes_ count]);
205   // The checkbox container should be shorter.
206   CGFloat new_container_height = NSHeight([dialog->checkbox_container_ frame]);
207   EXPECT_LT(new_container_height, old_container_height);