OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "ui/base/resource/resource_bundle.h" | 5 #include "ui/base/resource/resource_bundle.h" |
6 | 6 |
7 #include "base/base_paths.h" | 7 #include "base/base_paths.h" |
8 #include "base/file_path.h" | 8 #include "base/file_path.h" |
9 #include "base/file_util.h" | 9 #include "base/file_util.h" |
10 #include "base/memory/ref_counted_memory.h" | 10 #include "base/memory/ref_counted_memory.h" |
11 #include "base/path_service.h" | 11 #include "base/path_service.h" |
12 #include "base/scoped_temp_dir.h" | 12 #include "base/scoped_temp_dir.h" |
13 #include "base/utf_string_conversions.h" | 13 #include "base/utf_string_conversions.h" |
14 #include "testing/gmock/include/gmock/gmock.h" | 14 #include "testing/gmock/include/gmock/gmock.h" |
15 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
16 #include "third_party/skia/include/core/SkBitmap.h" | 16 #include "third_party/skia/include/core/SkBitmap.h" |
17 #include "ui/base/layout.h" | 17 #include "ui/base/layout.h" |
| 18 #include "ui/base/resource/data_pack.h" |
| 19 #include "ui/gfx/codec/png_codec.h" |
| 20 #include "ui/gfx/image/image_skia.h" |
18 | 21 |
19 using ::testing::_; | 22 using ::testing::_; |
20 using ::testing::Between; | 23 using ::testing::Between; |
21 using ::testing::Property; | 24 using ::testing::Property; |
22 using ::testing::Return; | 25 using ::testing::Return; |
23 using ::testing::ReturnArg; | 26 using ::testing::ReturnArg; |
24 | 27 |
25 namespace ui { | 28 namespace ui { |
26 | 29 |
27 extern const char kSamplePakContents[]; | 30 extern const char kSamplePakContents[]; |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
66 *value = GetLocalizedStringMock(message_id); | 69 *value = GetLocalizedStringMock(message_id); |
67 return true; | 70 return true; |
68 } | 71 } |
69 MOCK_METHOD1(GetFontMock, gfx::Font*(ui::ResourceBundle::FontStyle style)); | 72 MOCK_METHOD1(GetFontMock, gfx::Font*(ui::ResourceBundle::FontStyle style)); |
70 virtual scoped_ptr<gfx::Font> GetFont( | 73 virtual scoped_ptr<gfx::Font> GetFont( |
71 ui::ResourceBundle::FontStyle style) OVERRIDE { | 74 ui::ResourceBundle::FontStyle style) OVERRIDE { |
72 return scoped_ptr<gfx::Font>(GetFontMock(style)); | 75 return scoped_ptr<gfx::Font>(GetFontMock(style)); |
73 } | 76 } |
74 }; | 77 }; |
75 | 78 |
| 79 // Creates datapack at |path| with a single bitmap at resource ID 3 |
| 80 // which is |edge_size|x|edge_size| pixels. |
| 81 void CreateDataPackWithSingleBitmap(const FilePath& path, |
| 82 int edge_size) { |
| 83 SkBitmap bitmap; |
| 84 bitmap.setConfig(SkBitmap::kARGB_8888_Config, edge_size, edge_size); |
| 85 bitmap.allocPixels(); |
| 86 std::vector<unsigned char> bitmap_data; |
| 87 EXPECT_TRUE(gfx::PNGCodec::EncodeBGRASkBitmap(bitmap, false, &bitmap_data)); |
| 88 |
| 89 std::map<uint16, base::StringPiece> resources; |
| 90 resources[3u] = base::StringPiece( |
| 91 reinterpret_cast<const char*>(&bitmap_data[0]), bitmap_data.size()); |
| 92 DataPack::WritePack(path, resources, ui::DataPack::BINARY); |
| 93 } |
| 94 |
76 } // namespace | 95 } // namespace |
77 | 96 |
78 TEST(ResourceBundle, DelegateGetPathForResourcePack) { | 97 TEST(ResourceBundle, DelegateGetPathForResourcePack) { |
79 MockResourceBundleDelegate delegate; | 98 MockResourceBundleDelegate delegate; |
80 ResourceBundle resource_bundle(&delegate); | 99 ResourceBundle resource_bundle(&delegate); |
81 | 100 |
82 FilePath pack_path(FILE_PATH_LITERAL("/path/to/test_path.pak")); | 101 FilePath pack_path(FILE_PATH_LITERAL("/path/to/test_path.pak")); |
83 ui::ScaleFactor pack_scale_factor = ui::SCALE_FACTOR_200P; | 102 ui::ScaleFactor pack_scale_factor = ui::SCALE_FACTOR_200P; |
84 | 103 |
85 EXPECT_CALL(delegate, | 104 EXPECT_CALL(delegate, |
(...skipping 214 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
300 } | 319 } |
301 | 320 |
302 TEST(ResourceBundle, LocaleDataPakExists) { | 321 TEST(ResourceBundle, LocaleDataPakExists) { |
303 ResourceBundle resource_bundle(NULL); | 322 ResourceBundle resource_bundle(NULL); |
304 | 323 |
305 // Check that ResourceBundle::LocaleDataPakExists returns the correct results. | 324 // Check that ResourceBundle::LocaleDataPakExists returns the correct results. |
306 EXPECT_TRUE(resource_bundle.LocaleDataPakExists("en-US")); | 325 EXPECT_TRUE(resource_bundle.LocaleDataPakExists("en-US")); |
307 EXPECT_FALSE(resource_bundle.LocaleDataPakExists("not_a_real_locale")); | 326 EXPECT_FALSE(resource_bundle.LocaleDataPakExists("not_a_real_locale")); |
308 } | 327 } |
309 | 328 |
| 329 // Test requesting image reps at various scale factors from the image returned |
| 330 // via ResourceBundle::GetImageNamed(). |
| 331 TEST(ResourceBundle, GetImageNamed) { |
| 332 // On Windows, the default data is compiled into the binary so this does |
| 333 // nothing. |
| 334 ScopedTempDir dir; |
| 335 ASSERT_TRUE(dir.CreateUniqueTempDir()); |
| 336 |
| 337 FilePath locale_path = dir.path().Append(FILE_PATH_LITERAL("empty.pak")); |
| 338 FilePath data_path = dir.path().Append(FILE_PATH_LITERAL("sample.pak")); |
| 339 FilePath data_2x_path = dir.path().Append(FILE_PATH_LITERAL("sample_2x.pak")); |
| 340 |
| 341 { |
| 342 // Create the pak files. |
| 343 ASSERT_EQ(file_util::WriteFile(locale_path, kEmptyPakContents, |
| 344 kEmptyPakSize), static_cast<int>(kEmptyPakSize)); |
| 345 CreateDataPackWithSingleBitmap(data_path, 10); |
| 346 CreateDataPackWithSingleBitmap(data_2x_path, 20); |
| 347 |
| 348 // Load the regular and 2x pak files. |
| 349 ResourceBundle resource_bundle(NULL); |
| 350 resource_bundle.LoadTestResources(data_path, locale_path); |
| 351 resource_bundle.AddDataPackFromPath(data_2x_path, SCALE_FACTOR_200P); |
| 352 |
| 353 gfx::ImageSkia* image_skia = resource_bundle.GetImageSkiaNamed(3); |
| 354 |
| 355 // Resource ID 3 exists in both 1x and 2x paks. Image reps should be |
| 356 // available for both scale factors in |image_skia|. |
| 357 gfx::ImageSkiaRep image_rep = |
| 358 image_skia->GetRepresentation(ui::SCALE_FACTOR_100P); |
| 359 EXPECT_EQ(ui::SCALE_FACTOR_100P, image_rep.scale_factor()); |
| 360 image_rep = image_skia->GetRepresentation(ui::SCALE_FACTOR_200P); |
| 361 EXPECT_EQ(ui::SCALE_FACTOR_200P, image_rep.scale_factor()); |
| 362 |
| 363 // The 1.4x pack was not loaded. Requesting the 1.4x resource should return |
| 364 // either the 1x or the 2x resource. |
| 365 image_rep = image_skia->GetRepresentation(ui::SCALE_FACTOR_140P); |
| 366 EXPECT_TRUE(image_rep.scale_factor() == ui::SCALE_FACTOR_100P || |
| 367 image_rep.scale_factor() == ui::SCALE_FACTOR_200P); |
| 368 } |
| 369 } |
| 370 |
310 } // namespace ui | 371 } // namespace ui |
OLD | NEW |