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 "testing/gtest/include/gtest/gtest.h"
12 #include "ui/views/controls/button/checkbox.h"
15 using ::testing::AnyNumber
;
16 using ::testing::Mock
;
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::Uint64ToString(id
));
29 gallery
.display_name
= base::ASCIIToUTF16("Display Name");
35 class MediaGalleriesDialogTest
: public testing::Test
{
37 MediaGalleriesDialogTest() {}
38 ~MediaGalleriesDialogTest() override
{}
39 void SetUp() override
{
40 std::vector
<base::string16
> headers
;
41 headers
.push_back(base::string16());
42 headers
.push_back(base::ASCIIToUTF16("header2"));
43 ON_CALL(controller_
, GetSectionHeaders()).
44 WillByDefault(Return(headers
));
45 EXPECT_CALL(controller_
, GetSectionEntries(_
)).
49 void TearDown() override
{
50 Mock::VerifyAndClearExpectations(&controller_
);
53 NiceMock
<MediaGalleriesDialogControllerMock
>* controller() {
58 // TODO(gbillock): Get rid of this mock; make something specialized.
59 NiceMock
<MediaGalleriesDialogControllerMock
> controller_
;
61 DISALLOW_COPY_AND_ASSIGN(MediaGalleriesDialogTest
);
64 // Tests that checkboxes are initialized according to the contents of
65 // permissions in the registry.
66 TEST_F(MediaGalleriesDialogTest
, InitializeCheckboxes
) {
67 MediaGalleriesDialogController::Entries attached_permissions
;
68 attached_permissions
.push_back(
69 MediaGalleriesDialogController::Entry(MakePrefInfoForTesting(1), true));
70 attached_permissions
.push_back(
71 MediaGalleriesDialogController::Entry(MakePrefInfoForTesting(2), false));
72 EXPECT_CALL(*controller(), GetSectionEntries(0)).
73 WillRepeatedly(Return(attached_permissions
));
75 MediaGalleriesDialogViews
dialog(controller());
76 EXPECT_EQ(2U, dialog
.checkbox_map_
.size());
78 MediaGalleryCheckboxView
* checkbox_view1
= dialog
.checkbox_map_
[1];
79 EXPECT_TRUE(checkbox_view1
->checkbox()->checked());
81 MediaGalleryCheckboxView
* checkbox_view2
= dialog
.checkbox_map_
[2];
82 EXPECT_FALSE(checkbox_view2
->checkbox()->checked());
85 // Tests that toggling checkboxes updates the controller.
86 TEST_F(MediaGalleriesDialogTest
, ToggleCheckboxes
) {
87 MediaGalleriesDialogController::Entries attached_permissions
;
88 attached_permissions
.push_back(
89 MediaGalleriesDialogController::Entry(MakePrefInfoForTesting(1), true));
90 EXPECT_CALL(*controller(), GetSectionEntries(0)).
91 WillRepeatedly(Return(attached_permissions
));
93 MediaGalleriesDialogViews
dialog(controller());
94 EXPECT_EQ(1U, dialog
.checkbox_map_
.size());
95 views::Checkbox
* checkbox
= dialog
.checkbox_map_
[1]->checkbox();
96 EXPECT_TRUE(checkbox
->checked());
98 ui::KeyEvent
dummy_event(ui::ET_KEY_PRESSED
, ui::VKEY_A
, ui::EF_NONE
);
99 EXPECT_CALL(*controller(), DidToggleEntry(1, false));
100 checkbox
->SetChecked(false);
101 dialog
.ButtonPressed(checkbox
, dummy_event
);
103 EXPECT_CALL(*controller(), DidToggleEntry(1, true));
104 checkbox
->SetChecked(true);
105 dialog
.ButtonPressed(checkbox
, dummy_event
);
108 // Tests that UpdateGallery will add a new checkbox, but only if it refers to
109 // a gallery that the dialog hasn't seen before.
110 TEST_F(MediaGalleriesDialogTest
, UpdateAdds
) {
111 MediaGalleriesDialogViews
dialog(controller());
113 MediaGalleriesDialogController::Entries attached_permissions
;
114 EXPECT_CALL(*controller(), GetSectionEntries(0)).
115 WillRepeatedly(ReturnPointee(&attached_permissions
));
117 EXPECT_TRUE(dialog
.checkbox_map_
.empty());
119 MediaGalleryPrefInfo gallery1
= MakePrefInfoForTesting(1);
120 attached_permissions
.push_back(
121 MediaGalleriesDialogController::Entry(gallery1
, true));
122 dialog
.UpdateGalleries();
123 EXPECT_EQ(1U, dialog
.checkbox_map_
.size());
125 MediaGalleryPrefInfo gallery2
= MakePrefInfoForTesting(2);
126 attached_permissions
.push_back(
127 MediaGalleriesDialogController::Entry(gallery2
, true));
128 dialog
.UpdateGalleries();
129 EXPECT_EQ(2U, dialog
.checkbox_map_
.size());
131 attached_permissions
.push_back(
132 MediaGalleriesDialogController::Entry(gallery2
, false));
133 dialog
.UpdateGalleries();
134 EXPECT_EQ(2U, dialog
.checkbox_map_
.size());
137 TEST_F(MediaGalleriesDialogTest
, ForgetDeletes
) {
138 MediaGalleriesDialogViews
dialog(controller());
140 MediaGalleriesDialogController::Entries attached_permissions
;
141 EXPECT_CALL(*controller(), GetSectionEntries(0)).
142 WillRepeatedly(ReturnPointee(&attached_permissions
));
144 EXPECT_TRUE(dialog
.checkbox_map_
.empty());
146 MediaGalleryPrefInfo gallery1
= MakePrefInfoForTesting(1);
147 attached_permissions
.push_back(
148 MediaGalleriesDialogController::Entry(gallery1
, true));
149 dialog
.UpdateGalleries();
150 EXPECT_EQ(1U, dialog
.checkbox_map_
.size());
152 MediaGalleryPrefInfo gallery2
= MakePrefInfoForTesting(2);
153 attached_permissions
.push_back(
154 MediaGalleriesDialogController::Entry(gallery2
, true));
155 dialog
.UpdateGalleries();
156 EXPECT_EQ(2U, dialog
.checkbox_map_
.size());
158 attached_permissions
.pop_back();
159 dialog
.UpdateGalleries();
160 EXPECT_EQ(1U, dialog
.checkbox_map_
.size());