OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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/files/file_util.h" | 5 #include "base/files/file_util.h" |
6 #include "chrome/browser/extensions/api/developer_private/developer_private_api.
h" | 6 #include "chrome/browser/extensions/api/developer_private/developer_private_api.
h" |
7 #include "chrome/browser/extensions/extension_function_test_utils.h" | 7 #include "chrome/browser/extensions/extension_function_test_utils.h" |
8 #include "chrome/browser/extensions/extension_service.h" | 8 #include "chrome/browser/extensions/extension_service.h" |
9 #include "chrome/browser/extensions/extension_service_test_base.h" | 9 #include "chrome/browser/extensions/extension_service_test_base.h" |
10 #include "chrome/browser/extensions/extension_util.h" | 10 #include "chrome/browser/extensions/extension_util.h" |
11 #include "chrome/browser/extensions/test_extension_dir.h" | 11 #include "chrome/browser/extensions/test_extension_dir.h" |
| 12 #include "chrome/browser/extensions/test_extension_system.h" |
12 #include "chrome/browser/extensions/unpacked_installer.h" | 13 #include "chrome/browser/extensions/unpacked_installer.h" |
13 #include "chrome/browser/ui/browser.h" | 14 #include "chrome/browser/ui/browser.h" |
14 #include "chrome/browser/ui/host_desktop.h" | 15 #include "chrome/browser/ui/host_desktop.h" |
15 #include "chrome/common/extensions/api/developer_private.h" | 16 #include "chrome/common/extensions/api/developer_private.h" |
16 #include "chrome/test/base/test_browser_window.h" | 17 #include "chrome/test/base/test_browser_window.h" |
| 18 #include "content/public/test/test_web_contents_factory.h" |
17 #include "extensions/browser/extension_prefs.h" | 19 #include "extensions/browser/extension_prefs.h" |
18 #include "extensions/browser/extension_registry.h" | 20 #include "extensions/browser/extension_registry.h" |
19 #include "extensions/browser/extension_system.h" | 21 #include "extensions/browser/extension_system.h" |
20 #include "extensions/browser/test_extension_registry_observer.h" | 22 #include "extensions/browser/test_extension_registry_observer.h" |
21 #include "extensions/common/extension.h" | 23 #include "extensions/common/extension.h" |
22 #include "extensions/common/extension_set.h" | 24 #include "extensions/common/extension_set.h" |
| 25 #include "extensions/common/manifest_constants.h" |
23 #include "extensions/common/test_util.h" | 26 #include "extensions/common/test_util.h" |
24 | 27 |
25 namespace extensions { | 28 namespace extensions { |
26 | 29 |
| 30 namespace { |
| 31 |
| 32 KeyedService* BuildAPI(content::BrowserContext* context) { |
| 33 return new DeveloperPrivateAPI(context); |
| 34 } |
| 35 |
| 36 } // namespace |
| 37 |
27 class DeveloperPrivateApiUnitTest : public ExtensionServiceTestBase { | 38 class DeveloperPrivateApiUnitTest : public ExtensionServiceTestBase { |
28 protected: | 39 protected: |
29 DeveloperPrivateApiUnitTest() {} | 40 DeveloperPrivateApiUnitTest() {} |
30 ~DeveloperPrivateApiUnitTest() override {} | 41 ~DeveloperPrivateApiUnitTest() override {} |
31 | 42 |
32 // A wrapper around extension_function_test_utils::RunFunction that runs with | 43 // A wrapper around extension_function_test_utils::RunFunction that runs with |
33 // the associated browser, no flags, and can take stack-allocated arguments. | 44 // the associated browser, no flags, and can take stack-allocated arguments. |
34 bool RunFunction(const scoped_refptr<UIThreadExtensionFunction>& function, | 45 bool RunFunction(const scoped_refptr<UIThreadExtensionFunction>& function, |
35 const base::ListValue& args); | 46 const base::ListValue& args); |
36 | 47 |
(...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
173 | 184 |
174 void DeveloperPrivateApiUnitTest::SetUp() { | 185 void DeveloperPrivateApiUnitTest::SetUp() { |
175 ExtensionServiceTestBase::SetUp(); | 186 ExtensionServiceTestBase::SetUp(); |
176 InitializeEmptyExtensionService(); | 187 InitializeEmptyExtensionService(); |
177 | 188 |
178 browser_window_.reset(new TestBrowserWindow()); | 189 browser_window_.reset(new TestBrowserWindow()); |
179 Browser::CreateParams params(profile(), chrome::HOST_DESKTOP_TYPE_NATIVE); | 190 Browser::CreateParams params(profile(), chrome::HOST_DESKTOP_TYPE_NATIVE); |
180 params.type = Browser::TYPE_TABBED; | 191 params.type = Browser::TYPE_TABBED; |
181 params.window = browser_window_.get(); | 192 params.window = browser_window_.get(); |
182 browser_.reset(new Browser(params)); | 193 browser_.reset(new Browser(params)); |
| 194 |
| 195 // Allow the API to be created. |
| 196 static_cast<TestExtensionSystem*>(ExtensionSystem::Get(profile()))-> |
| 197 SetEventRouter(make_scoped_ptr( |
| 198 new EventRouter(profile(), ExtensionPrefs::Get(profile())))); |
| 199 DeveloperPrivateAPI::GetFactoryInstance()->SetTestingFactory( |
| 200 profile(), &BuildAPI); |
183 } | 201 } |
184 | 202 |
185 void DeveloperPrivateApiUnitTest::TearDown() { | 203 void DeveloperPrivateApiUnitTest::TearDown() { |
186 test_extension_dirs_.clear(); | 204 test_extension_dirs_.clear(); |
187 browser_.reset(); | 205 browser_.reset(); |
188 browser_window_.reset(); | 206 browser_window_.reset(); |
189 ExtensionServiceTestBase::TearDown(); | 207 ExtensionServiceTestBase::TearDown(); |
190 } | 208 } |
191 | 209 |
192 // Test developerPrivate.allowIncognito. | 210 // Test developerPrivate.allowIncognito. |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
257 base::DeleteFile(crx_path, false); | 275 base::DeleteFile(crx_path, false); |
258 EXPECT_TRUE(pack_args.Remove(1u, nullptr)); // Remove the pem key argument. | 276 EXPECT_TRUE(pack_args.Remove(1u, nullptr)); // Remove the pem key argument. |
259 EXPECT_TRUE(pack_args.Remove(1u, nullptr)); // Remove the flags argument. | 277 EXPECT_TRUE(pack_args.Remove(1u, nullptr)); // Remove the flags argument. |
260 EXPECT_TRUE(TestPackExtensionFunction( | 278 EXPECT_TRUE(TestPackExtensionFunction( |
261 pack_args, api::developer_private::PACK_STATUS_ERROR, 0)); | 279 pack_args, api::developer_private::PACK_STATUS_ERROR, 0)); |
262 | 280 |
263 base::DeleteFile(crx_path, false); | 281 base::DeleteFile(crx_path, false); |
264 base::DeleteFile(pem_path, false); | 282 base::DeleteFile(pem_path, false); |
265 } | 283 } |
266 | 284 |
| 285 // Test developerPrivate.choosePath. |
| 286 TEST_F(DeveloperPrivateApiUnitTest, DeveloperPrivateChoosePath) { |
| 287 ResetThreadBundle(content::TestBrowserThreadBundle::DEFAULT); |
| 288 content::TestWebContentsFactory web_contents_factory; |
| 289 content::WebContents* web_contents = |
| 290 web_contents_factory.CreateWebContents(profile()); |
| 291 |
| 292 base::FilePath expected_dir_path = data_dir().AppendASCII("good_unpacked"); |
| 293 api::EntryPicker::SkipPickerAndAlwaysSelectPathForTest(&expected_dir_path); |
| 294 |
| 295 // Try selecting a directory. |
| 296 base::ListValue choose_args; |
| 297 choose_args.AppendString("FOLDER"); |
| 298 choose_args.AppendString("LOAD"); |
| 299 scoped_refptr<UIThreadExtensionFunction> function( |
| 300 new api::DeveloperPrivateChoosePathFunction()); |
| 301 function->SetRenderViewHost(web_contents->GetRenderViewHost()); |
| 302 EXPECT_TRUE(RunFunction(function, choose_args)) << function->GetError(); |
| 303 std::string path; |
| 304 EXPECT_TRUE(function->GetResultList() && |
| 305 function->GetResultList()->GetString(0, &path)); |
| 306 EXPECT_EQ(path, expected_dir_path.AsUTF8Unsafe()); |
| 307 |
| 308 // Try selecting a pem file. |
| 309 base::FilePath expected_file_path = |
| 310 data_dir().AppendASCII("good_unpacked.pem"); |
| 311 api::EntryPicker::SkipPickerAndAlwaysSelectPathForTest(&expected_file_path); |
| 312 choose_args.Clear(); |
| 313 choose_args.AppendString("FILE"); |
| 314 choose_args.AppendString("PEM"); |
| 315 function = new api::DeveloperPrivateChoosePathFunction(); |
| 316 function->SetRenderViewHost(web_contents->GetRenderViewHost()); |
| 317 EXPECT_TRUE(RunFunction(function, choose_args)) << function->GetError(); |
| 318 EXPECT_TRUE(function->GetResultList() && |
| 319 function->GetResultList()->GetString(0, &path)); |
| 320 EXPECT_EQ(path, expected_file_path.AsUTF8Unsafe()); |
| 321 |
| 322 // Try canceling the file dialog. |
| 323 api::EntryPicker::SkipPickerAndAlwaysCancelForTest(); |
| 324 function = new api::DeveloperPrivateChoosePathFunction(); |
| 325 function->SetRenderViewHost(web_contents->GetRenderViewHost()); |
| 326 EXPECT_FALSE(RunFunction(function, choose_args)); |
| 327 EXPECT_EQ(std::string("File selection was canceled."), function->GetError()); |
| 328 } |
| 329 |
| 330 // Test developerPrivate.loadUnpacked. |
| 331 TEST_F(DeveloperPrivateApiUnitTest, DeveloperPrivateLoadUnpacked) { |
| 332 ResetThreadBundle(content::TestBrowserThreadBundle::DEFAULT); |
| 333 content::TestWebContentsFactory web_contents_factory; |
| 334 content::WebContents* web_contents = |
| 335 web_contents_factory.CreateWebContents(profile()); |
| 336 |
| 337 base::FilePath path = data_dir().AppendASCII("good_unpacked"); |
| 338 api::EntryPicker::SkipPickerAndAlwaysSelectPathForTest(&path); |
| 339 |
| 340 // Try loading a good extension (it should succeed, and the extension should |
| 341 // be added). |
| 342 scoped_refptr<UIThreadExtensionFunction> function( |
| 343 new api::DeveloperPrivateLoadUnpackedFunction()); |
| 344 function->SetRenderViewHost(web_contents->GetRenderViewHost()); |
| 345 ExtensionIdSet current_ids = registry()->enabled_extensions().GetIDs(); |
| 346 EXPECT_TRUE(RunFunction(function, base::ListValue())) << function->GetError(); |
| 347 // We should have added one new extension. |
| 348 ExtensionIdSet id_difference = base::STLSetDifference<ExtensionIdSet>( |
| 349 registry()->enabled_extensions().GetIDs(), current_ids); |
| 350 ASSERT_EQ(1u, id_difference.size()); |
| 351 // The new extension should have the same path. |
| 352 EXPECT_EQ( |
| 353 path, |
| 354 registry()->enabled_extensions().GetByID(*id_difference.begin())->path()); |
| 355 |
| 356 path = data_dir().AppendASCII("empty_manifest"); |
| 357 api::EntryPicker::SkipPickerAndAlwaysSelectPathForTest(&path); |
| 358 |
| 359 // Try loading a bad extension (it should fail, and we should get an error). |
| 360 function = new api::DeveloperPrivateLoadUnpackedFunction(); |
| 361 function->SetRenderViewHost(web_contents->GetRenderViewHost()); |
| 362 base::ListValue unpacked_args; |
| 363 scoped_ptr<base::DictionaryValue> options(new base::DictionaryValue()); |
| 364 options->SetBoolean("failQuietly", true); |
| 365 unpacked_args.Append(options.release()); |
| 366 current_ids = registry()->enabled_extensions().GetIDs(); |
| 367 EXPECT_FALSE(RunFunction(function, unpacked_args)); |
| 368 EXPECT_EQ(manifest_errors::kManifestUnreadable, function->GetError()); |
| 369 // We should have no new extensions installed. |
| 370 EXPECT_EQ(0u, base::STLSetDifference<ExtensionIdSet>( |
| 371 registry()->enabled_extensions().GetIDs(), |
| 372 current_ids).size()); |
| 373 } |
| 374 |
267 } // namespace extensions | 375 } // namespace extensions |
OLD | NEW |