1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ui/base/resource/resource_bundle.h"
#include "base/base_paths.h"
#include "base/file_path.h"
#include "base/file_util.h"
#include "base/logging.h"
#include "base/memory/ref_counted_memory.h"
#include "base/path_service.h"
#include "base/scoped_temp_dir.h"
#include "base/utf_string_conversions.h"
#include "net/base/big_endian.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "ui/base/layout.h"
#include "ui/base/resource/data_pack.h"
#include "ui/gfx/codec/png_codec.h"
#include "ui/gfx/image/image_skia.h"
#include "grit/ui_resources.h"
using ::testing::_;
using ::testing::Between;
using ::testing::Property;
using ::testing::Return;
using ::testing::ReturnArg;
namespace ui {
extern const char kSamplePakContents[];
extern const size_t kSamplePakSize;
extern const char kSamplePakContents2x[];
extern const size_t kSamplePakSize2x;
extern const char kEmptyPakContents[];
extern const size_t kEmptyPakSize;
namespace {
const unsigned char kPngMagic[8] = { 0x89, 'P', 'N', 'G', 13, 10, 26, 10 };
const size_t kPngChunkMetadataSize = 12;
const unsigned char kPngIHDRChunkType[4] = { 'I', 'H', 'D', 'R' };
// Custom chunk that GRIT adds to PNG to indicate that it could not find a
// bitmap at the requested scale factor and fell back to 1x.
const unsigned char kPngScaleChunk[12] = { 0x00, 0x00, 0x00, 0x00,
'c', 's', 'C', 'l',
0xc1, 0x30, 0x60, 0x4d };
// Mock for the ResourceBundle::Delegate class.
class MockResourceBundleDelegate : public ui::ResourceBundle::Delegate {
public:
MockResourceBundleDelegate() {
}
virtual ~MockResourceBundleDelegate() {
}
MOCK_METHOD2(GetPathForResourcePack, FilePath(const FilePath& pack_path,
ui::ScaleFactor scale_factor));
MOCK_METHOD2(GetPathForLocalePack, FilePath(const FilePath& pack_path,
const std::string& locale));
MOCK_METHOD1(GetImageNamed, gfx::Image(int resource_id));
MOCK_METHOD2(GetNativeImageNamed,
gfx::Image(int resource_id,
ui::ResourceBundle::ImageRTL rtl));
MOCK_METHOD2(LoadDataResourceBytes,
base::RefCountedStaticMemory*(int resource_id,
ui::ScaleFactor scale_factor));
MOCK_METHOD2(GetRawDataResourceMock, base::StringPiece(
int resource_id,
ui::ScaleFactor scale_factor));
virtual bool GetRawDataResource(int resource_id,
ui::ScaleFactor scale_factor,
base::StringPiece* value) OVERRIDE {
*value = GetRawDataResourceMock(resource_id, scale_factor);
return true;
}
MOCK_METHOD1(GetLocalizedStringMock, string16(int message_id));
virtual bool GetLocalizedString(int message_id, string16* value) OVERRIDE {
*value = GetLocalizedStringMock(message_id);
return true;
}
MOCK_METHOD1(GetFontMock, gfx::Font*(ui::ResourceBundle::FontStyle style));
virtual scoped_ptr<gfx::Font> GetFont(
ui::ResourceBundle::FontStyle style) OVERRIDE {
return scoped_ptr<gfx::Font>(GetFontMock(style));
}
};
// Returns |bitmap_data| with |custom_chunk| inserted after the IHDR chunk.
void AddCustomChunk(const base::StringPiece& custom_chunk,
std::vector<unsigned char>* bitmap_data) {
EXPECT_LT(arraysize(kPngMagic) + kPngChunkMetadataSize, bitmap_data->size());
EXPECT_TRUE(std::equal(
bitmap_data->begin(),
bitmap_data->begin() + arraysize(kPngMagic),
kPngMagic));
std::vector<unsigned char>::iterator ihdr_start =
bitmap_data->begin() + arraysize(kPngMagic);
char ihdr_length_data[sizeof(uint32)];
for (size_t i = 0; i < sizeof(uint32); ++i)
ihdr_length_data[i] = *(ihdr_start + i);
uint32 ihdr_chunk_length = 0;
net::ReadBigEndian(reinterpret_cast<char*>(ihdr_length_data),
&ihdr_chunk_length);
EXPECT_TRUE(std::equal(
ihdr_start + sizeof(uint32),
ihdr_start + sizeof(uint32) + sizeof(kPngIHDRChunkType),
kPngIHDRChunkType));
bitmap_data->insert(ihdr_start + kPngChunkMetadataSize + ihdr_chunk_length,
custom_chunk.begin(), custom_chunk.end());
}
// Creates datapack at |path| with a single bitmap at resource ID 3
// which is |edge_size|x|edge_size| pixels.
// If |custom_chunk| is non empty, adds it after the IHDR chunk
// in the encoded bitmap data.
void CreateDataPackWithSingleBitmap(const FilePath& path,
int edge_size,
const base::StringPiece& custom_chunk) {
SkBitmap bitmap;
bitmap.setConfig(SkBitmap::kARGB_8888_Config, edge_size, edge_size);
bitmap.allocPixels();
bitmap.eraseColor(SK_ColorWHITE);
std::vector<unsigned char> bitmap_data;
EXPECT_TRUE(gfx::PNGCodec::EncodeBGRASkBitmap(bitmap, false, &bitmap_data));
if (custom_chunk.size() > 0)
AddCustomChunk(custom_chunk, &bitmap_data);
std::map<uint16, base::StringPiece> resources;
resources[3u] = base::StringPiece(
reinterpret_cast<const char*>(&bitmap_data[0]), bitmap_data.size());
DataPack::WritePack(path, resources, ui::DataPack::BINARY);
}
} // namespace
class ResourceBundleTest : public testing::Test {
public:
ResourceBundleTest() : resource_bundle_(NULL) {
}
virtual ~ResourceBundleTest() {
}
// Overridden from testing::Test:
virtual void TearDown() OVERRIDE {
delete resource_bundle_;
}
// Returns new ResoureBundle with the specified |delegate|. The
// ResourceBundleTest class manages the lifetime of the returned
// ResourceBundle.
ResourceBundle* CreateResourceBundle(ResourceBundle::Delegate* delegate) {
DCHECK(!resource_bundle_);
resource_bundle_ = new ResourceBundle(delegate);
return resource_bundle_;
}
protected:
ResourceBundle* resource_bundle_;
private:
DISALLOW_COPY_AND_ASSIGN(ResourceBundleTest);
};
TEST_F(ResourceBundleTest, DelegateGetPathForResourcePack) {
MockResourceBundleDelegate delegate;
ResourceBundle* resource_bundle = CreateResourceBundle(&delegate);
FilePath pack_path(FILE_PATH_LITERAL("/path/to/test_path.pak"));
ui::ScaleFactor pack_scale_factor = ui::SCALE_FACTOR_200P;
EXPECT_CALL(delegate,
GetPathForResourcePack(
Property(&FilePath::value, pack_path.value()),
pack_scale_factor))
.Times(1)
.WillOnce(Return(pack_path));
resource_bundle->AddDataPackFromPath(pack_path, pack_scale_factor);
}
TEST_F(ResourceBundleTest, DelegateGetPathForLocalePack) {
MockResourceBundleDelegate delegate;
ResourceBundle* resource_bundle = CreateResourceBundle(&delegate);
std::string locale = "en-US";
// Cancel the load.
EXPECT_CALL(delegate, GetPathForLocalePack(_, locale))
.Times(2)
.WillRepeatedly(Return(FilePath()))
.RetiresOnSaturation();
EXPECT_FALSE(resource_bundle->LocaleDataPakExists(locale));
EXPECT_EQ("", resource_bundle->LoadLocaleResources(locale));
// Allow the load to proceed.
EXPECT_CALL(delegate, GetPathForLocalePack(_, locale))
.Times(2)
.WillRepeatedly(ReturnArg<0>());
EXPECT_TRUE(resource_bundle->LocaleDataPakExists(locale));
EXPECT_EQ(locale, resource_bundle->LoadLocaleResources(locale));
}
TEST_F(ResourceBundleTest, DelegateGetImageNamed) {
MockResourceBundleDelegate delegate;
ResourceBundle* resource_bundle = CreateResourceBundle(&delegate);
gfx::Image empty_image = resource_bundle->GetEmptyImage();
int resource_id = 5;
EXPECT_CALL(delegate, GetImageNamed(resource_id))
.Times(1)
.WillOnce(Return(empty_image));
gfx::Image result = resource_bundle->GetImageNamed(resource_id);
EXPECT_EQ(empty_image.ToSkBitmap(), result.ToSkBitmap());
}
TEST_F(ResourceBundleTest, DelegateGetNativeImageNamed) {
MockResourceBundleDelegate delegate;
ResourceBundle* resource_bundle = CreateResourceBundle(&delegate);
gfx::Image empty_image = resource_bundle->GetEmptyImage();
int resource_id = 5;
// Some platforms delegate GetNativeImageNamed calls to GetImageNamed.
EXPECT_CALL(delegate, GetImageNamed(resource_id))
.Times(Between(0, 1))
.WillOnce(Return(empty_image));
EXPECT_CALL(delegate,
GetNativeImageNamed(resource_id, ui::ResourceBundle::RTL_DISABLED))
.Times(Between(0, 1))
.WillOnce(Return(empty_image));
gfx::Image result = resource_bundle->GetNativeImageNamed(resource_id);
EXPECT_EQ(empty_image.ToSkBitmap(), result.ToSkBitmap());
}
TEST_F(ResourceBundleTest, DelegateLoadDataResourceBytes) {
MockResourceBundleDelegate delegate;
ResourceBundle* resource_bundle = CreateResourceBundle(&delegate);
// Create the data resource for testing purposes.
unsigned char data[] = "My test data";
scoped_refptr<base::RefCountedStaticMemory> static_memory(
new base::RefCountedStaticMemory(data, sizeof(data)));
int resource_id = 5;
ui::ScaleFactor scale_factor = ui::SCALE_FACTOR_NONE;
EXPECT_CALL(delegate, LoadDataResourceBytes(resource_id, scale_factor))
.Times(1)
.WillOnce(Return(static_memory));
scoped_refptr<base::RefCountedStaticMemory> result =
resource_bundle->LoadDataResourceBytesForScale(resource_id, scale_factor);
EXPECT_EQ(static_memory, result);
}
TEST_F(ResourceBundleTest, DelegateGetRawDataResource) {
MockResourceBundleDelegate delegate;
ResourceBundle* resource_bundle = CreateResourceBundle(&delegate);
// Create the string piece for testing purposes.
char data[] = "My test data";
base::StringPiece string_piece(data);
int resource_id = 5;
EXPECT_CALL(delegate, GetRawDataResourceMock(
resource_id, ui::SCALE_FACTOR_NONE))
.Times(1)
.WillOnce(Return(string_piece));
base::StringPiece result = resource_bundle->GetRawDataResource(
resource_id);
EXPECT_EQ(string_piece.data(), result.data());
}
TEST_F(ResourceBundleTest, DelegateGetLocalizedString) {
MockResourceBundleDelegate delegate;
ResourceBundle* resource_bundle = CreateResourceBundle(&delegate);
string16 data = ASCIIToUTF16("My test data");
int resource_id = 5;
EXPECT_CALL(delegate, GetLocalizedStringMock(resource_id))
.Times(1)
.WillOnce(Return(data));
string16 result = resource_bundle->GetLocalizedString(resource_id);
EXPECT_EQ(data, result);
}
TEST_F(ResourceBundleTest, DelegateGetFont) {
MockResourceBundleDelegate delegate;
ResourceBundle* resource_bundle = CreateResourceBundle(&delegate);
// Should be called once for each font type. When we return NULL the default
// font will be created.
gfx::Font* test_font = NULL;
EXPECT_CALL(delegate, GetFontMock(_))
.Times(7)
.WillRepeatedly(Return(test_font));
const gfx::Font* font =
&resource_bundle->GetFont(ui::ResourceBundle::BaseFont);
EXPECT_TRUE(font);
}
TEST_F(ResourceBundleTest, LocaleDataPakExists) {
ResourceBundle* resource_bundle = CreateResourceBundle(NULL);
// Check that ResourceBundle::LocaleDataPakExists returns the correct results.
EXPECT_TRUE(resource_bundle->LocaleDataPakExists("en-US"));
EXPECT_FALSE(resource_bundle->LocaleDataPakExists("not_a_real_locale"));
}
class ResourceBundleImageTest : public ResourceBundleTest {
public:
ResourceBundleImageTest() : locale_pack_(NULL) {
}
virtual ~ResourceBundleImageTest() {
}
virtual void SetUp() OVERRIDE {
// Create a temporary directory to write test resource bundles to.
ASSERT_TRUE(dir_.CreateUniqueTempDir());
}
// Returns resource bundle which uses an empty data pak for locale data.
ui::ResourceBundle* CreateResourceBundleWithEmptyLocalePak() {
// Write an empty data pak for locale data.
const FilePath& locale_path = dir_path().Append(
FILE_PATH_LITERAL("locale.pak"));
EXPECT_EQ(file_util::WriteFile(locale_path, kEmptyPakContents,
kEmptyPakSize),
static_cast<int>(kEmptyPakSize));
ui::ResourceBundle* resource_bundle = CreateResourceBundle(NULL);
// Load the empty locale data pak.
resource_bundle->LoadTestResources(FilePath(), locale_path);
return resource_bundle;
}
// Returns the path of temporary directory to write test data packs into.
const FilePath& dir_path() { return dir_.path(); }
private:
scoped_ptr<DataPack> locale_pack_;
ScopedTempDir dir_;
DISALLOW_COPY_AND_ASSIGN(ResourceBundleImageTest);
};
// Verify that we don't crash when trying to load a resource that is not found.
// In some cases, we fail to mmap resources.pak, but try to keep going anyway.
TEST_F(ResourceBundleImageTest, LoadDataResourceBytes) {
FilePath data_path = dir_path().Append(FILE_PATH_LITERAL("sample.pak"));
// Dump contents into the pak files.
ASSERT_EQ(file_util::WriteFile(data_path, kEmptyPakContents,
kEmptyPakSize), static_cast<int>(kEmptyPakSize));
// Create a resource bundle from the file.
ResourceBundle* resource_bundle = CreateResourceBundleWithEmptyLocalePak();
resource_bundle->AddDataPackFromPath(data_path, SCALE_FACTOR_100P);
const int kUnfoundResourceId = 10000;
EXPECT_EQ(NULL, resource_bundle->LoadDataResourceBytes(
kUnfoundResourceId));
// Give a .pak file that doesn't exist so we will fail to load it.
resource_bundle->AddDataPackFromPath(
FilePath(FILE_PATH_LITERAL("non-existant-file.pak")),
ui::SCALE_FACTOR_NONE);
EXPECT_EQ(NULL, resource_bundle->LoadDataResourceBytes(
kUnfoundResourceId));
}
TEST_F(ResourceBundleImageTest, GetRawDataResource) {
FilePath data_path = dir_path().Append(FILE_PATH_LITERAL("sample.pak"));
FilePath data_2x_path = dir_path().Append(FILE_PATH_LITERAL("sample_2x.pak"));
// Dump contents into the pak files.
ASSERT_EQ(file_util::WriteFile(data_path, kSamplePakContents,
kSamplePakSize), static_cast<int>(kSamplePakSize));
ASSERT_EQ(file_util::WriteFile(data_2x_path, kSamplePakContents2x,
kSamplePakSize2x), static_cast<int>(kSamplePakSize2x));
// Load the regular and 2x pak files.
ResourceBundle* resource_bundle = CreateResourceBundleWithEmptyLocalePak();
resource_bundle->AddDataPackFromPath(data_path, SCALE_FACTOR_100P);
resource_bundle->AddDataPackFromPath(data_2x_path, SCALE_FACTOR_200P);
// Resource ID 4 exists in both 1x and 2x paks, so we expect a different
// result when requesting the 2x scale.
EXPECT_EQ("this is id 4", resource_bundle->GetRawDataResourceForScale(4,
SCALE_FACTOR_100P));
EXPECT_EQ("this is id 4 2x", resource_bundle->GetRawDataResourceForScale(4,
SCALE_FACTOR_200P));
// Resource ID 6 only exists in the 1x pak so we expect the same resource
// for both scale factor requests.
EXPECT_EQ("this is id 6", resource_bundle->GetRawDataResourceForScale(6,
SCALE_FACTOR_100P));
EXPECT_EQ("this is id 6", resource_bundle->GetRawDataResourceForScale(6,
SCALE_FACTOR_200P));
}
// Test requesting image reps at various scale factors from the image returned
// via ResourceBundle::GetImageNamed().
TEST_F(ResourceBundleImageTest, GetImageNamed) {
FilePath data_1x_path = dir_path().AppendASCII("sample_1x.pak");
FilePath data_2x_path = dir_path().AppendASCII("sample_2x.pak");
// Create the pak files.
CreateDataPackWithSingleBitmap(data_1x_path, 10, base::StringPiece());
CreateDataPackWithSingleBitmap(data_2x_path, 20, base::StringPiece());
// Load the regular and 2x pak files.
ResourceBundle* resource_bundle = CreateResourceBundleWithEmptyLocalePak();
resource_bundle->AddDataPackFromPath(data_1x_path, SCALE_FACTOR_100P);
resource_bundle->AddDataPackFromPath(data_2x_path, SCALE_FACTOR_200P);
EXPECT_EQ(SCALE_FACTOR_200P, resource_bundle->max_scale_factor());
gfx::ImageSkia* image_skia = resource_bundle->GetImageSkiaNamed(3);
#if defined(OS_CHROMEOS)
// ChromeOS loads highest scale factor first.
EXPECT_EQ(ui::SCALE_FACTOR_200P, image_skia->image_reps()[0].scale_factor());
#else
EXPECT_EQ(ui::SCALE_FACTOR_100P, image_skia->image_reps()[0].scale_factor());
#endif
// Resource ID 3 exists in both 1x and 2x paks. Image reps should be
// available for both scale factors in |image_skia|.
gfx::ImageSkiaRep image_rep =
image_skia->GetRepresentation(ui::SCALE_FACTOR_100P);
EXPECT_EQ(ui::SCALE_FACTOR_100P, image_rep.scale_factor());
image_rep = image_skia->GetRepresentation(ui::SCALE_FACTOR_200P);
EXPECT_EQ(ui::SCALE_FACTOR_200P, image_rep.scale_factor());
// The 1.4x pack was not loaded. Requesting the 1.4x resource should return
// either the 1x or the 2x resource.
image_rep = image_skia->GetRepresentation(ui::SCALE_FACTOR_140P);
EXPECT_TRUE(image_rep.scale_factor() == ui::SCALE_FACTOR_100P ||
image_rep.scale_factor() == ui::SCALE_FACTOR_200P);
}
// Test that GetImageNamed() behaves properly for images which GRIT has
// annotated as having fallen back to 1x.
TEST_F(ResourceBundleImageTest, GetImageNamedFallback1x) {
FilePath data_path = dir_path().AppendASCII("sample.pak");
FilePath data_2x_path = dir_path().AppendASCII("sample_2x.pak");
// Create the pak files.
CreateDataPackWithSingleBitmap(data_path, 10, base::StringPiece());
// 2x data pack bitmap has custom chunk to indicate that the 2x bitmap is not
// available and that GRIT fell back to 1x.
CreateDataPackWithSingleBitmap(data_2x_path, 10, base::StringPiece(
reinterpret_cast<const char*>(kPngScaleChunk),
arraysize(kPngScaleChunk)));
// Load the regular and 2x pak files.
ResourceBundle* resource_bundle = CreateResourceBundleWithEmptyLocalePak();
resource_bundle->AddDataPackFromPath(data_path, SCALE_FACTOR_100P);
resource_bundle->AddDataPackFromPath(data_2x_path, SCALE_FACTOR_200P);
gfx::ImageSkia* image_skia = resource_bundle->GetImageSkiaNamed(3);
// The image rep for 2x should be available. It should be resized to the
// proper 2x size.
gfx::ImageSkiaRep image_rep =
image_skia->GetRepresentation(ui::SCALE_FACTOR_200P);
EXPECT_EQ(ui::SCALE_FACTOR_200P, image_rep.scale_factor());
EXPECT_EQ(20, image_rep.pixel_width());
EXPECT_EQ(20, image_rep.pixel_height());
}
TEST_F(ResourceBundleImageTest, FallbackToNone) {
FilePath data_default_path = dir_path().AppendASCII("sample.pak");
// Create the pak files.
CreateDataPackWithSingleBitmap(data_default_path, 10, base::StringPiece());
// Load the regular pak files only.
ResourceBundle* resource_bundle = CreateResourceBundleWithEmptyLocalePak();
resource_bundle->AddDataPackFromPath(data_default_path, SCALE_FACTOR_NONE);
gfx::ImageSkia* image_skia = resource_bundle->GetImageSkiaNamed(3);
EXPECT_EQ(1u, image_skia->image_reps().size());
EXPECT_EQ(ui::SCALE_FACTOR_100P,
image_skia->image_reps()[0].scale_factor());
}
} // namespace ui
|