| 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/logging.h" |
| 10 #include "base/memory/ref_counted_memory.h" | 11 #include "base/memory/ref_counted_memory.h" |
| 11 #include "base/path_service.h" | 12 #include "base/path_service.h" |
| 12 #include "base/scoped_temp_dir.h" | 13 #include "base/scoped_temp_dir.h" |
| 13 #include "base/utf_string_conversions.h" | 14 #include "base/utf_string_conversions.h" |
| 14 #include "testing/gmock/include/gmock/gmock.h" | 15 #include "testing/gmock/include/gmock/gmock.h" |
| 15 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
| 16 #include "third_party/skia/include/core/SkBitmap.h" | 17 #include "third_party/skia/include/core/SkBitmap.h" |
| 17 #include "ui/base/layout.h" | 18 #include "ui/base/layout.h" |
| 18 #include "ui/base/resource/data_pack.h" | 19 #include "ui/base/resource/data_pack.h" |
| 19 #include "ui/gfx/codec/png_codec.h" | 20 #include "ui/gfx/codec/png_codec.h" |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 87 EXPECT_TRUE(gfx::PNGCodec::EncodeBGRASkBitmap(bitmap, false, &bitmap_data)); | 88 EXPECT_TRUE(gfx::PNGCodec::EncodeBGRASkBitmap(bitmap, false, &bitmap_data)); |
| 88 | 89 |
| 89 std::map<uint16, base::StringPiece> resources; | 90 std::map<uint16, base::StringPiece> resources; |
| 90 resources[3u] = base::StringPiece( | 91 resources[3u] = base::StringPiece( |
| 91 reinterpret_cast<const char*>(&bitmap_data[0]), bitmap_data.size()); | 92 reinterpret_cast<const char*>(&bitmap_data[0]), bitmap_data.size()); |
| 92 DataPack::WritePack(path, resources, ui::DataPack::BINARY); | 93 DataPack::WritePack(path, resources, ui::DataPack::BINARY); |
| 93 } | 94 } |
| 94 | 95 |
| 95 } // namespace | 96 } // namespace |
| 96 | 97 |
| 97 TEST(ResourceBundle, DelegateGetPathForResourcePack) { | 98 class ResourceBundleTest : public testing::Test { |
| 99 public: |
| 100 ResourceBundleTest() : resource_bundle_(NULL) { |
| 101 } |
| 102 |
| 103 virtual ~ResourceBundleTest() { |
| 104 } |
| 105 |
| 106 // Overridden from testing::Test: |
| 107 virtual void TearDown() OVERRIDE { |
| 108 delete resource_bundle_; |
| 109 } |
| 110 |
| 111 // Returns new ResoureBundle with the specified |delegate|. The |
| 112 // ResourceBundleTest class manages the lifetime of the returned |
| 113 // ResourceBundle. |
| 114 ResourceBundle* CreateResourceBundle(ResourceBundle::Delegate* delegate) { |
| 115 DCHECK(!resource_bundle_); |
| 116 |
| 117 resource_bundle_ = new ResourceBundle(delegate); |
| 118 return resource_bundle_; |
| 119 } |
| 120 |
| 121 protected: |
| 122 ResourceBundle* resource_bundle_; |
| 123 |
| 124 private: |
| 125 DISALLOW_COPY_AND_ASSIGN(ResourceBundleTest); |
| 126 }; |
| 127 |
| 128 TEST_F(ResourceBundleTest, DelegateGetPathForResourcePack) { |
| 98 MockResourceBundleDelegate delegate; | 129 MockResourceBundleDelegate delegate; |
| 99 ResourceBundle resource_bundle(&delegate); | 130 ResourceBundle* resource_bundle = CreateResourceBundle(&delegate); |
| 100 | 131 |
| 101 FilePath pack_path(FILE_PATH_LITERAL("/path/to/test_path.pak")); | 132 FilePath pack_path(FILE_PATH_LITERAL("/path/to/test_path.pak")); |
| 102 ui::ScaleFactor pack_scale_factor = ui::SCALE_FACTOR_200P; | 133 ui::ScaleFactor pack_scale_factor = ui::SCALE_FACTOR_200P; |
| 103 | 134 |
| 104 EXPECT_CALL(delegate, | 135 EXPECT_CALL(delegate, |
| 105 GetPathForResourcePack( | 136 GetPathForResourcePack( |
| 106 Property(&FilePath::value, pack_path.value()), | 137 Property(&FilePath::value, pack_path.value()), |
| 107 pack_scale_factor)) | 138 pack_scale_factor)) |
| 108 .Times(1) | 139 .Times(1) |
| 109 .WillOnce(Return(pack_path)); | 140 .WillOnce(Return(pack_path)); |
| 110 | 141 |
| 111 resource_bundle.AddDataPackFromPath(pack_path, pack_scale_factor); | 142 resource_bundle->AddDataPackFromPath(pack_path, pack_scale_factor); |
| 112 } | 143 } |
| 113 | 144 |
| 114 TEST(ResourceBundle, DelegateGetPathForLocalePack) { | 145 TEST_F(ResourceBundleTest, DelegateGetPathForLocalePack) { |
| 115 MockResourceBundleDelegate delegate; | 146 MockResourceBundleDelegate delegate; |
| 116 ResourceBundle resource_bundle(&delegate); | 147 ResourceBundle* resource_bundle = CreateResourceBundle(&delegate); |
| 117 | 148 |
| 118 std::string locale = "en-US"; | 149 std::string locale = "en-US"; |
| 119 | 150 |
| 120 // Cancel the load. | 151 // Cancel the load. |
| 121 EXPECT_CALL(delegate, GetPathForLocalePack(_, locale)) | 152 EXPECT_CALL(delegate, GetPathForLocalePack(_, locale)) |
| 122 .Times(2) | 153 .Times(2) |
| 123 .WillRepeatedly(Return(FilePath())) | 154 .WillRepeatedly(Return(FilePath())) |
| 124 .RetiresOnSaturation(); | 155 .RetiresOnSaturation(); |
| 125 | 156 |
| 126 EXPECT_FALSE(resource_bundle.LocaleDataPakExists(locale)); | 157 EXPECT_FALSE(resource_bundle->LocaleDataPakExists(locale)); |
| 127 EXPECT_EQ("", resource_bundle.LoadLocaleResources(locale)); | 158 EXPECT_EQ("", resource_bundle->LoadLocaleResources(locale)); |
| 128 | 159 |
| 129 // Allow the load to proceed. | 160 // Allow the load to proceed. |
| 130 EXPECT_CALL(delegate, GetPathForLocalePack(_, locale)) | 161 EXPECT_CALL(delegate, GetPathForLocalePack(_, locale)) |
| 131 .Times(2) | 162 .Times(2) |
| 132 .WillRepeatedly(ReturnArg<0>()); | 163 .WillRepeatedly(ReturnArg<0>()); |
| 133 | 164 |
| 134 EXPECT_TRUE(resource_bundle.LocaleDataPakExists(locale)); | 165 EXPECT_TRUE(resource_bundle->LocaleDataPakExists(locale)); |
| 135 EXPECT_EQ(locale, resource_bundle.LoadLocaleResources(locale)); | 166 EXPECT_EQ(locale, resource_bundle->LoadLocaleResources(locale)); |
| 136 } | 167 } |
| 137 | 168 |
| 138 TEST(ResourceBundle, DelegateGetImageNamed) { | 169 TEST_F(ResourceBundleTest, DelegateGetImageNamed) { |
| 139 MockResourceBundleDelegate delegate; | 170 MockResourceBundleDelegate delegate; |
| 140 ResourceBundle resource_bundle(&delegate); | 171 ResourceBundle* resource_bundle = CreateResourceBundle(&delegate); |
| 141 | 172 |
| 142 gfx::Image empty_image = resource_bundle.GetEmptyImage(); | 173 gfx::Image empty_image = resource_bundle->GetEmptyImage(); |
| 143 int resource_id = 5; | 174 int resource_id = 5; |
| 144 | 175 |
| 145 EXPECT_CALL(delegate, GetImageNamed(resource_id)) | 176 EXPECT_CALL(delegate, GetImageNamed(resource_id)) |
| 146 .Times(1) | 177 .Times(1) |
| 147 .WillOnce(Return(empty_image)); | 178 .WillOnce(Return(empty_image)); |
| 148 | 179 |
| 149 gfx::Image result = resource_bundle.GetImageNamed(resource_id); | 180 gfx::Image result = resource_bundle->GetImageNamed(resource_id); |
| 150 EXPECT_EQ(empty_image.ToSkBitmap(), result.ToSkBitmap()); | 181 EXPECT_EQ(empty_image.ToSkBitmap(), result.ToSkBitmap()); |
| 151 } | 182 } |
| 152 | 183 |
| 153 TEST(ResourceBundle, DelegateGetNativeImageNamed) { | 184 TEST_F(ResourceBundleTest, DelegateGetNativeImageNamed) { |
| 154 MockResourceBundleDelegate delegate; | 185 MockResourceBundleDelegate delegate; |
| 155 ResourceBundle resource_bundle(&delegate); | 186 ResourceBundle* resource_bundle = CreateResourceBundle(&delegate); |
| 156 | 187 |
| 157 gfx::Image empty_image = resource_bundle.GetEmptyImage(); | 188 gfx::Image empty_image = resource_bundle->GetEmptyImage(); |
| 158 int resource_id = 5; | 189 int resource_id = 5; |
| 159 | 190 |
| 160 // Some platforms delegate GetNativeImageNamed calls to GetImageNamed. | 191 // Some platforms delegate GetNativeImageNamed calls to GetImageNamed. |
| 161 EXPECT_CALL(delegate, GetImageNamed(resource_id)) | 192 EXPECT_CALL(delegate, GetImageNamed(resource_id)) |
| 162 .Times(Between(0, 1)) | 193 .Times(Between(0, 1)) |
| 163 .WillOnce(Return(empty_image)); | 194 .WillOnce(Return(empty_image)); |
| 164 EXPECT_CALL(delegate, | 195 EXPECT_CALL(delegate, |
| 165 GetNativeImageNamed(resource_id, ui::ResourceBundle::RTL_DISABLED)) | 196 GetNativeImageNamed(resource_id, ui::ResourceBundle::RTL_DISABLED)) |
| 166 .Times(Between(0, 1)) | 197 .Times(Between(0, 1)) |
| 167 .WillOnce(Return(empty_image)); | 198 .WillOnce(Return(empty_image)); |
| 168 | 199 |
| 169 gfx::Image result = resource_bundle.GetNativeImageNamed(resource_id); | 200 gfx::Image result = resource_bundle->GetNativeImageNamed(resource_id); |
| 170 EXPECT_EQ(empty_image.ToSkBitmap(), result.ToSkBitmap()); | 201 EXPECT_EQ(empty_image.ToSkBitmap(), result.ToSkBitmap()); |
| 171 } | 202 } |
| 172 | 203 |
| 173 TEST(ResourceBundle, DelegateLoadDataResourceBytes) { | 204 TEST_F(ResourceBundleTest, DelegateLoadDataResourceBytes) { |
| 174 MockResourceBundleDelegate delegate; | 205 MockResourceBundleDelegate delegate; |
| 175 ResourceBundle resource_bundle(&delegate); | 206 ResourceBundle* resource_bundle = CreateResourceBundle(&delegate); |
| 176 | 207 |
| 177 // Create the data resource for testing purposes. | 208 // Create the data resource for testing purposes. |
| 178 unsigned char data[] = "My test data"; | 209 unsigned char data[] = "My test data"; |
| 179 scoped_refptr<base::RefCountedStaticMemory> static_memory( | 210 scoped_refptr<base::RefCountedStaticMemory> static_memory( |
| 180 new base::RefCountedStaticMemory(data, sizeof(data))); | 211 new base::RefCountedStaticMemory(data, sizeof(data))); |
| 181 | 212 |
| 182 int resource_id = 5; | 213 int resource_id = 5; |
| 183 ui::ScaleFactor scale_factor = ui::SCALE_FACTOR_NONE; | 214 ui::ScaleFactor scale_factor = ui::SCALE_FACTOR_NONE; |
| 184 | 215 |
| 185 EXPECT_CALL(delegate, LoadDataResourceBytes(resource_id, scale_factor)) | 216 EXPECT_CALL(delegate, LoadDataResourceBytes(resource_id, scale_factor)) |
| 186 .Times(1) | 217 .Times(1) |
| 187 .WillOnce(Return(static_memory)); | 218 .WillOnce(Return(static_memory)); |
| 188 | 219 |
| 189 scoped_refptr<base::RefCountedStaticMemory> result = | 220 scoped_refptr<base::RefCountedStaticMemory> result = |
| 190 resource_bundle.LoadDataResourceBytesForScale(resource_id, scale_factor); | 221 resource_bundle->LoadDataResourceBytesForScale(resource_id, scale_factor); |
| 191 EXPECT_EQ(static_memory, result); | 222 EXPECT_EQ(static_memory, result); |
| 192 } | 223 } |
| 193 | 224 |
| 194 TEST(ResourceBundle, DelegateGetRawDataResource) { | 225 TEST_F(ResourceBundleTest, DelegateGetRawDataResource) { |
| 195 MockResourceBundleDelegate delegate; | 226 MockResourceBundleDelegate delegate; |
| 196 ResourceBundle resource_bundle(&delegate); | 227 ResourceBundle* resource_bundle = CreateResourceBundle(&delegate); |
| 197 | 228 |
| 198 // Create the string piece for testing purposes. | 229 // Create the string piece for testing purposes. |
| 199 char data[] = "My test data"; | 230 char data[] = "My test data"; |
| 200 base::StringPiece string_piece(data); | 231 base::StringPiece string_piece(data); |
| 201 | 232 |
| 202 int resource_id = 5; | 233 int resource_id = 5; |
| 203 | 234 |
| 204 EXPECT_CALL(delegate, GetRawDataResourceMock( | 235 EXPECT_CALL(delegate, GetRawDataResourceMock( |
| 205 resource_id, ui::SCALE_FACTOR_NONE)) | 236 resource_id, ui::SCALE_FACTOR_NONE)) |
| 206 .Times(1) | 237 .Times(1) |
| 207 .WillOnce(Return(string_piece)); | 238 .WillOnce(Return(string_piece)); |
| 208 | 239 |
| 209 base::StringPiece result = resource_bundle.GetRawDataResource( | 240 base::StringPiece result = resource_bundle->GetRawDataResource( |
| 210 resource_id); | 241 resource_id); |
| 211 EXPECT_EQ(string_piece.data(), result.data()); | 242 EXPECT_EQ(string_piece.data(), result.data()); |
| 212 } | 243 } |
| 213 | 244 |
| 214 TEST(ResourceBundle, DelegateGetLocalizedString) { | 245 TEST_F(ResourceBundleTest, DelegateGetLocalizedString) { |
| 215 MockResourceBundleDelegate delegate; | 246 MockResourceBundleDelegate delegate; |
| 216 ResourceBundle resource_bundle(&delegate); | 247 ResourceBundle* resource_bundle = CreateResourceBundle(&delegate); |
| 217 | 248 |
| 218 string16 data = ASCIIToUTF16("My test data"); | 249 string16 data = ASCIIToUTF16("My test data"); |
| 219 int resource_id = 5; | 250 int resource_id = 5; |
| 220 | 251 |
| 221 EXPECT_CALL(delegate, GetLocalizedStringMock(resource_id)) | 252 EXPECT_CALL(delegate, GetLocalizedStringMock(resource_id)) |
| 222 .Times(1) | 253 .Times(1) |
| 223 .WillOnce(Return(data)); | 254 .WillOnce(Return(data)); |
| 224 | 255 |
| 225 string16 result = resource_bundle.GetLocalizedString(resource_id); | 256 string16 result = resource_bundle->GetLocalizedString(resource_id); |
| 226 EXPECT_EQ(data, result); | 257 EXPECT_EQ(data, result); |
| 227 } | 258 } |
| 228 | 259 |
| 229 TEST(ResourceBundle, DelegateGetFont) { | 260 TEST_F(ResourceBundleTest, DelegateGetFont) { |
| 230 MockResourceBundleDelegate delegate; | 261 MockResourceBundleDelegate delegate; |
| 231 ResourceBundle resource_bundle(&delegate); | 262 ResourceBundle* resource_bundle = CreateResourceBundle(&delegate); |
| 232 | 263 |
| 233 // Should be called once for each font type. When we return NULL the default | 264 // Should be called once for each font type. When we return NULL the default |
| 234 // font will be created. | 265 // font will be created. |
| 235 gfx::Font* test_font = NULL; | 266 gfx::Font* test_font = NULL; |
| 236 EXPECT_CALL(delegate, GetFontMock(_)) | 267 EXPECT_CALL(delegate, GetFontMock(_)) |
| 237 .Times(7) | 268 .Times(7) |
| 238 .WillRepeatedly(Return(test_font)); | 269 .WillRepeatedly(Return(test_font)); |
| 239 | 270 |
| 240 const gfx::Font* font = | 271 const gfx::Font* font = |
| 241 &resource_bundle.GetFont(ui::ResourceBundle::BaseFont); | 272 &resource_bundle->GetFont(ui::ResourceBundle::BaseFont); |
| 242 EXPECT_TRUE(font); | 273 EXPECT_TRUE(font); |
| 243 } | 274 } |
| 244 | 275 |
| 245 TEST(ResourceBundle, LoadDataResourceBytes) { | 276 TEST_F(ResourceBundleTest, LocaleDataPakExists) { |
| 246 // On Windows, the default data is compiled into the binary so this does | 277 ResourceBundle* resource_bundle = CreateResourceBundle(NULL); |
| 247 // nothing. | |
| 248 ScopedTempDir dir; | |
| 249 ASSERT_TRUE(dir.CreateUniqueTempDir()); | |
| 250 FilePath data_path = dir.path().Append(FILE_PATH_LITERAL("sample.pak")); | |
| 251 | 278 |
| 252 // Put the ResourceBundle in a different scope so that it's destroyed before | 279 // Check that ResourceBundle::LocaleDataPakExists returns the correct results. |
| 253 // the ScopedTempDir. | 280 EXPECT_TRUE(resource_bundle->LocaleDataPakExists("en-US")); |
| 254 { | 281 EXPECT_FALSE(resource_bundle->LocaleDataPakExists("not_a_real_locale")); |
| 255 // Verify that we don't crash when trying to load a resource that is not | |
| 256 // found. In some cases, we fail to mmap resources.pak, but try to keep | |
| 257 // going anyway. | |
| 258 ResourceBundle resource_bundle(NULL); | |
| 259 | |
| 260 // Dump contents into the pak file. | |
| 261 ASSERT_EQ(file_util::WriteFile(data_path, kSamplePakContents, | |
| 262 kSamplePakSize), | |
| 263 static_cast<int>(kSamplePakSize)); | |
| 264 | |
| 265 // Create a resource bundle from the file. | |
| 266 resource_bundle.LoadTestResources(data_path, data_path); | |
| 267 | |
| 268 const int kUnfoundResourceId = 10000; | |
| 269 EXPECT_EQ(NULL, resource_bundle.LoadDataResourceBytes( | |
| 270 kUnfoundResourceId)); | |
| 271 | |
| 272 // Give a .pak file that doesn't exist so we will fail to load it. | |
| 273 resource_bundle.AddDataPackFromPath( | |
| 274 FilePath(FILE_PATH_LITERAL("non-existant-file.pak")), | |
| 275 ui::SCALE_FACTOR_NONE); | |
| 276 EXPECT_EQ(NULL, resource_bundle.LoadDataResourceBytes( | |
| 277 kUnfoundResourceId)); | |
| 278 } | |
| 279 } | 282 } |
| 280 | 283 |
| 281 TEST(ResourceBundle, GetRawDataResource) { | 284 class ResourceBundleImageTest : public ResourceBundleTest { |
| 285 public: |
| 286 ResourceBundleImageTest() : locale_pack_(NULL) { |
| 287 } |
| 282 | 288 |
| 283 // On Windows, the default data is compiled into the binary so this does | 289 virtual ~ResourceBundleImageTest() { |
| 284 // nothing. | 290 } |
| 285 ScopedTempDir dir; | |
| 286 ASSERT_TRUE(dir.CreateUniqueTempDir()); | |
| 287 FilePath locale_path = dir.path().Append(FILE_PATH_LITERAL("empty.pak")); | |
| 288 FilePath data_path = dir.path().Append(FILE_PATH_LITERAL("sample.pak")); | |
| 289 FilePath data_2x_path = dir.path().Append(FILE_PATH_LITERAL("sample_2x.pak")); | |
| 290 | 291 |
| 291 { | 292 virtual void SetUp() OVERRIDE { |
| 292 ResourceBundle resource_bundle(NULL); | 293 // Create a temporary directory to write test resource bundles to. |
| 293 // Dump contents into the pak files. | 294 ASSERT_TRUE(dir_.CreateUniqueTempDir()); |
| 294 ASSERT_EQ(file_util::WriteFile(locale_path, kEmptyPakContents, | 295 } |
| 295 kEmptyPakSize), static_cast<int>(kEmptyPakSize)); | |
| 296 ASSERT_EQ(file_util::WriteFile(data_path, kSamplePakContents, | |
| 297 kSamplePakSize), static_cast<int>(kSamplePakSize)); | |
| 298 ASSERT_EQ(file_util::WriteFile(data_2x_path, kSamplePakContents2x, | |
| 299 kSamplePakSize2x), static_cast<int>(kSamplePakSize2x)); | |
| 300 | 296 |
| 301 // Load the regular and 2x pak files. | 297 // Returns resource bundle which uses an empty data pak for locale data. |
| 302 resource_bundle.LoadTestResources(data_path, locale_path); | 298 ui::ResourceBundle* CreateResourceBundleWithEmptyLocalePak() { |
| 303 resource_bundle.AddDataPackFromPath(data_2x_path, SCALE_FACTOR_200P); | 299 // Write an empty data pak for locale data. |
| 300 const FilePath& locale_path = dir_path().Append( |
| 301 FILE_PATH_LITERAL("locale.pak")); |
| 302 EXPECT_EQ(file_util::WriteFile(locale_path, kEmptyPakContents, |
| 303 kEmptyPakSize), |
| 304 static_cast<int>(kEmptyPakSize)); |
| 304 | 305 |
| 305 // Resource ID 4 exists in both 1x and 2x paks, so we expect a different | 306 ui::ResourceBundle* resource_bundle = CreateResourceBundle(NULL); |
| 306 // result when requesting the 2x scale. | |
| 307 EXPECT_EQ("this is id 4", resource_bundle.GetRawDataResourceForScale(4, | |
| 308 SCALE_FACTOR_100P)); | |
| 309 EXPECT_EQ("this is id 4 2x", resource_bundle.GetRawDataResourceForScale(4, | |
| 310 SCALE_FACTOR_200P)); | |
| 311 | 307 |
| 312 // Resource ID 6 only exists in the 1x pak so we expect the same resource | 308 // Load the empty locale data pak. |
| 313 // for both scale factor requests. | 309 resource_bundle->LoadTestResources(FilePath(), locale_path); |
| 314 EXPECT_EQ("this is id 6", resource_bundle.GetRawDataResourceForScale(6, | 310 return resource_bundle; |
| 315 SCALE_FACTOR_100P)); | |
| 316 EXPECT_EQ("this is id 6", resource_bundle.GetRawDataResourceForScale(6, | |
| 317 SCALE_FACTOR_200P)); | |
| 318 } | 311 } |
| 312 |
| 313 // Returns the path of temporary directory to write test data packs into. |
| 314 const FilePath& dir_path() { return dir_.path(); } |
| 315 |
| 316 private: |
| 317 scoped_ptr<DataPack> locale_pack_; |
| 318 ScopedTempDir dir_; |
| 319 |
| 320 DISALLOW_COPY_AND_ASSIGN(ResourceBundleImageTest); |
| 321 }; |
| 322 |
| 323 // Verify that we don't crash when trying to load a resource that is not found. |
| 324 // In some cases, we fail to mmap resources.pak, but try to keep going anyway. |
| 325 TEST_F(ResourceBundleImageTest, LoadDataResourceBytes) { |
| 326 FilePath data_path = dir_path().Append(FILE_PATH_LITERAL("sample.pak")); |
| 327 |
| 328 // Dump contents into the pak files. |
| 329 ASSERT_EQ(file_util::WriteFile(data_path, kEmptyPakContents, |
| 330 kEmptyPakSize), static_cast<int>(kEmptyPakSize)); |
| 331 |
| 332 // Create a resource bundle from the file. |
| 333 ResourceBundle* resource_bundle = CreateResourceBundleWithEmptyLocalePak(); |
| 334 resource_bundle->AddDataPackFromPath(data_path, SCALE_FACTOR_100P); |
| 335 |
| 336 const int kUnfoundResourceId = 10000; |
| 337 EXPECT_EQ(NULL, resource_bundle->LoadDataResourceBytes( |
| 338 kUnfoundResourceId)); |
| 339 |
| 340 // Give a .pak file that doesn't exist so we will fail to load it. |
| 341 resource_bundle->AddDataPackFromPath( |
| 342 FilePath(FILE_PATH_LITERAL("non-existant-file.pak")), |
| 343 ui::SCALE_FACTOR_NONE); |
| 344 EXPECT_EQ(NULL, resource_bundle->LoadDataResourceBytes( |
| 345 kUnfoundResourceId)); |
| 319 } | 346 } |
| 320 | 347 |
| 321 TEST(ResourceBundle, LocaleDataPakExists) { | 348 TEST_F(ResourceBundleImageTest, GetRawDataResource) { |
| 322 ResourceBundle resource_bundle(NULL); | 349 FilePath data_path = dir_path().Append(FILE_PATH_LITERAL("sample.pak")); |
| 350 FilePath data_2x_path = dir_path().Append(FILE_PATH_LITERAL("sample_2x.pak")); |
| 323 | 351 |
| 324 // Check that ResourceBundle::LocaleDataPakExists returns the correct results. | 352 // Dump contents into the pak files. |
| 325 EXPECT_TRUE(resource_bundle.LocaleDataPakExists("en-US")); | 353 ASSERT_EQ(file_util::WriteFile(data_path, kSamplePakContents, |
| 326 EXPECT_FALSE(resource_bundle.LocaleDataPakExists("not_a_real_locale")); | 354 kSamplePakSize), static_cast<int>(kSamplePakSize)); |
| 355 ASSERT_EQ(file_util::WriteFile(data_2x_path, kSamplePakContents2x, |
| 356 kSamplePakSize2x), static_cast<int>(kSamplePakSize2x)); |
| 357 |
| 358 // Load the regular and 2x pak files. |
| 359 ResourceBundle* resource_bundle = CreateResourceBundleWithEmptyLocalePak(); |
| 360 resource_bundle->AddDataPackFromPath(data_path, SCALE_FACTOR_100P); |
| 361 resource_bundle->AddDataPackFromPath(data_2x_path, SCALE_FACTOR_200P); |
| 362 |
| 363 // Resource ID 4 exists in both 1x and 2x paks, so we expect a different |
| 364 // result when requesting the 2x scale. |
| 365 EXPECT_EQ("this is id 4", resource_bundle->GetRawDataResourceForScale(4, |
| 366 SCALE_FACTOR_100P)); |
| 367 EXPECT_EQ("this is id 4 2x", resource_bundle->GetRawDataResourceForScale(4, |
| 368 SCALE_FACTOR_200P)); |
| 369 |
| 370 // Resource ID 6 only exists in the 1x pak so we expect the same resource |
| 371 // for both scale factor requests. |
| 372 EXPECT_EQ("this is id 6", resource_bundle->GetRawDataResourceForScale(6, |
| 373 SCALE_FACTOR_100P)); |
| 374 EXPECT_EQ("this is id 6", resource_bundle->GetRawDataResourceForScale(6, |
| 375 SCALE_FACTOR_200P)); |
| 327 } | 376 } |
| 328 | 377 |
| 329 // Test requesting image reps at various scale factors from the image returned | 378 // Test requesting image reps at various scale factors from the image returned |
| 330 // via ResourceBundle::GetImageNamed(). | 379 // via ResourceBundle::GetImageNamed(). |
| 331 TEST(ResourceBundle, GetImageNamed) { | 380 TEST_F(ResourceBundleImageTest, GetImageNamed) { |
| 332 // On Windows, the default data is compiled into the binary so this does | 381 FilePath data_path = dir_path().Append(FILE_PATH_LITERAL("sample.pak")); |
| 333 // nothing. | 382 FilePath data_2x_path = dir_path().Append(FILE_PATH_LITERAL("sample_2x.pak")); |
| 334 ScopedTempDir dir; | |
| 335 ASSERT_TRUE(dir.CreateUniqueTempDir()); | |
| 336 | 383 |
| 337 FilePath locale_path = dir.path().Append(FILE_PATH_LITERAL("empty.pak")); | 384 // Create the pak files. |
| 338 FilePath data_path = dir.path().Append(FILE_PATH_LITERAL("sample.pak")); | 385 CreateDataPackWithSingleBitmap(data_path, 10); |
| 339 FilePath data_2x_path = dir.path().Append(FILE_PATH_LITERAL("sample_2x.pak")); | 386 CreateDataPackWithSingleBitmap(data_2x_path, 20); |
| 340 | 387 |
| 341 { | 388 // Load the regular and 2x pak files. |
| 342 // Create the pak files. | 389 ResourceBundle* resource_bundle = CreateResourceBundleWithEmptyLocalePak(); |
| 343 ASSERT_EQ(file_util::WriteFile(locale_path, kEmptyPakContents, | 390 resource_bundle->AddDataPackFromPath(data_path, SCALE_FACTOR_100P); |
| 344 kEmptyPakSize), static_cast<int>(kEmptyPakSize)); | 391 resource_bundle->AddDataPackFromPath(data_2x_path, SCALE_FACTOR_200P); |
| 345 CreateDataPackWithSingleBitmap(data_path, 10); | |
| 346 CreateDataPackWithSingleBitmap(data_2x_path, 20); | |
| 347 | 392 |
| 348 // Load the regular and 2x pak files. | 393 gfx::ImageSkia* image_skia = resource_bundle->GetImageSkiaNamed(3); |
| 349 ResourceBundle resource_bundle(NULL); | |
| 350 resource_bundle.LoadTestResources(data_path, locale_path); | |
| 351 resource_bundle.AddDataPackFromPath(data_2x_path, SCALE_FACTOR_200P); | |
| 352 | 394 |
| 353 gfx::ImageSkia* image_skia = resource_bundle.GetImageSkiaNamed(3); | 395 // Resource ID 3 exists in both 1x and 2x paks. Image reps should be |
| 396 // available for both scale factors in |image_skia|. |
| 397 gfx::ImageSkiaRep image_rep = |
| 398 image_skia->GetRepresentation(ui::SCALE_FACTOR_100P); |
| 399 EXPECT_EQ(ui::SCALE_FACTOR_100P, image_rep.scale_factor()); |
| 400 image_rep = image_skia->GetRepresentation(ui::SCALE_FACTOR_200P); |
| 401 EXPECT_EQ(ui::SCALE_FACTOR_200P, image_rep.scale_factor()); |
| 354 | 402 |
| 355 // Resource ID 3 exists in both 1x and 2x paks. Image reps should be | 403 // The 1.4x pack was not loaded. Requesting the 1.4x resource should return |
| 356 // available for both scale factors in |image_skia|. | 404 // either the 1x or the 2x resource. |
| 357 gfx::ImageSkiaRep image_rep = | 405 image_rep = image_skia->GetRepresentation(ui::SCALE_FACTOR_140P); |
| 358 image_skia->GetRepresentation(ui::SCALE_FACTOR_100P); | 406 EXPECT_TRUE(image_rep.scale_factor() == ui::SCALE_FACTOR_100P || |
| 359 EXPECT_EQ(ui::SCALE_FACTOR_100P, image_rep.scale_factor()); | 407 image_rep.scale_factor() == ui::SCALE_FACTOR_200P); |
| 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 } | 408 } |
| 370 | 409 |
| 371 } // namespace ui | 410 } // namespace ui |
| OLD | NEW |