Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/macros.h" | 5 #include "base/macros.h" |
| 6 #include "base/run_loop.h" | 6 #include "base/run_loop.h" |
| 7 #include "chrome/browser/extensions/extension_service.h" | 7 #include "chrome/browser/extensions/extension_service.h" |
| 8 #include "chrome/browser/extensions/extension_uninstall_dialog.h" | 8 #include "chrome/browser/extensions/extension_uninstall_dialog.h" |
| 9 #include "chrome/browser/ui/browser.h" | 9 #include "chrome/browser/ui/browser.h" |
| 10 #include "chrome/browser/ui/browser_window.h" | 10 #include "chrome/browser/ui/browser_window.h" |
| 11 #include "chrome/browser/ui/tabs/tab_strip_model.h" | |
| 11 #include "chrome/test/base/in_process_browser_test.h" | 12 #include "chrome/test/base/in_process_browser_test.h" |
| 13 #include "content/public/test/browser_test_utils.h" | |
| 12 #include "content/public/test/test_utils.h" | 14 #include "content/public/test/test_utils.h" |
| 15 #include "extensions/browser/extension_dialog_auto_confirm.h" | |
| 13 #include "extensions/browser/extension_system.h" | 16 #include "extensions/browser/extension_system.h" |
| 14 #include "extensions/common/extension.h" | 17 #include "extensions/common/extension.h" |
| 15 #include "extensions/common/extension_builder.h" | 18 #include "extensions/common/extension_builder.h" |
| 19 #include "extensions/common/extension_urls.h" | |
| 16 #include "extensions/common/value_builder.h" | 20 #include "extensions/common/value_builder.h" |
| 17 | 21 |
| 18 namespace { | 22 namespace { |
| 19 | 23 |
| 24 const char kReferrerId[] = "chrome-remove-extension-dialog"; | |
| 25 | |
| 26 // A preference key storing the url loaded when an extension is uninstalled. | |
| 27 const char kUninstallUrl[] = "uninstall_url"; | |
| 28 | |
| 20 scoped_refptr<extensions::Extension> BuildTestExtension() { | 29 scoped_refptr<extensions::Extension> BuildTestExtension() { |
| 21 return extensions::ExtensionBuilder() | 30 return extensions::ExtensionBuilder() |
| 22 .SetManifest(extensions::DictionaryBuilder() | 31 .SetManifest(extensions::DictionaryBuilder() |
| 23 .Set("name", "foo") | 32 .Set("name", "foo") |
| 24 .Set("version", "1.0") | 33 .Set("version", "1.0") |
| 25 .Build()) | 34 .Build()) |
| 26 .Build(); | 35 .Build(); |
| 27 } | 36 } |
| 28 | 37 |
| 38 const GURL& GetActiveUrl(Browser* browser) { | |
| 39 return browser->tab_strip_model() | |
| 40 ->GetActiveWebContents() | |
| 41 ->GetLastCommittedURL(); | |
| 42 } | |
| 43 | |
| 44 void SetUninstallURL(extensions::ExtensionPrefs* prefs, | |
| 45 const std::string& extension_id) { | |
| 46 std::string uninstall_url = "https://www.google.com/"; | |
| 47 prefs->UpdateExtensionPref(extension_id, kUninstallUrl, | |
| 48 base::MakeUnique<base::Value>(uninstall_url)); | |
| 49 } | |
| 50 | |
| 29 class TestExtensionUninstallDialogDelegate | 51 class TestExtensionUninstallDialogDelegate |
| 30 : public extensions::ExtensionUninstallDialog::Delegate { | 52 : public extensions::ExtensionUninstallDialog::Delegate { |
| 31 public: | 53 public: |
| 32 explicit TestExtensionUninstallDialogDelegate( | 54 explicit TestExtensionUninstallDialogDelegate( |
| 33 const base::Closure& quit_closure) | 55 const base::Closure& quit_closure) |
| 34 : quit_closure_(quit_closure), canceled_(false) {} | 56 : quit_closure_(quit_closure), canceled_(false) {} |
| 35 | 57 |
| 36 ~TestExtensionUninstallDialogDelegate() override {} | 58 ~TestExtensionUninstallDialogDelegate() override {} |
| 37 | 59 |
| 38 bool canceled() { return canceled_; } | 60 bool canceled() { return canceled_; } |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 100 extensions::UNINSTALL_REASON_FOR_TESTING, | 122 extensions::UNINSTALL_REASON_FOR_TESTING, |
| 101 extensions::UNINSTALL_SOURCE_FOR_TESTING); | 123 extensions::UNINSTALL_SOURCE_FOR_TESTING); |
| 102 | 124 |
| 103 content::RunAllPendingInMessageLoop(); | 125 content::RunAllPendingInMessageLoop(); |
| 104 | 126 |
| 105 // Kill parent window. | 127 // Kill parent window. |
| 106 browser()->window()->Close(); | 128 browser()->window()->Close(); |
| 107 run_loop.Run(); | 129 run_loop.Run(); |
| 108 EXPECT_TRUE(delegate.canceled()); | 130 EXPECT_TRUE(delegate.canceled()); |
| 109 } | 131 } |
| 132 | |
| 133 // Test that when the user clicks Uninstall on the ExtensionUninstallDialog, the | |
| 134 // extension's uninstall url (when it is specified) should open and be the | |
| 135 // active tab. | |
| 136 IN_PROC_BROWSER_TEST_F(ExtensionUninstallDialogViewBrowserTest, | |
| 137 EnsureExtensionUninstallURLIsActiveTabAfterUninstall) { | |
| 138 scoped_refptr<extensions::Extension> extension(BuildTestExtension()); | |
| 139 ExtensionService* extension_service = | |
| 140 extensions::ExtensionSystem::Get(browser()->profile()) | |
| 141 ->extension_service(); | |
| 142 std::string uninstall_url = "https://www.google.com/"; | |
|
Devlin
2017/05/19 22:56:56
nit: we should pull this up to a const char (near
| |
| 143 extension_service->AddExtension(extension.get()); | |
| 144 SetUninstallURL( | |
| 145 extensions::ExtensionPrefs::Get(extension_service->GetBrowserContext()), | |
| 146 extension->id()); | |
| 147 | |
| 148 // Auto-confirm the uninstall dialog. | |
| 149 extensions::ScopedTestDialogAutoConfirm auto_confirm( | |
| 150 extensions::ScopedTestDialogAutoConfirm::ACCEPT); | |
| 151 | |
| 152 base::RunLoop run_loop; | |
| 153 TestExtensionUninstallDialogDelegate delegate(run_loop.QuitClosure()); | |
| 154 std::unique_ptr<extensions::ExtensionUninstallDialog> dialog( | |
| 155 extensions::ExtensionUninstallDialog::Create( | |
| 156 browser()->profile(), browser()->window()->GetNativeWindow(), | |
| 157 &delegate)); | |
| 158 content::RunAllPendingInMessageLoop(); | |
| 159 | |
| 160 dialog->ConfirmUninstall(extension, | |
| 161 // UNINSTALL_REASON_USER_INITIATED is used to trigger | |
| 162 // complete uninstallation. | |
| 163 extensions::UNINSTALL_REASON_USER_INITIATED, | |
| 164 extensions::UNINSTALL_SOURCE_FOR_TESTING); | |
| 165 | |
| 166 content::RunAllPendingInMessageLoop(); | |
| 167 | |
| 168 // There should be 2 tabs open: chrome://about and the extension's uninstall | |
| 169 // url. | |
| 170 EXPECT_EQ(2, browser()->tab_strip_model()->count()); | |
| 171 // This navigation can fail, since the uninstall url isn't hooked up to the | |
| 172 // test server. That's fine, since we only care about the intended target, | |
| 173 // which is valid. | |
| 174 content::WaitForLoadStop( | |
| 175 browser()->tab_strip_model()->GetActiveWebContents()); | |
| 176 // Verifying that the extension's uninstall url is the active tab. | |
| 177 EXPECT_EQ(GetActiveUrl(browser()), uninstall_url); | |
|
Devlin
2017/05/19 22:56:56
nit: EXPECT_EQ(expected, actual)
so: EXPECT_EQ(uni
| |
| 178 | |
| 179 run_loop.Run(); | |
| 180 // The delegate should not be canceled because the user chose to uninstall the | |
| 181 // extension, which should be successful. | |
| 182 EXPECT_TRUE(!delegate.canceled()); | |
| 183 } | |
| 184 | |
| 185 // Test that when the user clicks the Report Abuse checkbox and clicks Uninstall | |
| 186 // on the ExtensionUninstallDialog, the extension's uninstall url (when it is | |
| 187 // specified) and the CWS Report Abuse survey are opened in the browser, also | |
| 188 // testing that the CWS survey is the active tab. | |
| 189 IN_PROC_BROWSER_TEST_F(ExtensionUninstallDialogViewBrowserTest, | |
| 190 EnsureCWSReportAbusePageIsActiveTabAfterUninstall) { | |
| 191 scoped_refptr<extensions::Extension> extension(BuildTestExtension()); | |
| 192 ExtensionService* extension_service = | |
| 193 extensions::ExtensionSystem::Get(browser()->profile()) | |
| 194 ->extension_service(); | |
| 195 std::string uninstall_url = "https://www.google.com/"; | |
| 196 SetUninstallURL( | |
| 197 extensions::ExtensionPrefs::Get(extension_service->GetBrowserContext()), | |
| 198 extension->id()); | |
| 199 extension_service->AddExtension(extension.get()); | |
| 200 | |
| 201 // Auto-confirm the uninstall dialog. | |
| 202 extensions::ScopedTestDialogAutoConfirm auto_confirm( | |
| 203 extensions::ScopedTestDialogAutoConfirm::ACCEPT_AND_OPTION); | |
| 204 | |
| 205 base::RunLoop run_loop; | |
| 206 TestExtensionUninstallDialogDelegate delegate(run_loop.QuitClosure()); | |
| 207 std::unique_ptr<extensions::ExtensionUninstallDialog> dialog( | |
| 208 extensions::ExtensionUninstallDialog::Create( | |
| 209 browser()->profile(), browser()->window()->GetNativeWindow(), | |
| 210 &delegate)); | |
| 211 content::RunAllPendingInMessageLoop(); | |
| 212 | |
| 213 dialog->ConfirmUninstall(extension, | |
| 214 // UNINSTALL_REASON_USER_INITIATED is used to trigger | |
| 215 // complete uninstallation. | |
| 216 extensions::UNINSTALL_REASON_USER_INITIATED, | |
| 217 extensions::UNINSTALL_SOURCE_FOR_TESTING); | |
| 218 | |
| 219 content::RunAllPendingInMessageLoop(); | |
| 220 // There should be 3 tabs open: chrome://about, the extension's uninstall url, | |
| 221 // and the CWS Report Abuse survey. | |
| 222 EXPECT_EQ(3, browser()->tab_strip_model()->count()); | |
| 223 // This navigation can fail, since the webstore report abuse url isn't hooked | |
| 224 // up to the test server. That's fine, since we only care about the intended | |
| 225 // target, which is valid. | |
| 226 content::WaitForLoadStop( | |
| 227 browser()->tab_strip_model()->GetActiveWebContents()); | |
| 228 // The CWS Report Abuse survey should be the active tab. | |
| 229 EXPECT_EQ( | |
| 230 extension_urls::GetWebstoreReportAbuseUrl(extension->id(), kReferrerId), | |
| 231 GetActiveUrl(browser())); | |
| 232 // Similar to the scenario above, this navigation can fail. The uninstall url | |
| 233 // isn't hooked up to our test server. | |
| 234 content::WaitForLoadStop(browser()->tab_strip_model()->GetWebContentsAt(1)); | |
| 235 // Verifying that the extension's uninstall url was opened. It should not be | |
| 236 // the active tab. | |
| 237 EXPECT_EQ( | |
| 238 browser()->tab_strip_model()->GetWebContentsAt(1)->GetLastCommittedURL(), | |
| 239 uninstall_url); | |
| 240 | |
| 241 run_loop.Run(); | |
| 242 // The delegate should not be canceled because the user chose to uninstall the | |
| 243 // extension, which should be successful. | |
| 244 EXPECT_TRUE(!delegate.canceled()); | |
| 245 } | |
| OLD | NEW |