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/big_endian.h"
9 #include "base/file_util.h"
10 #include "base/files/file_path.h"
11 #include "base/files/scoped_temp_dir.h"
12 #include "base/logging.h"
13 #include "base/memory/ref_counted_memory.h"
14 #include "base/path_service.h"
15 #include "base/strings/utf_string_conversions.h"
16 #include "grit/ui_resources.h"
17 #include "testing/gmock/include/gmock/gmock.h"
18 #include "testing/gtest/include/gtest/gtest.h"
19 #include "third_party/skia/include/core/SkBitmap.h"
20 #include "ui/base/layout.h"
21 #include "ui/base/resource/data_pack.h"
22 #include "ui/gfx/codec/png_codec.h"
23 #include "ui/gfx/image/image_skia.h"
25 #include "ui/gfx/win/dpi.h"
29 using ::testing::Between;
30 using ::testing::Property;
31 using ::testing::Return;
32 using ::testing::ReturnArg;
36 extern const char kSamplePakContents[];
37 extern const size_t kSamplePakSize;
38 extern const char kSamplePakContents2x[];
39 extern const size_t kSamplePakSize2x;
40 extern const char kEmptyPakContents[];
41 extern const size_t kEmptyPakSize;
45 const unsigned char kPngMagic[8] = { 0x89, 'P', 'N', 'G', 13, 10, 26, 10 };
46 const size_t kPngChunkMetadataSize = 12;
47 const unsigned char kPngIHDRChunkType[4] = { 'I', 'H', 'D', 'R' };
49 // Custom chunk that GRIT adds to PNG to indicate that it could not find a
50 // bitmap at the requested scale factor and fell back to 1x.
51 const unsigned char kPngScaleChunk[12] = { 0x00, 0x00, 0x00, 0x00,
53 0xc1, 0x30, 0x60, 0x4d };
55 // Mock for the ResourceBundle::Delegate class.
56 class MockResourceBundleDelegate : public ui::ResourceBundle::Delegate {
58 MockResourceBundleDelegate() {
60 virtual ~MockResourceBundleDelegate() {
63 MOCK_METHOD2(GetPathForResourcePack, base::FilePath(
64 const base::FilePath& pack_path, ui::ScaleFactor scale_factor));
65 MOCK_METHOD2(GetPathForLocalePack, base::FilePath(
66 const base::FilePath& pack_path, const std::string& locale));
67 MOCK_METHOD1(GetImageNamed, gfx::Image(int resource_id));
68 MOCK_METHOD2(GetNativeImageNamed,
69 gfx::Image(int resource_id,
70 ui::ResourceBundle::ImageRTL rtl));
71 MOCK_METHOD2(LoadDataResourceBytes,
72 base::RefCountedStaticMemory*(int resource_id,
73 ui::ScaleFactor scale_factor));
74 MOCK_METHOD2(GetRawDataResourceMock, base::StringPiece(
76 ui::ScaleFactor scale_factor));
77 virtual bool GetRawDataResource(int resource_id,
78 ui::ScaleFactor scale_factor,
79 base::StringPiece* value) OVERRIDE {
80 *value = GetRawDataResourceMock(resource_id, scale_factor);
83 MOCK_METHOD1(GetLocalizedStringMock, base::string16(int message_id));
84 virtual bool GetLocalizedString(int message_id,
85 base::string16* value) OVERRIDE {
86 *value = GetLocalizedStringMock(message_id);
89 MOCK_METHOD1(GetFontMock,
90 gfx::Font*(ui::ResourceBundle::FontStyle style));
91 virtual scoped_ptr<gfx::Font> GetFont(
92 ui::ResourceBundle::FontStyle style) OVERRIDE {
93 return scoped_ptr<gfx::Font>(GetFontMock(style));
97 // Returns |bitmap_data| with |custom_chunk| inserted after the IHDR chunk.
98 void AddCustomChunk(const base::StringPiece& custom_chunk,
99 std::vector<unsigned char>* bitmap_data) {
100 EXPECT_LT(arraysize(kPngMagic) + kPngChunkMetadataSize, bitmap_data->size());
101 EXPECT_TRUE(std::equal(
102 bitmap_data->begin(),
103 bitmap_data->begin() + arraysize(kPngMagic),
105 std::vector<unsigned char>::iterator ihdr_start =
106 bitmap_data->begin() + arraysize(kPngMagic);
107 char ihdr_length_data[sizeof(uint32)];
108 for (size_t i = 0; i < sizeof(uint32); ++i)
109 ihdr_length_data[i] = *(ihdr_start + i);
110 uint32 ihdr_chunk_length = 0;
111 base::ReadBigEndian(reinterpret_cast<char*>(ihdr_length_data),
113 EXPECT_TRUE(std::equal(
114 ihdr_start + sizeof(uint32),
115 ihdr_start + sizeof(uint32) + sizeof(kPngIHDRChunkType),
118 bitmap_data->insert(ihdr_start + kPngChunkMetadataSize + ihdr_chunk_length,
119 custom_chunk.begin(), custom_chunk.end());
122 // Creates datapack at |path| with a single bitmap at resource ID 3
123 // which is |edge_size|x|edge_size| pixels.
124 // If |custom_chunk| is non empty, adds it after the IHDR chunk
125 // in the encoded bitmap data.
126 void CreateDataPackWithSingleBitmap(const base::FilePath& path,
128 const base::StringPiece& custom_chunk) {
130 bitmap.allocN32Pixels(edge_size, edge_size);
131 bitmap.eraseColor(SK_ColorWHITE);
132 std::vector<unsigned char> bitmap_data;
133 EXPECT_TRUE(gfx::PNGCodec::EncodeBGRASkBitmap(bitmap, false, &bitmap_data));
135 if (custom_chunk.size() > 0)
136 AddCustomChunk(custom_chunk, &bitmap_data);
138 std::map<uint16, base::StringPiece> resources;
139 resources[3u] = base::StringPiece(
140 reinterpret_cast<const char*>(&bitmap_data[0]), bitmap_data.size());
141 DataPack::WritePack(path, resources, ui::DataPack::BINARY);
146 class ResourceBundleTest : public testing::Test {
148 ResourceBundleTest() : resource_bundle_(NULL) {
151 virtual ~ResourceBundleTest() {
154 // Overridden from testing::Test:
155 virtual void TearDown() OVERRIDE {
156 delete resource_bundle_;
159 // Returns new ResoureBundle with the specified |delegate|. The
160 // ResourceBundleTest class manages the lifetime of the returned
162 ResourceBundle* CreateResourceBundle(ResourceBundle::Delegate* delegate) {
163 DCHECK(!resource_bundle_);
165 resource_bundle_ = new ResourceBundle(delegate);
166 return resource_bundle_;
170 ResourceBundle* resource_bundle_;
173 DISALLOW_COPY_AND_ASSIGN(ResourceBundleTest);
176 TEST_F(ResourceBundleTest, DelegateGetPathForResourcePack) {
177 MockResourceBundleDelegate delegate;
178 ResourceBundle* resource_bundle = CreateResourceBundle(&delegate);
180 base::FilePath pack_path(FILE_PATH_LITERAL("/path/to/test_path.pak"));
181 ui::ScaleFactor pack_scale_factor = ui::SCALE_FACTOR_200P;
183 EXPECT_CALL(delegate,
184 GetPathForResourcePack(
185 Property(&base::FilePath::value, pack_path.value()),
188 .WillOnce(Return(pack_path));
190 resource_bundle->AddDataPackFromPath(pack_path, pack_scale_factor);
193 #if defined(OS_LINUX)
194 // Fails consistently on Linux: crbug.com/161902
195 #define MAYBE_DelegateGetPathForLocalePack DISABLED_DelegateGetPathForLocalePack
197 #define MAYBE_DelegateGetPathForLocalePack DelegateGetPathForLocalePack
199 TEST_F(ResourceBundleTest, MAYBE_DelegateGetPathForLocalePack) {
200 MockResourceBundleDelegate delegate;
201 ResourceBundle* resource_bundle = CreateResourceBundle(&delegate);
203 std::string locale = "en-US";
206 EXPECT_CALL(delegate, GetPathForLocalePack(_, locale))
208 .WillRepeatedly(Return(base::FilePath()))
209 .RetiresOnSaturation();
211 EXPECT_FALSE(resource_bundle->LocaleDataPakExists(locale));
212 EXPECT_EQ("", resource_bundle->LoadLocaleResources(locale));
214 // Allow the load to proceed.
215 EXPECT_CALL(delegate, GetPathForLocalePack(_, locale))
217 .WillRepeatedly(ReturnArg<0>());
219 EXPECT_TRUE(resource_bundle->LocaleDataPakExists(locale));
220 EXPECT_EQ(locale, resource_bundle->LoadLocaleResources(locale));
223 TEST_F(ResourceBundleTest, DelegateGetImageNamed) {
224 MockResourceBundleDelegate delegate;
225 ResourceBundle* resource_bundle = CreateResourceBundle(&delegate);
227 gfx::Image empty_image = resource_bundle->GetEmptyImage();
230 EXPECT_CALL(delegate, GetImageNamed(resource_id))
232 .WillOnce(Return(empty_image));
234 gfx::Image result = resource_bundle->GetImageNamed(resource_id);
235 EXPECT_EQ(empty_image.ToSkBitmap(), result.ToSkBitmap());
238 TEST_F(ResourceBundleTest, DelegateGetNativeImageNamed) {
239 MockResourceBundleDelegate delegate;
240 ResourceBundle* resource_bundle = CreateResourceBundle(&delegate);
242 gfx::Image empty_image = resource_bundle->GetEmptyImage();
245 // Some platforms delegate GetNativeImageNamed calls to GetImageNamed.
246 EXPECT_CALL(delegate, GetImageNamed(resource_id))
247 .Times(Between(0, 1))
248 .WillOnce(Return(empty_image));
249 EXPECT_CALL(delegate,
250 GetNativeImageNamed(resource_id, ui::ResourceBundle::RTL_DISABLED))
251 .Times(Between(0, 1))
252 .WillOnce(Return(empty_image));
254 gfx::Image result = resource_bundle->GetNativeImageNamed(resource_id);
255 EXPECT_EQ(empty_image.ToSkBitmap(), result.ToSkBitmap());
258 TEST_F(ResourceBundleTest, DelegateLoadDataResourceBytes) {
259 MockResourceBundleDelegate delegate;
260 ResourceBundle* resource_bundle = CreateResourceBundle(&delegate);
262 // Create the data resource for testing purposes.
263 unsigned char data[] = "My test data";
264 scoped_refptr<base::RefCountedStaticMemory> static_memory(
265 new base::RefCountedStaticMemory(data, sizeof(data)));
268 ui::ScaleFactor scale_factor = ui::SCALE_FACTOR_NONE;
270 EXPECT_CALL(delegate, LoadDataResourceBytes(resource_id, scale_factor))
271 .Times(1).WillOnce(Return(static_memory.get()));
273 scoped_refptr<base::RefCountedStaticMemory> result =
274 resource_bundle->LoadDataResourceBytesForScale(resource_id, scale_factor);
275 EXPECT_EQ(static_memory, result);
278 TEST_F(ResourceBundleTest, DelegateGetRawDataResource) {
279 MockResourceBundleDelegate delegate;
280 ResourceBundle* resource_bundle = CreateResourceBundle(&delegate);
282 // Create the string piece for testing purposes.
283 char data[] = "My test data";
284 base::StringPiece string_piece(data);
288 EXPECT_CALL(delegate, GetRawDataResourceMock(
289 resource_id, ui::SCALE_FACTOR_NONE))
291 .WillOnce(Return(string_piece));
293 base::StringPiece result = resource_bundle->GetRawDataResource(
295 EXPECT_EQ(string_piece.data(), result.data());
298 TEST_F(ResourceBundleTest, DelegateGetLocalizedString) {
299 MockResourceBundleDelegate delegate;
300 ResourceBundle* resource_bundle = CreateResourceBundle(&delegate);
302 base::string16 data = base::ASCIIToUTF16("My test data");
305 EXPECT_CALL(delegate, GetLocalizedStringMock(resource_id))
307 .WillOnce(Return(data));
309 base::string16 result = resource_bundle->GetLocalizedString(resource_id);
310 EXPECT_EQ(data, result);
313 TEST_F(ResourceBundleTest, OverrideStringResource) {
314 ResourceBundle* resource_bundle = CreateResourceBundle(NULL);
316 base::string16 data = base::ASCIIToUTF16("My test data");
319 base::string16 result = resource_bundle->GetLocalizedString(resource_id);
320 EXPECT_EQ(base::string16(), result);
322 resource_bundle->OverrideLocaleStringResource(resource_id, data);
324 result = resource_bundle->GetLocalizedString(resource_id);
325 EXPECT_EQ(data, result);
328 TEST_F(ResourceBundleTest, DelegateGetLocalizedStringWithOverride) {
329 MockResourceBundleDelegate delegate;
330 ResourceBundle* resource_bundle = CreateResourceBundle(&delegate);
332 base::string16 delegate_data = base::ASCIIToUTF16("My delegate data");
335 EXPECT_CALL(delegate, GetLocalizedStringMock(resource_id)).Times(1).WillOnce(
336 Return(delegate_data));
338 base::string16 override_data = base::ASCIIToUTF16("My override data");
340 base::string16 result = resource_bundle->GetLocalizedString(resource_id);
341 EXPECT_EQ(delegate_data, result);
344 #if defined(USE_OZONE) && !defined(USE_PANGO)
345 #define MAYBE_DelegateGetFontList DISABLED_DelegateGetFontList
347 #define MAYBE_DelegateGetFontList DelegateGetFontList
350 TEST_F(ResourceBundleTest, MAYBE_DelegateGetFontList) {
351 MockResourceBundleDelegate delegate;
352 ResourceBundle* resource_bundle = CreateResourceBundle(&delegate);
354 // Should be called once for each font type. When we return NULL the default
355 // font will be created.
356 gfx::Font* test_font = NULL;
357 EXPECT_CALL(delegate, GetFontMock(_))
359 .WillRepeatedly(Return(test_font));
361 const gfx::FontList* font_list =
362 &resource_bundle->GetFontList(ui::ResourceBundle::BaseFont);
363 EXPECT_TRUE(font_list);
365 const gfx::Font* font =
366 &resource_bundle->GetFont(ui::ResourceBundle::BaseFont);
370 TEST_F(ResourceBundleTest, LocaleDataPakExists) {
371 ResourceBundle* resource_bundle = CreateResourceBundle(NULL);
373 // Check that ResourceBundle::LocaleDataPakExists returns the correct results.
374 EXPECT_TRUE(resource_bundle->LocaleDataPakExists("en-US"));
375 EXPECT_FALSE(resource_bundle->LocaleDataPakExists("not_a_real_locale"));
378 class ResourceBundleImageTest : public ResourceBundleTest {
380 ResourceBundleImageTest() {}
382 virtual ~ResourceBundleImageTest() {
385 virtual void SetUp() OVERRIDE {
386 // Create a temporary directory to write test resource bundles to.
387 ASSERT_TRUE(dir_.CreateUniqueTempDir());
390 // Returns resource bundle which uses an empty data pak for locale data.
391 ui::ResourceBundle* CreateResourceBundleWithEmptyLocalePak() {
392 // Write an empty data pak for locale data.
393 const base::FilePath& locale_path = dir_path().Append(
394 FILE_PATH_LITERAL("locale.pak"));
395 EXPECT_EQ(base::WriteFile(locale_path, kEmptyPakContents, kEmptyPakSize),
396 static_cast<int>(kEmptyPakSize));
398 ui::ResourceBundle* resource_bundle = CreateResourceBundle(NULL);
400 // Load the empty locale data pak.
401 resource_bundle->LoadTestResources(base::FilePath(), locale_path);
402 return resource_bundle;
405 // Returns the path of temporary directory to write test data packs into.
406 const base::FilePath& dir_path() { return dir_.path(); }
409 scoped_ptr<DataPack> locale_pack_;
410 base::ScopedTempDir dir_;
412 DISALLOW_COPY_AND_ASSIGN(ResourceBundleImageTest);
415 // Verify that we don't crash when trying to load a resource that is not found.
416 // In some cases, we fail to mmap resources.pak, but try to keep going anyway.
417 TEST_F(ResourceBundleImageTest, LoadDataResourceBytes) {
418 base::FilePath data_path = dir_path().Append(FILE_PATH_LITERAL("sample.pak"));
420 // Dump contents into the pak files.
421 ASSERT_EQ(base::WriteFile(data_path, kEmptyPakContents,
422 kEmptyPakSize), static_cast<int>(kEmptyPakSize));
424 // Create a resource bundle from the file.
425 ResourceBundle* resource_bundle = CreateResourceBundleWithEmptyLocalePak();
426 resource_bundle->AddDataPackFromPath(data_path, SCALE_FACTOR_100P);
428 const int kUnfoundResourceId = 10000;
429 EXPECT_EQ(NULL, resource_bundle->LoadDataResourceBytes(
430 kUnfoundResourceId));
432 // Give a .pak file that doesn't exist so we will fail to load it.
433 resource_bundle->AddDataPackFromPath(
434 base::FilePath(FILE_PATH_LITERAL("non-existant-file.pak")),
435 ui::SCALE_FACTOR_NONE);
436 EXPECT_EQ(NULL, resource_bundle->LoadDataResourceBytes(
437 kUnfoundResourceId));
440 TEST_F(ResourceBundleImageTest, GetRawDataResource) {
441 base::FilePath data_path = dir_path().Append(FILE_PATH_LITERAL("sample.pak"));
442 base::FilePath data_2x_path =
443 dir_path().Append(FILE_PATH_LITERAL("sample_2x.pak"));
445 // Dump contents into the pak files.
446 ASSERT_EQ(base::WriteFile(data_path, kSamplePakContents,
447 kSamplePakSize), static_cast<int>(kSamplePakSize));
448 ASSERT_EQ(base::WriteFile(data_2x_path, kSamplePakContents2x,
449 kSamplePakSize2x), static_cast<int>(kSamplePakSize2x));
451 // Load the regular and 2x pak files.
452 ResourceBundle* resource_bundle = CreateResourceBundleWithEmptyLocalePak();
453 resource_bundle->AddDataPackFromPath(data_path, SCALE_FACTOR_100P);
454 resource_bundle->AddDataPackFromPath(data_2x_path, SCALE_FACTOR_200P);
456 // Resource ID 4 exists in both 1x and 2x paks, so we expect a different
457 // result when requesting the 2x scale.
458 EXPECT_EQ("this is id 4", resource_bundle->GetRawDataResourceForScale(4,
460 EXPECT_EQ("this is id 4 2x", resource_bundle->GetRawDataResourceForScale(4,
463 // Resource ID 6 only exists in the 1x pak so we expect the same resource
464 // for both scale factor requests.
465 EXPECT_EQ("this is id 6", resource_bundle->GetRawDataResourceForScale(6,
467 EXPECT_EQ("this is id 6", resource_bundle->GetRawDataResourceForScale(6,
471 // Test requesting image reps at various scale factors from the image returned
472 // via ResourceBundle::GetImageNamed().
473 TEST_F(ResourceBundleImageTest, GetImageNamed) {
475 gfx::ForceHighDPISupportForTesting(2.0);
477 std::vector<ScaleFactor> supported_factors;
478 supported_factors.push_back(SCALE_FACTOR_100P);
479 supported_factors.push_back(SCALE_FACTOR_200P);
480 test::ScopedSetSupportedScaleFactors scoped_supported(supported_factors);
481 base::FilePath data_1x_path = dir_path().AppendASCII("sample_1x.pak");
482 base::FilePath data_2x_path = dir_path().AppendASCII("sample_2x.pak");
484 // Create the pak files.
485 CreateDataPackWithSingleBitmap(data_1x_path, 10, base::StringPiece());
486 CreateDataPackWithSingleBitmap(data_2x_path, 20, base::StringPiece());
488 // Load the regular and 2x pak files.
489 ResourceBundle* resource_bundle = CreateResourceBundleWithEmptyLocalePak();
490 resource_bundle->AddDataPackFromPath(data_1x_path, SCALE_FACTOR_100P);
491 resource_bundle->AddDataPackFromPath(data_2x_path, SCALE_FACTOR_200P);
493 EXPECT_EQ(SCALE_FACTOR_200P, resource_bundle->GetMaxScaleFactor());
495 gfx::ImageSkia* image_skia = resource_bundle->GetImageSkiaNamed(3);
497 #if defined(OS_CHROMEOS) || defined(OS_WIN)
498 // ChromeOS/Windows load highest scale factor first.
499 EXPECT_EQ(ui::SCALE_FACTOR_200P,
500 GetSupportedScaleFactor(image_skia->image_reps()[0].scale()));
502 EXPECT_EQ(ui::SCALE_FACTOR_100P,
503 GetSupportedScaleFactor(image_skia->image_reps()[0].scale()));
506 // Resource ID 3 exists in both 1x and 2x paks. Image reps should be
507 // available for both scale factors in |image_skia|.
508 gfx::ImageSkiaRep image_rep =
509 image_skia->GetRepresentation(
510 GetScaleForScaleFactor(ui::SCALE_FACTOR_100P));
511 EXPECT_EQ(ui::SCALE_FACTOR_100P, GetSupportedScaleFactor(image_rep.scale()));
513 image_skia->GetRepresentation(
514 GetScaleForScaleFactor(ui::SCALE_FACTOR_200P));
515 EXPECT_EQ(ui::SCALE_FACTOR_200P, GetSupportedScaleFactor(image_rep.scale()));
517 // The 1.4x pack was not loaded. Requesting the 1.4x resource should return
518 // either the 1x or the 2x resource.
519 image_rep = image_skia->GetRepresentation(
520 ui::GetScaleForScaleFactor(ui::SCALE_FACTOR_140P));
521 ui::ScaleFactor scale_factor = GetSupportedScaleFactor(image_rep.scale());
522 EXPECT_TRUE(scale_factor == ui::SCALE_FACTOR_100P ||
523 scale_factor == ui::SCALE_FACTOR_200P);
525 // ImageSkia scales image if the one for the requested scale factor is not
527 EXPECT_EQ(1.4f, image_skia->GetRepresentation(1.4f).scale());
530 // Test that GetImageNamed() behaves properly for images which GRIT has
531 // annotated as having fallen back to 1x.
532 TEST_F(ResourceBundleImageTest, GetImageNamedFallback1x) {
533 std::vector<ScaleFactor> supported_factors;
534 supported_factors.push_back(SCALE_FACTOR_100P);
535 supported_factors.push_back(SCALE_FACTOR_200P);
536 test::ScopedSetSupportedScaleFactors scoped_supported(supported_factors);
537 base::FilePath data_path = dir_path().AppendASCII("sample.pak");
538 base::FilePath data_2x_path = dir_path().AppendASCII("sample_2x.pak");
540 // Create the pak files.
541 CreateDataPackWithSingleBitmap(data_path, 10, base::StringPiece());
542 // 2x data pack bitmap has custom chunk to indicate that the 2x bitmap is not
543 // available and that GRIT fell back to 1x.
544 CreateDataPackWithSingleBitmap(data_2x_path, 10, base::StringPiece(
545 reinterpret_cast<const char*>(kPngScaleChunk),
546 arraysize(kPngScaleChunk)));
548 // Load the regular and 2x pak files.
549 ResourceBundle* resource_bundle = CreateResourceBundleWithEmptyLocalePak();
550 resource_bundle->AddDataPackFromPath(data_path, SCALE_FACTOR_100P);
551 resource_bundle->AddDataPackFromPath(data_2x_path, SCALE_FACTOR_200P);
553 gfx::ImageSkia* image_skia = resource_bundle->GetImageSkiaNamed(3);
555 // The image rep for 2x should be available. It should be resized to the
557 gfx::ImageSkiaRep image_rep =
558 image_skia->GetRepresentation(GetScaleForScaleFactor(
559 ui::SCALE_FACTOR_200P));
560 EXPECT_EQ(ui::SCALE_FACTOR_200P, GetSupportedScaleFactor(image_rep.scale()));
561 EXPECT_EQ(20, image_rep.pixel_width());
562 EXPECT_EQ(20, image_rep.pixel_height());
566 // Tests GetImageNamed() behaves properly when the size of a scaled image
567 // requires rounding as a result of using a non-integer scale factor.
568 // Scale factors of 140 and 1805 are Windows specific.
569 TEST_F(ResourceBundleImageTest, GetImageNamedFallback1xRounding) {
570 std::vector<ScaleFactor> supported_factors;
571 supported_factors.push_back(SCALE_FACTOR_100P);
572 supported_factors.push_back(SCALE_FACTOR_140P);
573 supported_factors.push_back(SCALE_FACTOR_180P);
574 test::ScopedSetSupportedScaleFactors scoped_supported(supported_factors);
576 base::FilePath data_path = dir_path().AppendASCII("sample.pak");
577 base::FilePath data_140P_path = dir_path().AppendASCII("sample_140P.pak");
578 base::FilePath data_180P_path = dir_path().AppendASCII("sample_180P.pak");
580 CreateDataPackWithSingleBitmap(data_path, 8, base::StringPiece());
581 // Mark 140% and 180% images as requiring 1x fallback.
582 CreateDataPackWithSingleBitmap(data_140P_path, 8, base::StringPiece(
583 reinterpret_cast<const char*>(kPngScaleChunk),
584 arraysize(kPngScaleChunk)));
585 CreateDataPackWithSingleBitmap(data_180P_path, 8, base::StringPiece(
586 reinterpret_cast<const char*>(kPngScaleChunk),
587 arraysize(kPngScaleChunk)));
589 ResourceBundle* resource_bundle = CreateResourceBundleWithEmptyLocalePak();
590 resource_bundle->AddDataPackFromPath(data_path, SCALE_FACTOR_100P);
591 resource_bundle->AddDataPackFromPath(data_140P_path, SCALE_FACTOR_140P);
592 resource_bundle->AddDataPackFromPath(data_180P_path, SCALE_FACTOR_180P);
594 // Non-integer dimensions should be rounded up.
595 gfx::ImageSkia* image_skia = resource_bundle->GetImageSkiaNamed(3);
596 gfx::ImageSkiaRep image_rep =
597 image_skia->GetRepresentation(
598 GetScaleForScaleFactor(ui::SCALE_FACTOR_140P));
599 EXPECT_EQ(12, image_rep.pixel_width());
600 image_rep = image_skia->GetRepresentation(
601 GetScaleForScaleFactor(ui::SCALE_FACTOR_180P));
602 EXPECT_EQ(15, image_rep.pixel_width());
607 // Fails on devices that have non-100P scaling. See crbug.com/298406
608 #define MAYBE_FallbackToNone DISABLED_FallbackToNone
610 #define MAYBE_FallbackToNone FallbackToNone
612 TEST_F(ResourceBundleImageTest, MAYBE_FallbackToNone) {
613 base::FilePath data_default_path = dir_path().AppendASCII("sample.pak");
615 // Create the pak files.
616 CreateDataPackWithSingleBitmap(data_default_path, 10, base::StringPiece());
618 // Load the regular pak files only.
619 ResourceBundle* resource_bundle = CreateResourceBundleWithEmptyLocalePak();
620 resource_bundle->AddDataPackFromPath(data_default_path, SCALE_FACTOR_NONE);
622 gfx::ImageSkia* image_skia = resource_bundle->GetImageSkiaNamed(3);
623 EXPECT_EQ(1u, image_skia->image_reps().size());
624 EXPECT_TRUE(image_skia->image_reps()[0].unscaled());
625 EXPECT_EQ(ui::SCALE_FACTOR_100P,
626 GetSupportedScaleFactor(image_skia->image_reps()[0].scale()));