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 "ui/base/resource/data_pack.h" | 5 #include "ui/base/resource/data_pack.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 | 9 |
10 #include <utility> | 10 #include <utility> |
(...skipping 17 matching lines...) Expand all Loading... |
28 }; | 28 }; |
29 | 29 |
30 extern const char kSamplePakContents[]; | 30 extern const char kSamplePakContents[]; |
31 extern const char kSampleCorruptPakContents[]; | 31 extern const char kSampleCorruptPakContents[]; |
32 extern const size_t kSamplePakSize; | 32 extern const size_t kSamplePakSize; |
33 extern const size_t kSampleCorruptPakSize; | 33 extern const size_t kSampleCorruptPakSize; |
34 | 34 |
35 TEST(DataPackTest, LoadFromPath) { | 35 TEST(DataPackTest, LoadFromPath) { |
36 base::ScopedTempDir dir; | 36 base::ScopedTempDir dir; |
37 ASSERT_TRUE(dir.CreateUniqueTempDir()); | 37 ASSERT_TRUE(dir.CreateUniqueTempDir()); |
38 base::FilePath data_path = dir.path().Append(FILE_PATH_LITERAL("sample.pak")); | 38 base::FilePath data_path = |
| 39 dir.GetPath().Append(FILE_PATH_LITERAL("sample.pak")); |
39 | 40 |
40 // Dump contents into the pak file. | 41 // Dump contents into the pak file. |
41 ASSERT_EQ(base::WriteFile(data_path, kSamplePakContents, kSamplePakSize), | 42 ASSERT_EQ(base::WriteFile(data_path, kSamplePakContents, kSamplePakSize), |
42 static_cast<int>(kSamplePakSize)); | 43 static_cast<int>(kSamplePakSize)); |
43 | 44 |
44 // Load the file through the data pack API. | 45 // Load the file through the data pack API. |
45 DataPack pack(SCALE_FACTOR_100P); | 46 DataPack pack(SCALE_FACTOR_100P); |
46 ASSERT_TRUE(pack.LoadFromPath(data_path)); | 47 ASSERT_TRUE(pack.LoadFromPath(data_path)); |
47 | 48 |
48 base::StringPiece data; | 49 base::StringPiece data; |
(...skipping 11 matching lines...) Expand all Loading... |
60 EXPECT_EQ(0U, data.length()); | 61 EXPECT_EQ(0U, data.length()); |
61 | 62 |
62 // Try looking up an invalid key. | 63 // Try looking up an invalid key. |
63 ASSERT_FALSE(pack.HasResource(140)); | 64 ASSERT_FALSE(pack.HasResource(140)); |
64 ASSERT_FALSE(pack.GetStringPiece(140, &data)); | 65 ASSERT_FALSE(pack.GetStringPiece(140, &data)); |
65 } | 66 } |
66 | 67 |
67 TEST(DataPackTest, LoadFromFile) { | 68 TEST(DataPackTest, LoadFromFile) { |
68 base::ScopedTempDir dir; | 69 base::ScopedTempDir dir; |
69 ASSERT_TRUE(dir.CreateUniqueTempDir()); | 70 ASSERT_TRUE(dir.CreateUniqueTempDir()); |
70 base::FilePath data_path = dir.path().Append(FILE_PATH_LITERAL("sample.pak")); | 71 base::FilePath data_path = |
| 72 dir.GetPath().Append(FILE_PATH_LITERAL("sample.pak")); |
71 | 73 |
72 // Dump contents into the pak file. | 74 // Dump contents into the pak file. |
73 ASSERT_EQ(base::WriteFile(data_path, kSamplePakContents, kSamplePakSize), | 75 ASSERT_EQ(base::WriteFile(data_path, kSamplePakContents, kSamplePakSize), |
74 static_cast<int>(kSamplePakSize)); | 76 static_cast<int>(kSamplePakSize)); |
75 | 77 |
76 base::File file(data_path, base::File::FLAG_OPEN | base::File::FLAG_READ); | 78 base::File file(data_path, base::File::FLAG_OPEN | base::File::FLAG_READ); |
77 ASSERT_TRUE(file.IsValid()); | 79 ASSERT_TRUE(file.IsValid()); |
78 | 80 |
79 // Load the file through the data pack API. | 81 // Load the file through the data pack API. |
80 DataPack pack(SCALE_FACTOR_100P); | 82 DataPack pack(SCALE_FACTOR_100P); |
(...skipping 14 matching lines...) Expand all Loading... |
95 EXPECT_EQ(0U, data.length()); | 97 EXPECT_EQ(0U, data.length()); |
96 | 98 |
97 // Try looking up an invalid key. | 99 // Try looking up an invalid key. |
98 ASSERT_FALSE(pack.HasResource(140)); | 100 ASSERT_FALSE(pack.HasResource(140)); |
99 ASSERT_FALSE(pack.GetStringPiece(140, &data)); | 101 ASSERT_FALSE(pack.GetStringPiece(140, &data)); |
100 } | 102 } |
101 | 103 |
102 TEST(DataPackTest, LoadFromFileRegion) { | 104 TEST(DataPackTest, LoadFromFileRegion) { |
103 base::ScopedTempDir dir; | 105 base::ScopedTempDir dir; |
104 ASSERT_TRUE(dir.CreateUniqueTempDir()); | 106 ASSERT_TRUE(dir.CreateUniqueTempDir()); |
105 base::FilePath data_path = dir.path().Append(FILE_PATH_LITERAL("sample.pak")); | 107 base::FilePath data_path = |
| 108 dir.GetPath().Append(FILE_PATH_LITERAL("sample.pak")); |
106 | 109 |
107 // Construct a file which has a non page-aligned zero-filled header followed | 110 // Construct a file which has a non page-aligned zero-filled header followed |
108 // by the actual pak file content. | 111 // by the actual pak file content. |
109 const char kPadding[5678] = {0}; | 112 const char kPadding[5678] = {0}; |
110 ASSERT_EQ(static_cast<int>(sizeof(kPadding)), | 113 ASSERT_EQ(static_cast<int>(sizeof(kPadding)), |
111 base::WriteFile(data_path, kPadding, sizeof(kPadding))); | 114 base::WriteFile(data_path, kPadding, sizeof(kPadding))); |
112 ASSERT_TRUE(base::AppendToFile( | 115 ASSERT_TRUE(base::AppendToFile( |
113 data_path, kSamplePakContents, kSamplePakSize)); | 116 data_path, kSamplePakContents, kSamplePakSize)); |
114 | 117 |
115 base::File file(data_path, base::File::FLAG_OPEN | base::File::FLAG_READ); | 118 base::File file(data_path, base::File::FLAG_OPEN | base::File::FLAG_READ); |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
151 ASSERT_TRUE(PathService::Get(UI_DIR_TEST_DATA, &data_path)); | 154 ASSERT_TRUE(PathService::Get(UI_DIR_TEST_DATA, &data_path)); |
152 data_path = data_path.AppendASCII("data_pack_unittest/truncated-header.pak"); | 155 data_path = data_path.AppendASCII("data_pack_unittest/truncated-header.pak"); |
153 | 156 |
154 DataPack pack(SCALE_FACTOR_100P); | 157 DataPack pack(SCALE_FACTOR_100P); |
155 ASSERT_FALSE(pack.LoadFromPath(data_path)); | 158 ASSERT_FALSE(pack.LoadFromPath(data_path)); |
156 } | 159 } |
157 | 160 |
158 TEST_P(DataPackTest, Write) { | 161 TEST_P(DataPackTest, Write) { |
159 base::ScopedTempDir dir; | 162 base::ScopedTempDir dir; |
160 ASSERT_TRUE(dir.CreateUniqueTempDir()); | 163 ASSERT_TRUE(dir.CreateUniqueTempDir()); |
161 base::FilePath file = dir.path().Append(FILE_PATH_LITERAL("data.pak")); | 164 base::FilePath file = dir.GetPath().Append(FILE_PATH_LITERAL("data.pak")); |
162 | 165 |
163 std::string one("one"); | 166 std::string one("one"); |
164 std::string two("two"); | 167 std::string two("two"); |
165 std::string three("three"); | 168 std::string three("three"); |
166 std::string four("four"); | 169 std::string four("four"); |
167 std::string fifteen("fifteen"); | 170 std::string fifteen("fifteen"); |
168 | 171 |
169 std::map<uint16_t, base::StringPiece> resources; | 172 std::map<uint16_t, base::StringPiece> resources; |
170 resources.insert(std::make_pair(1, base::StringPiece(one))); | 173 resources.insert(std::make_pair(1, base::StringPiece(one))); |
171 resources.insert(std::make_pair(2, base::StringPiece(two))); | 174 resources.insert(std::make_pair(2, base::StringPiece(two))); |
(...skipping 17 matching lines...) Expand all Loading... |
189 ASSERT_TRUE(pack.GetStringPiece(4, &data)); | 192 ASSERT_TRUE(pack.GetStringPiece(4, &data)); |
190 EXPECT_EQ(four, data); | 193 EXPECT_EQ(four, data); |
191 ASSERT_TRUE(pack.GetStringPiece(15, &data)); | 194 ASSERT_TRUE(pack.GetStringPiece(15, &data)); |
192 EXPECT_EQ(fifteen, data); | 195 EXPECT_EQ(fifteen, data); |
193 } | 196 } |
194 | 197 |
195 #if defined(OS_POSIX) | 198 #if defined(OS_POSIX) |
196 TEST(DataPackTest, ModifiedWhileUsed) { | 199 TEST(DataPackTest, ModifiedWhileUsed) { |
197 base::ScopedTempDir dir; | 200 base::ScopedTempDir dir; |
198 ASSERT_TRUE(dir.CreateUniqueTempDir()); | 201 ASSERT_TRUE(dir.CreateUniqueTempDir()); |
199 base::FilePath data_path = dir.path().Append(FILE_PATH_LITERAL("sample.pak")); | 202 base::FilePath data_path = |
| 203 dir.GetPath().Append(FILE_PATH_LITERAL("sample.pak")); |
200 | 204 |
201 // Dump contents into the pak file. | 205 // Dump contents into the pak file. |
202 ASSERT_EQ(base::WriteFile(data_path, kSamplePakContents, kSamplePakSize), | 206 ASSERT_EQ(base::WriteFile(data_path, kSamplePakContents, kSamplePakSize), |
203 static_cast<int>(kSamplePakSize)); | 207 static_cast<int>(kSamplePakSize)); |
204 | 208 |
205 base::File file(data_path, base::File::FLAG_OPEN | base::File::FLAG_READ); | 209 base::File file(data_path, base::File::FLAG_OPEN | base::File::FLAG_READ); |
206 ASSERT_TRUE(file.IsValid()); | 210 ASSERT_TRUE(file.IsValid()); |
207 | 211 |
208 // Load the file through the data pack API. | 212 // Load the file through the data pack API. |
209 DataPack pack(SCALE_FACTOR_100P); | 213 DataPack pack(SCALE_FACTOR_100P); |
210 ASSERT_TRUE(pack.LoadFromFile(std::move(file))); | 214 ASSERT_TRUE(pack.LoadFromFile(std::move(file))); |
211 | 215 |
212 base::StringPiece data; | 216 base::StringPiece data; |
213 ASSERT_TRUE(pack.HasResource(10)); | 217 ASSERT_TRUE(pack.HasResource(10)); |
214 ASSERT_TRUE(pack.GetStringPiece(10, &data)); | 218 ASSERT_TRUE(pack.GetStringPiece(10, &data)); |
215 | 219 |
216 ASSERT_EQ(base::WriteFile(data_path, kSampleCorruptPakContents, | 220 ASSERT_EQ(base::WriteFile(data_path, kSampleCorruptPakContents, |
217 kSampleCorruptPakSize), | 221 kSampleCorruptPakSize), |
218 static_cast<int>(kSampleCorruptPakSize)); | 222 static_cast<int>(kSampleCorruptPakSize)); |
219 | 223 |
220 // Reading asset #10 should now fail as it extends past the end of the file. | 224 // Reading asset #10 should now fail as it extends past the end of the file. |
221 ASSERT_TRUE(pack.HasResource(10)); | 225 ASSERT_TRUE(pack.HasResource(10)); |
222 ASSERT_FALSE(pack.GetStringPiece(10, &data)); | 226 ASSERT_FALSE(pack.GetStringPiece(10, &data)); |
223 } | 227 } |
224 #endif | 228 #endif |
225 | 229 |
226 } // namespace ui | 230 } // namespace ui |
OLD | NEW |