| 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/common/extensions/extension_file_util.h" | 5 #include "chrome/common/extensions/extension_file_util.h" |
| 6 | 6 |
| 7 #include "base/file_util.h" | 7 #include "base/file_util.h" |
| 8 #include "base/json/json_string_value_serializer.h" | 8 #include "base/json/json_string_value_serializer.h" |
| 9 #include "base/path_service.h" | 9 #include "base/path_service.h" |
| 10 #include "base/scoped_temp_dir.h" | 10 #include "base/scoped_temp_dir.h" |
| 11 #include "base/stringprintf.h" | 11 #include "base/stringprintf.h" |
| 12 #include "base/utf_string_conversions.h" | 12 #include "base/utf_string_conversions.h" |
| 13 #include "chrome/common/chrome_paths.h" | 13 #include "chrome/common/chrome_paths.h" |
| 14 #include "chrome/common/extensions/extension.h" | 14 #include "chrome/common/extensions/extension.h" |
| 15 #include "chrome/common/extensions/extension_manifest_constants.h" | 15 #include "chrome/common/extensions/extension_manifest_constants.h" |
| 16 #include "grit/generated_resources.h" | 16 #include "grit/generated_resources.h" |
| 17 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
| 18 #include "ui/base/l10n/l10n_util.h" | 18 #include "ui/base/l10n/l10n_util.h" |
| 19 | 19 |
| 20 namespace keys = extension_manifest_keys; | 20 namespace keys = extension_manifest_keys; |
| 21 | 21 |
| 22 #if defined(OS_WIN) | |
| 23 // http://crbug.com/106381 | |
| 24 #define InstallUninstallGarbageCollect DISABLED_InstallUninstallGarbageCollect | |
| 25 #endif | |
| 26 TEST(ExtensionFileUtil, InstallUninstallGarbageCollect) { | |
| 27 ScopedTempDir temp; | |
| 28 ASSERT_TRUE(temp.CreateUniqueTempDir()); | |
| 29 | |
| 30 // Create a source extension. | |
| 31 std::string extension_id("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"); | |
| 32 std::string version("1.0"); | |
| 33 FilePath src = temp.path().AppendASCII(extension_id); | |
| 34 ASSERT_TRUE(file_util::CreateDirectory(src)); | |
| 35 | |
| 36 // Create a extensions tree. | |
| 37 FilePath all_extensions = temp.path().AppendASCII("extensions"); | |
| 38 ASSERT_TRUE(file_util::CreateDirectory(all_extensions)); | |
| 39 | |
| 40 // Install in empty directory. Should create parent directories as needed. | |
| 41 FilePath version_1 = extension_file_util::InstallExtension(src, | |
| 42 extension_id, | |
| 43 version, | |
| 44 all_extensions); | |
| 45 ASSERT_EQ(version_1.value(), | |
| 46 all_extensions.AppendASCII(extension_id).AppendASCII("1.0_0") | |
| 47 .value()); | |
| 48 ASSERT_TRUE(file_util::DirectoryExists(version_1)); | |
| 49 | |
| 50 // Should have moved the source. | |
| 51 ASSERT_FALSE(file_util::DirectoryExists(src)); | |
| 52 | |
| 53 // Install again. Should create a new one with different name. | |
| 54 ASSERT_TRUE(file_util::CreateDirectory(src)); | |
| 55 FilePath version_2 = extension_file_util::InstallExtension(src, | |
| 56 extension_id, | |
| 57 version, | |
| 58 all_extensions); | |
| 59 ASSERT_EQ(version_2.value(), | |
| 60 all_extensions.AppendASCII(extension_id).AppendASCII("1.0_1") | |
| 61 .value()); | |
| 62 ASSERT_TRUE(file_util::DirectoryExists(version_2)); | |
| 63 | |
| 64 // Collect garbage. Should remove first one. | |
| 65 std::map<std::string, FilePath> extension_paths; | |
| 66 extension_paths[extension_id] = | |
| 67 FilePath().AppendASCII(extension_id).Append(version_2.BaseName()); | |
| 68 extension_file_util::GarbageCollectExtensions(all_extensions, | |
| 69 extension_paths); | |
| 70 ASSERT_FALSE(file_util::DirectoryExists(version_1)); | |
| 71 ASSERT_TRUE(file_util::DirectoryExists(version_2)); | |
| 72 | |
| 73 // Uninstall. Should remove entire extension subtree. | |
| 74 extension_file_util::UninstallExtension(all_extensions, extension_id); | |
| 75 ASSERT_FALSE(file_util::DirectoryExists(version_2.DirName())); | |
| 76 ASSERT_TRUE(file_util::DirectoryExists(all_extensions)); | |
| 77 } | |
| 78 | |
| 79 TEST(ExtensionFileUtil, LoadExtensionWithValidLocales) { | 22 TEST(ExtensionFileUtil, LoadExtensionWithValidLocales) { |
| 80 FilePath install_dir; | 23 FilePath install_dir; |
| 81 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &install_dir)); | 24 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &install_dir)); |
| 82 install_dir = install_dir.AppendASCII("extensions") | 25 install_dir = install_dir.AppendASCII("extensions") |
| 83 .AppendASCII("good") | 26 .AppendASCII("good") |
| 84 .AppendASCII("Extensions") | 27 .AppendASCII("Extensions") |
| 85 .AppendASCII("behllobkkfkfnphdnhnkndlbkcpglgmj") | 28 .AppendASCII("behllobkkfkfnphdnhnkndlbkcpglgmj") |
| 86 .AppendASCII("1.0.0.0"); | 29 .AppendASCII("1.0.0.0"); |
| 87 | 30 |
| 88 std::string error; | 31 std::string error; |
| (...skipping 268 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 357 EXPECT_FALSE(extension_file_util::ValidateExtension(extension, &error)); | 300 EXPECT_FALSE(extension_file_util::ValidateExtension(extension, &error)); |
| 358 EXPECT_EQ(l10n_util::GetStringFUTF8( | 301 EXPECT_EQ(l10n_util::GetStringFUTF8( |
| 359 IDS_EXTENSION_LOAD_BACKGROUND_SCRIPT_FAILED, | 302 IDS_EXTENSION_LOAD_BACKGROUND_SCRIPT_FAILED, |
| 360 ASCIIToUTF16("http://google.com/foo.js")), | 303 ASCIIToUTF16("http://google.com/foo.js")), |
| 361 error); | 304 error); |
| 362 } | 305 } |
| 363 | 306 |
| 364 // TODO(aa): More tests as motivation allows. Maybe steal some from | 307 // TODO(aa): More tests as motivation allows. Maybe steal some from |
| 365 // ExtensionService? Many of them could probably be tested here without the | 308 // ExtensionService? Many of them could probably be tested here without the |
| 366 // MessageLoop shenanigans. | 309 // MessageLoop shenanigans. |
| OLD | NEW |