Popular sites on the NTP: check that experiment group StartsWith (rather than IS...
[chromium-blink-merge.git] / chrome / browser / ui / cocoa / extensions / media_galleries_dialog_cocoa_unittest.mm
blob06216954db0c4aaa07527377f8f1f94427d6bdbe
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/ui/cocoa/extensions/media_galleries_dialog_cocoa.h"
9 #include "components/storage_monitor/storage_info.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::AnyNumber;
16 using ::testing::Mock;
17 using ::testing::NiceMock;
18 using ::testing::Return;
19 using ::testing::ReturnPointee;
20 using ::testing::ReturnRef;
22 @interface MediaGalleryListEntry (testing)
23 - (NSInteger)state;
24 - (void)performClick:(id)sender;
25 @end
27 @implementation MediaGalleryListEntry (testing)
29 - (NSInteger)state {
30   return [checkbox_ state];
33 - (void)performClick:(id)sender {
34   [checkbox_ performClick:sender];
37 @end
40 MediaGalleryPrefInfo MakePrefInfoForTesting(MediaGalleryPrefId pref_id) {
41   MediaGalleryPrefInfo gallery;
42   gallery.pref_id = pref_id;
43   gallery.device_id = storage_monitor::StorageInfo::MakeDeviceId(
44       storage_monitor::StorageInfo::FIXED_MASS_STORAGE,
45       base::Int64ToString(pref_id));
46   gallery.display_name = base::ASCIIToUTF16("name");
47   return gallery;
50 class MediaGalleriesDialogTest : public testing::Test {
51  public:
52   MediaGalleriesDialogTest() {}
53   ~MediaGalleriesDialogTest() override {}
55   void SetUp() override {
56     std::vector<base::string16> headers;
57     headers.push_back(base::string16());
58     headers.push_back(base::ASCIIToUTF16("header2"));
59     ON_CALL(controller_, GetSectionHeaders()).
60         WillByDefault(Return(headers));
61     EXPECT_CALL(controller_, GetSectionEntries(_)).
62         Times(AnyNumber());
63   }
65   void TearDown() override {
66     Mock::VerifyAndClearExpectations(&controller_);
67     dialog_.reset();
68   }
70   NiceMock<MediaGalleriesDialogControllerMock>* controller() {
71     return &controller_;
72   }
74   MediaGalleriesDialogCocoa* GetOrCreateDialog() {
75     if (!dialog_.get()) {
76       dialog_.reset(static_cast<MediaGalleriesDialogCocoa*>(
77           MediaGalleriesDialog::Create(&controller_)));
78     }
79     return dialog_.get();
80   }
82  private:
83   NiceMock<MediaGalleriesDialogControllerMock> controller_;
85   scoped_ptr<MediaGalleriesDialogCocoa> dialog_;
87   DISALLOW_COPY_AND_ASSIGN(MediaGalleriesDialogTest);
90 // Tests that checkboxes are initialized according to the contents of
91 // permissions().
92 TEST_F(MediaGalleriesDialogTest, InitializeCheckboxes) {
93   MediaGalleriesDialogController::Entries attached_permissions;
94   attached_permissions.push_back(
95       MediaGalleriesDialogController::Entry(MakePrefInfoForTesting(1), true));
96   attached_permissions.push_back(
97       MediaGalleriesDialogController::Entry(MakePrefInfoForTesting(2), false));
98   EXPECT_CALL(*controller(), GetSectionEntries(0)).
99       WillRepeatedly(Return(attached_permissions));
101   // Initializing checkboxes should not cause them to be toggled.
102   EXPECT_CALL(*controller(), DidToggleEntry(_, _)).
103       Times(0);
105   EXPECT_EQ(2U, [[GetOrCreateDialog()->checkbox_container_ subviews] count]);
107   NSButton* checkbox1 =
108       [[GetOrCreateDialog()->checkbox_container_ subviews] objectAtIndex:0];
109   EXPECT_EQ([checkbox1 state], NSOnState);
111   NSButton* checkbox2 =
112       [[GetOrCreateDialog()->checkbox_container_ subviews] objectAtIndex:1];
113   EXPECT_EQ([checkbox2 state], NSOffState);
116 // Tests that toggling checkboxes updates the controller.
117 TEST_F(MediaGalleriesDialogTest, ToggleCheckboxes) {
118   MediaGalleriesDialogController::Entries attached_permissions;
119   attached_permissions.push_back(
120       MediaGalleriesDialogController::Entry(MakePrefInfoForTesting(1), true));
121   EXPECT_CALL(*controller(), GetSectionEntries(0)).
122       WillRepeatedly(Return(attached_permissions));
124   EXPECT_EQ(1U, [[GetOrCreateDialog()->checkbox_container_ subviews] count]);
126   NSButton* checkbox =
127       [[GetOrCreateDialog()->checkbox_container_ subviews] objectAtIndex:0];
128   EXPECT_EQ([checkbox state], NSOnState);
130   EXPECT_CALL(*controller(), DidToggleEntry(1, false));
131   [checkbox performClick:nil];
132   EXPECT_EQ([checkbox state], NSOffState);
134   EXPECT_CALL(*controller(), DidToggleEntry(1, true));
135   [checkbox performClick:nil];
136   EXPECT_EQ([checkbox state], NSOnState);
139 // Tests that UpdateGalleries will add a new checkbox, but only if it refers to
140 // a gallery that the dialog hasn't seen before.
141 TEST_F(MediaGalleriesDialogTest, UpdateAdds) {
142   MediaGalleriesDialogController::Entries attached_permissions;
143   EXPECT_CALL(*controller(), GetSectionEntries(0)).
144       WillRepeatedly(ReturnPointee(&attached_permissions));
146   EXPECT_EQ(0U, [[GetOrCreateDialog()->checkbox_container_ subviews] count]);
147   CGFloat old_container_height =
148       NSHeight([GetOrCreateDialog()->checkbox_container_ frame]);
150   attached_permissions.push_back(
151       MediaGalleriesDialogController::Entry(MakePrefInfoForTesting(1), true));
152   GetOrCreateDialog()->UpdateGalleries();
153   EXPECT_EQ(1U, [[GetOrCreateDialog()->checkbox_container_ subviews] count]);
155   // The checkbox container should be taller.
156   CGFloat new_container_height =
157       NSHeight([GetOrCreateDialog()->checkbox_container_ frame]);
158   EXPECT_GT(new_container_height, old_container_height);
159   old_container_height = new_container_height;
161   attached_permissions.push_back(
162       MediaGalleriesDialogController::Entry(MakePrefInfoForTesting(2), true));
163   GetOrCreateDialog()->UpdateGalleries();
164   EXPECT_EQ(2U, [[GetOrCreateDialog()->checkbox_container_ subviews] count]);
166   // The checkbox container should be taller.
167   new_container_height =
168       NSHeight([GetOrCreateDialog()->checkbox_container_ frame]);
169   EXPECT_GT(new_container_height, old_container_height);
170   old_container_height = new_container_height;
172   attached_permissions[1].selected = false;
173   GetOrCreateDialog()->UpdateGalleries();
174   EXPECT_EQ(2U, [[GetOrCreateDialog()->checkbox_container_ subviews] count]);
176   // The checkbox container height should not have changed.
177   new_container_height =
178       NSHeight([GetOrCreateDialog()->checkbox_container_ frame]);
179   EXPECT_EQ(new_container_height, old_container_height);
182 TEST_F(MediaGalleriesDialogTest, ForgetDeletes) {
183   MediaGalleriesDialogController::Entries attached_permissions;
184   EXPECT_CALL(*controller(), GetSectionEntries(0)).
185       WillRepeatedly(ReturnPointee(&attached_permissions));
187   GetOrCreateDialog();
189   // Add a couple of galleries.
190   attached_permissions.push_back(
191       MediaGalleriesDialogController::Entry(MakePrefInfoForTesting(1), true));
192   GetOrCreateDialog()->UpdateGalleries();
193   attached_permissions.push_back(
194       MediaGalleriesDialogController::Entry(MakePrefInfoForTesting(2), true));
195   GetOrCreateDialog()->UpdateGalleries();
196   EXPECT_EQ(2U, [[GetOrCreateDialog()->checkbox_container_ subviews] count]);
197   CGFloat old_container_height =
198       NSHeight([GetOrCreateDialog()->checkbox_container_ frame]);
200   // Remove a gallery.
201   attached_permissions.erase(attached_permissions.begin());
202   GetOrCreateDialog()->UpdateGalleries();
203   EXPECT_EQ(1U, [[GetOrCreateDialog()->checkbox_container_ subviews] count]);
205   // The checkbox container should be shorter.
206   CGFloat new_container_height =
207       NSHeight([GetOrCreateDialog()->checkbox_container_ frame]);
208   EXPECT_LT(new_container_height, old_container_height);