| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/ui/views/website_settings/website_settings_popup_view.h
" | 5 #include "chrome/browser/ui/views/website_settings/website_settings_popup_view.h
" |
| 6 | 6 |
| 7 #include "base/macros.h" | 7 #include "base/macros.h" |
| 8 #include "base/strings/utf_string_conversions.h" | 8 #include "base/strings/utf_string_conversions.h" |
| 9 #include "chrome/browser/ui/exclusive_access/exclusive_access_manager.h" | 9 #include "chrome/browser/ui/exclusive_access/exclusive_access_manager.h" |
| 10 #include "chrome/browser/ui/views/website_settings/chosen_object_row.h" | 10 #include "chrome/browser/ui/views/website_settings/chosen_object_row.h" |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 49 views::View* anchor_view = nullptr; | 49 views::View* anchor_view = nullptr; |
| 50 view_ = | 50 view_ = |
| 51 new WebsiteSettingsPopupView(anchor_view, parent_, profile_, | 51 new WebsiteSettingsPopupView(anchor_view, parent_, profile_, |
| 52 web_contents_, GURL(kUrl), security_info); | 52 web_contents_, GURL(kUrl), security_info); |
| 53 } | 53 } |
| 54 | 54 |
| 55 WebsiteSettingsPopupView* view() { return view_; } | 55 WebsiteSettingsPopupView* view() { return view_; } |
| 56 views::View* permissions_view() { return view_->permissions_view_; } | 56 views::View* permissions_view() { return view_->permissions_view_; } |
| 57 | 57 |
| 58 PermissionSelectorRow* GetPermissionSelectorAt(int index) { | 58 PermissionSelectorRow* GetPermissionSelectorAt(int index) { |
| 59 return static_cast<PermissionSelectorRow*>( | 59 return view_->selector_rows_[index].get(); |
| 60 permissions_view()->child_at(index)); | 60 } |
| 61 |
| 62 // Returns the permission label text of the |index|th permission selector row. |
| 63 // This function returns an empty string if the permission selector row's |
| 64 // |label_| element isn't actually a |views::Label|. |
| 65 base::string16 GetPermissionLabelTextAt(int index) { |
| 66 views::View* view = GetPermissionSelectorAt(index)->label_; |
| 67 if (view->GetClassName() == views::Label::kViewClassName) |
| 68 return static_cast<views::Label*>(view)->text(); |
| 69 return base::string16(); |
| 61 } | 70 } |
| 62 | 71 |
| 63 base::string16 GetPermissionButtonTextAt(int index) { | 72 base::string16 GetPermissionButtonTextAt(int index) { |
| 64 const int kButtonIndex = 2; // Button should be the third child. | 73 views::View* view = GetPermissionSelectorAt(index)->button(); |
| 65 views::View* view = GetPermissionSelectorAt(index)->child_at(kButtonIndex); | |
| 66 if (view->GetClassName() == views::MenuButton::kViewClassName) { | 74 if (view->GetClassName() == views::MenuButton::kViewClassName) { |
| 67 return static_cast<views::MenuButton*>(view)->GetText(); | 75 return static_cast<views::MenuButton*>(view)->GetText(); |
| 68 } else if (view->GetClassName() == views::Combobox::kViewClassName) { | 76 } else if (view->GetClassName() == views::Combobox::kViewClassName) { |
| 69 views::Combobox* combobox = static_cast<views::Combobox*>(view); | 77 views::Combobox* combobox = static_cast<views::Combobox*>(view); |
| 70 return combobox->GetTextForRow(combobox->GetSelectedRow()); | 78 return combobox->GetTextForRow(combobox->GetSelectedRow()); |
| 71 } else { | 79 } else { |
| 72 NOTREACHED() << "Unknown class " << view->GetClassName(); | 80 NOTREACHED() << "Unknown class " << view->GetClassName(); |
| 73 return base::string16(); | 81 return base::string16(); |
| 74 } | 82 } |
| 75 } | 83 } |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 158 // test creates settings that are left at their defaults, leading to zero | 166 // test creates settings that are left at their defaults, leading to zero |
| 159 // checked options, and checks that the text on the MenuButtons is right. Since | 167 // checked options, and checks that the text on the MenuButtons is right. Since |
| 160 // the Comboboxes the MacViews version of this dialog uses don't have separate | 168 // the Comboboxes the MacViews version of this dialog uses don't have separate |
| 161 // text, this test doesn't work. | 169 // text, this test doesn't work. |
| 162 #if defined(OS_MACOSX) | 170 #if defined(OS_MACOSX) |
| 163 #define MAYBE_SetPermissionInfo DISABLED_SetPermissionInfo | 171 #define MAYBE_SetPermissionInfo DISABLED_SetPermissionInfo |
| 164 #else | 172 #else |
| 165 #define MAYBE_SetPermissionInfo SetPermissionInfo | 173 #define MAYBE_SetPermissionInfo SetPermissionInfo |
| 166 #endif | 174 #endif |
| 167 | 175 |
| 176 // Each permission selector row is like this: [icon] [label] [selector] |
| 177 constexpr int kViewsPerPermissionRow = 3; |
| 178 |
| 168 // Test UI construction and reconstruction via | 179 // Test UI construction and reconstruction via |
| 169 // WebsiteSettingsPopupView::SetPermissionInfo(). | 180 // WebsiteSettingsPopupView::SetPermissionInfo(). |
| 170 TEST_F(WebsiteSettingsPopupViewTest, MAYBE_SetPermissionInfo) { | 181 TEST_F(WebsiteSettingsPopupViewTest, MAYBE_SetPermissionInfo) { |
| 171 PermissionInfoList list(1); | 182 PermissionInfoList list(1); |
| 172 list.back().type = CONTENT_SETTINGS_TYPE_GEOLOCATION; | 183 list.back().type = CONTENT_SETTINGS_TYPE_GEOLOCATION; |
| 173 list.back().source = content_settings::SETTING_SOURCE_USER; | 184 list.back().source = content_settings::SETTING_SOURCE_USER; |
| 174 list.back().is_incognito = false; | 185 list.back().is_incognito = false; |
| 175 list.back().setting = CONTENT_SETTING_DEFAULT; | 186 list.back().setting = CONTENT_SETTING_DEFAULT; |
| 176 | 187 |
| 177 const int kExpectedChildren = | 188 const int kExpectedChildren = |
| 178 ExclusiveAccessManager::IsSimplifiedFullscreenUIEnabled() ? 11 : 13; | 189 kViewsPerPermissionRow * |
| 190 (ExclusiveAccessManager::IsSimplifiedFullscreenUIEnabled() ? 11 : 13); |
| 179 EXPECT_EQ(kExpectedChildren, api_->permissions_view()->child_count()); | 191 EXPECT_EQ(kExpectedChildren, api_->permissions_view()->child_count()); |
| 180 | 192 |
| 181 list.back().setting = CONTENT_SETTING_ALLOW; | 193 list.back().setting = CONTENT_SETTING_ALLOW; |
| 182 api_->SetPermissionInfo(list); | 194 api_->SetPermissionInfo(list); |
| 183 EXPECT_EQ(kExpectedChildren, api_->permissions_view()->child_count()); | 195 EXPECT_EQ(kExpectedChildren, api_->permissions_view()->child_count()); |
| 184 | 196 |
| 185 PermissionSelectorRow* selector = api_->GetPermissionSelectorAt(0); | 197 PermissionSelectorRow* selector = api_->GetPermissionSelectorAt(0); |
| 186 EXPECT_EQ(3, selector->child_count()); | 198 EXPECT_TRUE(selector); |
| 187 | 199 |
| 188 // Verify labels match the settings on the PermissionInfoList. | 200 // Verify labels match the settings on the PermissionInfoList. |
| 189 const int kLabelIndex = 1; | 201 EXPECT_EQ(base::ASCIIToUTF16("Location"), api_->GetPermissionLabelTextAt(0)); |
| 190 EXPECT_EQ(views::Label::kViewClassName, | |
| 191 selector->child_at(kLabelIndex)->GetClassName()); | |
| 192 views::Label* label = | |
| 193 static_cast<views::Label*>(selector->child_at(kLabelIndex)); | |
| 194 EXPECT_EQ(base::ASCIIToUTF16("Location"), label->text()); | |
| 195 EXPECT_EQ(base::ASCIIToUTF16("Allow"), api_->GetPermissionButtonTextAt(0)); | 202 EXPECT_EQ(base::ASCIIToUTF16("Allow"), api_->GetPermissionButtonTextAt(0)); |
| 196 | 203 |
| 197 // Verify calling SetPermisisonInfo() directly updates the UI. | 204 // Verify calling SetPermisisonInfo() directly updates the UI. |
| 198 list.back().setting = CONTENT_SETTING_BLOCK; | 205 list.back().setting = CONTENT_SETTING_BLOCK; |
| 199 api_->SetPermissionInfo(list); | 206 api_->SetPermissionInfo(list); |
| 200 EXPECT_EQ(base::ASCIIToUTF16("Block"), api_->GetPermissionButtonTextAt(0)); | 207 EXPECT_EQ(base::ASCIIToUTF16("Block"), api_->GetPermissionButtonTextAt(0)); |
| 201 | 208 |
| 202 // Simulate a user selection via the UI. Note this will also cover logic in | 209 // Simulate a user selection via the UI. Note this will also cover logic in |
| 203 // WebsiteSettings to update the pref. | 210 // WebsiteSettings to update the pref. |
| 204 list.back().setting = CONTENT_SETTING_ALLOW; | 211 list.back().setting = CONTENT_SETTING_ALLOW; |
| 205 api_->GetPermissionSelectorAt(0)->PermissionChanged(list.back()); | 212 api_->GetPermissionSelectorAt(0)->PermissionChanged(list.back()); |
| 206 EXPECT_EQ(kExpectedChildren, api_->permissions_view()->child_count()); | 213 EXPECT_EQ(kExpectedChildren, api_->permissions_view()->child_count()); |
| 207 EXPECT_EQ(base::ASCIIToUTF16("Allow"), api_->GetPermissionButtonTextAt(0)); | 214 EXPECT_EQ(base::ASCIIToUTF16("Allow"), api_->GetPermissionButtonTextAt(0)); |
| 208 | 215 |
| 209 // Setting to the default via the UI should keep the button around. | 216 // Setting to the default via the UI should keep the button around. |
| 210 list.back().setting = CONTENT_SETTING_ASK; | 217 list.back().setting = CONTENT_SETTING_ASK; |
| 211 api_->GetPermissionSelectorAt(0)->PermissionChanged(list.back()); | 218 api_->GetPermissionSelectorAt(0)->PermissionChanged(list.back()); |
| 212 EXPECT_EQ(kExpectedChildren, api_->permissions_view()->child_count()); | 219 EXPECT_EQ(kExpectedChildren, api_->permissions_view()->child_count()); |
| 213 EXPECT_EQ(base::ASCIIToUTF16("Ask"), api_->GetPermissionButtonTextAt(0)); | 220 EXPECT_EQ(base::ASCIIToUTF16("Ask"), api_->GetPermissionButtonTextAt(0)); |
| 214 | 221 |
| 215 // However, since the setting is now default, recreating the dialog with those | 222 // However, since the setting is now default, recreating the dialog with those |
| 216 // settings should omit the permission from the UI. | 223 // settings should omit the permission from the UI. |
| 217 api_->SetPermissionInfo(list); | 224 api_->SetPermissionInfo(list); |
| 218 EXPECT_EQ(kExpectedChildren, api_->permissions_view()->child_count()); | 225 EXPECT_EQ(kExpectedChildren, api_->permissions_view()->child_count()); |
| 219 } | 226 } |
| 220 | 227 |
| 221 // Test UI construction and reconstruction with USB devices. | 228 // Test UI construction and reconstruction with USB devices. |
| 222 TEST_F(WebsiteSettingsPopupViewTest, SetPermissionInfoWithUsbDevice) { | 229 TEST_F(WebsiteSettingsPopupViewTest, SetPermissionInfoWithUsbDevice) { |
| 223 const int kExpectedChildren = | 230 const int kExpectedChildren = |
| 224 ExclusiveAccessManager::IsSimplifiedFullscreenUIEnabled() ? 11 : 13; | 231 kViewsPerPermissionRow * |
| 232 (ExclusiveAccessManager::IsSimplifiedFullscreenUIEnabled() ? 11 : 13); |
| 225 EXPECT_EQ(kExpectedChildren, api_->permissions_view()->child_count()); | 233 EXPECT_EQ(kExpectedChildren, api_->permissions_view()->child_count()); |
| 226 | 234 |
| 227 const GURL origin = GURL(kUrl).GetOrigin(); | 235 const GURL origin = GURL(kUrl).GetOrigin(); |
| 228 scoped_refptr<device::UsbDevice> device = | 236 scoped_refptr<device::UsbDevice> device = |
| 229 new device::MockUsbDevice(0, 0, "Google", "Gizmo", "1234567890"); | 237 new device::MockUsbDevice(0, 0, "Google", "Gizmo", "1234567890"); |
| 230 device_client_.usb_service()->AddDevice(device); | 238 device_client_.usb_service()->AddDevice(device); |
| 231 UsbChooserContext* store = | 239 UsbChooserContext* store = |
| 232 UsbChooserContextFactory::GetForProfile(web_contents_helper_.profile()); | 240 UsbChooserContextFactory::GetForProfile(web_contents_helper_.profile()); |
| 233 store->GrantDevicePermission(origin, origin, device->guid()); | 241 store->GrantDevicePermission(origin, origin, device->guid()); |
| 234 | 242 |
| (...skipping 16 matching lines...) Expand all Loading... |
| 251 | 259 |
| 252 const ui::MouseEvent event(ui::ET_MOUSE_PRESSED, gfx::Point(), gfx::Point(), | 260 const ui::MouseEvent event(ui::ET_MOUSE_PRESSED, gfx::Point(), gfx::Point(), |
| 253 ui::EventTimeForNow(), 0, 0); | 261 ui::EventTimeForNow(), 0, 0); |
| 254 views::ButtonListener* button_listener = | 262 views::ButtonListener* button_listener = |
| 255 static_cast<views::ButtonListener*>(object_view); | 263 static_cast<views::ButtonListener*>(object_view); |
| 256 button_listener->ButtonPressed(button, event); | 264 button_listener->ButtonPressed(button, event); |
| 257 api_->SetPermissionInfo(list); | 265 api_->SetPermissionInfo(list); |
| 258 EXPECT_EQ(kExpectedChildren, api_->permissions_view()->child_count()); | 266 EXPECT_EQ(kExpectedChildren, api_->permissions_view()->child_count()); |
| 259 EXPECT_FALSE(store->HasDevicePermission(origin, origin, device)); | 267 EXPECT_FALSE(store->HasDevicePermission(origin, origin, device)); |
| 260 } | 268 } |
| OLD | NEW |