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 kUninstallUrl[] = "https://www.google.com/"; |
| 25 |
| 26 const char kReferrerId[] = "chrome-remove-extension-dialog"; |
| 27 |
| 28 // A preference key storing the url loaded when an extension is uninstalled. |
| 29 const char kUninstallUrlPrefKey[] = "uninstall_url"; |
| 30 |
20 scoped_refptr<extensions::Extension> BuildTestExtension() { | 31 scoped_refptr<extensions::Extension> BuildTestExtension() { |
21 return extensions::ExtensionBuilder() | 32 return extensions::ExtensionBuilder() |
22 .SetManifest(extensions::DictionaryBuilder() | 33 .SetManifest(extensions::DictionaryBuilder() |
23 .Set("name", "foo") | 34 .Set("name", "foo") |
24 .Set("version", "1.0") | 35 .Set("version", "1.0") |
25 .Build()) | 36 .Build()) |
26 .Build(); | 37 .Build(); |
27 } | 38 } |
28 | 39 |
| 40 std::string GetActiveUrl(Browser* browser) { |
| 41 return browser->tab_strip_model() |
| 42 ->GetActiveWebContents() |
| 43 ->GetLastCommittedURL() |
| 44 .spec(); |
| 45 } |
| 46 |
| 47 void SetUninstallURL(extensions::ExtensionPrefs* prefs, |
| 48 const std::string& extension_id) { |
| 49 prefs->UpdateExtensionPref(extension_id, kUninstallUrlPrefKey, |
| 50 base::MakeUnique<base::Value>(kUninstallUrl)); |
| 51 } |
| 52 |
29 class TestExtensionUninstallDialogDelegate | 53 class TestExtensionUninstallDialogDelegate |
30 : public extensions::ExtensionUninstallDialog::Delegate { | 54 : public extensions::ExtensionUninstallDialog::Delegate { |
31 public: | 55 public: |
32 explicit TestExtensionUninstallDialogDelegate( | 56 explicit TestExtensionUninstallDialogDelegate( |
33 const base::Closure& quit_closure) | 57 const base::Closure& quit_closure) |
34 : quit_closure_(quit_closure), canceled_(false) {} | 58 : quit_closure_(quit_closure), canceled_(false) {} |
35 | 59 |
36 ~TestExtensionUninstallDialogDelegate() override {} | 60 ~TestExtensionUninstallDialogDelegate() override {} |
37 | 61 |
38 bool canceled() { return canceled_; } | 62 bool canceled() { return canceled_; } |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
100 extensions::UNINSTALL_REASON_FOR_TESTING, | 124 extensions::UNINSTALL_REASON_FOR_TESTING, |
101 extensions::UNINSTALL_SOURCE_FOR_TESTING); | 125 extensions::UNINSTALL_SOURCE_FOR_TESTING); |
102 | 126 |
103 content::RunAllPendingInMessageLoop(); | 127 content::RunAllPendingInMessageLoop(); |
104 | 128 |
105 // Kill parent window. | 129 // Kill parent window. |
106 browser()->window()->Close(); | 130 browser()->window()->Close(); |
107 run_loop.Run(); | 131 run_loop.Run(); |
108 EXPECT_TRUE(delegate.canceled()); | 132 EXPECT_TRUE(delegate.canceled()); |
109 } | 133 } |
| 134 |
| 135 // Test that when the user clicks Uninstall on the ExtensionUninstallDialog, the |
| 136 // extension's uninstall url (when it is specified) should open and be the |
| 137 // active tab. |
| 138 IN_PROC_BROWSER_TEST_F(ExtensionUninstallDialogViewBrowserTest, |
| 139 EnsureExtensionUninstallURLIsActiveTabAfterUninstall) { |
| 140 scoped_refptr<extensions::Extension> extension(BuildTestExtension()); |
| 141 ExtensionService* extension_service = |
| 142 extensions::ExtensionSystem::Get(browser()->profile()) |
| 143 ->extension_service(); |
| 144 extension_service->AddExtension(extension.get()); |
| 145 SetUninstallURL( |
| 146 extensions::ExtensionPrefs::Get(extension_service->GetBrowserContext()), |
| 147 extension->id()); |
| 148 |
| 149 // Auto-confirm the uninstall dialog. |
| 150 extensions::ScopedTestDialogAutoConfirm auto_confirm( |
| 151 extensions::ScopedTestDialogAutoConfirm::ACCEPT); |
| 152 |
| 153 base::RunLoop run_loop; |
| 154 TestExtensionUninstallDialogDelegate delegate(run_loop.QuitClosure()); |
| 155 std::unique_ptr<extensions::ExtensionUninstallDialog> dialog( |
| 156 extensions::ExtensionUninstallDialog::Create( |
| 157 browser()->profile(), browser()->window()->GetNativeWindow(), |
| 158 &delegate)); |
| 159 content::RunAllPendingInMessageLoop(); |
| 160 |
| 161 dialog->ConfirmUninstall(extension, |
| 162 // UNINSTALL_REASON_USER_INITIATED is used to trigger |
| 163 // complete uninstallation. |
| 164 extensions::UNINSTALL_REASON_USER_INITIATED, |
| 165 extensions::UNINSTALL_SOURCE_FOR_TESTING); |
| 166 |
| 167 content::RunAllPendingInMessageLoop(); |
| 168 |
| 169 // There should be 2 tabs open: chrome://about and the extension's uninstall |
| 170 // url. |
| 171 EXPECT_EQ(2, browser()->tab_strip_model()->count()); |
| 172 // This navigation can fail, since the uninstall url isn't hooked up to the |
| 173 // test server. That's fine, since we only care about the intended target, |
| 174 // which is valid. |
| 175 content::WaitForLoadStop( |
| 176 browser()->tab_strip_model()->GetActiveWebContents()); |
| 177 // Verifying that the extension's uninstall url is the active tab. |
| 178 EXPECT_EQ(kUninstallUrl, GetActiveUrl(browser())); |
| 179 |
| 180 run_loop.Run(); |
| 181 // The delegate should not be canceled because the user chose to uninstall the |
| 182 // extension, which should be successful. |
| 183 EXPECT_TRUE(!delegate.canceled()); |
| 184 } |
| 185 |
| 186 // Test that when the user clicks the Report Abuse checkbox and clicks Uninstall |
| 187 // on the ExtensionUninstallDialog, the extension's uninstall url (when it is |
| 188 // specified) and the CWS Report Abuse survey are opened in the browser, also |
| 189 // testing that the CWS survey is the active tab. |
| 190 IN_PROC_BROWSER_TEST_F(ExtensionUninstallDialogViewBrowserTest, |
| 191 EnsureCWSReportAbusePageIsActiveTabAfterUninstall) { |
| 192 scoped_refptr<extensions::Extension> extension(BuildTestExtension()); |
| 193 ExtensionService* extension_service = |
| 194 extensions::ExtensionSystem::Get(browser()->profile()) |
| 195 ->extension_service(); |
| 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(kUninstallUrl, browser() |
| 238 ->tab_strip_model() |
| 239 ->GetWebContentsAt(1) |
| 240 ->GetLastCommittedURL() |
| 241 .spec()); |
| 242 |
| 243 run_loop.Run(); |
| 244 // The delegate should not be canceled because the user chose to uninstall the |
| 245 // extension, which should be successful. |
| 246 EXPECT_TRUE(!delegate.canceled()); |
| 247 } |
OLD | NEW |