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 "base/file_path.h" | 5 #include "base/file_path.h" |
6 #include "base/file_util.h" | 6 #include "base/file_util.h" |
| 7 #include "base/files/scoped_temp_dir.h" |
7 #include "base/path_service.h" | 8 #include "base/path_service.h" |
8 #include "base/scoped_temp_dir.h" | |
9 #include "base/string_piece.h" | 9 #include "base/string_piece.h" |
10 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
11 #include "ui/base/resource/data_pack.h" | 11 #include "ui/base/resource/data_pack.h" |
12 | 12 |
13 namespace ui { | 13 namespace ui { |
14 | 14 |
15 class DataPackTest | 15 class DataPackTest |
16 : public testing::TestWithParam<DataPack::TextEncodingType> { | 16 : public testing::TestWithParam<DataPack::TextEncodingType> { |
17 public: | 17 public: |
18 DataPackTest() {} | 18 DataPackTest() {} |
19 }; | 19 }; |
20 | 20 |
21 extern const char kSamplePakContents[]; | 21 extern const char kSamplePakContents[]; |
22 extern const size_t kSamplePakSize; | 22 extern const size_t kSamplePakSize; |
23 | 23 |
24 TEST(DataPackTest, LoadFromPath) { | 24 TEST(DataPackTest, LoadFromPath) { |
25 ScopedTempDir dir; | 25 base::ScopedTempDir dir; |
26 ASSERT_TRUE(dir.CreateUniqueTempDir()); | 26 ASSERT_TRUE(dir.CreateUniqueTempDir()); |
27 FilePath data_path = dir.path().Append(FILE_PATH_LITERAL("sample.pak")); | 27 FilePath data_path = dir.path().Append(FILE_PATH_LITERAL("sample.pak")); |
28 | 28 |
29 // Dump contents into the pak file. | 29 // Dump contents into the pak file. |
30 ASSERT_EQ(file_util::WriteFile(data_path, kSamplePakContents, kSamplePakSize), | 30 ASSERT_EQ(file_util::WriteFile(data_path, kSamplePakContents, kSamplePakSize), |
31 static_cast<int>(kSamplePakSize)); | 31 static_cast<int>(kSamplePakSize)); |
32 | 32 |
33 // Load the file through the data pack API. | 33 // Load the file through the data pack API. |
34 DataPack pack(SCALE_FACTOR_100P); | 34 DataPack pack(SCALE_FACTOR_100P); |
35 ASSERT_TRUE(pack.LoadFromPath(data_path)); | 35 ASSERT_TRUE(pack.LoadFromPath(data_path)); |
(...skipping 11 matching lines...) Expand all Loading... |
47 EXPECT_EQ(0U, data.length()); | 47 EXPECT_EQ(0U, data.length()); |
48 ASSERT_TRUE(pack.GetStringPiece(10, &data)); | 48 ASSERT_TRUE(pack.GetStringPiece(10, &data)); |
49 EXPECT_EQ(0U, data.length()); | 49 EXPECT_EQ(0U, data.length()); |
50 | 50 |
51 // Try looking up an invalid key. | 51 // Try looking up an invalid key. |
52 ASSERT_FALSE(pack.HasResource(140)); | 52 ASSERT_FALSE(pack.HasResource(140)); |
53 ASSERT_FALSE(pack.GetStringPiece(140, &data)); | 53 ASSERT_FALSE(pack.GetStringPiece(140, &data)); |
54 } | 54 } |
55 | 55 |
56 TEST(DataPackTest, LoadFromFile) { | 56 TEST(DataPackTest, LoadFromFile) { |
57 ScopedTempDir dir; | 57 base::ScopedTempDir dir; |
58 ASSERT_TRUE(dir.CreateUniqueTempDir()); | 58 ASSERT_TRUE(dir.CreateUniqueTempDir()); |
59 FilePath data_path = dir.path().Append(FILE_PATH_LITERAL("sample.pak")); | 59 FilePath data_path = dir.path().Append(FILE_PATH_LITERAL("sample.pak")); |
60 | 60 |
61 // Dump contents into the pak file. | 61 // Dump contents into the pak file. |
62 ASSERT_EQ(file_util::WriteFile(data_path, kSamplePakContents, kSamplePakSize), | 62 ASSERT_EQ(file_util::WriteFile(data_path, kSamplePakContents, kSamplePakSize), |
63 static_cast<int>(kSamplePakSize)); | 63 static_cast<int>(kSamplePakSize)); |
64 | 64 |
65 bool created = false; | 65 bool created = false; |
66 base::PlatformFileError error_code = base::PLATFORM_FILE_OK; | 66 base::PlatformFileError error_code = base::PLATFORM_FILE_OK; |
67 base::PlatformFile file = base::CreatePlatformFile( | 67 base::PlatformFile file = base::CreatePlatformFile( |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
104 FilePath data_path; | 104 FilePath data_path; |
105 PathService::Get(base::DIR_SOURCE_ROOT, &data_path); | 105 PathService::Get(base::DIR_SOURCE_ROOT, &data_path); |
106 data_path = data_path.Append(FILE_PATH_LITERAL( | 106 data_path = data_path.Append(FILE_PATH_LITERAL( |
107 "ui/base/test/data/data_pack_unittest/truncated-header.pak")); | 107 "ui/base/test/data/data_pack_unittest/truncated-header.pak")); |
108 | 108 |
109 DataPack pack(SCALE_FACTOR_100P); | 109 DataPack pack(SCALE_FACTOR_100P); |
110 ASSERT_FALSE(pack.LoadFromPath(data_path)); | 110 ASSERT_FALSE(pack.LoadFromPath(data_path)); |
111 } | 111 } |
112 | 112 |
113 TEST_P(DataPackTest, Write) { | 113 TEST_P(DataPackTest, Write) { |
114 ScopedTempDir dir; | 114 base::ScopedTempDir dir; |
115 ASSERT_TRUE(dir.CreateUniqueTempDir()); | 115 ASSERT_TRUE(dir.CreateUniqueTempDir()); |
116 FilePath file = dir.path().Append(FILE_PATH_LITERAL("data.pak")); | 116 FilePath file = dir.path().Append(FILE_PATH_LITERAL("data.pak")); |
117 | 117 |
118 std::string one("one"); | 118 std::string one("one"); |
119 std::string two("two"); | 119 std::string two("two"); |
120 std::string three("three"); | 120 std::string three("three"); |
121 std::string four("four"); | 121 std::string four("four"); |
122 std::string fifteen("fifteen"); | 122 std::string fifteen("fifteen"); |
123 | 123 |
124 std::map<uint16, base::StringPiece> resources; | 124 std::map<uint16, base::StringPiece> resources; |
(...skipping 16 matching lines...) Expand all Loading... |
141 EXPECT_EQ(two, data); | 141 EXPECT_EQ(two, data); |
142 ASSERT_TRUE(pack.GetStringPiece(3, &data)); | 142 ASSERT_TRUE(pack.GetStringPiece(3, &data)); |
143 EXPECT_EQ(three, data); | 143 EXPECT_EQ(three, data); |
144 ASSERT_TRUE(pack.GetStringPiece(4, &data)); | 144 ASSERT_TRUE(pack.GetStringPiece(4, &data)); |
145 EXPECT_EQ(four, data); | 145 EXPECT_EQ(four, data); |
146 ASSERT_TRUE(pack.GetStringPiece(15, &data)); | 146 ASSERT_TRUE(pack.GetStringPiece(15, &data)); |
147 EXPECT_EQ(fifteen, data); | 147 EXPECT_EQ(fifteen, data); |
148 } | 148 } |
149 | 149 |
150 } // namespace ui | 150 } // namespace ui |
OLD | NEW |