| OLD | NEW |
| 1 // Copyright (c) 2006-2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2009 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 <algorithm> | 5 #include <algorithm> |
| 6 #include <vector> | 6 #include <vector> |
| 7 | 7 |
| 8 #include "base/file_path.h" | 8 #include "base/file_path.h" |
| 9 #include "base/file_util.h" | 9 #include "base/file_util.h" |
| 10 #include "base/json_reader.h" | 10 #include "base/json_reader.h" |
| (...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 131 | 131 |
| 132 default: | 132 default: |
| 133 DCHECK(false); | 133 DCHECK(false); |
| 134 } | 134 } |
| 135 } | 135 } |
| 136 | 136 |
| 137 void SetExtensionsEnabled(bool enabled) { | 137 void SetExtensionsEnabled(bool enabled) { |
| 138 service_->set_extensions_enabled(enabled); | 138 service_->set_extensions_enabled(enabled); |
| 139 } | 139 } |
| 140 | 140 |
| 141 void TestInstallExtension(const FilePath& path, | |
| 142 bool should_succeed) { | |
| 143 InstallExtension(path, should_succeed, false); | |
| 144 } | |
| 145 | |
| 146 void TestInstallTheme(const FilePath& path, | |
| 147 bool should_succeed) { | |
| 148 InstallExtension(path, should_succeed, true); | |
| 149 } | |
| 150 | |
| 151 protected: | 141 protected: |
| 152 void InstallExtension(const FilePath& path, | 142 void InstallExtension(const FilePath& path, |
| 153 bool should_succeed, | 143 bool should_succeed) { |
| 154 bool is_theme) { | |
| 155 ASSERT_TRUE(file_util::PathExists(path)); | 144 ASSERT_TRUE(file_util::PathExists(path)); |
| 156 service_->InstallExtension(path); | 145 service_->InstallExtension(path); |
| 157 loop_.RunAllPending(); | 146 loop_.RunAllPending(); |
| 158 std::vector<std::string> errors = GetErrors(); | 147 std::vector<std::string> errors = GetErrors(); |
| 159 if (should_succeed) { | 148 if (should_succeed) { |
| 160 ++total_successes_; | 149 ++total_successes_; |
| 161 | 150 |
| 162 EXPECT_TRUE(installed_) << path.value(); | 151 EXPECT_TRUE(installed_) << path.value(); |
| 163 | 152 |
| 164 // Themes aren't loaded. | 153 EXPECT_EQ(1u, loaded_.size()) << path.value(); |
| 165 if (is_theme) | |
| 166 EXPECT_EQ(0u, loaded_.size()) << path.value(); | |
| 167 else | |
| 168 EXPECT_EQ(1u, loaded_.size()) << path.value(); | |
| 169 | |
| 170 EXPECT_EQ(0u, errors.size()) << path.value(); | 154 EXPECT_EQ(0u, errors.size()) << path.value(); |
| 171 EXPECT_EQ(total_successes_, service_->extensions()->size()) << | 155 EXPECT_EQ(total_successes_, service_->extensions()->size()) << |
| 172 path.value(); | 156 path.value(); |
| 173 if (loaded_.size() > 0) { | 157 EXPECT_TRUE(service_->GetExtensionByID(loaded_[0]->id())) << |
| 174 EXPECT_TRUE(service_->GetExtensionByID(loaded_[0]->id())) << | 158 path.value(); |
| 175 path.value(); | |
| 176 } | |
| 177 for (std::vector<std::string>::iterator err = errors.begin(); | 159 for (std::vector<std::string>::iterator err = errors.begin(); |
| 178 err != errors.end(); ++err) { | 160 err != errors.end(); ++err) { |
| 179 LOG(ERROR) << *err; | 161 LOG(ERROR) << *err; |
| 180 } | 162 } |
| 181 } else { | 163 } else { |
| 182 EXPECT_FALSE(installed_) << path.value(); | 164 EXPECT_FALSE(installed_) << path.value(); |
| 183 EXPECT_EQ(1u, errors.size()) << path.value(); | 165 EXPECT_EQ(1u, errors.size()) << path.value(); |
| 184 } | 166 } |
| 185 | 167 |
| 186 installed_ = NULL; | 168 installed_ = NULL; |
| (...skipping 198 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 385 | 367 |
| 386 // Test installing extensions. | 368 // Test installing extensions. |
| 387 TEST_F(ExtensionsServiceTest, InstallExtension) { | 369 TEST_F(ExtensionsServiceTest, InstallExtension) { |
| 388 FilePath extensions_path; | 370 FilePath extensions_path; |
| 389 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &extensions_path)); | 371 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &extensions_path)); |
| 390 extensions_path = extensions_path.AppendASCII("extensions"); | 372 extensions_path = extensions_path.AppendASCII("extensions"); |
| 391 | 373 |
| 392 // Extensions not enabled. | 374 // Extensions not enabled. |
| 393 SetExtensionsEnabled(false); | 375 SetExtensionsEnabled(false); |
| 394 FilePath path = extensions_path.AppendASCII("good.crx"); | 376 FilePath path = extensions_path.AppendASCII("good.crx"); |
| 395 TestInstallExtension(path, false); | 377 InstallExtension(path, false); |
| 396 SetExtensionsEnabled(true); | 378 SetExtensionsEnabled(true); |
| 397 | 379 |
| 398 ValidatePrefKeyCount(0); | 380 ValidatePrefKeyCount(0); |
| 399 | 381 |
| 400 // A simple extension that should install without error. | 382 // A simple extension that should install without error. |
| 401 path = extensions_path.AppendASCII("good.crx"); | 383 path = extensions_path.AppendASCII("good.crx"); |
| 402 TestInstallExtension(path, true); | 384 InstallExtension(path, true); |
| 403 // TODO(erikkay): verify the contents of the installed extension. | 385 // TODO(erikkay): verify the contents of the installed extension. |
| 404 | 386 |
| 405 int pref_count = 0; | 387 int pref_count = 0; |
| 406 ValidatePrefKeyCount(++pref_count); | 388 ValidatePrefKeyCount(++pref_count); |
| 407 ValidatePref(good_crx, L"state", Extension::ENABLED); | 389 ValidatePref(good_crx, L"state", Extension::ENABLED); |
| 408 ValidatePref(good_crx, L"location", Extension::INTERNAL); | 390 ValidatePref(good_crx, L"location", Extension::INTERNAL); |
| 409 | 391 |
| 410 // An extension with page actions. | 392 // An extension with page actions. |
| 411 path = extensions_path.AppendASCII("page_action.crx"); | 393 path = extensions_path.AppendASCII("page_action.crx"); |
| 412 TestInstallExtension(path, true); | 394 InstallExtension(path, true); |
| 413 ValidatePrefKeyCount(++pref_count); | 395 ValidatePrefKeyCount(++pref_count); |
| 414 ValidatePref(page_action, L"state", Extension::ENABLED); | 396 ValidatePref(page_action, L"state", Extension::ENABLED); |
| 415 ValidatePref(page_action, L"location", Extension::INTERNAL); | 397 ValidatePref(page_action, L"location", Extension::INTERNAL); |
| 416 | 398 |
| 417 // 0-length extension file. | 399 // 0-length extension file. |
| 418 path = extensions_path.AppendASCII("not_an_extension.crx"); | 400 path = extensions_path.AppendASCII("not_an_extension.crx"); |
| 419 TestInstallExtension(path, false); | 401 InstallExtension(path, false); |
| 420 ValidatePrefKeyCount(pref_count); | 402 ValidatePrefKeyCount(pref_count); |
| 421 | 403 |
| 422 // Bad magic number. | 404 // Bad magic number. |
| 423 path = extensions_path.AppendASCII("bad_magic.crx"); | 405 path = extensions_path.AppendASCII("bad_magic.crx"); |
| 424 TestInstallExtension(path, false); | 406 InstallExtension(path, false); |
| 425 ValidatePrefKeyCount(pref_count); | 407 ValidatePrefKeyCount(pref_count); |
| 426 | 408 |
| 427 // Poorly formed JSON. | 409 // Poorly formed JSON. |
| 428 path = extensions_path.AppendASCII("bad_json.crx"); | 410 path = extensions_path.AppendASCII("bad_json.crx"); |
| 429 TestInstallExtension(path, false); | 411 InstallExtension(path, false); |
| 430 ValidatePrefKeyCount(pref_count); | 412 ValidatePrefKeyCount(pref_count); |
| 431 | 413 |
| 432 // Incorrect zip hash. | 414 // Incorrect zip hash. |
| 433 path = extensions_path.AppendASCII("bad_hash.crx"); | 415 path = extensions_path.AppendASCII("bad_hash.crx"); |
| 434 TestInstallExtension(path, false); | 416 InstallExtension(path, false); |
| 435 ValidatePrefKeyCount(pref_count); | 417 ValidatePrefKeyCount(pref_count); |
| 436 | 418 |
| 437 // TODO(erikkay): add more tests for many of the failure cases. | 419 // TODO(erikkay): add more tests for many of the failure cases. |
| 438 // TODO(erikkay): add tests for upgrade cases. | 420 // TODO(erikkay): add tests for upgrade cases. |
| 439 } | 421 } |
| 440 | 422 |
| 441 TEST_F(ExtensionsServiceTest, InstallTheme) { | 423 TEST_F(ExtensionsServiceTest, InstallTheme) { |
| 442 FilePath extensions_path; | 424 FilePath extensions_path; |
| 443 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &extensions_path)); | 425 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &extensions_path)); |
| 444 extensions_path = extensions_path.AppendASCII("extensions"); | 426 extensions_path = extensions_path.AppendASCII("extensions"); |
| 445 | 427 |
| 446 // A theme. | 428 // A theme. |
| 447 FilePath path = extensions_path.AppendASCII("theme.crx"); | 429 FilePath path = extensions_path.AppendASCII("theme.crx"); |
| 448 TestInstallTheme(path, true); | 430 InstallExtension(path, true); |
| 449 int pref_count = 0; | 431 int pref_count = 0; |
| 450 ValidatePrefKeyCount(++pref_count); | 432 ValidatePrefKeyCount(++pref_count); |
| 451 ValidatePref(theme_crx, L"state", Extension::ENABLED); | 433 ValidatePref(theme_crx, L"state", Extension::ENABLED); |
| 452 ValidatePref(theme_crx, L"location", Extension::INTERNAL); | 434 ValidatePref(theme_crx, L"location", Extension::INTERNAL); |
| 453 | 435 |
| 454 // A theme when extensions are disabled. Themes can be installed even though | 436 // A theme when extensions are disabled. Themes can be installed even though |
| 455 // extensions are disabled. | 437 // extensions are disabled. |
| 456 SetExtensionsEnabled(false); | 438 SetExtensionsEnabled(false); |
| 457 path = extensions_path.AppendASCII("theme2.crx"); | 439 path = extensions_path.AppendASCII("theme2.crx"); |
| 458 TestInstallTheme(path, true); | 440 InstallExtension(path, true); |
| 459 ValidatePrefKeyCount(++pref_count); | 441 ValidatePrefKeyCount(++pref_count); |
| 460 ValidatePref(theme2_crx, L"state", Extension::ENABLED); | 442 ValidatePref(theme2_crx, L"state", Extension::ENABLED); |
| 461 ValidatePref(theme2_crx, L"location", Extension::INTERNAL); | 443 ValidatePref(theme2_crx, L"location", Extension::INTERNAL); |
| 462 SetExtensionsEnabled(true); | 444 SetExtensionsEnabled(true); |
| 463 | 445 |
| 464 // A theme with extension elements. Themes cannot have extension elements so | 446 // A theme with extension elements. Themes cannot have extension elements so |
| 465 // this test should fail. | 447 // this test should fail. |
| 466 path = extensions_path.AppendASCII("theme_with_extension.crx"); | 448 path = extensions_path.AppendASCII("theme_with_extension.crx"); |
| 467 TestInstallTheme(path, false); | 449 InstallExtension(path, false); |
| 468 ValidatePrefKeyCount(pref_count); | 450 ValidatePrefKeyCount(pref_count); |
| 469 | 451 |
| 470 // A theme with image resources missing (misspelt path). | 452 // A theme with image resources missing (misspelt path). |
| 471 path = extensions_path.AppendASCII("theme_missing_image.crx"); | 453 path = extensions_path.AppendASCII("theme_missing_image.crx"); |
| 472 TestInstallTheme(path, false); | 454 InstallExtension(path, false); |
| 473 ValidatePrefKeyCount(pref_count); | 455 ValidatePrefKeyCount(pref_count); |
| 474 } | 456 } |
| 475 | 457 |
| 476 // Test that when an extension version is reinstalled, nothing happens. | 458 // Test that when an extension version is reinstalled, nothing happens. |
| 477 TEST_F(ExtensionsServiceTest, Reinstall) { | 459 TEST_F(ExtensionsServiceTest, Reinstall) { |
| 478 FilePath extensions_path; | 460 FilePath extensions_path; |
| 479 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &extensions_path)); | 461 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &extensions_path)); |
| 480 extensions_path = extensions_path.AppendASCII("extensions"); | 462 extensions_path = extensions_path.AppendASCII("extensions"); |
| 481 | 463 |
| 482 // A simple extension that should install without error. | 464 // A simple extension that should install without error. |
| (...skipping 25 matching lines...) Expand all Loading... |
| 508 } | 490 } |
| 509 | 491 |
| 510 // Tests uninstalling normal extensions | 492 // Tests uninstalling normal extensions |
| 511 TEST_F(ExtensionsServiceTest, UninstallExtension) { | 493 TEST_F(ExtensionsServiceTest, UninstallExtension) { |
| 512 FilePath extensions_path; | 494 FilePath extensions_path; |
| 513 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &extensions_path)); | 495 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &extensions_path)); |
| 514 extensions_path = extensions_path.AppendASCII("extensions"); | 496 extensions_path = extensions_path.AppendASCII("extensions"); |
| 515 | 497 |
| 516 // A simple extension that should install without error. | 498 // A simple extension that should install without error. |
| 517 FilePath path = extensions_path.AppendASCII("good.crx"); | 499 FilePath path = extensions_path.AppendASCII("good.crx"); |
| 518 TestInstallExtension(path, true); | 500 InstallExtension(path, true); |
| 519 | 501 |
| 520 // The directory should be there now. | 502 // The directory should be there now. |
| 521 FilePath install_path = profile_->GetPath().AppendASCII("Extensions"); | 503 FilePath install_path = profile_->GetPath().AppendASCII("Extensions"); |
| 522 const char* extension_id = good_crx; | 504 const char* extension_id = good_crx; |
| 523 FilePath extension_path = install_path.AppendASCII(extension_id); | 505 FilePath extension_path = install_path.AppendASCII(extension_id); |
| 524 EXPECT_TRUE(file_util::PathExists(extension_path)); | 506 EXPECT_TRUE(file_util::PathExists(extension_path)); |
| 525 | 507 |
| 526 ValidatePrefKeyCount(1); | 508 ValidatePrefKeyCount(1); |
| 527 ValidatePref(good_crx, L"state", Extension::ENABLED); | 509 ValidatePref(good_crx, L"state", Extension::ENABLED); |
| 528 ValidatePref(good_crx, L"location", Extension::INTERNAL); | 510 ValidatePref(good_crx, L"location", Extension::INTERNAL); |
| (...skipping 13 matching lines...) Expand all Loading... |
| 542 // The extension should not be in the service anymore. | 524 // The extension should not be in the service anymore. |
| 543 ASSERT_FALSE(service_->GetExtensionByID(extension_id)); | 525 ASSERT_FALSE(service_->GetExtensionByID(extension_id)); |
| 544 loop_.RunAllPending(); | 526 loop_.RunAllPending(); |
| 545 | 527 |
| 546 // The directory should be gone. | 528 // The directory should be gone. |
| 547 EXPECT_FALSE(file_util::PathExists(extension_path)); | 529 EXPECT_FALSE(file_util::PathExists(extension_path)); |
| 548 | 530 |
| 549 // Try uinstalling one that doesn't have a Current Version file for some | 531 // Try uinstalling one that doesn't have a Current Version file for some |
| 550 // reason. | 532 // reason. |
| 551 unloaded_id_.clear(); | 533 unloaded_id_.clear(); |
| 552 TestInstallExtension(path, true); | 534 InstallExtension(path, true); |
| 553 FilePath current_version_file = | 535 FilePath current_version_file = |
| 554 extension_path.AppendASCII(ExtensionsService::kCurrentVersionFileName); | 536 extension_path.AppendASCII(ExtensionsService::kCurrentVersionFileName); |
| 555 EXPECT_TRUE(file_util::Delete(current_version_file, true)); | 537 EXPECT_TRUE(file_util::Delete(current_version_file, true)); |
| 556 service_->UninstallExtension(extension_id); | 538 service_->UninstallExtension(extension_id); |
| 557 loop_.RunAllPending(); | 539 loop_.RunAllPending(); |
| 558 EXPECT_FALSE(file_util::PathExists(extension_path)); | 540 EXPECT_FALSE(file_util::PathExists(extension_path)); |
| 559 | 541 |
| 560 ValidatePrefKeyCount(1); | 542 ValidatePrefKeyCount(1); |
| 561 ValidatePref(good_crx, L"state", Extension::DISABLED); | 543 ValidatePref(good_crx, L"state", Extension::DISABLED); |
| 562 ValidatePref(good_crx, L"location", Extension::INTERNAL); | 544 ValidatePref(good_crx, L"location", Extension::INTERNAL); |
| (...skipping 279 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 842 ValidatePrefKeyCount(1); | 824 ValidatePrefKeyCount(1); |
| 843 ValidatePref(good_crx, L"state", Extension::KILLBIT); | 825 ValidatePref(good_crx, L"state", Extension::KILLBIT); |
| 844 ValidatePref(good_crx, L"location", Extension::EXTERNAL_PREF); | 826 ValidatePref(good_crx, L"location", Extension::EXTERNAL_PREF); |
| 845 | 827 |
| 846 // The extension should also be gone from disk. | 828 // The extension should also be gone from disk. |
| 847 FilePath extension_path = install_path.DirName(); | 829 FilePath extension_path = install_path.DirName(); |
| 848 extension_path = extension_path.AppendASCII(good_crx); | 830 extension_path = extension_path.AppendASCII(good_crx); |
| 849 EXPECT_FALSE(file_util::PathExists(extension_path)) << | 831 EXPECT_FALSE(file_util::PathExists(extension_path)) << |
| 850 extension_path.ToWStringHack(); | 832 extension_path.ToWStringHack(); |
| 851 } | 833 } |
| OLD | NEW |