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/extensions/extension_install_prompt.h"
7 #include "base/run_loop.h"
8 #include "chrome/browser/extensions/extension_install_prompt_show_params.h"
9 #include "chrome/browser/ui/browser.h"
10 #include "chrome/browser/ui/browser_window.h"
11 #include "chrome/browser/ui/tabs/tab_strip_model.h"
12 #include "chrome/test/base/in_process_browser_test.h"
13 #include "content/public/test/test_utils.h"
14 #include "extensions/common/extension.h"
15 #include "extensions/common/extension_builder.h"
16 #include "extensions/common/value_builder.h"
20 scoped_refptr
<extensions::Extension
> BuildTestExtension() {
21 return extensions::ExtensionBuilder()
22 .SetManifest(extensions::DictionaryBuilder()
24 .Set("version", "1.0"))
28 // ExtensionInstallPrompt::ShowDialogCallback which proceeds without showing the
30 void TestShowDialogCallback(
31 ExtensionInstallPromptShowParams
* params
,
32 ExtensionInstallPrompt::Delegate
* delegate
,
33 scoped_refptr
<ExtensionInstallPrompt::Prompt
> prompt
) {
34 delegate
->InstallUIProceed();
37 class TestExtensionInstallPromptDelegate
38 : public ExtensionInstallPrompt::Delegate
{
40 explicit TestExtensionInstallPromptDelegate(const base::Closure
& quit_closure
)
41 : quit_closure_(quit_closure
), proceeded_(false), aborted_(false) {}
43 ~TestExtensionInstallPromptDelegate() override
{
55 void InstallUIProceed() override
{
60 void InstallUIAbort(bool user_initiated
) override
{
65 base::Closure quit_closure_
;
69 DISALLOW_COPY_AND_ASSIGN(TestExtensionInstallPromptDelegate
);
74 typedef InProcessBrowserTest ExtensionInstallPromptBrowserTest
;
76 // Test that ExtensionInstallPrompt aborts the install if the web contents which
77 // were passed to the ExtensionInstallPrompt constructor get destroyed.
78 // CrxInstaller takes in ExtensionInstallPrompt in the constructor and does a
79 // bunch of asynchronous processing prior to confirming the install. A user may
80 // close the current tab while this processing is taking place.
81 IN_PROC_BROWSER_TEST_F(ExtensionInstallPromptBrowserTest
,
82 TrackParentWebContentsDestruction
) {
83 AddBlankTabAndShow(browser());
84 TabStripModel
* tab_strip_model
= browser()->tab_strip_model();
85 content::WebContents
* web_contents
= tab_strip_model
->GetActiveWebContents();
86 int web_contents_index
= tab_strip_model
->GetIndexOfWebContents(web_contents
);
87 scoped_refptr
<extensions::Extension
> extension(BuildTestExtension());
89 ExtensionInstallPrompt
prompt(web_contents
);
90 tab_strip_model
->CloseWebContentsAt(web_contents_index
,
91 TabStripModel::CLOSE_NONE
);
92 content::RunAllPendingInMessageLoop();
94 base::RunLoop run_loop
;
95 TestExtensionInstallPromptDelegate
delegate(run_loop
.QuitClosure());
96 prompt
.ConfirmInstall(&delegate
, extension
.get(),
97 base::Bind(&TestShowDialogCallback
));
99 EXPECT_TRUE(delegate
.DidAbort());
102 // Test that ExtensionInstallPrompt aborts the install if the gfx::NativeWindow
103 // which is passed to the ExtensionInstallPrompt constructor is destroyed.
104 IN_PROC_BROWSER_TEST_F(ExtensionInstallPromptBrowserTest
,
105 TrackParentWindowDestruction
) {
106 // Create a second browser to prevent the app from exiting when the browser is
108 CreateBrowser(browser()->profile());
110 scoped_refptr
<extensions::Extension
> extension(BuildTestExtension());
112 ExtensionInstallPrompt
prompt(browser()->profile(),
113 browser()->window()->GetNativeWindow());
114 browser()->window()->Close();
115 content::RunAllPendingInMessageLoop();
117 base::RunLoop run_loop
;
118 TestExtensionInstallPromptDelegate
delegate(run_loop
.QuitClosure());
119 prompt
.ConfirmInstall(&delegate
, extension
.get(),
120 base::Bind(&TestShowDialogCallback
));
122 EXPECT_TRUE(delegate
.DidAbort());
125 // Test that ExtensionInstallPrompt shows the dialog normally if no parent
126 // web contents or parent gfx::NativeWindow is passed to the
127 // ExtensionInstallPrompt constructor.
128 IN_PROC_BROWSER_TEST_F(ExtensionInstallPromptBrowserTest
, NoParent
) {
129 scoped_refptr
<extensions::Extension
> extension(BuildTestExtension());
131 ExtensionInstallPrompt
prompt(browser()->profile(), NULL
);
132 base::RunLoop run_loop
;
133 TestExtensionInstallPromptDelegate
delegate(run_loop
.QuitClosure());
134 prompt
.ConfirmInstall(&delegate
, extension
.get(),
135 base::Bind(&TestShowDialogCallback
));
138 // TestShowDialogCallback() should have signaled the install to proceed.
139 EXPECT_TRUE(delegate
.DidProceed());