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