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/permission_message_provider.h"
20 #include "extensions/common/permissions/permissions_data.h"
21 #include "extensions/common/test_util.h"
22 #include "ui/views/controls/scroll_view.h"
23 #include "ui/views/view.h"
24 #include "ui/views/widget/widget.h"
26 using extensions::PermissionIDSet
;
27 using extensions::PermissionMessage
;
28 using extensions::PermissionMessages
;
30 // A simple delegate implementation that counts the number of times
31 // |InstallUIProceed| and |InstallUIAbort| are called.
32 class MockExtensionInstallPromptDelegate
33 : public ExtensionInstallPrompt::Delegate
{
35 MockExtensionInstallPromptDelegate()
39 // ExtensionInstallPrompt::Delegate overrides.
40 void InstallUIProceed() override
;
41 void InstallUIAbort(bool user_initiated
) override
;
43 int proceed_count() { return proceed_count_
; }
44 int abort_count() { return abort_count_
; }
51 void MockExtensionInstallPromptDelegate::InstallUIProceed() {
55 void MockExtensionInstallPromptDelegate::InstallUIAbort(bool user_initiated
) {
59 // This lets us construct the parent for the prompt we're constructing in our
61 class MockExtensionInstallPrompt
: public ExtensionInstallPrompt
{
63 explicit MockExtensionInstallPrompt(content::WebContents
* web_contents
)
64 : ExtensionInstallPrompt(web_contents
), prompt_(NULL
) {}
65 ~MockExtensionInstallPrompt() override
{}
66 void set_prompt(ExtensionInstallPrompt::Prompt
* prompt
) {
69 ExtensionInstallPrompt::Prompt
* get_prompt() {
74 ExtensionInstallPrompt::Prompt
* prompt_
;
77 class ExtensionInstallDialogViewTestBase
: public ExtensionBrowserTest
{
79 explicit ExtensionInstallDialogViewTestBase(
80 ExtensionInstallPrompt::PromptType prompt_type
);
81 ~ExtensionInstallDialogViewTestBase() override
{}
83 void SetUpOnMainThread() override
;
85 ExtensionInstallPrompt::Prompt
* prompt() { return prompt_
.get(); }
86 content::WebContents
* web_contents() { return web_contents_
; }
87 MockExtensionInstallPromptDelegate
* delegate() { return &delegate_
; }
89 void SetPromptPermissions(const PermissionMessages
& permissions
);
90 void SetPromptRetainedFiles(std::vector
<base::FilePath
> files
);
93 const extensions::Extension
* extension_
;
94 MockExtensionInstallPrompt
* install_prompt_
;
95 scoped_refptr
<ExtensionInstallPrompt::Prompt
> prompt_
;
96 content::WebContents
* web_contents_
;
97 MockExtensionInstallPromptDelegate delegate_
;
99 DISALLOW_COPY_AND_ASSIGN(ExtensionInstallDialogViewTestBase
);
102 ExtensionInstallDialogViewTestBase::ExtensionInstallDialogViewTestBase(
103 ExtensionInstallPrompt::PromptType prompt_type
)
105 install_prompt_(NULL
),
106 prompt_(new ExtensionInstallPrompt::Prompt(prompt_type
)),
107 web_contents_(NULL
) {
110 void ExtensionInstallDialogViewTestBase::SetUpOnMainThread() {
111 ExtensionBrowserTest::SetUpOnMainThread();
113 extension_
= ExtensionBrowserTest::LoadExtension(test_data_dir_
.AppendASCII(
114 "install_prompt/permissions_scrollbar_regression"));
116 web_contents_
= browser()->tab_strip_model()->GetWebContentsAt(0);
118 install_prompt_
= new MockExtensionInstallPrompt(web_contents_
);
119 install_prompt_
->set_prompt(prompt_
.get());
120 prompt_
->set_extension(extension_
);
122 scoped_ptr
<ExtensionIconManager
> icon_manager(new ExtensionIconManager());
123 const SkBitmap icon_bitmap
= icon_manager
->GetIcon(extension_
->id());
124 gfx::Image icon
= gfx::Image::CreateFrom1xBitmap(icon_bitmap
);
125 prompt_
->set_icon(icon
);
127 this->SetPromptPermissions(PermissionMessages());
128 this->SetPromptRetainedFiles(std::vector
<base::FilePath
>());
131 void ExtensionInstallDialogViewTestBase::SetPromptPermissions(
132 const PermissionMessages
& permissions
) {
133 prompt_
->SetPermissions(permissions
,
134 ExtensionInstallPrompt::REGULAR_PERMISSIONS
);
137 void ExtensionInstallDialogViewTestBase::SetPromptRetainedFiles(
138 std::vector
<base::FilePath
> files
) {
139 prompt_
->set_retained_files(files
);
142 class ScrollbarTest
: public ExtensionInstallDialogViewTestBase
{
145 ~ScrollbarTest() override
{}
147 bool IsScrollbarVisible();
150 DISALLOW_COPY_AND_ASSIGN(ScrollbarTest
);
153 ScrollbarTest::ScrollbarTest()
154 : ExtensionInstallDialogViewTestBase(
155 ExtensionInstallPrompt::PERMISSIONS_PROMPT
) {
158 bool ScrollbarTest::IsScrollbarVisible() {
159 ExtensionInstallDialogView
* dialog
= new ExtensionInstallDialogView(
165 // Create the modal view around the install dialog view.
166 views::Widget
* modal
= constrained_window::CreateBrowserModalDialogViews(
167 dialog
, web_contents()->GetTopLevelNativeWindow());
169 content::RunAllBlockingPoolTasksUntilIdle();
171 // Check if the vertical scrollbar is visible.
172 return dialog
->scroll_view()->vertical_scroll_bar()->visible();
175 // Tests that a scrollbar _is_ shown for an excessively long extension
177 IN_PROC_BROWSER_TEST_F(ScrollbarTest
, LongPromptScrollbar
) {
178 base::string16
permission_string(base::ASCIIToUTF16("Test"));
179 PermissionMessages permissions
;
180 for (int i
= 0; i
< 20; i
++) {
181 permissions
.push_back(PermissionMessage(permission_string
,
184 this->SetPromptPermissions(permissions
);
185 ASSERT_TRUE(IsScrollbarVisible()) << "Scrollbar is not visible";
188 // Tests that a scrollbar isn't shown for this regression case.
189 // See crbug.com/385570 for details.
190 IN_PROC_BROWSER_TEST_F(ScrollbarTest
, ScrollbarRegression
) {
191 base::string16
permission_string(base::ASCIIToUTF16(
192 "Read and modify your data on *.facebook.com"));
193 PermissionMessages permissions
;
194 permissions
.push_back(PermissionMessage(permission_string
,
196 this->SetPromptPermissions(permissions
);
197 ASSERT_FALSE(IsScrollbarVisible()) << "Scrollbar is visible";
200 class ExtensionInstallDialogViewTest
201 : public ExtensionInstallDialogViewTestBase
{
203 ExtensionInstallDialogViewTest()
204 : ExtensionInstallDialogViewTestBase(
205 ExtensionInstallPrompt::INSTALL_PROMPT
) {}
206 ~ExtensionInstallDialogViewTest() override
{}
209 DISALLOW_COPY_AND_ASSIGN(ExtensionInstallDialogViewTest
);
212 // Verifies that the delegate is notified when the user selects to accept or
213 // cancel the install.
214 IN_PROC_BROWSER_TEST_F(ExtensionInstallDialogViewTest
, NotifyDelegate
) {
216 // The user confirms the install.
217 MockExtensionInstallPromptDelegate delegate
;
218 scoped_ptr
<ExtensionInstallDialogView
> dialog(
219 new ExtensionInstallDialogView(
220 profile(), web_contents(), &delegate
, prompt()));
221 views::DialogDelegateView
* delegate_view
= dialog
.get();
223 delegate_view
->Accept();
224 delegate_view
->OnClosed();
227 EXPECT_EQ(0, delegate
.abort_count());
228 EXPECT_EQ(1, delegate
.proceed_count());
232 // The user cancels the install.
233 MockExtensionInstallPromptDelegate delegate
;
234 scoped_ptr
<ExtensionInstallDialogView
> dialog(
235 new ExtensionInstallDialogView(
236 profile(), web_contents(), &delegate
, prompt()));
237 views::DialogDelegateView
* delegate_view
= dialog
.get();
239 delegate_view
->Cancel();
240 delegate_view
->OnClosed();
243 EXPECT_EQ(1, delegate
.abort_count());
244 EXPECT_EQ(0, delegate
.proceed_count());
248 // Corner case: Dialog is closed without the user explicitly choosing to
249 // proceed or cancel.
250 MockExtensionInstallPromptDelegate delegate
;
251 scoped_ptr
<ExtensionInstallDialogView
> dialog(
252 new ExtensionInstallDialogView(
253 profile(), web_contents(), &delegate
, prompt()));
256 EXPECT_EQ(1, delegate
.abort_count());
257 EXPECT_EQ(0, delegate
.proceed_count());