Home | History | Annotate | Download | only in resource
      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
      3 // found in the LICENSE file.
      4 
      5 #include "base/file_util.h"
      6 #include "base/files/file_path.h"
      7 #include "base/files/scoped_temp_dir.h"
      8 #include "base/path_service.h"
      9 #include "base/strings/string_piece.h"
     10 #include "testing/gtest/include/gtest/gtest.h"
     11 #include "ui/base/resource/data_pack.h"
     12 
     13 namespace ui {
     14 
     15 class DataPackTest
     16     : public testing::TestWithParam<DataPack::TextEncodingType> {
     17  public:
     18   DataPackTest() {}
     19 };
     20 
     21 extern const char kSamplePakContents[];
     22 extern const size_t kSamplePakSize;
     23 
     24 TEST(DataPackTest, LoadFromPath) {
     25   base::ScopedTempDir dir;
     26   ASSERT_TRUE(dir.CreateUniqueTempDir());
     27   base::FilePath data_path = dir.path().Append(FILE_PATH_LITERAL("sample.pak"));
     28 
     29   // Dump contents into the pak file.
     30   ASSERT_EQ(file_util::WriteFile(data_path, kSamplePakContents, kSamplePakSize),
     31             static_cast<int>(kSamplePakSize));
     32 
     33   // Load the file through the data pack API.
     34   DataPack pack(SCALE_FACTOR_100P);
     35   ASSERT_TRUE(pack.LoadFromPath(data_path));
     36 
     37   base::StringPiece data;
     38   ASSERT_TRUE(pack.HasResource(4));
     39   ASSERT_TRUE(pack.GetStringPiece(4, &data));
     40   EXPECT_EQ("this is id 4", data);
     41   ASSERT_TRUE(pack.HasResource(6));
     42   ASSERT_TRUE(pack.GetStringPiece(6, &data));
     43   EXPECT_EQ("this is id 6", data);
     44 
     45   // Try reading zero-length data blobs, just in case.
     46   ASSERT_TRUE(pack.GetStringPiece(1, &data));
     47   EXPECT_EQ(0U, data.length());
     48   ASSERT_TRUE(pack.GetStringPiece(10, &data));
     49   EXPECT_EQ(0U, data.length());
     50 
     51   // Try looking up an invalid key.
     52   ASSERT_FALSE(pack.HasResource(140));
     53   ASSERT_FALSE(pack.GetStringPiece(140, &data));
     54 }
     55 
     56 TEST(DataPackTest, LoadFromFile) {
     57   base::ScopedTempDir dir;
     58   ASSERT_TRUE(dir.CreateUniqueTempDir());
     59   base::FilePath data_path = dir.path().Append(FILE_PATH_LITERAL("sample.pak"));
     60 
     61   // Dump contents into the pak file.
     62   ASSERT_EQ(file_util::WriteFile(data_path, kSamplePakContents, kSamplePakSize),
     63             static_cast<int>(kSamplePakSize));
     64 
     65   bool created = false;
     66   base::PlatformFileError error_code = base::PLATFORM_FILE_OK;
     67   base::PlatformFile file = base::CreatePlatformFile(
     68       data_path, base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_READ,
     69       &created, &error_code);
     70 
     71   // Load the file through the data pack API.
     72   DataPack pack(SCALE_FACTOR_100P);
     73   ASSERT_TRUE(pack.LoadFromFile(file));
     74 
     75   base::StringPiece data;
     76   ASSERT_TRUE(pack.HasResource(4));
     77   ASSERT_TRUE(pack.GetStringPiece(4, &data));
     78   EXPECT_EQ("this is id 4", data);
     79   ASSERT_TRUE(pack.HasResource(6));
     80   ASSERT_TRUE(pack.GetStringPiece(6, &data));
     81   EXPECT_EQ("this is id 6", data);
     82 
     83   // Try reading zero-length data blobs, just in case.
     84   ASSERT_TRUE(pack.GetStringPiece(1, &data));
     85   EXPECT_EQ(0U, data.length());
     86   ASSERT_TRUE(pack.GetStringPiece(10, &data));
     87   EXPECT_EQ(0U, data.length());
     88 
     89   // Try looking up an invalid key.
     90   ASSERT_FALSE(pack.HasResource(140));
     91   ASSERT_FALSE(pack.GetStringPiece(140, &data));
     92 
     93   base::ClosePlatformFile(file);
     94 }
     95 
     96 INSTANTIATE_TEST_CASE_P(WriteBINARY, DataPackTest, ::testing::Values(
     97     DataPack::BINARY));
     98 INSTANTIATE_TEST_CASE_P(WriteUTF8, DataPackTest, ::testing::Values(
     99     DataPack::UTF8));
    100 INSTANTIATE_TEST_CASE_P(WriteUTF16, DataPackTest, ::testing::Values(
    101     DataPack::UTF16));
    102 
    103 TEST(DataPackTest, LoadFileWithTruncatedHeader) {
    104   base::FilePath data_path;
    105   PathService::Get(base::DIR_SOURCE_ROOT, &data_path);
    106   data_path = data_path.Append(FILE_PATH_LITERAL(
    107       "ui/base/test/data/data_pack_unittest/truncated-header.pak"));
    108 
    109   DataPack pack(SCALE_FACTOR_100P);
    110   ASSERT_FALSE(pack.LoadFromPath(data_path));
    111 }
    112 
    113 TEST_P(DataPackTest, Write) {
    114   base::ScopedTempDir dir;
    115   ASSERT_TRUE(dir.CreateUniqueTempDir());
    116   base::FilePath file = dir.path().Append(FILE_PATH_LITERAL("data.pak"));
    117 
    118   std::string one("one");
    119   std::string two("two");
    120   std::string three("three");
    121   std::string four("four");
    122   std::string fifteen("fifteen");
    123 
    124   std::map<uint16, base::StringPiece> resources;
    125   resources.insert(std::make_pair(1, base::StringPiece(one)));
    126   resources.insert(std::make_pair(2, base::StringPiece(two)));
    127   resources.insert(std::make_pair(15, base::StringPiece(fifteen)));
    128   resources.insert(std::make_pair(3, base::StringPiece(three)));
    129   resources.insert(std::make_pair(4, base::StringPiece(four)));
    130   ASSERT_TRUE(DataPack::WritePack(file, resources, GetParam()));
    131 
    132   // Now try to read the data back in.
    133   DataPack pack(SCALE_FACTOR_100P);
    134   ASSERT_TRUE(pack.LoadFromPath(file));
    135   EXPECT_EQ(pack.GetTextEncodingType(), GetParam());
    136 
    137   base::StringPiece data;
    138   ASSERT_TRUE(pack.GetStringPiece(1, &data));
    139   EXPECT_EQ(one, data);
    140   ASSERT_TRUE(pack.GetStringPiece(2, &data));
    141   EXPECT_EQ(two, data);
    142   ASSERT_TRUE(pack.GetStringPiece(3, &data));
    143   EXPECT_EQ(three, data);
    144   ASSERT_TRUE(pack.GetStringPiece(4, &data));
    145   EXPECT_EQ(four, data);
    146   ASSERT_TRUE(pack.GetStringPiece(15, &data));
    147   EXPECT_EQ(fifteen, data);
    148 }
    149 
    150 }  // namespace ui
    151