Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(92)

Side by Side Diff: extensions/common/extension_resource_unittest.cc

Issue 12578008: Move CrxFile, FileReader, ExtensionResource to src/extensions. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: rebase Created 7 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « extensions/common/extension_resource.cc ('k') | extensions/common/id_util.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 <algorithm> 5 #include <algorithm>
6 6
7 #include "base/file_util.h" 7 #include "base/file_util.h"
8 #include "base/files/scoped_temp_dir.h" 8 #include "base/files/scoped_temp_dir.h"
9 #include "base/path_service.h" 9 #include "base/path_service.h"
10 #include "chrome/common/chrome_paths.h" 10 #include "chrome/common/chrome_paths.h"
11 #include "chrome/common/extensions/extension.h" 11 #include "extensions/common/constants.h"
12 #include "chrome/common/extensions/extension_l10n_util.h" 12 #include "extensions/common/extension_resource.h"
13 #include "chrome/common/extensions/extension_resource.h" 13 #include "extensions/common/id_util.h"
14 #include "chrome/common/extensions/extension_test_util.h"
15 #include "testing/gtest/include/gtest/gtest.h" 14 #include "testing/gtest/include/gtest/gtest.h"
16 #include "ui/base/l10n/l10n_util.h" 15 #include "ui/base/l10n/l10n_util.h"
17 16
17 namespace extensions {
18
18 TEST(ExtensionResourceTest, CreateEmptyResource) { 19 TEST(ExtensionResourceTest, CreateEmptyResource) {
19 ExtensionResource resource; 20 ExtensionResource resource;
20 21
21 EXPECT_TRUE(resource.extension_root().empty()); 22 EXPECT_TRUE(resource.extension_root().empty());
22 EXPECT_TRUE(resource.relative_path().empty()); 23 EXPECT_TRUE(resource.relative_path().empty());
23 EXPECT_TRUE(resource.GetFilePath().empty()); 24 EXPECT_TRUE(resource.GetFilePath().empty());
24 } 25 }
25 26
26 const base::FilePath::StringType ToLower( 27 const base::FilePath::StringType ToLower(
27 const base::FilePath::StringType& in_str) { 28 const base::FilePath::StringType& in_str) {
28 base::FilePath::StringType str(in_str); 29 base::FilePath::StringType str(in_str);
29 std::transform(str.begin(), str.end(), str.begin(), tolower); 30 std::transform(str.begin(), str.end(), str.begin(), tolower);
30 return str; 31 return str;
31 } 32 }
32 33
33 TEST(ExtensionResourceTest, CreateWithMissingResourceOnDisk) { 34 TEST(ExtensionResourceTest, CreateWithMissingResourceOnDisk) {
34 base::FilePath root_path; 35 base::FilePath root_path;
35 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &root_path)); 36 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &root_path));
36 base::FilePath relative_path; 37 base::FilePath relative_path;
37 relative_path = relative_path.AppendASCII("cira.js"); 38 relative_path = relative_path.AppendASCII("cira.js");
38 std::string extension_id = extension_test_util::MakeId("test"); 39 std::string extension_id = id_util::GenerateId("test");
39 ExtensionResource resource(extension_id, root_path, relative_path); 40 ExtensionResource resource(extension_id, root_path, relative_path);
40 41
41 // The path doesn't exist on disk, we will be returned an empty path. 42 // The path doesn't exist on disk, we will be returned an empty path.
42 EXPECT_EQ(root_path.value(), resource.extension_root().value()); 43 EXPECT_EQ(root_path.value(), resource.extension_root().value());
43 EXPECT_EQ(relative_path.value(), resource.relative_path().value()); 44 EXPECT_EQ(relative_path.value(), resource.relative_path().value());
44 EXPECT_TRUE(resource.GetFilePath().empty()); 45 EXPECT_TRUE(resource.GetFilePath().empty());
45 } 46 }
46 47
47 TEST(ExtensionResourceTest, ResourcesOutsideOfPath) { 48 TEST(ExtensionResourceTest, ResourcesOutsideOfPath) {
48 base::ScopedTempDir temp; 49 base::ScopedTempDir temp;
49 ASSERT_TRUE(temp.CreateUniqueTempDir()); 50 ASSERT_TRUE(temp.CreateUniqueTempDir());
50 51
51 base::FilePath inner_dir = temp.path().AppendASCII("directory"); 52 base::FilePath inner_dir = temp.path().AppendASCII("directory");
52 ASSERT_TRUE(file_util::CreateDirectory(inner_dir)); 53 ASSERT_TRUE(file_util::CreateDirectory(inner_dir));
53 base::FilePath sub_dir = inner_dir.AppendASCII("subdir"); 54 base::FilePath sub_dir = inner_dir.AppendASCII("subdir");
54 ASSERT_TRUE(file_util::CreateDirectory(sub_dir)); 55 ASSERT_TRUE(file_util::CreateDirectory(sub_dir));
55 base::FilePath inner_file = inner_dir.AppendASCII("inner"); 56 base::FilePath inner_file = inner_dir.AppendASCII("inner");
56 base::FilePath outer_file = temp.path().AppendASCII("outer"); 57 base::FilePath outer_file = temp.path().AppendASCII("outer");
57 ASSERT_TRUE(file_util::WriteFile(outer_file, "X", 1)); 58 ASSERT_TRUE(file_util::WriteFile(outer_file, "X", 1));
58 ASSERT_TRUE(file_util::WriteFile(inner_file, "X", 1)); 59 ASSERT_TRUE(file_util::WriteFile(inner_file, "X", 1));
59 std::string extension_id = extension_test_util::MakeId("test"); 60 std::string extension_id = id_util::GenerateId("test");
60 61
61 #if defined(OS_POSIX) 62 #if defined(OS_POSIX)
62 base::FilePath symlink_file = inner_dir.AppendASCII("symlink"); 63 base::FilePath symlink_file = inner_dir.AppendASCII("symlink");
63 file_util::CreateSymbolicLink( 64 file_util::CreateSymbolicLink(
64 base::FilePath().AppendASCII("..").AppendASCII("outer"), 65 base::FilePath().AppendASCII("..").AppendASCII("outer"),
65 symlink_file); 66 symlink_file);
66 #endif 67 #endif
67 68
68 // A non-packing extension should be able to access the file within the 69 // A non-packing extension should be able to access the file within the
69 // directory. 70 // directory.
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
119 ASSERT_TRUE(temp.CreateUniqueTempDir()); 120 ASSERT_TRUE(temp.CreateUniqueTempDir());
120 121
121 // Create resource in the extension root. 122 // Create resource in the extension root.
122 const char* filename = "res.ico"; 123 const char* filename = "res.ico";
123 base::FilePath root_resource = temp.path().AppendASCII(filename); 124 base::FilePath root_resource = temp.path().AppendASCII(filename);
124 std::string data = "some foo"; 125 std::string data = "some foo";
125 ASSERT_TRUE(file_util::WriteFile(root_resource, data.c_str(), data.length())); 126 ASSERT_TRUE(file_util::WriteFile(root_resource, data.c_str(), data.length()));
126 127
127 // Create l10n resources (for current locale and its parents). 128 // Create l10n resources (for current locale and its parents).
128 base::FilePath l10n_path = 129 base::FilePath l10n_path =
129 temp.path().Append(extensions::Extension::kLocaleFolder); 130 temp.path().Append(kLocaleFolder);
130 ASSERT_TRUE(file_util::CreateDirectory(l10n_path)); 131 ASSERT_TRUE(file_util::CreateDirectory(l10n_path));
131 132
132 std::vector<std::string> locales; 133 std::vector<std::string> locales;
133 l10n_util::GetParentLocales(l10n_util::GetApplicationLocale(""), &locales); 134 l10n_util::GetParentLocales(l10n_util::GetApplicationLocale(""), &locales);
134 ASSERT_FALSE(locales.empty()); 135 ASSERT_FALSE(locales.empty());
135 for (size_t i = 0; i < locales.size(); i++) { 136 for (size_t i = 0; i < locales.size(); i++) {
136 base::FilePath make_path; 137 base::FilePath make_path;
137 make_path = l10n_path.AppendASCII(locales[i]); 138 make_path = l10n_path.AppendASCII(locales[i]);
138 ASSERT_TRUE(file_util::CreateDirectory(make_path)); 139 ASSERT_TRUE(file_util::CreateDirectory(make_path));
139 ASSERT_TRUE(file_util::WriteFile(make_path.AppendASCII(filename), 140 ASSERT_TRUE(file_util::WriteFile(make_path.AppendASCII(filename),
140 data.c_str(), data.length())); 141 data.c_str(), data.length()));
141 } 142 }
142 143
143 base::FilePath path; 144 base::FilePath path;
144 std::string extension_id = extension_test_util::MakeId("test"); 145 std::string extension_id = id_util::GenerateId("test");
145 ExtensionResource resource(extension_id, temp.path(), 146 ExtensionResource resource(extension_id, temp.path(),
146 base::FilePath().AppendASCII(filename)); 147 base::FilePath().AppendASCII(filename));
147 base::FilePath resolved_path = resource.GetFilePath(); 148 base::FilePath resolved_path = resource.GetFilePath();
148 149
149 base::FilePath expected_path; 150 base::FilePath expected_path;
150 // Expect default path only, since fallback logic is disabled. 151 // Expect default path only, since fallback logic is disabled.
151 // See http://crbug.com/27359. 152 // See http://crbug.com/27359.
152 expected_path = root_resource; 153 expected_path = root_resource;
153 ASSERT_TRUE(file_util::AbsolutePath(&expected_path)); 154 ASSERT_TRUE(file_util::AbsolutePath(&expected_path));
154 155
155 EXPECT_EQ(ToLower(expected_path.value()), ToLower(resolved_path.value())); 156 EXPECT_EQ(ToLower(expected_path.value()), ToLower(resolved_path.value()));
156 EXPECT_EQ(ToLower(temp.path().value()), 157 EXPECT_EQ(ToLower(temp.path().value()),
157 ToLower(resource.extension_root().value())); 158 ToLower(resource.extension_root().value()));
158 EXPECT_EQ(ToLower(base::FilePath().AppendASCII(filename).value()), 159 EXPECT_EQ(ToLower(base::FilePath().AppendASCII(filename).value()),
159 ToLower(resource.relative_path().value())); 160 ToLower(resource.relative_path().value()));
160 } 161 }
162
163 } // namespace extensions
OLDNEW
« no previous file with comments | « extensions/common/extension_resource.cc ('k') | extensions/common/id_util.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698