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.
4
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/gtk/extensions/media_galleries_dialog_gtk.h"
10 #include "chrome/common/extensions/extension_test_util.h"
11 #include "extensions/common/extension.h"
12 #include "testing/gtest/include/gtest/gtest.h"
13
14 using ::testing::_;
15 using ::testing::NiceMock;
16 using ::testing::Return;
17 using ::testing::ReturnPointee;
18 using ::testing::ReturnRef;
19
20 namespace {
21
MakePrefInfoForTesting(MediaGalleryPrefId id)22 MediaGalleryPrefInfo MakePrefInfoForTesting(MediaGalleryPrefId id) {
23 MediaGalleryPrefInfo gallery;
24 gallery.pref_id = id;
25 gallery.device_id = StorageInfo::MakeDeviceId(StorageInfo::FIXED_MASS_STORAGE,
26 base::Int64ToString(id));
27 gallery.display_name = ASCIIToUTF16("Display Name");
28 return gallery;
29 }
30
31 } // namespace
32
33 class MediaGalleriesDialogTest : public testing::Test {
34 public:
MediaGalleriesDialogTest()35 MediaGalleriesDialogTest() {}
~MediaGalleriesDialogTest()36 virtual ~MediaGalleriesDialogTest() {}
SetUp()37 virtual void SetUp() OVERRIDE {
38 dummy_extension_ = extension_test_util::CreateExtensionWithID("dummy");
39 }
TearDown()40 virtual void TearDown() OVERRIDE {
41 dummy_extension_ = NULL;
42 }
43
dummy_extension() const44 const extensions::Extension& dummy_extension() const {
45 return *dummy_extension_;
46 }
47
48 private:
49 scoped_refptr<extensions::Extension> dummy_extension_;
50
51 DISALLOW_COPY_AND_ASSIGN(MediaGalleriesDialogTest);
52 };
53
54 // Tests that checkboxes are initialized according to the contents of
55 // permissions in the registry.
TEST_F(MediaGalleriesDialogTest,InitializeCheckboxes)56 TEST_F(MediaGalleriesDialogTest, InitializeCheckboxes) {
57 // TODO(gbillock): Get rid of this mock; make something specialized.
58 NiceMock<MediaGalleriesDialogControllerMock> controller(dummy_extension());
59
60 MediaGalleriesDialogController::GalleryPermissionsVector attached_permissions;
61 attached_permissions.push_back(
62 MediaGalleriesDialogController::GalleryPermission(
63 MakePrefInfoForTesting(1), true));
64 attached_permissions.push_back(
65 MediaGalleriesDialogController::GalleryPermission(
66 MakePrefInfoForTesting(2), false));
67 EXPECT_CALL(controller, AttachedPermissions()).
68 WillRepeatedly(Return(attached_permissions));
69
70 MediaGalleriesDialogController::GalleryPermissionsVector
71 unattached_permissions;
72 EXPECT_CALL(controller, UnattachedPermissions()).
73 WillRepeatedly(Return(unattached_permissions));
74
75 MediaGalleriesDialogGtk dialog(&controller);
76 EXPECT_EQ(2U, dialog.checkbox_map_.size());
77
78 GtkWidget* checkbox1 = dialog.checkbox_map_[1];
79 ASSERT_TRUE(GTK_IS_TOGGLE_BUTTON(checkbox1));
80 EXPECT_TRUE(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(checkbox1)));
81
82 GtkWidget* checkbox2 = dialog.checkbox_map_[2];
83 ASSERT_TRUE(GTK_IS_TOGGLE_BUTTON(checkbox2));
84 EXPECT_FALSE(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(checkbox2)));
85 }
86
87 // Tests that toggling checkboxes updates the controller.
TEST_F(MediaGalleriesDialogTest,ToggleCheckboxes)88 TEST_F(MediaGalleriesDialogTest, ToggleCheckboxes) {
89 NiceMock<MediaGalleriesDialogControllerMock> controller(dummy_extension());
90
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));
97
98 MediaGalleriesDialogController::GalleryPermissionsVector
99 unattached_permissions;
100 EXPECT_CALL(controller, UnattachedPermissions()).
101 WillRepeatedly(Return(unattached_permissions));
102
103 MediaGalleriesDialogGtk dialog(&controller);
104 EXPECT_EQ(1U, dialog.checkbox_map_.size());
105 GtkWidget* checkbox =
106 dialog.checkbox_map_[1];
107 ASSERT_TRUE(GTK_IS_TOGGLE_BUTTON(checkbox));
108 EXPECT_TRUE(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(checkbox)));
109
110 EXPECT_CALL(controller, DidToggleGalleryId(1, false));
111 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkbox), FALSE);
112
113 EXPECT_CALL(controller, DidToggleGalleryId(1, true));
114 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkbox), TRUE);
115 }
116
117 // Tests that UpdateGallery will add a new checkbox, but only if it refers to
118 // a gallery that the dialog hasn't seen before.
TEST_F(MediaGalleriesDialogTest,UpdateAdds)119 TEST_F(MediaGalleriesDialogTest, UpdateAdds) {
120 NiceMock<MediaGalleriesDialogControllerMock> controller(dummy_extension());
121
122 MediaGalleriesDialogController::GalleryPermissionsVector attached_permissions;
123 EXPECT_CALL(controller, AttachedPermissions()).
124 WillRepeatedly(ReturnPointee(&attached_permissions));
125
126 MediaGalleriesDialogController::GalleryPermissionsVector
127 unattached_permissions;
128 EXPECT_CALL(controller, UnattachedPermissions()).
129 WillRepeatedly(Return(unattached_permissions));
130
131 MediaGalleriesDialogGtk dialog(&controller);
132
133 EXPECT_TRUE(dialog.checkbox_map_.empty());
134
135 MediaGalleryPrefInfo gallery1 = MakePrefInfoForTesting(1);
136 attached_permissions.push_back(
137 MediaGalleriesDialogController::GalleryPermission(gallery1, true));
138 dialog.UpdateGalleries();
139 EXPECT_EQ(1U, dialog.checkbox_map_.size());
140
141 MediaGalleryPrefInfo gallery2 = MakePrefInfoForTesting(2);
142 attached_permissions.push_back(
143 MediaGalleriesDialogController::GalleryPermission(gallery2, true));
144 dialog.UpdateGalleries();
145 EXPECT_EQ(2U, dialog.checkbox_map_.size());
146
147 attached_permissions.push_back(
148 MediaGalleriesDialogController::GalleryPermission(gallery2, false));
149 dialog.UpdateGalleries();
150 EXPECT_EQ(2U, dialog.checkbox_map_.size());
151 }
152
TEST_F(MediaGalleriesDialogTest,ForgetDeletes)153 TEST_F(MediaGalleriesDialogTest, ForgetDeletes) {
154 NiceMock<MediaGalleriesDialogControllerMock> controller(dummy_extension());
155
156 MediaGalleriesDialogController::GalleryPermissionsVector attached_permissions;
157 EXPECT_CALL(controller, AttachedPermissions()).
158 WillRepeatedly(ReturnPointee(&attached_permissions));
159
160 MediaGalleriesDialogController::GalleryPermissionsVector
161 unattached_permissions;
162 EXPECT_CALL(controller, UnattachedPermissions()).
163 WillRepeatedly(Return(unattached_permissions));
164
165 MediaGalleriesDialogGtk dialog(&controller);
166
167 EXPECT_TRUE(dialog.checkbox_map_.empty());
168
169 MediaGalleryPrefInfo gallery1 = MakePrefInfoForTesting(1);
170 attached_permissions.push_back(
171 MediaGalleriesDialogController::GalleryPermission(gallery1, true));
172 dialog.UpdateGalleries();
173 EXPECT_EQ(1U, dialog.checkbox_map_.size());
174
175 MediaGalleryPrefInfo gallery2 = MakePrefInfoForTesting(2);
176 attached_permissions.push_back(
177 MediaGalleriesDialogController::GalleryPermission(gallery2, true));
178 dialog.UpdateGalleries();
179 EXPECT_EQ(2U, dialog.checkbox_map_.size());
180
181 attached_permissions.pop_back();
182 dialog.UpdateGalleries();
183 EXPECT_EQ(1U, dialog.checkbox_map_.size());
184 }
185