1 // Copyright 2014 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/extensions/extension_install_dialog_view.h"
7 #include "base/strings/utf_string_conversions.h"
8 #include "chrome/browser/extensions/extension_browsertest.h"
9 #include "chrome/browser/extensions/extension_icon_manager.h"
10 #include "chrome/browser/extensions/extension_install_prompt.h"
11 #include "chrome/browser/ui/browser.h"
12 #include "chrome/browser/ui/tabs/tab_strip_model.h"
13 #include "chrome/browser/ui/webui/extensions/extension_settings_handler.h"
14 #include "chrome/common/extensions/extension_test_util.h"
15 #include "components/constrained_window/constrained_window_views.h"
16 #include "content/public/browser/browser_thread.h"
17 #include "content/public/test/test_utils.h"
18 #include "extensions/common/extension.h"
19 #include "extensions/common/permissions/permissions_data.h"
20 #include "extensions/common/test_util.h"
21 #include "ui/views/controls/scroll_view.h"
22 #include "ui/views/view.h"
23 #include "ui/views/widget/widget.h"
25 // A simple delegate implementation that counts the number of times
26 // |InstallUIProceed| and |InstallUIAbort| are called.
27 class MockExtensionInstallPromptDelegate
28 : public ExtensionInstallPrompt::Delegate
{
30 MockExtensionInstallPromptDelegate()
34 // ExtensionInstallPrompt::Delegate overrides.
35 void InstallUIProceed() override
;
36 void InstallUIAbort(bool user_initiated
) override
;
38 int proceed_count() { return proceed_count_
; }
39 int abort_count() { return abort_count_
; }
46 void MockExtensionInstallPromptDelegate::InstallUIProceed() {
50 void MockExtensionInstallPromptDelegate::InstallUIAbort(bool user_initiated
) {
54 // This lets us construct the parent for the prompt we're constructing in our
56 class MockExtensionInstallPrompt
: public ExtensionInstallPrompt
{
58 explicit MockExtensionInstallPrompt(content::WebContents
* web_contents
)
59 : ExtensionInstallPrompt(web_contents
), prompt_(NULL
) {}
60 ~MockExtensionInstallPrompt() override
{}
61 void set_prompt(ExtensionInstallPrompt::Prompt
* prompt
) {
64 ExtensionInstallPrompt::Prompt
* get_prompt() {
69 ExtensionInstallPrompt::Prompt
* prompt_
;
72 class ExtensionInstallDialogViewTestBase
: public ExtensionBrowserTest
{
74 explicit ExtensionInstallDialogViewTestBase(
75 ExtensionInstallPrompt::PromptType prompt_type
);
76 ~ExtensionInstallDialogViewTestBase() override
{}
78 void SetUpOnMainThread() override
;
80 ExtensionInstallPrompt::Prompt
* prompt() { return prompt_
.get(); }
81 content::WebContents
* web_contents() { return web_contents_
; }
82 MockExtensionInstallPromptDelegate
* delegate() { return &delegate_
; }
84 void SetPromptPermissions(std::vector
<base::string16
> permissions
);
85 void SetPromptDetails(std::vector
<base::string16
> details
);
86 void SetPromptRetainedFiles(std::vector
<base::FilePath
> files
);
89 const extensions::Extension
* extension_
;
90 MockExtensionInstallPrompt
* install_prompt_
;
91 scoped_refptr
<ExtensionInstallPrompt::Prompt
> prompt_
;
92 content::WebContents
* web_contents_
;
93 MockExtensionInstallPromptDelegate delegate_
;
95 DISALLOW_COPY_AND_ASSIGN(ExtensionInstallDialogViewTestBase
);
98 ExtensionInstallDialogViewTestBase::ExtensionInstallDialogViewTestBase(
99 ExtensionInstallPrompt::PromptType prompt_type
)
101 install_prompt_(NULL
),
102 prompt_(new ExtensionInstallPrompt::Prompt(prompt_type
)),
103 web_contents_(NULL
) {
106 void ExtensionInstallDialogViewTestBase::SetUpOnMainThread() {
107 ExtensionBrowserTest::SetUpOnMainThread();
109 extension_
= ExtensionBrowserTest::LoadExtension(test_data_dir_
.AppendASCII(
110 "install_prompt/permissions_scrollbar_regression"));
112 web_contents_
= browser()->tab_strip_model()->GetWebContentsAt(0);
114 install_prompt_
= new MockExtensionInstallPrompt(web_contents_
);
115 install_prompt_
->set_prompt(prompt_
.get());
116 prompt_
->set_extension(extension_
);
118 scoped_ptr
<ExtensionIconManager
> icon_manager(new ExtensionIconManager());
119 const SkBitmap icon_bitmap
= icon_manager
->GetIcon(extension_
->id());
120 gfx::Image icon
= gfx::Image::CreateFrom1xBitmap(icon_bitmap
);
121 prompt_
->set_icon(icon
);
123 this->SetPromptPermissions(std::vector
<base::string16
>());
124 this->SetPromptDetails(std::vector
<base::string16
>());
125 this->SetPromptRetainedFiles(std::vector
<base::FilePath
>());
128 void ExtensionInstallDialogViewTestBase::SetPromptPermissions(
129 std::vector
<base::string16
> permissions
) {
130 prompt_
->SetPermissions(permissions
,
131 ExtensionInstallPrompt::REGULAR_PERMISSIONS
);
134 void ExtensionInstallDialogViewTestBase::SetPromptDetails(
135 std::vector
<base::string16
> details
) {
136 prompt_
->SetPermissionsDetails(details
,
137 ExtensionInstallPrompt::REGULAR_PERMISSIONS
);
140 void ExtensionInstallDialogViewTestBase::SetPromptRetainedFiles(
141 std::vector
<base::FilePath
> files
) {
142 prompt_
->set_retained_files(files
);
145 class ScrollbarTest
: public ExtensionInstallDialogViewTestBase
{
148 ~ScrollbarTest() override
{}
150 bool IsScrollbarVisible();
153 DISALLOW_COPY_AND_ASSIGN(ScrollbarTest
);
156 ScrollbarTest::ScrollbarTest()
157 : ExtensionInstallDialogViewTestBase(
158 ExtensionInstallPrompt::PERMISSIONS_PROMPT
) {
161 bool ScrollbarTest::IsScrollbarVisible() {
162 ExtensionInstallDialogView
* dialog
= new ExtensionInstallDialogView(
168 // Create the modal view around the install dialog view.
169 views::Widget
* modal
= constrained_window::CreateBrowserModalDialogViews(
170 dialog
, web_contents()->GetTopLevelNativeWindow());
172 content::RunAllBlockingPoolTasksUntilIdle();
174 // Check if the vertical scrollbar is visible.
175 return dialog
->scroll_view()->vertical_scroll_bar()->visible();
178 // Tests that a scrollbar _is_ shown for an excessively long extension
180 IN_PROC_BROWSER_TEST_F(ScrollbarTest
, LongPromptScrollbar
) {
181 base::string16
permission_string(base::ASCIIToUTF16("Test"));
182 std::vector
<base::string16
> permissions
;
183 std::vector
<base::string16
> details
;
184 for (int i
= 0; i
< 20; i
++) {
185 permissions
.push_back(permission_string
);
186 details
.push_back(base::string16());
188 this->SetPromptPermissions(permissions
);
189 this->SetPromptDetails(details
);
190 ASSERT_TRUE(IsScrollbarVisible()) << "Scrollbar is not visible";
193 // Tests that a scrollbar isn't shown for this regression case.
194 // See crbug.com/385570 for details.
195 IN_PROC_BROWSER_TEST_F(ScrollbarTest
, ScrollbarRegression
) {
196 base::string16
permission_string(base::ASCIIToUTF16(
197 "Read and modify your data on *.facebook.com"));
198 std::vector
<base::string16
> permissions
;
199 permissions
.push_back(permission_string
);
200 this->SetPromptPermissions(permissions
);
201 std::vector
<base::string16
> details
;
202 details
.push_back(base::string16());
203 this->SetPromptDetails(details
);
204 ASSERT_FALSE(IsScrollbarVisible()) << "Scrollbar is visible";
207 class ExtensionInstallDialogViewTest
208 : public ExtensionInstallDialogViewTestBase
{
210 ExtensionInstallDialogViewTest()
211 : ExtensionInstallDialogViewTestBase(
212 ExtensionInstallPrompt::INSTALL_PROMPT
) {}
213 ~ExtensionInstallDialogViewTest() override
{}
216 DISALLOW_COPY_AND_ASSIGN(ExtensionInstallDialogViewTest
);
219 // Verifies that the delegate is notified when the user selects to accept or
220 // cancel the install.
221 IN_PROC_BROWSER_TEST_F(ExtensionInstallDialogViewTest
, NotifyDelegate
) {
223 // The user confirms the install.
224 MockExtensionInstallPromptDelegate delegate
;
225 scoped_ptr
<ExtensionInstallDialogView
> dialog(
226 new ExtensionInstallDialogView(
227 profile(), web_contents(), &delegate
, prompt()));
228 views::DialogDelegateView
* delegate_view
= dialog
.get();
230 delegate_view
->Accept();
231 delegate_view
->OnClosed();
234 EXPECT_EQ(0, delegate
.abort_count());
235 EXPECT_EQ(1, delegate
.proceed_count());
239 // The user cancels the install.
240 MockExtensionInstallPromptDelegate delegate
;
241 scoped_ptr
<ExtensionInstallDialogView
> dialog(
242 new ExtensionInstallDialogView(
243 profile(), web_contents(), &delegate
, prompt()));
244 views::DialogDelegateView
* delegate_view
= dialog
.get();
246 delegate_view
->Cancel();
247 delegate_view
->OnClosed();
250 EXPECT_EQ(1, delegate
.abort_count());
251 EXPECT_EQ(0, delegate
.proceed_count());
255 // Corner case: Dialog is closed without the user explicitly choosing to
256 // proceed or cancel.
257 MockExtensionInstallPromptDelegate delegate
;
258 scoped_ptr
<ExtensionInstallDialogView
> dialog(
259 new ExtensionInstallDialogView(
260 profile(), web_contents(), &delegate
, prompt()));
263 EXPECT_EQ(1, delegate
.abort_count());
264 EXPECT_EQ(0, delegate
.proceed_count());