OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 5 #include "chrome/browser/extensions/extension_install_prompt.h" |
| 6 |
| 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" |
| 17 |
| 18 namespace { |
| 19 |
| 20 scoped_refptr<extensions::Extension> BuildTestExtension() { |
| 21 return extensions::ExtensionBuilder() |
| 22 .SetManifest(extensions::DictionaryBuilder() |
| 23 .Set("name", "foo") |
| 24 .Set("version", "1.0")) |
| 25 .Build(); |
| 26 } |
| 27 |
| 28 // ExtensionInstallPrompt::ShowDialogCallback which proceeds without showing the |
| 29 // prompt. |
| 30 void TestShowDialogCallback( |
| 31 ExtensionInstallPromptShowParams* params, |
| 32 ExtensionInstallPrompt::Delegate* delegate, |
| 33 scoped_refptr<ExtensionInstallPrompt::Prompt> prompt) { |
| 34 delegate->InstallUIProceed(); |
| 35 } |
| 36 |
| 37 class TestExtensionInstallPromptDelegate |
| 38 : public ExtensionInstallPrompt::Delegate { |
| 39 public: |
| 40 explicit TestExtensionInstallPromptDelegate(const base::Closure& quit_closure) |
| 41 : quit_closure_(quit_closure), proceeded_(false), aborted_(false) {} |
| 42 |
| 43 ~TestExtensionInstallPromptDelegate() override { |
| 44 } |
| 45 |
| 46 bool DidProceed() { |
| 47 return proceeded_; |
| 48 } |
| 49 |
| 50 bool DidAbort() { |
| 51 return aborted_; |
| 52 } |
| 53 |
| 54 private: |
| 55 void InstallUIProceed() override { |
| 56 proceeded_ = true; |
| 57 quit_closure_.Run(); |
| 58 } |
| 59 |
| 60 void InstallUIAbort(bool user_initiated) override { |
| 61 aborted_ = true; |
| 62 quit_closure_.Run(); |
| 63 } |
| 64 |
| 65 base::Closure quit_closure_; |
| 66 bool proceeded_; |
| 67 bool aborted_; |
| 68 |
| 69 DISALLOW_COPY_AND_ASSIGN(TestExtensionInstallPromptDelegate); |
| 70 }; |
| 71 |
| 72 } // namespace |
| 73 |
| 74 typedef InProcessBrowserTest ExtensionInstallPromptBrowserTest; |
| 75 |
| 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()); |
| 88 |
| 89 ExtensionInstallPrompt prompt(web_contents); |
| 90 tab_strip_model->CloseWebContentsAt(web_contents_index, |
| 91 TabStripModel::CLOSE_NONE); |
| 92 content::RunAllPendingInMessageLoop(); |
| 93 |
| 94 base::RunLoop run_loop; |
| 95 TestExtensionInstallPromptDelegate delegate(run_loop.QuitClosure()); |
| 96 prompt.ConfirmInstall(&delegate, extension.get(), |
| 97 base::Bind(&TestShowDialogCallback)); |
| 98 run_loop.Run(); |
| 99 EXPECT_TRUE(delegate.DidAbort()); |
| 100 } |
| 101 |
| 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 |
| 107 // closed. |
| 108 CreateBrowser(browser()->profile()); |
| 109 |
| 110 scoped_refptr<extensions::Extension> extension(BuildTestExtension()); |
| 111 |
| 112 ExtensionInstallPrompt prompt(browser()->profile(), |
| 113 browser()->window()->GetNativeWindow()); |
| 114 browser()->window()->Close(); |
| 115 content::RunAllPendingInMessageLoop(); |
| 116 |
| 117 base::RunLoop run_loop; |
| 118 TestExtensionInstallPromptDelegate delegate(run_loop.QuitClosure()); |
| 119 prompt.ConfirmInstall(&delegate, extension.get(), |
| 120 base::Bind(&TestShowDialogCallback)); |
| 121 run_loop.Run(); |
| 122 EXPECT_TRUE(delegate.DidAbort()); |
| 123 } |
| 124 |
| 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()); |
| 130 |
| 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)); |
| 136 run_loop.Run(); |
| 137 |
| 138 // TestShowDialogCallback() should have signaled the install to proceed. |
| 139 EXPECT_TRUE(delegate.DidProceed()); |
| 140 } |
OLD | NEW |