Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(684)

Side by Side Diff: ui/base/resource/resource_bundle_mac_unittest.mm

Issue 2209363002: Remove material design resource pak infrastructure (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: remove another mac test file Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « ui/base/resource/resource_bundle_mac.mm ('k') | ui/base/resource/resource_bundle_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2015 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 "ui/base/resource/resource_bundle.h"
6
7 #import <AppKit/AppKit.h>
8 #include <stddef.h>
9 #include <stdint.h>
10
11 #include "base/base_paths.h"
12 #include "base/big_endian.h"
13 #include "base/files/file_path.h"
14 #include "base/files/file_util.h"
15 #include "base/files/scoped_temp_dir.h"
16 #include "base/logging.h"
17 #include "base/macros.h"
18 #include "base/memory/ref_counted_memory.h"
19 #include "testing/gmock/include/gmock/gmock.h"
20 #include "testing/gtest/include/gtest/gtest.h"
21 #include "third_party/skia/include/core/SkBitmap.h"
22 #include "ui/base/resource/data_pack.h"
23 #include "ui/gfx/codec/png_codec.h"
24 #include "ui/gfx/image/image_skia.h"
25 #include "ui/resources/grit/ui_resources.h"
26 #include "ui/strings/grit/app_locale_settings.h"
27
28 namespace ui {
29
30 extern const char kEmptyPakContents[];
31 extern const size_t kEmptyPakSize;
32
33 namespace {
34
35 const unsigned char kPngMagic[8] = { 0x89, 'P', 'N', 'G', 13, 10, 26, 10 };
36 const size_t kPngChunkMetadataSize = 12;
37 const unsigned char kPngIHDRChunkType[4] = { 'I', 'H', 'D', 'R' };
38
39 // Returns |bitmap_data| with |custom_chunk| inserted after the IHDR chunk.
40 void AddCustomChunk(const base::StringPiece& custom_chunk,
41 std::vector<unsigned char>* bitmap_data) {
42 EXPECT_LT(arraysize(kPngMagic) + kPngChunkMetadataSize, bitmap_data->size());
43 EXPECT_TRUE(std::equal(
44 bitmap_data->begin(),
45 bitmap_data->begin() + arraysize(kPngMagic),
46 kPngMagic));
47 std::vector<unsigned char>::iterator ihdr_start =
48 bitmap_data->begin() + arraysize(kPngMagic);
49 char ihdr_length_data[sizeof(uint32_t)];
50 for (size_t i = 0; i < sizeof(uint32_t); ++i)
51 ihdr_length_data[i] = *(ihdr_start + i);
52 uint32_t ihdr_chunk_length = 0;
53 base::ReadBigEndian(reinterpret_cast<char*>(ihdr_length_data),
54 &ihdr_chunk_length);
55 EXPECT_TRUE(
56 std::equal(ihdr_start + sizeof(uint32_t),
57 ihdr_start + sizeof(uint32_t) + sizeof(kPngIHDRChunkType),
58 kPngIHDRChunkType));
59
60 bitmap_data->insert(ihdr_start + kPngChunkMetadataSize + ihdr_chunk_length,
61 custom_chunk.begin(), custom_chunk.end());
62 }
63
64 // Creates datapack at |path| with a single bitmap at resource ID 3
65 // which is |edge_size|x|edge_size| pixels.
66 // If |custom_chunk| is non empty, adds it after the IHDR chunk
67 // in the encoded bitmap data.
68 void CreateDataPackWithSingleBitmap(const base::FilePath& path,
69 int edge_size,
70 const base::StringPiece& custom_chunk) {
71 SkBitmap bitmap;
72 bitmap.allocN32Pixels(edge_size, edge_size);
73 bitmap.eraseColor(SK_ColorWHITE);
74 std::vector<unsigned char> bitmap_data;
75 EXPECT_TRUE(gfx::PNGCodec::EncodeBGRASkBitmap(bitmap, false, &bitmap_data));
76
77 if (custom_chunk.size() > 0)
78 AddCustomChunk(custom_chunk, &bitmap_data);
79
80 std::map<uint16_t, base::StringPiece> resources;
81 resources[3u] = base::StringPiece(
82 reinterpret_cast<const char*>(&bitmap_data[0]), bitmap_data.size());
83 DataPack::WritePack(path, resources, ui::DataPack::BINARY);
84 }
85
86 } // namespace
87
88 class ResourceBundleMacImageTest : public testing::Test {
89 public:
90 ResourceBundleMacImageTest() : resource_bundle_(NULL) {}
91
92 ~ResourceBundleMacImageTest() override {}
93
94 void SetUp() override {
95 // Create a temporary directory to write test resource bundles to.
96 ASSERT_TRUE(dir_.CreateUniqueTempDir());
97 }
98
99 // Overridden from testing::Test:
100 void TearDown() override { delete resource_bundle_; }
101
102 // Returns new ResoureBundle with the specified |delegate|. The
103 // ResourceBundleTest class manages the lifetime of the returned
104 // ResourceBundle.
105 ResourceBundle* CreateResourceBundle(ResourceBundle::Delegate* delegate) {
106 DCHECK(!resource_bundle_);
107
108 resource_bundle_ = new ResourceBundle(delegate);
109 return resource_bundle_;
110 }
111
112 // Returns resource bundle which uses an empty data pak for locale data.
113 ui::ResourceBundle* CreateResourceBundleWithEmptyLocalePak() {
114 // Write an empty data pak for locale data.
115 const base::FilePath& locale_path = dir_path().Append(
116 FILE_PATH_LITERAL("locale.pak"));
117 EXPECT_EQ(base::WriteFile(locale_path, kEmptyPakContents, kEmptyPakSize),
118 static_cast<int>(kEmptyPakSize));
119
120 ui::ResourceBundle* resource_bundle = CreateResourceBundle(NULL);
121
122 // Load the empty locale data pak.
123 resource_bundle->LoadTestResources(base::FilePath(), locale_path);
124 return resource_bundle;
125 }
126
127 // Returns the path of temporary directory to write test data packs into.
128 const base::FilePath& dir_path() { return dir_.path(); }
129
130 // Returns the number of DataPacks managed by |resource_bundle| which are
131 // flagged as containing only material design resources.
132 size_t NumberOfMaterialDesignDataPacksInResourceBundle(
133 ResourceBundle* resource_bundle) {
134 DCHECK(resource_bundle);
135 size_t num_material_packs = 0;
136 for (size_t i = 0; i < resource_bundle->data_packs_.size(); i++) {
137 if (resource_bundle->data_packs_[i]->HasOnlyMaterialDesignAssets())
138 num_material_packs++;
139 }
140
141 return num_material_packs;
142 }
143
144 protected:
145 ResourceBundle* resource_bundle_;
146
147 private:
148 std::unique_ptr<DataPack> locale_pack_;
149 base::ScopedTempDir dir_;
150
151 DISALLOW_COPY_AND_ASSIGN(ResourceBundleMacImageTest);
152 };
153
154 // Verifies that ResourceBundle searches the Material Design data pack before
155 // the default data pack, and that the returned image contains only the
156 // representation from the Material Design pack.
157 TEST_F(ResourceBundleMacImageTest, CheckImageFromMaterialDesign) {
158 // Create two .pak files, each containing a single image with the
159 // same asset ID but different sizes (note that the images must be
160 // different sizes in this test in order to correctly determine
161 // from which data pack the asset was pulled). Note also that the value
162 // of |material_size| was chosen to be divisible by 3, since iOS may
163 // use this scale factor.
164 const int default_size = 10;
165 const int material_size = 48;
166 ASSERT_NE(default_size, material_size);
167 base::FilePath default_path = dir_path().AppendASCII("default.pak");
168 base::FilePath material_path = dir_path().AppendASCII("material.pak");
169 CreateDataPackWithSingleBitmap(default_path,
170 default_size,
171 base::StringPiece());
172 CreateDataPackWithSingleBitmap(material_path,
173 material_size,
174 base::StringPiece());
175
176 ScaleFactor scale_factor = SCALE_FACTOR_100P;
177 ResourceBundle* resource_bundle = CreateResourceBundleWithEmptyLocalePak();
178
179 // Load the 'material' data pack into ResourceBundle first.
180 resource_bundle->AddMaterialDesignDataPackFromPath(material_path,
181 scale_factor);
182 resource_bundle->AddDataPackFromPath(default_path, scale_factor);
183
184 // Confirm that there are two data packs available.
185 const unsigned int data_packs_size = resource_bundle->data_packs_.size();
186 const unsigned int expected_size = 2;
187 EXPECT_EQ(expected_size, data_packs_size);
188
189 const size_t md_data_pack_count =
190 NumberOfMaterialDesignDataPacksInResourceBundle(resource_bundle);
191 const size_t expected_pack_count = 1;
192 EXPECT_EQ(expected_pack_count, md_data_pack_count);
193
194 // Normally with two packs containing the same image, GetNativeImageNamed()
195 // returns an NSImage that contains both representations. With the Material
196 // Design pack, any images it contains should override the ones in the default
197 // pack, so if both packs contain the same image, the returned NSImage should
198 // contain just a single representation.
199 NSImage* icon = resource_bundle->GetNativeImageNamed(3).ToNSImage();
200 const unsigned long representations_count = [[icon representations] count];
201 const unsigned long expected_count = 1;
202 EXPECT_EQ(expected_count, representations_count);
203 }
204
205 } // namespace ui
OLDNEW
« no previous file with comments | « ui/base/resource/resource_bundle_mac.mm ('k') | ui/base/resource/resource_bundle_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698