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" |
| 6 |
| 7 #include <utility> |
| 8 |
5 #include "base/files/file.h" | 9 #include "base/files/file.h" |
6 #include "base/files/file_path.h" | 10 #include "base/files/file_path.h" |
7 #include "base/files/file_util.h" | 11 #include "base/files/file_util.h" |
8 #include "base/files/scoped_temp_dir.h" | 12 #include "base/files/scoped_temp_dir.h" |
9 #include "base/path_service.h" | 13 #include "base/path_service.h" |
10 #include "base/strings/string_piece.h" | 14 #include "base/strings/string_piece.h" |
11 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
12 #include "ui/base/resource/data_pack.h" | |
13 #include "ui/base/ui_base_paths.h" | 16 #include "ui/base/ui_base_paths.h" |
14 | 17 |
15 namespace ui { | 18 namespace ui { |
16 | 19 |
17 class DataPackTest | 20 class DataPackTest |
18 : public testing::TestWithParam<DataPack::TextEncodingType> { | 21 : public testing::TestWithParam<DataPack::TextEncodingType> { |
19 public: | 22 public: |
20 DataPackTest() {} | 23 DataPackTest() {} |
21 }; | 24 }; |
22 | 25 |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
64 | 67 |
65 // Dump contents into the pak file. | 68 // Dump contents into the pak file. |
66 ASSERT_EQ(base::WriteFile(data_path, kSamplePakContents, kSamplePakSize), | 69 ASSERT_EQ(base::WriteFile(data_path, kSamplePakContents, kSamplePakSize), |
67 static_cast<int>(kSamplePakSize)); | 70 static_cast<int>(kSamplePakSize)); |
68 | 71 |
69 base::File file(data_path, base::File::FLAG_OPEN | base::File::FLAG_READ); | 72 base::File file(data_path, base::File::FLAG_OPEN | base::File::FLAG_READ); |
70 ASSERT_TRUE(file.IsValid()); | 73 ASSERT_TRUE(file.IsValid()); |
71 | 74 |
72 // Load the file through the data pack API. | 75 // Load the file through the data pack API. |
73 DataPack pack(SCALE_FACTOR_100P); | 76 DataPack pack(SCALE_FACTOR_100P); |
74 ASSERT_TRUE(pack.LoadFromFile(file.Pass())); | 77 ASSERT_TRUE(pack.LoadFromFile(std::move(file))); |
75 | 78 |
76 base::StringPiece data; | 79 base::StringPiece data; |
77 ASSERT_TRUE(pack.HasResource(4)); | 80 ASSERT_TRUE(pack.HasResource(4)); |
78 ASSERT_TRUE(pack.GetStringPiece(4, &data)); | 81 ASSERT_TRUE(pack.GetStringPiece(4, &data)); |
79 EXPECT_EQ("this is id 4", data); | 82 EXPECT_EQ("this is id 4", data); |
80 ASSERT_TRUE(pack.HasResource(6)); | 83 ASSERT_TRUE(pack.HasResource(6)); |
81 ASSERT_TRUE(pack.GetStringPiece(6, &data)); | 84 ASSERT_TRUE(pack.GetStringPiece(6, &data)); |
82 EXPECT_EQ("this is id 6", data); | 85 EXPECT_EQ("this is id 6", data); |
83 | 86 |
84 // Try reading zero-length data blobs, just in case. | 87 // Try reading zero-length data blobs, just in case. |
(...skipping 19 matching lines...) Expand all Loading... |
104 base::WriteFile(data_path, kPadding, sizeof(kPadding))); | 107 base::WriteFile(data_path, kPadding, sizeof(kPadding))); |
105 ASSERT_TRUE(base::AppendToFile( | 108 ASSERT_TRUE(base::AppendToFile( |
106 data_path, kSamplePakContents, kSamplePakSize)); | 109 data_path, kSamplePakContents, kSamplePakSize)); |
107 | 110 |
108 base::File file(data_path, base::File::FLAG_OPEN | base::File::FLAG_READ); | 111 base::File file(data_path, base::File::FLAG_OPEN | base::File::FLAG_READ); |
109 ASSERT_TRUE(file.IsValid()); | 112 ASSERT_TRUE(file.IsValid()); |
110 | 113 |
111 // Load the file through the data pack API. | 114 // Load the file through the data pack API. |
112 DataPack pack(SCALE_FACTOR_100P); | 115 DataPack pack(SCALE_FACTOR_100P); |
113 base::MemoryMappedFile::Region region = {sizeof(kPadding), kSamplePakSize}; | 116 base::MemoryMappedFile::Region region = {sizeof(kPadding), kSamplePakSize}; |
114 ASSERT_TRUE(pack.LoadFromFileRegion(file.Pass(), region)); | 117 ASSERT_TRUE(pack.LoadFromFileRegion(std::move(file), region)); |
115 | 118 |
116 base::StringPiece data; | 119 base::StringPiece data; |
117 ASSERT_TRUE(pack.HasResource(4)); | 120 ASSERT_TRUE(pack.HasResource(4)); |
118 ASSERT_TRUE(pack.GetStringPiece(4, &data)); | 121 ASSERT_TRUE(pack.GetStringPiece(4, &data)); |
119 EXPECT_EQ("this is id 4", data); | 122 EXPECT_EQ("this is id 4", data); |
120 ASSERT_TRUE(pack.HasResource(6)); | 123 ASSERT_TRUE(pack.HasResource(6)); |
121 ASSERT_TRUE(pack.GetStringPiece(6, &data)); | 124 ASSERT_TRUE(pack.GetStringPiece(6, &data)); |
122 EXPECT_EQ("this is id 6", data); | 125 EXPECT_EQ("this is id 6", data); |
123 | 126 |
124 // Try reading zero-length data blobs, just in case. | 127 // Try reading zero-length data blobs, just in case. |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
193 | 196 |
194 // Dump contents into the pak file. | 197 // Dump contents into the pak file. |
195 ASSERT_EQ(base::WriteFile(data_path, kSamplePakContents, kSamplePakSize), | 198 ASSERT_EQ(base::WriteFile(data_path, kSamplePakContents, kSamplePakSize), |
196 static_cast<int>(kSamplePakSize)); | 199 static_cast<int>(kSamplePakSize)); |
197 | 200 |
198 base::File file(data_path, base::File::FLAG_OPEN | base::File::FLAG_READ); | 201 base::File file(data_path, base::File::FLAG_OPEN | base::File::FLAG_READ); |
199 ASSERT_TRUE(file.IsValid()); | 202 ASSERT_TRUE(file.IsValid()); |
200 | 203 |
201 // Load the file through the data pack API. | 204 // Load the file through the data pack API. |
202 DataPack pack(SCALE_FACTOR_100P); | 205 DataPack pack(SCALE_FACTOR_100P); |
203 ASSERT_TRUE(pack.LoadFromFile(file.Pass())); | 206 ASSERT_TRUE(pack.LoadFromFile(std::move(file))); |
204 | 207 |
205 base::StringPiece data; | 208 base::StringPiece data; |
206 ASSERT_TRUE(pack.HasResource(10)); | 209 ASSERT_TRUE(pack.HasResource(10)); |
207 ASSERT_TRUE(pack.GetStringPiece(10, &data)); | 210 ASSERT_TRUE(pack.GetStringPiece(10, &data)); |
208 | 211 |
209 ASSERT_EQ(base::WriteFile(data_path, kSampleCorruptPakContents, | 212 ASSERT_EQ(base::WriteFile(data_path, kSampleCorruptPakContents, |
210 kSampleCorruptPakSize), | 213 kSampleCorruptPakSize), |
211 static_cast<int>(kSampleCorruptPakSize)); | 214 static_cast<int>(kSampleCorruptPakSize)); |
212 | 215 |
213 // Reading asset #10 should now fail as it extends past the end of the file. | 216 // Reading asset #10 should now fail as it extends past the end of the file. |
214 ASSERT_TRUE(pack.HasResource(10)); | 217 ASSERT_TRUE(pack.HasResource(10)); |
215 ASSERT_FALSE(pack.GetStringPiece(10, &data)); | 218 ASSERT_FALSE(pack.GetStringPiece(10, &data)); |
216 } | 219 } |
217 #endif | 220 #endif |
218 | 221 |
219 } // namespace ui | 222 } // namespace ui |
OLD | NEW |