1 // Copyright 2013 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"
6 #include "chrome/browser/extensions/tab_helper.h"
7 #include "chrome/browser/extensions/webstore_inline_installer.h"
8 #include "chrome/browser/extensions/webstore_inline_installer_factory.h"
9 #include "chrome/browser/extensions/webstore_installer_test.h"
10 #include "chrome/browser/extensions/webstore_standalone_installer.h"
11 #include "chrome/browser/ui/browser.h"
12 #include "chrome/browser/ui/tabs/tab_strip_model.h"
13 #include "chrome/test/base/ui_test_utils.h"
14 #include "content/public/browser/render_view_host.h"
15 #include "content/public/browser/web_contents.h"
18 using content::WebContents
;
19 using extensions::Extension
;
20 using extensions::TabHelper
;
21 using extensions::WebstoreInlineInstaller
;
22 using extensions::WebstoreInlineInstallerFactory
;
23 using extensions::WebstoreStandaloneInstaller
;
25 const char kWebstoreDomain
[] = "cws.com";
26 const char kAppDomain
[] = "app.com";
27 const char kNonAppDomain
[] = "nonapp.com";
28 const char kTestExtensionId
[] = "ecglahbcnmdpdciemllbhojghbkagdje";
29 const char kTestDataPath
[] = "extensions/api_test/webstore_inline_install";
30 const char kCrxFilename
[] = "extension.crx";
32 class WebstoreInlineInstallerTest
: public WebstoreInstallerTest
{
34 WebstoreInlineInstallerTest()
35 : WebstoreInstallerTest(
43 class ProgrammableInstallPrompt
: public ExtensionInstallPrompt
{
45 explicit ProgrammableInstallPrompt(WebContents
* contents
)
46 : ExtensionInstallPrompt(contents
)
49 virtual ~ProgrammableInstallPrompt() {}
51 virtual void ConfirmStandaloneInstall(
53 const Extension
* extension
,
55 const ExtensionInstallPrompt::Prompt
& prompt
) OVERRIDE
{
60 return delegate_
!= NULL
;
63 static void Accept() {
64 delegate_
->InstallUIProceed();
67 static void Reject() {
68 delegate_
->InstallUIAbort(true);
72 static Delegate
* delegate_
;
75 ExtensionInstallPrompt::Delegate
* ProgrammableInstallPrompt::delegate_
;
78 // Fake inline installer which creates a programmable prompt in place of
79 // the normal dialog UI.
80 class WebstoreInlineInstallerForTest
: public WebstoreInlineInstaller
{
82 WebstoreInlineInstallerForTest(WebContents
* contents
,
83 const std::string
& extension_id
,
84 const GURL
& requestor_url
,
85 const Callback
& callback
)
86 : WebstoreInlineInstaller(
90 base::Bind(DummyCallback
)),
91 programmable_prompt_(NULL
) {
94 virtual scoped_ptr
<ExtensionInstallPrompt
> CreateInstallUI() OVERRIDE
{
95 programmable_prompt_
= new ProgrammableInstallPrompt(web_contents());
96 return make_scoped_ptr(programmable_prompt_
).
97 PassAs
<ExtensionInstallPrompt
>();
101 virtual ~WebstoreInlineInstallerForTest() {}
103 friend class base::RefCountedThreadSafe
<WebstoreStandaloneInstaller
>;
105 static void DummyCallback(bool /*success*/, const std::string
& /*error*/) {
108 ProgrammableInstallPrompt
* programmable_prompt_
;
111 class WebstoreInlineInstallerForTestFactory
:
112 public WebstoreInlineInstallerFactory
{
113 virtual ~WebstoreInlineInstallerForTestFactory() {}
114 virtual WebstoreInlineInstaller
* CreateInstaller(
115 WebContents
* contents
,
116 const std::string
& webstore_item_id
,
117 const GURL
& requestor_url
,
118 const WebstoreStandaloneInstaller::Callback
& callback
) OVERRIDE
{
119 return new WebstoreInlineInstallerForTest(
120 contents
, webstore_item_id
, requestor_url
, callback
);
124 IN_PROC_BROWSER_TEST_F(WebstoreInlineInstallerTest
,
125 CloseTabBeforeInstallConfirmation
) {
126 GURL install_url
= GenerateTestServerUrl(kAppDomain
, "install.html");
127 ui_test_utils::NavigateToURL(browser(), install_url
);
128 WebContents
* web_contents
=
129 browser()->tab_strip_model()->GetActiveWebContents();
130 TabHelper
* tab_helper
= TabHelper::FromWebContents(web_contents
);
131 tab_helper
->SetWebstoreInlineInstallerFactoryForTests(
132 new WebstoreInlineInstallerForTestFactory());
133 RunTestAsync("runTest");
134 while (!ProgrammableInstallPrompt::Ready())
135 base::RunLoop().RunUntilIdle();
136 web_contents
->Close();
137 ProgrammableInstallPrompt::Accept();