OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "chrome/browser/download/download_crx_util.h" | 5 #include "chrome/browser/download/download_crx_util.h" |
6 #include "chrome/browser/download/download_service.h" | 6 #include "chrome/browser/download/download_service.h" |
7 #include "chrome/browser/download/download_service_factory.h" | 7 #include "chrome/browser/download/download_service_factory.h" |
8 #include "chrome/browser/download/download_test_observer.h" | 8 #include "chrome/browser/download/download_test_observer.h" |
9 #include "chrome/browser/extensions/crx_installer.h" | 9 #include "chrome/browser/extensions/crx_installer.h" |
10 #include "chrome/browser/extensions/extension_browsertest.h" | 10 #include "chrome/browser/extensions/extension_browsertest.h" |
11 #include "chrome/browser/extensions/extension_install_ui.h" | 11 #include "chrome/browser/extensions/extension_install_prompt.h" |
12 #include "chrome/browser/extensions/extension_service.h" | 12 #include "chrome/browser/extensions/extension_service.h" |
13 #include "chrome/browser/profiles/profile.h" | 13 #include "chrome/browser/profiles/profile.h" |
14 #include "chrome/browser/ui/browser.h" | 14 #include "chrome/browser/ui/browser.h" |
15 #include "chrome/common/chrome_switches.h" | 15 #include "chrome/common/chrome_switches.h" |
16 #include "chrome/common/extensions/extension_file_util.h" | 16 #include "chrome/common/extensions/extension_file_util.h" |
17 #include "chrome/common/extensions/extension_switch_utils.h" | 17 #include "chrome/common/extensions/extension_switch_utils.h" |
18 #include "chrome/test/base/ui_test_utils.h" | 18 #include "chrome/test/base/ui_test_utils.h" |
19 #include "grit/generated_resources.h" | 19 #include "grit/generated_resources.h" |
20 #include "ui/base/l10n/l10n_util.h" | 20 #include "ui/base/l10n/l10n_util.h" |
21 | 21 |
22 class SkBitmap; | 22 class SkBitmap; |
23 | 23 |
24 namespace { | 24 namespace { |
25 | 25 |
26 // Observer waits for exactly one download to finish. | 26 // Observer waits for exactly one download to finish. |
27 | 27 |
28 class MockInstallUI : public ExtensionInstallUI { | 28 class MockInstallPrompt : public ExtensionInstallPrompt { |
29 public: | 29 public: |
30 explicit MockInstallUI(Profile* profile) : | 30 explicit MockInstallPrompt(Profile* profile) : |
31 ExtensionInstallUI(profile), | 31 ExtensionInstallPrompt(profile), |
32 did_succeed_(false), | 32 did_succeed_(false), |
33 confirmation_requested_(false) {} | 33 confirmation_requested_(false) {} |
34 | 34 |
35 bool did_succeed() const { return did_succeed_; } | 35 bool did_succeed() const { return did_succeed_; } |
36 bool confirmation_requested() const { return confirmation_requested_; } | 36 bool confirmation_requested() const { return confirmation_requested_; } |
37 const string16& error() const { return error_; } | 37 const string16& error() const { return error_; } |
38 | 38 |
39 // Overriding some of the ExtensionInstallUI API. | 39 // Overriding some of the ExtensionInstallUI API. |
40 void ConfirmInstall(Delegate* delegate, | 40 void ConfirmInstall(Delegate* delegate, |
41 const extensions::Extension* extension) { | 41 const extensions::Extension* extension) { |
(...skipping 19 matching lines...) Expand all Loading... |
61 } // namespace | 61 } // namespace |
62 | 62 |
63 class ExtensionCrxInstallerTest : public ExtensionBrowserTest { | 63 class ExtensionCrxInstallerTest : public ExtensionBrowserTest { |
64 public: | 64 public: |
65 // Installs a crx from |crx_relpath| (a path relative to the extension test | 65 // Installs a crx from |crx_relpath| (a path relative to the extension test |
66 // data dir) with expected id |id|. Returns whether a confirmation prompt | 66 // data dir) with expected id |id|. Returns whether a confirmation prompt |
67 // happened or not. | 67 // happened or not. |
68 bool DidWhitelistInstallPrompt(const std::string& ext_relpath, | 68 bool DidWhitelistInstallPrompt(const std::string& ext_relpath, |
69 const std::string& id) { | 69 const std::string& id) { |
70 ExtensionService* service = browser()->profile()->GetExtensionService(); | 70 ExtensionService* service = browser()->profile()->GetExtensionService(); |
71 MockInstallUI* mock_install_ui = new MockInstallUI(browser()->profile()); | 71 MockInstallPrompt* mock_install_prompt = |
| 72 new MockInstallPrompt(browser()->profile()); |
72 FilePath ext_path = test_data_dir_.AppendASCII(ext_relpath); | 73 FilePath ext_path = test_data_dir_.AppendASCII(ext_relpath); |
73 | 74 |
74 std::string error; | 75 std::string error; |
75 base::DictionaryValue* parsed_manifest = | 76 base::DictionaryValue* parsed_manifest = |
76 extension_file_util::LoadManifest(ext_path, &error); | 77 extension_file_util::LoadManifest(ext_path, &error); |
77 if (!parsed_manifest) | 78 if (!parsed_manifest) |
78 return false; | 79 return false; |
79 | 80 |
80 scoped_ptr<WebstoreInstaller::Approval> approval( | 81 scoped_ptr<WebstoreInstaller::Approval> approval( |
81 WebstoreInstaller::Approval::CreateWithNoInstallPrompt( | 82 WebstoreInstaller::Approval::CreateWithNoInstallPrompt( |
82 browser()->profile(), | 83 browser()->profile(), |
83 id, | 84 id, |
84 scoped_ptr<base::DictionaryValue>(parsed_manifest))); | 85 scoped_ptr<base::DictionaryValue>(parsed_manifest))); |
85 | 86 |
86 scoped_refptr<CrxInstaller> installer( | 87 scoped_refptr<CrxInstaller> installer( |
87 CrxInstaller::Create(service, | 88 CrxInstaller::Create(service, |
88 mock_install_ui, /* ownership transferred */ | 89 mock_install_prompt, /* ownership transferred */ |
89 approval.get() /* keep ownership */)); | 90 approval.get() /* keep ownership */)); |
90 installer->set_allow_silent_install(true); | 91 installer->set_allow_silent_install(true); |
91 installer->set_is_gallery_install(true); | 92 installer->set_is_gallery_install(true); |
92 installer->InstallCrx(PackExtension(ext_path)); | 93 installer->InstallCrx(PackExtension(ext_path)); |
93 ui_test_utils::RunMessageLoop(); | 94 ui_test_utils::RunMessageLoop(); |
94 | 95 |
95 EXPECT_TRUE(mock_install_ui->did_succeed()); | 96 EXPECT_TRUE(mock_install_prompt->did_succeed()); |
96 return mock_install_ui->confirmation_requested(); | 97 return mock_install_prompt->confirmation_requested(); |
97 } | 98 } |
98 }; | 99 }; |
99 | 100 |
100 #if defined(OS_CHROMEOS) | 101 #if defined(OS_CHROMEOS) |
101 #define MAYBE_Whitelisting DISABLED_Whitelisting | 102 #define MAYBE_Whitelisting DISABLED_Whitelisting |
102 #else | 103 #else |
103 #define MAYBE_Whitelisting Whitelisting | 104 #define MAYBE_Whitelisting Whitelisting |
104 #endif | 105 #endif |
105 IN_PROC_BROWSER_TEST_F(ExtensionCrxInstallerTest, MAYBE_Whitelisting) { | 106 IN_PROC_BROWSER_TEST_F(ExtensionCrxInstallerTest, MAYBE_Whitelisting) { |
106 std::string id = "hdgllgikmikobbofgnabhfimcfoopgnd"; | 107 std::string id = "hdgllgikmikobbofgnabhfimcfoopgnd"; |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
152 const int kNumDownloadsExpected = 1; | 153 const int kNumDownloadsExpected = 1; |
153 const bool kExpectFileSelectDialog = false; | 154 const bool kExpectFileSelectDialog = false; |
154 | 155 |
155 LOG(ERROR) << "PackAndInstallExtension: Packing extension"; | 156 LOG(ERROR) << "PackAndInstallExtension: Packing extension"; |
156 FilePath crx_path = PackExtension( | 157 FilePath crx_path = PackExtension( |
157 test_data_dir_.AppendASCII("common/background_page")); | 158 test_data_dir_.AppendASCII("common/background_page")); |
158 ASSERT_FALSE(crx_path.empty()); | 159 ASSERT_FALSE(crx_path.empty()); |
159 std::string crx_path_string(crx_path.value().begin(), crx_path.value().end()); | 160 std::string crx_path_string(crx_path.value().begin(), crx_path.value().end()); |
160 GURL url = GURL(std::string("file:///").append(crx_path_string)); | 161 GURL url = GURL(std::string("file:///").append(crx_path_string)); |
161 | 162 |
162 MockInstallUI* mock_ui = new MockInstallUI(browser()->profile()); | 163 MockInstallPrompt* mock_prompt = new MockInstallPrompt(browser()->profile()); |
163 download_crx_util::SetMockInstallUIForTesting(mock_ui); | 164 download_crx_util::SetMockInstallPromptForTesting(mock_prompt); |
164 | 165 |
165 LOG(ERROR) << "PackAndInstallExtension: Getting download manager"; | 166 LOG(ERROR) << "PackAndInstallExtension: Getting download manager"; |
166 content::DownloadManager* download_manager = | 167 content::DownloadManager* download_manager = |
167 content::BrowserContext::GetDownloadManager(browser()->profile()); | 168 content::BrowserContext::GetDownloadManager(browser()->profile()); |
168 | 169 |
169 LOG(ERROR) << "PackAndInstallExtension: Setting observer"; | 170 LOG(ERROR) << "PackAndInstallExtension: Setting observer"; |
170 scoped_ptr<DownloadTestObserver> observer( | 171 scoped_ptr<DownloadTestObserver> observer( |
171 new DownloadTestObserverTerminal( | 172 new DownloadTestObserverTerminal( |
172 download_manager, kNumDownloadsExpected, kExpectFileSelectDialog, | 173 download_manager, kNumDownloadsExpected, kExpectFileSelectDialog, |
173 DownloadTestObserver::ON_DANGEROUS_DOWNLOAD_ACCEPT)); | 174 DownloadTestObserver::ON_DANGEROUS_DOWNLOAD_ACCEPT)); |
174 LOG(ERROR) << "PackAndInstallExtension: Navigating to URL"; | 175 LOG(ERROR) << "PackAndInstallExtension: Navigating to URL"; |
175 ui_test_utils::NavigateToURLWithDisposition(browser(), url, CURRENT_TAB, | 176 ui_test_utils::NavigateToURLWithDisposition(browser(), url, CURRENT_TAB, |
176 ui_test_utils::BROWSER_TEST_NONE); | 177 ui_test_utils::BROWSER_TEST_NONE); |
177 | 178 |
178 EXPECT_TRUE(WaitForExtensionInstall()); | 179 EXPECT_TRUE(WaitForExtensionInstall()); |
179 LOG(ERROR) << "PackAndInstallExtension: Extension install"; | 180 LOG(ERROR) << "PackAndInstallExtension: Extension install"; |
180 EXPECT_TRUE(mock_ui->confirmation_requested()); | 181 EXPECT_TRUE(mock_prompt->confirmation_requested()); |
181 LOG(ERROR) << "PackAndInstallExtension: Extension install confirmed"; | 182 LOG(ERROR) << "PackAndInstallExtension: Extension install confirmed"; |
182 } | 183 } |
183 | 184 |
184 // Off-store install cannot yet be disabled on Aura. | 185 // Off-store install cannot yet be disabled on Aura. |
185 #if defined(USE_AURA) | 186 #if defined(USE_AURA) |
186 #define MAYBE_AllowOffStore DISABLED_AllowOffStore | 187 #define MAYBE_AllowOffStore DISABLED_AllowOffStore |
187 #else | 188 #else |
188 #define MAYBE_AllowOffStore AllowOffStore | 189 #define MAYBE_AllowOffStore AllowOffStore |
189 #endif | 190 #endif |
190 IN_PROC_BROWSER_TEST_F(ExtensionCrxInstallerTest, MAYBE_AllowOffStore) { | 191 IN_PROC_BROWSER_TEST_F(ExtensionCrxInstallerTest, MAYBE_AllowOffStore) { |
191 ExtensionService* service = browser()->profile()->GetExtensionService(); | 192 ExtensionService* service = browser()->profile()->GetExtensionService(); |
192 const bool kTestData[] = {false, true}; | 193 const bool kTestData[] = {false, true}; |
193 | 194 |
194 for (size_t i = 0; i < arraysize(kTestData); ++i) { | 195 for (size_t i = 0; i < arraysize(kTestData); ++i) { |
195 MockInstallUI* mock_ui = new MockInstallUI(browser()->profile()); | 196 MockInstallPrompt* mock_prompt = |
| 197 new MockInstallPrompt(browser()->profile()); |
196 scoped_refptr<CrxInstaller> crx_installer( | 198 scoped_refptr<CrxInstaller> crx_installer( |
197 CrxInstaller::Create(service, mock_ui)); | 199 CrxInstaller::Create(service, mock_prompt)); |
198 crx_installer->set_install_cause( | 200 crx_installer->set_install_cause( |
199 extension_misc::INSTALL_CAUSE_USER_DOWNLOAD); | 201 extension_misc::INSTALL_CAUSE_USER_DOWNLOAD); |
200 crx_installer->set_allow_off_store_install(kTestData[i]); | 202 crx_installer->set_allow_off_store_install(kTestData[i]); |
201 | 203 |
202 crx_installer->InstallCrx(test_data_dir_.AppendASCII("good.crx")); | 204 crx_installer->InstallCrx(test_data_dir_.AppendASCII("good.crx")); |
203 EXPECT_EQ(kTestData[i], WaitForExtensionInstall()) << kTestData[i]; | 205 EXPECT_EQ(kTestData[i], WaitForExtensionInstall()) << kTestData[i]; |
204 EXPECT_EQ(kTestData[i], mock_ui->did_succeed()); | 206 EXPECT_EQ(kTestData[i], mock_prompt->did_succeed()); |
205 EXPECT_EQ(kTestData[i], mock_ui->confirmation_requested()) << kTestData[i]; | 207 EXPECT_EQ(kTestData[i], mock_prompt->confirmation_requested()) << |
| 208 kTestData[i]; |
206 if (kTestData[i]) { | 209 if (kTestData[i]) { |
207 EXPECT_EQ(string16(), mock_ui->error()) << kTestData[i]; | 210 EXPECT_EQ(string16(), mock_prompt->error()) << kTestData[i]; |
208 } else { | 211 } else { |
209 EXPECT_EQ(l10n_util::GetStringUTF16( | 212 EXPECT_EQ(l10n_util::GetStringUTF16( |
210 IDS_EXTENSION_INSTALL_DISALLOWED_ON_SITE), | 213 IDS_EXTENSION_INSTALL_DISALLOWED_ON_SITE), |
211 mock_ui->error()) << kTestData[i]; | 214 mock_prompt->error()) << kTestData[i]; |
212 } | 215 } |
213 } | 216 } |
214 } | 217 } |
OLD | NEW |