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
3 // found in the LICENSE file.
5 #include "chrome/browser/ui/views/website_settings/website_settings_popup_view.h"
7 #include "chrome/browser/ui/views/website_settings/permission_selector_view.h"
8 #include "chrome/test/base/testing_profile.h"
9 #include "content/public/common/ssl_status.h"
10 #include "content/public/test/test_browser_thread_bundle.h"
11 #include "content/public/test/test_web_contents_factory.h"
12 #include "testing/gtest/include/gtest/gtest.h"
13 #include "ui/views/controls/button/menu_button.h"
14 #include "ui/views/controls/label.h"
15 #include "ui/views/test/scoped_views_test_helper.h"
19 class WebsiteSettingsPopupViewTestApi
{
21 WebsiteSettingsPopupViewTestApi(gfx::NativeView parent
,
23 content::WebContents
* web_contents
)
27 web_contents_(web_contents
) {
33 view_
->GetWidget()->CloseNow();
35 GURL
url("http://www.example.com");
36 SecurityStateModel::SecurityInfo security_info
;
37 views::View
* anchor_view
= nullptr;
38 view_
= new WebsiteSettingsPopupView(anchor_view
, parent_
, profile_
,
39 web_contents_
, url
, security_info
);
42 WebsiteSettingsPopupView
* view() { return view_
; }
43 views::View
* permissions_content() { return view_
->permissions_content_
; }
45 PermissionSelectorView
* GetPermissionSelectorAt(int index
) {
46 return static_cast<PermissionSelectorView
*>(
47 permissions_content()->child_at(index
));
50 views::MenuButton
* GetPermissionButtonAt(int index
) {
51 const int kButtonIndex
= 2; // Button should be the third child.
52 views::View
* view
= GetPermissionSelectorAt(index
)->child_at(kButtonIndex
);
53 EXPECT_EQ(views::MenuButton::kViewClassName
, view
->GetClassName());
54 return static_cast<views::MenuButton
*>(view
);
57 // Simulates recreating the dialog with a new PermissionInfoList.
58 void SetPermissionInfo(const PermissionInfoList
& list
) {
59 for (const WebsiteSettingsPopupView::PermissionInfo
& info
: list
)
60 view_
->presenter_
->OnSitePermissionChanged(info
.type
, info
.setting
);
65 WebsiteSettingsPopupView
* view_
; // Weak. Owned by its Widget.
67 // For recreating the view.
68 gfx::NativeView parent_
;
70 content::WebContents
* web_contents_
;
72 DISALLOW_COPY_AND_ASSIGN(WebsiteSettingsPopupViewTestApi
);
79 // Helper class that wraps a TestingProfile and a TestWebContents for a test
80 // harness. Inspired by RenderViewHostTestHarness, but doesn't use inheritance
81 // so the helper can be composed with other helpers in the test harness.
82 class ScopedWebContentsTestHelper
{
84 ScopedWebContentsTestHelper() {
85 web_contents_
= factory_
.CreateWebContents(&profile_
);
88 Profile
* profile() { return &profile_
; }
89 content::WebContents
* web_contents() { return web_contents_
; }
92 content::TestBrowserThreadBundle thread_bundle_
;
93 TestingProfile profile_
;
94 content::TestWebContentsFactory factory_
;
95 content::WebContents
* web_contents_
; // Weak. Owned by factory_.
97 DISALLOW_COPY_AND_ASSIGN(ScopedWebContentsTestHelper
);
100 class WebsiteSettingsPopupViewTest
: public testing::Test
{
102 WebsiteSettingsPopupViewTest() {}
105 void SetUp() override
{
106 views::Widget::InitParams parent_params
;
107 parent_params
.context
= views_helper_
.GetContext();
108 parent_window_
= new views::Widget();
109 parent_window_
->Init(parent_params
);
111 content::WebContents
* web_contents
= web_contents_helper_
.web_contents();
112 TabSpecificContentSettings::CreateForWebContents(web_contents
);
113 api_
.reset(new test::WebsiteSettingsPopupViewTestApi(
114 parent_window_
->GetNativeView(), web_contents_helper_
.profile(),
118 void TearDown() override
{ parent_window_
->CloseNow(); }
121 ScopedWebContentsTestHelper web_contents_helper_
;
122 views::ScopedViewsTestHelper views_helper_
;
124 views::Widget
* parent_window_
= nullptr; // Weak. Owned by the NativeWidget.
125 scoped_ptr
<test::WebsiteSettingsPopupViewTestApi
> api_
;
127 DISALLOW_COPY_AND_ASSIGN(WebsiteSettingsPopupViewTest
);
132 // Test UI construction and reconstruction via
133 // WebsiteSettingsPopupView::SetPermissionInfo().
134 TEST_F(WebsiteSettingsPopupViewTest
, SetPermissionInfo
) {
135 PermissionInfoList
list(1);
136 list
.back().type
= CONTENT_SETTINGS_TYPE_GEOLOCATION
;
137 list
.back().source
= content_settings::SETTING_SOURCE_USER
;
139 EXPECT_EQ(0, api_
->permissions_content()->child_count());
141 list
.back().setting
= CONTENT_SETTING_ALLOW
;
142 api_
->SetPermissionInfo(list
);
143 EXPECT_EQ(1, api_
->permissions_content()->child_count());
145 PermissionSelectorView
* selector
= api_
->GetPermissionSelectorAt(0);
146 EXPECT_EQ(3, selector
->child_count());
148 // Verify labels match the settings on the PermissionInfoList.
149 const int kLabelIndex
= 1;
150 EXPECT_EQ(views::Label::kViewClassName
,
151 selector
->child_at(kLabelIndex
)->GetClassName());
152 views::Label
* label
=
153 static_cast<views::Label
*>(selector
->child_at(kLabelIndex
));
154 EXPECT_EQ(base::ASCIIToUTF16("Location:"), label
->text());
155 EXPECT_EQ(base::ASCIIToUTF16("Allowed by you"),
156 api_
->GetPermissionButtonAt(0)->GetText());
158 // Verify calling SetPermisisonInfo() directly updates the UI.
159 list
.back().setting
= CONTENT_SETTING_BLOCK
;
160 api_
->SetPermissionInfo(list
);
161 EXPECT_EQ(base::ASCIIToUTF16("Blocked by you"),
162 api_
->GetPermissionButtonAt(0)->GetText());
164 // Simulate a user selection via the UI. Note this will also cover logic in
165 // WebsiteSettings to update the pref.
166 list
.back().setting
= CONTENT_SETTING_ALLOW
;
167 api_
->GetPermissionSelectorAt(0)->PermissionChanged(list
.back());
168 EXPECT_EQ(1, api_
->permissions_content()->child_count());
169 EXPECT_EQ(base::ASCIIToUTF16("Allowed by you"),
170 api_
->GetPermissionButtonAt(0)->GetText());
172 // Setting to the default via the UI should keep the button around.
173 list
.back().setting
= CONTENT_SETTING_ASK
;
174 api_
->GetPermissionSelectorAt(0)->PermissionChanged(list
.back());
175 EXPECT_EQ(1, api_
->permissions_content()->child_count());
176 EXPECT_EQ(base::ASCIIToUTF16("Ask by you"),
177 api_
->GetPermissionButtonAt(0)->GetText());
179 // However, since the setting is now default, recreating the dialog with those
180 // settings should omit the permission from the UI.
181 api_
->SetPermissionInfo(list
);
182 EXPECT_EQ(0, api_
->permissions_content()->child_count());