1 // Copyright 2014 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/views/extensions/media_galleries_dialog_views.h"
9 #include "chrome/browser/ui/views/extensions/media_gallery_checkbox_view.h"
10 #include "components/storage_monitor/storage_info.h"
11 #include "extensions/common/extension.h"
12 #include "extensions/common/test_util.h"
13 #include "testing/gtest/include/gtest/gtest.h"
14 #include "ui/views/controls/button/checkbox.h"
17 using ::testing::NiceMock
;
18 using ::testing::Return
;
19 using ::testing::ReturnPointee
;
23 MediaGalleryPrefInfo
MakePrefInfoForTesting(MediaGalleryPrefId id
) {
24 MediaGalleryPrefInfo gallery
;
26 gallery
.device_id
= storage_monitor::StorageInfo::MakeDeviceId(
27 storage_monitor::StorageInfo::FIXED_MASS_STORAGE
,
28 base::Int64ToString(id
));
29 gallery
.display_name
= base::ASCIIToUTF16("Display Name");
35 class MediaGalleriesDialogTest
: public testing::Test
{
37 MediaGalleriesDialogTest() {}
38 virtual ~MediaGalleriesDialogTest() {}
39 virtual void SetUp() OVERRIDE
{
40 dummy_extension_
= extensions::test_util::CreateExtensionWithID("dummy");
42 virtual void TearDown() OVERRIDE
{
43 dummy_extension_
= NULL
;
46 const extensions::Extension
& dummy_extension() const {
47 return *dummy_extension_
;
51 scoped_refptr
<extensions::Extension
> dummy_extension_
;
53 DISALLOW_COPY_AND_ASSIGN(MediaGalleriesDialogTest
);
56 // Tests that checkboxes are initialized according to the contents of
57 // permissions in the registry.
58 TEST_F(MediaGalleriesDialogTest
, InitializeCheckboxes
) {
59 // TODO(gbillock): Get rid of this mock; make something specialized.
60 NiceMock
<MediaGalleriesDialogControllerMock
> controller(dummy_extension());
62 MediaGalleriesDialogController::GalleryPermissionsVector attached_permissions
;
63 attached_permissions
.push_back(
64 MediaGalleriesDialogController::GalleryPermission(
65 1, MakePrefInfoForTesting(10), true));
66 attached_permissions
.push_back(
67 MediaGalleriesDialogController::GalleryPermission(
68 2, MakePrefInfoForTesting(20), false));
69 EXPECT_CALL(controller
, AttachedPermissions()).
70 WillRepeatedly(Return(attached_permissions
));
72 MediaGalleriesDialogController::GalleryPermissionsVector
73 unattached_permissions
;
74 EXPECT_CALL(controller
, UnattachedPermissions()).
75 WillRepeatedly(Return(unattached_permissions
));
77 MediaGalleriesDialogViews
dialog(&controller
);
78 EXPECT_EQ(2U, dialog
.checkbox_map_
.size());
80 MediaGalleryCheckboxView
* checkbox_view1
= dialog
.checkbox_map_
[1];
81 EXPECT_TRUE(checkbox_view1
->checkbox()->checked());
83 MediaGalleryCheckboxView
* checkbox_view2
= dialog
.checkbox_map_
[2];
84 EXPECT_FALSE(checkbox_view2
->checkbox()->checked());
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 1, MakePrefInfoForTesting(10), 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 MediaGalleriesDialogViews
dialog(&controller
);
104 EXPECT_EQ(1U, dialog
.checkbox_map_
.size());
105 views::Checkbox
* checkbox
= dialog
.checkbox_map_
[1]->checkbox();
106 EXPECT_TRUE(checkbox
->checked());
108 ui::KeyEvent
dummy_event(ui::ET_KEY_PRESSED
, ui::VKEY_A
, ui::EF_NONE
, false);
109 EXPECT_CALL(controller
, DidToggleGallery(1, false));
110 checkbox
->SetChecked(false);
111 dialog
.ButtonPressed(checkbox
, dummy_event
);
113 EXPECT_CALL(controller
, DidToggleGallery(1, true));
114 checkbox
->SetChecked(true);
115 dialog
.ButtonPressed(checkbox
, dummy_event
);
118 // Tests that UpdateGallery will add a new checkbox, but only if it refers to
119 // a gallery that the dialog hasn't seen before.
120 TEST_F(MediaGalleriesDialogTest
, UpdateAdds
) {
121 NiceMock
<MediaGalleriesDialogControllerMock
> controller(dummy_extension());
123 MediaGalleriesDialogController::GalleryPermissionsVector attached_permissions
;
124 EXPECT_CALL(controller
, AttachedPermissions()).
125 WillRepeatedly(ReturnPointee(&attached_permissions
));
127 MediaGalleriesDialogController::GalleryPermissionsVector
128 unattached_permissions
;
129 EXPECT_CALL(controller
, UnattachedPermissions()).
130 WillRepeatedly(Return(unattached_permissions
));
132 MediaGalleriesDialogViews
dialog(&controller
);
134 EXPECT_TRUE(dialog
.checkbox_map_
.empty());
136 MediaGalleryPrefInfo gallery1
= MakePrefInfoForTesting(10);
137 attached_permissions
.push_back(
138 MediaGalleriesDialogController::GalleryPermission(1, gallery1
, true));
139 dialog
.UpdateGalleries();
140 EXPECT_EQ(1U, dialog
.checkbox_map_
.size());
142 MediaGalleryPrefInfo gallery2
= MakePrefInfoForTesting(20);
143 attached_permissions
.push_back(
144 MediaGalleriesDialogController::GalleryPermission(2, gallery2
, true));
145 dialog
.UpdateGalleries();
146 EXPECT_EQ(2U, dialog
.checkbox_map_
.size());
148 attached_permissions
.push_back(
149 MediaGalleriesDialogController::GalleryPermission(2, gallery2
, false));
150 dialog
.UpdateGalleries();
151 EXPECT_EQ(2U, dialog
.checkbox_map_
.size());
154 TEST_F(MediaGalleriesDialogTest
, ForgetDeletes
) {
155 NiceMock
<MediaGalleriesDialogControllerMock
> controller(dummy_extension());
157 MediaGalleriesDialogController::GalleryPermissionsVector attached_permissions
;
158 EXPECT_CALL(controller
, AttachedPermissions()).
159 WillRepeatedly(ReturnPointee(&attached_permissions
));
161 MediaGalleriesDialogController::GalleryPermissionsVector
162 unattached_permissions
;
163 EXPECT_CALL(controller
, UnattachedPermissions()).
164 WillRepeatedly(Return(unattached_permissions
));
166 MediaGalleriesDialogViews
dialog(&controller
);
168 EXPECT_TRUE(dialog
.checkbox_map_
.empty());
170 MediaGalleryPrefInfo gallery1
= MakePrefInfoForTesting(10);
171 attached_permissions
.push_back(
172 MediaGalleriesDialogController::GalleryPermission(1, gallery1
, true));
173 dialog
.UpdateGalleries();
174 EXPECT_EQ(1U, dialog
.checkbox_map_
.size());
176 MediaGalleryPrefInfo gallery2
= MakePrefInfoForTesting(20);
177 attached_permissions
.push_back(
178 MediaGalleriesDialogController::GalleryPermission(2, gallery2
, true));
179 dialog
.UpdateGalleries();
180 EXPECT_EQ(2U, dialog
.checkbox_map_
.size());
182 attached_permissions
.pop_back();
183 dialog
.UpdateGalleries();
184 EXPECT_EQ(1U, dialog
.checkbox_map_
.size());