| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/path_service.h" | 7 #include "base/path_service.h" |
| 8 #include "base/scoped_temp_dir.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 | |
| 16 : public testing::TestWithParam<DataPack::TextEncodingType> { | |
| 17 public: | |
| 18 DataPackTest() {} | |
| 19 }; | |
| 20 | |
| 21 extern const char kSamplePakContents[]; | 15 extern const char kSamplePakContents[]; |
| 22 extern const size_t kSamplePakSize; | 16 extern const size_t kSamplePakSize; |
| 23 | 17 |
| 24 TEST(DataPackTest, Load) { | 18 TEST(DataPackTest, Load) { |
| 25 ScopedTempDir dir; | 19 ScopedTempDir dir; |
| 26 ASSERT_TRUE(dir.CreateUniqueTempDir()); | 20 ASSERT_TRUE(dir.CreateUniqueTempDir()); |
| 27 FilePath data_path = dir.path().Append(FILE_PATH_LITERAL("sample.pak")); | 21 FilePath data_path = dir.path().Append(FILE_PATH_LITERAL("sample.pak")); |
| 28 | 22 |
| 29 // Dump contents into the pak file. | 23 // Dump contents into the pak file. |
| 30 ASSERT_EQ(file_util::WriteFile(data_path, kSamplePakContents, kSamplePakSize), | 24 ASSERT_EQ(file_util::WriteFile(data_path, kSamplePakContents, kSamplePakSize), |
| (...skipping 12 matching lines...) Expand all Loading... |
| 43 // Try reading zero-length data blobs, just in case. | 37 // Try reading zero-length data blobs, just in case. |
| 44 ASSERT_TRUE(pack.GetStringPiece(1, &data)); | 38 ASSERT_TRUE(pack.GetStringPiece(1, &data)); |
| 45 EXPECT_EQ(0U, data.length()); | 39 EXPECT_EQ(0U, data.length()); |
| 46 ASSERT_TRUE(pack.GetStringPiece(10, &data)); | 40 ASSERT_TRUE(pack.GetStringPiece(10, &data)); |
| 47 EXPECT_EQ(0U, data.length()); | 41 EXPECT_EQ(0U, data.length()); |
| 48 | 42 |
| 49 // Try looking up an invalid key. | 43 // Try looking up an invalid key. |
| 50 ASSERT_FALSE(pack.GetStringPiece(140, &data)); | 44 ASSERT_FALSE(pack.GetStringPiece(140, &data)); |
| 51 } | 45 } |
| 52 | 46 |
| 53 INSTANTIATE_TEST_CASE_P(WriteBINARY, DataPackTest, ::testing::Values( | |
| 54 DataPack::BINARY)); | |
| 55 INSTANTIATE_TEST_CASE_P(WriteUTF8, DataPackTest, ::testing::Values( | |
| 56 DataPack::UTF8)); | |
| 57 INSTANTIATE_TEST_CASE_P(WriteUTF16, DataPackTest, ::testing::Values( | |
| 58 DataPack::UTF16)); | |
| 59 | |
| 60 TEST(DataPackTest, LoadFileWithTruncatedHeader) { | 47 TEST(DataPackTest, LoadFileWithTruncatedHeader) { |
| 61 FilePath data_path; | 48 FilePath data_path; |
| 62 PathService::Get(base::DIR_SOURCE_ROOT, &data_path); | 49 PathService::Get(base::DIR_SOURCE_ROOT, &data_path); |
| 63 data_path = data_path.Append(FILE_PATH_LITERAL( | 50 data_path = data_path.Append(FILE_PATH_LITERAL( |
| 64 "ui/base/test/data/data_pack_unittest/truncated-header.pak")); | 51 "ui/base/test/data/data_pack_unittest/truncated-header.pak")); |
| 65 | 52 |
| 66 DataPack pack; | 53 DataPack pack; |
| 67 ASSERT_FALSE(pack.Load(data_path)); | 54 ASSERT_FALSE(pack.Load(data_path)); |
| 68 } | 55 } |
| 69 | 56 |
| 70 TEST_P(DataPackTest, Write) { | 57 TEST(DataPackTest, Write) { |
| 71 ScopedTempDir dir; | 58 ScopedTempDir dir; |
| 72 ASSERT_TRUE(dir.CreateUniqueTempDir()); | 59 ASSERT_TRUE(dir.CreateUniqueTempDir()); |
| 73 FilePath file = dir.path().Append(FILE_PATH_LITERAL("data.pak")); | 60 FilePath file = dir.path().Append(FILE_PATH_LITERAL("data.pak")); |
| 74 | 61 |
| 75 std::string one("one"); | 62 std::string one("one"); |
| 76 std::string two("two"); | 63 std::string two("two"); |
| 77 std::string three("three"); | 64 std::string three("three"); |
| 78 std::string four("four"); | 65 std::string four("four"); |
| 79 std::string fifteen("fifteen"); | 66 std::string fifteen("fifteen"); |
| 80 | 67 |
| 81 std::map<uint16, base::StringPiece> resources; | 68 std::map<uint16, base::StringPiece> resources; |
| 82 resources.insert(std::make_pair(1, base::StringPiece(one))); | 69 resources.insert(std::make_pair(1, base::StringPiece(one))); |
| 83 resources.insert(std::make_pair(2, base::StringPiece(two))); | 70 resources.insert(std::make_pair(2, base::StringPiece(two))); |
| 84 resources.insert(std::make_pair(15, base::StringPiece(fifteen))); | 71 resources.insert(std::make_pair(15, base::StringPiece(fifteen))); |
| 85 resources.insert(std::make_pair(3, base::StringPiece(three))); | 72 resources.insert(std::make_pair(3, base::StringPiece(three))); |
| 86 resources.insert(std::make_pair(4, base::StringPiece(four))); | 73 resources.insert(std::make_pair(4, base::StringPiece(four))); |
| 87 ASSERT_TRUE(DataPack::WritePack(file, resources, GetParam())); | 74 ASSERT_TRUE(DataPack::WritePack(file, resources)); |
| 88 | 75 |
| 89 // Now try to read the data back in. | 76 // Now try to read the data back in. |
| 90 DataPack pack; | 77 DataPack pack; |
| 91 ASSERT_TRUE(pack.Load(file)); | 78 ASSERT_TRUE(pack.Load(file)); |
| 92 EXPECT_EQ(pack.GetTextEncodingType(), GetParam()); | |
| 93 | 79 |
| 94 base::StringPiece data; | 80 base::StringPiece data; |
| 95 ASSERT_TRUE(pack.GetStringPiece(1, &data)); | 81 ASSERT_TRUE(pack.GetStringPiece(1, &data)); |
| 96 EXPECT_EQ(one, data); | 82 EXPECT_EQ(one, data); |
| 97 ASSERT_TRUE(pack.GetStringPiece(2, &data)); | 83 ASSERT_TRUE(pack.GetStringPiece(2, &data)); |
| 98 EXPECT_EQ(two, data); | 84 EXPECT_EQ(two, data); |
| 99 ASSERT_TRUE(pack.GetStringPiece(3, &data)); | 85 ASSERT_TRUE(pack.GetStringPiece(3, &data)); |
| 100 EXPECT_EQ(three, data); | 86 EXPECT_EQ(three, data); |
| 101 ASSERT_TRUE(pack.GetStringPiece(4, &data)); | 87 ASSERT_TRUE(pack.GetStringPiece(4, &data)); |
| 102 EXPECT_EQ(four, data); | 88 EXPECT_EQ(four, data); |
| 103 ASSERT_TRUE(pack.GetStringPiece(15, &data)); | 89 ASSERT_TRUE(pack.GetStringPiece(15, &data)); |
| 104 EXPECT_EQ(fifteen, data); | 90 EXPECT_EQ(fifteen, data); |
| 105 } | 91 } |
| 106 | 92 |
| 107 } // namespace ui | 93 } // namespace ui |
| OLD | NEW |