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