Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / ui / gtk / extensions / media_galleries_dialog_gtk_unittest.cc
blob7a644170b2c8bf700a3f267fed1ae60e3c23bfac
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/gtk/extensions/media_galleries_dialog_gtk.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 namespace {
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 = base::ASCIIToUTF16("Display Name");
28 return gallery;
31 } // namespace
33 class MediaGalleriesDialogTest : public testing::Test {
34 public:
35 MediaGalleriesDialogTest() {}
36 virtual ~MediaGalleriesDialogTest() {}
37 virtual void SetUp() OVERRIDE {
38 dummy_extension_ = extensions::test_util::CreateExtensionWithID("dummy");
40 virtual void TearDown() OVERRIDE {
41 dummy_extension_ = NULL;
44 const extensions::Extension& dummy_extension() const {
45 return *dummy_extension_;
48 private:
49 scoped_refptr<extensions::Extension> dummy_extension_;
51 DISALLOW_COPY_AND_ASSIGN(MediaGalleriesDialogTest);
54 // Tests that checkboxes are initialized according to the contents of
55 // permissions in the registry.
56 TEST_F(MediaGalleriesDialogTest, InitializeCheckboxes) {
57 // TODO(gbillock): Get rid of this mock; make something specialized.
58 NiceMock<MediaGalleriesDialogControllerMock> controller(dummy_extension());
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));
70 MediaGalleriesDialogController::GalleryPermissionsVector
71 unattached_permissions;
72 EXPECT_CALL(controller, UnattachedPermissions()).
73 WillRepeatedly(Return(unattached_permissions));
75 MediaGalleriesDialogGtk dialog(&controller);
76 EXPECT_EQ(2U, dialog.checkbox_map_.size());
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)));
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)));
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 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)));
110 EXPECT_CALL(controller, DidToggleGalleryId(1, false));
111 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkbox), FALSE);
113 EXPECT_CALL(controller, DidToggleGalleryId(1, true));
114 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkbox), TRUE);
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.
119 TEST_F(MediaGalleriesDialogTest, UpdateAdds) {
120 NiceMock<MediaGalleriesDialogControllerMock> controller(dummy_extension());
122 MediaGalleriesDialogController::GalleryPermissionsVector attached_permissions;
123 EXPECT_CALL(controller, AttachedPermissions()).
124 WillRepeatedly(ReturnPointee(&attached_permissions));
126 MediaGalleriesDialogController::GalleryPermissionsVector
127 unattached_permissions;
128 EXPECT_CALL(controller, UnattachedPermissions()).
129 WillRepeatedly(Return(unattached_permissions));
131 MediaGalleriesDialogGtk dialog(&controller);
133 EXPECT_TRUE(dialog.checkbox_map_.empty());
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());
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());
147 attached_permissions.push_back(
148 MediaGalleriesDialogController::GalleryPermission(gallery2, false));
149 dialog.UpdateGalleries();
150 EXPECT_EQ(2U, dialog.checkbox_map_.size());
153 TEST_F(MediaGalleriesDialogTest, ForgetDeletes) {
154 NiceMock<MediaGalleriesDialogControllerMock> controller(dummy_extension());
156 MediaGalleriesDialogController::GalleryPermissionsVector attached_permissions;
157 EXPECT_CALL(controller, AttachedPermissions()).
158 WillRepeatedly(ReturnPointee(&attached_permissions));
160 MediaGalleriesDialogController::GalleryPermissionsVector
161 unattached_permissions;
162 EXPECT_CALL(controller, UnattachedPermissions()).
163 WillRepeatedly(Return(unattached_permissions));
165 MediaGalleriesDialogGtk dialog(&controller);
167 EXPECT_TRUE(dialog.checkbox_map_.empty());
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());
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());
181 attached_permissions.pop_back();
182 dialog.UpdateGalleries();
183 EXPECT_EQ(1U, dialog.checkbox_map_.size());