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.
5 #include "ui/base/resource/resource_bundle.h"
7 #include "base/base_paths.h"
8 #include "base/file_util.h"
9 #include "base/files/file_path.h"
10 #include "base/files/scoped_temp_dir.h"
11 #include "base/logging.h"
12 #include "base/memory/ref_counted_memory.h"
13 #include "base/path_service.h"
14 #include "base/strings/utf_string_conversions.h"
15 #include "net/base/big_endian.h"
16 #include "testing/gmock/include/gmock/gmock.h"
17 #include "testing/gtest/include/gtest/gtest.h"
18 #include "third_party/skia/include/core/SkBitmap.h"
19 #include "ui/base/layout.h"
20 #include "ui/base/resource/data_pack.h"
21 #include "ui/gfx/codec/png_codec.h"
22 #include "ui/gfx/image/image_skia.h"
24 #include "grit/ui_resources.h"
27 using ::testing::Between
;
28 using ::testing::Property
;
29 using ::testing::Return
;
30 using ::testing::ReturnArg
;
34 extern const char kSamplePakContents
[];
35 extern const size_t kSamplePakSize
;
36 extern const char kSamplePakContents2x
[];
37 extern const size_t kSamplePakSize2x
;
38 extern const char kEmptyPakContents
[];
39 extern const size_t kEmptyPakSize
;
43 const unsigned char kPngMagic
[8] = { 0x89, 'P', 'N', 'G', 13, 10, 26, 10 };
44 const size_t kPngChunkMetadataSize
= 12;
45 const unsigned char kPngIHDRChunkType
[4] = { 'I', 'H', 'D', 'R' };
47 // Custom chunk that GRIT adds to PNG to indicate that it could not find a
48 // bitmap at the requested scale factor and fell back to 1x.
49 const unsigned char kPngScaleChunk
[12] = { 0x00, 0x00, 0x00, 0x00,
51 0xc1, 0x30, 0x60, 0x4d };
53 // Mock for the ResourceBundle::Delegate class.
54 class MockResourceBundleDelegate
: public ui::ResourceBundle::Delegate
{
56 MockResourceBundleDelegate() {
58 virtual ~MockResourceBundleDelegate() {
61 MOCK_METHOD2(GetPathForResourcePack
, base::FilePath(
62 const base::FilePath
& pack_path
, ui::ScaleFactor scale_factor
));
63 MOCK_METHOD2(GetPathForLocalePack
, base::FilePath(
64 const base::FilePath
& pack_path
, const std::string
& locale
));
65 MOCK_METHOD1(GetImageNamed
, gfx::Image(int resource_id
));
66 MOCK_METHOD2(GetNativeImageNamed
,
67 gfx::Image(int resource_id
,
68 ui::ResourceBundle::ImageRTL rtl
));
69 MOCK_METHOD2(LoadDataResourceBytes
,
70 base::RefCountedStaticMemory
*(int resource_id
,
71 ui::ScaleFactor scale_factor
));
72 MOCK_METHOD2(GetRawDataResourceMock
, base::StringPiece(
74 ui::ScaleFactor scale_factor
));
75 virtual bool GetRawDataResource(int resource_id
,
76 ui::ScaleFactor scale_factor
,
77 base::StringPiece
* value
) OVERRIDE
{
78 *value
= GetRawDataResourceMock(resource_id
, scale_factor
);
81 MOCK_METHOD1(GetLocalizedStringMock
, base::string16(int message_id
));
82 virtual bool GetLocalizedString(int message_id
,
83 base::string16
* value
) OVERRIDE
{
84 *value
= GetLocalizedStringMock(message_id
);
87 MOCK_METHOD1(GetFontMock
,
88 gfx::Font
*(ui::ResourceBundle::FontStyle style
));
89 virtual scoped_ptr
<gfx::Font
> GetFont(
90 ui::ResourceBundle::FontStyle style
) OVERRIDE
{
91 return scoped_ptr
<gfx::Font
>(GetFontMock(style
));
95 // Returns |bitmap_data| with |custom_chunk| inserted after the IHDR chunk.
96 void AddCustomChunk(const base::StringPiece
& custom_chunk
,
97 std::vector
<unsigned char>* bitmap_data
) {
98 EXPECT_LT(arraysize(kPngMagic
) + kPngChunkMetadataSize
, bitmap_data
->size());
99 EXPECT_TRUE(std::equal(
100 bitmap_data
->begin(),
101 bitmap_data
->begin() + arraysize(kPngMagic
),
103 std::vector
<unsigned char>::iterator ihdr_start
=
104 bitmap_data
->begin() + arraysize(kPngMagic
);
105 char ihdr_length_data
[sizeof(uint32
)];
106 for (size_t i
= 0; i
< sizeof(uint32
); ++i
)
107 ihdr_length_data
[i
] = *(ihdr_start
+ i
);
108 uint32 ihdr_chunk_length
= 0;
109 net::ReadBigEndian(reinterpret_cast<char*>(ihdr_length_data
),
111 EXPECT_TRUE(std::equal(
112 ihdr_start
+ sizeof(uint32
),
113 ihdr_start
+ sizeof(uint32
) + sizeof(kPngIHDRChunkType
),
116 bitmap_data
->insert(ihdr_start
+ kPngChunkMetadataSize
+ ihdr_chunk_length
,
117 custom_chunk
.begin(), custom_chunk
.end());
120 // Creates datapack at |path| with a single bitmap at resource ID 3
121 // which is |edge_size|x|edge_size| pixels.
122 // If |custom_chunk| is non empty, adds it after the IHDR chunk
123 // in the encoded bitmap data.
124 void CreateDataPackWithSingleBitmap(const base::FilePath
& path
,
126 const base::StringPiece
& custom_chunk
) {
128 bitmap
.setConfig(SkBitmap::kARGB_8888_Config
, edge_size
, edge_size
);
129 bitmap
.allocPixels();
130 bitmap
.eraseColor(SK_ColorWHITE
);
131 std::vector
<unsigned char> bitmap_data
;
132 EXPECT_TRUE(gfx::PNGCodec::EncodeBGRASkBitmap(bitmap
, false, &bitmap_data
));
134 if (custom_chunk
.size() > 0)
135 AddCustomChunk(custom_chunk
, &bitmap_data
);
137 std::map
<uint16
, base::StringPiece
> resources
;
138 resources
[3u] = base::StringPiece(
139 reinterpret_cast<const char*>(&bitmap_data
[0]), bitmap_data
.size());
140 DataPack::WritePack(path
, resources
, ui::DataPack::BINARY
);
145 class ResourceBundleTest
: public testing::Test
{
147 ResourceBundleTest() : resource_bundle_(NULL
) {
150 virtual ~ResourceBundleTest() {
153 // Overridden from testing::Test:
154 virtual void TearDown() OVERRIDE
{
155 delete resource_bundle_
;
158 // Returns new ResoureBundle with the specified |delegate|. The
159 // ResourceBundleTest class manages the lifetime of the returned
161 ResourceBundle
* CreateResourceBundle(ResourceBundle::Delegate
* delegate
) {
162 DCHECK(!resource_bundle_
);
164 resource_bundle_
= new ResourceBundle(delegate
);
165 return resource_bundle_
;
169 ResourceBundle
* resource_bundle_
;
172 DISALLOW_COPY_AND_ASSIGN(ResourceBundleTest
);
175 TEST_F(ResourceBundleTest
, DelegateGetPathForResourcePack
) {
176 MockResourceBundleDelegate delegate
;
177 ResourceBundle
* resource_bundle
= CreateResourceBundle(&delegate
);
179 base::FilePath
pack_path(FILE_PATH_LITERAL("/path/to/test_path.pak"));
180 ui::ScaleFactor pack_scale_factor
= ui::SCALE_FACTOR_200P
;
182 EXPECT_CALL(delegate
,
183 GetPathForResourcePack(
184 Property(&base::FilePath::value
, pack_path
.value()),
187 .WillOnce(Return(pack_path
));
189 resource_bundle
->AddDataPackFromPath(pack_path
, pack_scale_factor
);
192 #if defined(OS_LINUX)
193 // Fails consistently on Linux: crbug.com/161902
194 #define MAYBE_DelegateGetPathForLocalePack DISABLED_DelegateGetPathForLocalePack
196 #define MAYBE_DelegateGetPathForLocalePack DelegateGetPathForLocalePack
198 TEST_F(ResourceBundleTest
, MAYBE_DelegateGetPathForLocalePack
) {
199 MockResourceBundleDelegate delegate
;
200 ResourceBundle
* resource_bundle
= CreateResourceBundle(&delegate
);
202 std::string locale
= "en-US";
205 EXPECT_CALL(delegate
, GetPathForLocalePack(_
, locale
))
207 .WillRepeatedly(Return(base::FilePath()))
208 .RetiresOnSaturation();
210 EXPECT_FALSE(resource_bundle
->LocaleDataPakExists(locale
));
211 EXPECT_EQ("", resource_bundle
->LoadLocaleResources(locale
));
213 // Allow the load to proceed.
214 EXPECT_CALL(delegate
, GetPathForLocalePack(_
, locale
))
216 .WillRepeatedly(ReturnArg
<0>());
218 EXPECT_TRUE(resource_bundle
->LocaleDataPakExists(locale
));
219 EXPECT_EQ(locale
, resource_bundle
->LoadLocaleResources(locale
));
222 TEST_F(ResourceBundleTest
, DelegateGetImageNamed
) {
223 MockResourceBundleDelegate delegate
;
224 ResourceBundle
* resource_bundle
= CreateResourceBundle(&delegate
);
226 gfx::Image empty_image
= resource_bundle
->GetEmptyImage();
229 EXPECT_CALL(delegate
, GetImageNamed(resource_id
))
231 .WillOnce(Return(empty_image
));
233 gfx::Image result
= resource_bundle
->GetImageNamed(resource_id
);
234 EXPECT_EQ(empty_image
.ToSkBitmap(), result
.ToSkBitmap());
237 TEST_F(ResourceBundleTest
, DelegateGetNativeImageNamed
) {
238 MockResourceBundleDelegate delegate
;
239 ResourceBundle
* resource_bundle
= CreateResourceBundle(&delegate
);
241 gfx::Image empty_image
= resource_bundle
->GetEmptyImage();
244 // Some platforms delegate GetNativeImageNamed calls to GetImageNamed.
245 EXPECT_CALL(delegate
, GetImageNamed(resource_id
))
246 .Times(Between(0, 1))
247 .WillOnce(Return(empty_image
));
248 EXPECT_CALL(delegate
,
249 GetNativeImageNamed(resource_id
, ui::ResourceBundle::RTL_DISABLED
))
250 .Times(Between(0, 1))
251 .WillOnce(Return(empty_image
));
253 gfx::Image result
= resource_bundle
->GetNativeImageNamed(resource_id
);
254 EXPECT_EQ(empty_image
.ToSkBitmap(), result
.ToSkBitmap());
257 TEST_F(ResourceBundleTest
, DelegateLoadDataResourceBytes
) {
258 MockResourceBundleDelegate delegate
;
259 ResourceBundle
* resource_bundle
= CreateResourceBundle(&delegate
);
261 // Create the data resource for testing purposes.
262 unsigned char data
[] = "My test data";
263 scoped_refptr
<base::RefCountedStaticMemory
> static_memory(
264 new base::RefCountedStaticMemory(data
, sizeof(data
)));
267 ui::ScaleFactor scale_factor
= ui::SCALE_FACTOR_NONE
;
269 EXPECT_CALL(delegate
, LoadDataResourceBytes(resource_id
, scale_factor
))
270 .Times(1).WillOnce(Return(static_memory
.get()));
272 scoped_refptr
<base::RefCountedStaticMemory
> result
=
273 resource_bundle
->LoadDataResourceBytesForScale(resource_id
, scale_factor
);
274 EXPECT_EQ(static_memory
, result
);
277 TEST_F(ResourceBundleTest
, DelegateGetRawDataResource
) {
278 MockResourceBundleDelegate delegate
;
279 ResourceBundle
* resource_bundle
= CreateResourceBundle(&delegate
);
281 // Create the string piece for testing purposes.
282 char data
[] = "My test data";
283 base::StringPiece
string_piece(data
);
287 EXPECT_CALL(delegate
, GetRawDataResourceMock(
288 resource_id
, ui::SCALE_FACTOR_NONE
))
290 .WillOnce(Return(string_piece
));
292 base::StringPiece result
= resource_bundle
->GetRawDataResource(
294 EXPECT_EQ(string_piece
.data(), result
.data());
297 TEST_F(ResourceBundleTest
, DelegateGetLocalizedString
) {
298 MockResourceBundleDelegate delegate
;
299 ResourceBundle
* resource_bundle
= CreateResourceBundle(&delegate
);
301 base::string16 data
= base::ASCIIToUTF16("My test data");
304 EXPECT_CALL(delegate
, GetLocalizedStringMock(resource_id
))
306 .WillOnce(Return(data
));
308 base::string16 result
= resource_bundle
->GetLocalizedString(resource_id
);
309 EXPECT_EQ(data
, result
);
312 #if defined(USE_OZONE) && !defined(USE_PANGO)
313 #define MAYBE_DelegateGetFontList DISABLED_DelegateGetFontList
315 #define MAYBE_DelegateGetFontList DelegateGetFontList
318 TEST_F(ResourceBundleTest
, MAYBE_DelegateGetFontList
) {
319 MockResourceBundleDelegate delegate
;
320 ResourceBundle
* resource_bundle
= CreateResourceBundle(&delegate
);
322 // Should be called once for each font type. When we return NULL the default
323 // font will be created.
324 gfx::Font
* test_font
= NULL
;
325 EXPECT_CALL(delegate
, GetFontMock(_
))
327 .WillRepeatedly(Return(test_font
));
329 const gfx::FontList
* font_list
=
330 &resource_bundle
->GetFontList(ui::ResourceBundle::BaseFont
);
331 EXPECT_TRUE(font_list
);
333 const gfx::Font
* font
=
334 &resource_bundle
->GetFont(ui::ResourceBundle::BaseFont
);
338 TEST_F(ResourceBundleTest
, LocaleDataPakExists
) {
339 ResourceBundle
* resource_bundle
= CreateResourceBundle(NULL
);
341 // Check that ResourceBundle::LocaleDataPakExists returns the correct results.
342 EXPECT_TRUE(resource_bundle
->LocaleDataPakExists("en-US"));
343 EXPECT_FALSE(resource_bundle
->LocaleDataPakExists("not_a_real_locale"));
346 class ResourceBundleImageTest
: public ResourceBundleTest
{
348 ResourceBundleImageTest() {}
350 virtual ~ResourceBundleImageTest() {
353 virtual void SetUp() OVERRIDE
{
354 // Create a temporary directory to write test resource bundles to.
355 ASSERT_TRUE(dir_
.CreateUniqueTempDir());
358 // Returns resource bundle which uses an empty data pak for locale data.
359 ui::ResourceBundle
* CreateResourceBundleWithEmptyLocalePak() {
360 // Write an empty data pak for locale data.
361 const base::FilePath
& locale_path
= dir_path().Append(
362 FILE_PATH_LITERAL("locale.pak"));
363 EXPECT_EQ(file_util::WriteFile(locale_path
, kEmptyPakContents
,
365 static_cast<int>(kEmptyPakSize
));
367 ui::ResourceBundle
* resource_bundle
= CreateResourceBundle(NULL
);
369 // Load the empty locale data pak.
370 resource_bundle
->LoadTestResources(base::FilePath(), locale_path
);
371 return resource_bundle
;
374 // Returns the path of temporary directory to write test data packs into.
375 const base::FilePath
& dir_path() { return dir_
.path(); }
378 scoped_ptr
<DataPack
> locale_pack_
;
379 base::ScopedTempDir dir_
;
381 DISALLOW_COPY_AND_ASSIGN(ResourceBundleImageTest
);
384 // Verify that we don't crash when trying to load a resource that is not found.
385 // In some cases, we fail to mmap resources.pak, but try to keep going anyway.
386 TEST_F(ResourceBundleImageTest
, LoadDataResourceBytes
) {
387 base::FilePath data_path
= dir_path().Append(FILE_PATH_LITERAL("sample.pak"));
389 // Dump contents into the pak files.
390 ASSERT_EQ(file_util::WriteFile(data_path
, kEmptyPakContents
,
391 kEmptyPakSize
), static_cast<int>(kEmptyPakSize
));
393 // Create a resource bundle from the file.
394 ResourceBundle
* resource_bundle
= CreateResourceBundleWithEmptyLocalePak();
395 resource_bundle
->AddDataPackFromPath(data_path
, SCALE_FACTOR_100P
);
397 const int kUnfoundResourceId
= 10000;
398 EXPECT_EQ(NULL
, resource_bundle
->LoadDataResourceBytes(
399 kUnfoundResourceId
));
401 // Give a .pak file that doesn't exist so we will fail to load it.
402 resource_bundle
->AddDataPackFromPath(
403 base::FilePath(FILE_PATH_LITERAL("non-existant-file.pak")),
404 ui::SCALE_FACTOR_NONE
);
405 EXPECT_EQ(NULL
, resource_bundle
->LoadDataResourceBytes(
406 kUnfoundResourceId
));
409 TEST_F(ResourceBundleImageTest
, GetRawDataResource
) {
410 base::FilePath data_path
= dir_path().Append(FILE_PATH_LITERAL("sample.pak"));
411 base::FilePath data_2x_path
=
412 dir_path().Append(FILE_PATH_LITERAL("sample_2x.pak"));
414 // Dump contents into the pak files.
415 ASSERT_EQ(file_util::WriteFile(data_path
, kSamplePakContents
,
416 kSamplePakSize
), static_cast<int>(kSamplePakSize
));
417 ASSERT_EQ(file_util::WriteFile(data_2x_path
, kSamplePakContents2x
,
418 kSamplePakSize2x
), static_cast<int>(kSamplePakSize2x
));
420 // Load the regular and 2x pak files.
421 ResourceBundle
* resource_bundle
= CreateResourceBundleWithEmptyLocalePak();
422 resource_bundle
->AddDataPackFromPath(data_path
, SCALE_FACTOR_100P
);
423 resource_bundle
->AddDataPackFromPath(data_2x_path
, SCALE_FACTOR_200P
);
425 // Resource ID 4 exists in both 1x and 2x paks, so we expect a different
426 // result when requesting the 2x scale.
427 EXPECT_EQ("this is id 4", resource_bundle
->GetRawDataResourceForScale(4,
429 EXPECT_EQ("this is id 4 2x", resource_bundle
->GetRawDataResourceForScale(4,
432 // Resource ID 6 only exists in the 1x pak so we expect the same resource
433 // for both scale factor requests.
434 EXPECT_EQ("this is id 6", resource_bundle
->GetRawDataResourceForScale(6,
436 EXPECT_EQ("this is id 6", resource_bundle
->GetRawDataResourceForScale(6,
440 // Test requesting image reps at various scale factors from the image returned
441 // via ResourceBundle::GetImageNamed().
442 TEST_F(ResourceBundleImageTest
, GetImageNamed
) {
443 std::vector
<ScaleFactor
> supported_factors
;
444 supported_factors
.push_back(SCALE_FACTOR_100P
);
445 supported_factors
.push_back(SCALE_FACTOR_200P
);
446 test::ScopedSetSupportedScaleFactors
scoped_supported(supported_factors
);
447 base::FilePath data_1x_path
= dir_path().AppendASCII("sample_1x.pak");
448 base::FilePath data_2x_path
= dir_path().AppendASCII("sample_2x.pak");
450 // Create the pak files.
451 CreateDataPackWithSingleBitmap(data_1x_path
, 10, base::StringPiece());
452 CreateDataPackWithSingleBitmap(data_2x_path
, 20, base::StringPiece());
454 // Load the regular and 2x pak files.
455 ResourceBundle
* resource_bundle
= CreateResourceBundleWithEmptyLocalePak();
456 resource_bundle
->AddDataPackFromPath(data_1x_path
, SCALE_FACTOR_100P
);
457 resource_bundle
->AddDataPackFromPath(data_2x_path
, SCALE_FACTOR_200P
);
459 EXPECT_EQ(SCALE_FACTOR_200P
, resource_bundle
->GetMaxScaleFactor());
461 gfx::ImageSkia
* image_skia
= resource_bundle
->GetImageSkiaNamed(3);
463 #if defined(OS_CHROMEOS)
464 // ChromeOS loads highest scale factor first.
465 EXPECT_EQ(ui::SCALE_FACTOR_200P
,
466 GetSupportedScaleFactor(image_skia
->image_reps()[0].scale()));
468 EXPECT_EQ(ui::SCALE_FACTOR_100P
,
469 GetSupportedScaleFactor(image_skia
->image_reps()[0].scale()));
472 // Resource ID 3 exists in both 1x and 2x paks. Image reps should be
473 // available for both scale factors in |image_skia|.
474 gfx::ImageSkiaRep image_rep
=
475 image_skia
->GetRepresentation(GetImageScale(ui::SCALE_FACTOR_100P
));
476 EXPECT_EQ(ui::SCALE_FACTOR_100P
, GetSupportedScaleFactor(image_rep
.scale()));
478 image_skia
->GetRepresentation(GetImageScale(ui::SCALE_FACTOR_200P
));
479 EXPECT_EQ(ui::SCALE_FACTOR_200P
, GetSupportedScaleFactor(image_rep
.scale()));
481 // The 1.4x pack was not loaded. Requesting the 1.4x resource should return
482 // either the 1x or the 2x resource.
483 image_rep
= image_skia
->GetRepresentation(
484 ui::GetImageScale(ui::SCALE_FACTOR_140P
));
485 ui::ScaleFactor scale_factor
= GetSupportedScaleFactor(image_rep
.scale());
486 EXPECT_TRUE(scale_factor
== ui::SCALE_FACTOR_100P
||
487 scale_factor
== ui::SCALE_FACTOR_200P
);
490 // Test that GetImageNamed() behaves properly for images which GRIT has
491 // annotated as having fallen back to 1x.
492 TEST_F(ResourceBundleImageTest
, GetImageNamedFallback1x
) {
493 std::vector
<ScaleFactor
> supported_factors
;
494 supported_factors
.push_back(SCALE_FACTOR_100P
);
495 supported_factors
.push_back(SCALE_FACTOR_200P
);
496 test::ScopedSetSupportedScaleFactors
scoped_supported(supported_factors
);
497 base::FilePath data_path
= dir_path().AppendASCII("sample.pak");
498 base::FilePath data_2x_path
= dir_path().AppendASCII("sample_2x.pak");
500 // Create the pak files.
501 CreateDataPackWithSingleBitmap(data_path
, 10, base::StringPiece());
502 // 2x data pack bitmap has custom chunk to indicate that the 2x bitmap is not
503 // available and that GRIT fell back to 1x.
504 CreateDataPackWithSingleBitmap(data_2x_path
, 10, base::StringPiece(
505 reinterpret_cast<const char*>(kPngScaleChunk
),
506 arraysize(kPngScaleChunk
)));
508 // Load the regular and 2x pak files.
509 ResourceBundle
* resource_bundle
= CreateResourceBundleWithEmptyLocalePak();
510 resource_bundle
->AddDataPackFromPath(data_path
, SCALE_FACTOR_100P
);
511 resource_bundle
->AddDataPackFromPath(data_2x_path
, SCALE_FACTOR_200P
);
513 gfx::ImageSkia
* image_skia
= resource_bundle
->GetImageSkiaNamed(3);
515 // The image rep for 2x should be available. It should be resized to the
517 gfx::ImageSkiaRep image_rep
=
518 image_skia
->GetRepresentation(GetImageScale(ui::SCALE_FACTOR_200P
));
519 EXPECT_EQ(ui::SCALE_FACTOR_200P
, GetSupportedScaleFactor(image_rep
.scale()));
520 EXPECT_EQ(20, image_rep
.pixel_width());
521 EXPECT_EQ(20, image_rep
.pixel_height());
525 // Tests GetImageNamed() behaves properly when the size of a scaled image
526 // requires rounding as a result of using a non-integer scale factor.
527 // Scale factors of 140 and 1805 are Windows specific.
528 TEST_F(ResourceBundleImageTest
, GetImageNamedFallback1xRounding
) {
529 std::vector
<ScaleFactor
> supported_factors
;
530 supported_factors
.push_back(SCALE_FACTOR_100P
);
531 supported_factors
.push_back(SCALE_FACTOR_140P
);
532 supported_factors
.push_back(SCALE_FACTOR_180P
);
533 test::ScopedSetSupportedScaleFactors
scoped_supported(supported_factors
);
535 base::FilePath data_path
= dir_path().AppendASCII("sample.pak");
536 base::FilePath data_140P_path
= dir_path().AppendASCII("sample_140P.pak");
537 base::FilePath data_180P_path
= dir_path().AppendASCII("sample_180P.pak");
539 CreateDataPackWithSingleBitmap(data_path
, 8, base::StringPiece());
540 // Mark 140% and 180% images as requiring 1x fallback.
541 CreateDataPackWithSingleBitmap(data_140P_path
, 8, base::StringPiece(
542 reinterpret_cast<const char*>(kPngScaleChunk
),
543 arraysize(kPngScaleChunk
)));
544 CreateDataPackWithSingleBitmap(data_180P_path
, 8, base::StringPiece(
545 reinterpret_cast<const char*>(kPngScaleChunk
),
546 arraysize(kPngScaleChunk
)));
548 ResourceBundle
* resource_bundle
= CreateResourceBundleWithEmptyLocalePak();
549 resource_bundle
->AddDataPackFromPath(data_path
, SCALE_FACTOR_100P
);
550 resource_bundle
->AddDataPackFromPath(data_140P_path
, SCALE_FACTOR_140P
);
551 resource_bundle
->AddDataPackFromPath(data_180P_path
, SCALE_FACTOR_180P
);
553 // Non-integer dimensions should be rounded up.
554 gfx::ImageSkia
* image_skia
= resource_bundle
->GetImageSkiaNamed(3);
555 gfx::ImageSkiaRep image_rep
=
556 image_skia
->GetRepresentation(
557 GetImageScale(ui::SCALE_FACTOR_140P
));
558 EXPECT_EQ(12, image_rep
.pixel_width());
559 image_rep
= image_skia
->GetRepresentation(
560 GetImageScale(ui::SCALE_FACTOR_180P
));
561 EXPECT_EQ(15, image_rep
.pixel_width());
566 // Fails on devices that have non-100P scaling. See crbug.com/298406
567 #define MAYBE_FallbackToNone DISABLED_FallbackToNone
569 #define MAYBE_FallbackToNone FallbackToNone
571 TEST_F(ResourceBundleImageTest
, MAYBE_FallbackToNone
) {
572 base::FilePath data_default_path
= dir_path().AppendASCII("sample.pak");
574 // Create the pak files.
575 CreateDataPackWithSingleBitmap(data_default_path
, 10, base::StringPiece());
577 // Load the regular pak files only.
578 ResourceBundle
* resource_bundle
= CreateResourceBundleWithEmptyLocalePak();
579 resource_bundle
->AddDataPackFromPath(data_default_path
, SCALE_FACTOR_NONE
);
581 gfx::ImageSkia
* image_skia
= resource_bundle
->GetImageSkiaNamed(3);
582 EXPECT_EQ(1u, image_skia
->image_reps().size());
583 EXPECT_EQ(ui::SCALE_FACTOR_100P
,
584 GetSupportedScaleFactor(image_skia
->image_reps()[0].scale()));