| 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/big_endian.h" | 8 #include "base/big_endian.h" | 
| 9 #include "base/file_util.h" | 9 #include "base/file_util.h" | 
| 10 #include "base/files/file_path.h" | 10 #include "base/files/file_path.h" | 
| (...skipping 341 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 352   virtual void SetUp() OVERRIDE { | 352   virtual void SetUp() OVERRIDE { | 
| 353     // Create a temporary directory to write test resource bundles to. | 353     // Create a temporary directory to write test resource bundles to. | 
| 354     ASSERT_TRUE(dir_.CreateUniqueTempDir()); | 354     ASSERT_TRUE(dir_.CreateUniqueTempDir()); | 
| 355   } | 355   } | 
| 356 | 356 | 
| 357   // Returns resource bundle which uses an empty data pak for locale data. | 357   // Returns resource bundle which uses an empty data pak for locale data. | 
| 358   ui::ResourceBundle* CreateResourceBundleWithEmptyLocalePak() { | 358   ui::ResourceBundle* CreateResourceBundleWithEmptyLocalePak() { | 
| 359     // Write an empty data pak for locale data. | 359     // Write an empty data pak for locale data. | 
| 360     const base::FilePath& locale_path = dir_path().Append( | 360     const base::FilePath& locale_path = dir_path().Append( | 
| 361         FILE_PATH_LITERAL("locale.pak")); | 361         FILE_PATH_LITERAL("locale.pak")); | 
| 362     EXPECT_EQ(file_util::WriteFile(locale_path, kEmptyPakContents, | 362     EXPECT_EQ(base::WriteFile(locale_path, kEmptyPakContents, | 
| 363                                    kEmptyPakSize), | 363                                    kEmptyPakSize), | 
| 364               static_cast<int>(kEmptyPakSize)); | 364               static_cast<int>(kEmptyPakSize)); | 
| 365 | 365 | 
| 366     ui::ResourceBundle* resource_bundle = CreateResourceBundle(NULL); | 366     ui::ResourceBundle* resource_bundle = CreateResourceBundle(NULL); | 
| 367 | 367 | 
| 368     // Load the empty locale data pak. | 368     // Load the empty locale data pak. | 
| 369     resource_bundle->LoadTestResources(base::FilePath(), locale_path); | 369     resource_bundle->LoadTestResources(base::FilePath(), locale_path); | 
| 370     return resource_bundle; | 370     return resource_bundle; | 
| 371   } | 371   } | 
| 372 | 372 | 
| 373   // Returns the path of temporary directory to write test data packs into. | 373   // Returns the path of temporary directory to write test data packs into. | 
| 374   const base::FilePath& dir_path() { return dir_.path(); } | 374   const base::FilePath& dir_path() { return dir_.path(); } | 
| 375 | 375 | 
| 376  private: | 376  private: | 
| 377   scoped_ptr<DataPack> locale_pack_; | 377   scoped_ptr<DataPack> locale_pack_; | 
| 378   base::ScopedTempDir dir_; | 378   base::ScopedTempDir dir_; | 
| 379 | 379 | 
| 380   DISALLOW_COPY_AND_ASSIGN(ResourceBundleImageTest); | 380   DISALLOW_COPY_AND_ASSIGN(ResourceBundleImageTest); | 
| 381 }; | 381 }; | 
| 382 | 382 | 
| 383 // Verify that we don't crash when trying to load a resource that is not found. | 383 // Verify that we don't crash when trying to load a resource that is not found. | 
| 384 // In some cases, we fail to mmap resources.pak, but try to keep going anyway. | 384 // In some cases, we fail to mmap resources.pak, but try to keep going anyway. | 
| 385 TEST_F(ResourceBundleImageTest, LoadDataResourceBytes) { | 385 TEST_F(ResourceBundleImageTest, LoadDataResourceBytes) { | 
| 386   base::FilePath data_path = dir_path().Append(FILE_PATH_LITERAL("sample.pak")); | 386   base::FilePath data_path = dir_path().Append(FILE_PATH_LITERAL("sample.pak")); | 
| 387 | 387 | 
| 388   // Dump contents into the pak files. | 388   // Dump contents into the pak files. | 
| 389   ASSERT_EQ(file_util::WriteFile(data_path, kEmptyPakContents, | 389   ASSERT_EQ(base::WriteFile(data_path, kEmptyPakContents, | 
| 390       kEmptyPakSize), static_cast<int>(kEmptyPakSize)); | 390       kEmptyPakSize), static_cast<int>(kEmptyPakSize)); | 
| 391 | 391 | 
| 392   // Create a resource bundle from the file. | 392   // Create a resource bundle from the file. | 
| 393   ResourceBundle* resource_bundle = CreateResourceBundleWithEmptyLocalePak(); | 393   ResourceBundle* resource_bundle = CreateResourceBundleWithEmptyLocalePak(); | 
| 394   resource_bundle->AddDataPackFromPath(data_path, SCALE_FACTOR_100P); | 394   resource_bundle->AddDataPackFromPath(data_path, SCALE_FACTOR_100P); | 
| 395 | 395 | 
| 396   const int kUnfoundResourceId = 10000; | 396   const int kUnfoundResourceId = 10000; | 
| 397   EXPECT_EQ(NULL, resource_bundle->LoadDataResourceBytes( | 397   EXPECT_EQ(NULL, resource_bundle->LoadDataResourceBytes( | 
| 398       kUnfoundResourceId)); | 398       kUnfoundResourceId)); | 
| 399 | 399 | 
| 400   // Give a .pak file that doesn't exist so we will fail to load it. | 400   // Give a .pak file that doesn't exist so we will fail to load it. | 
| 401   resource_bundle->AddDataPackFromPath( | 401   resource_bundle->AddDataPackFromPath( | 
| 402       base::FilePath(FILE_PATH_LITERAL("non-existant-file.pak")), | 402       base::FilePath(FILE_PATH_LITERAL("non-existant-file.pak")), | 
| 403       ui::SCALE_FACTOR_NONE); | 403       ui::SCALE_FACTOR_NONE); | 
| 404   EXPECT_EQ(NULL, resource_bundle->LoadDataResourceBytes( | 404   EXPECT_EQ(NULL, resource_bundle->LoadDataResourceBytes( | 
| 405       kUnfoundResourceId)); | 405       kUnfoundResourceId)); | 
| 406 } | 406 } | 
| 407 | 407 | 
| 408 TEST_F(ResourceBundleImageTest, GetRawDataResource) { | 408 TEST_F(ResourceBundleImageTest, GetRawDataResource) { | 
| 409   base::FilePath data_path = dir_path().Append(FILE_PATH_LITERAL("sample.pak")); | 409   base::FilePath data_path = dir_path().Append(FILE_PATH_LITERAL("sample.pak")); | 
| 410   base::FilePath data_2x_path = | 410   base::FilePath data_2x_path = | 
| 411       dir_path().Append(FILE_PATH_LITERAL("sample_2x.pak")); | 411       dir_path().Append(FILE_PATH_LITERAL("sample_2x.pak")); | 
| 412 | 412 | 
| 413   // Dump contents into the pak files. | 413   // Dump contents into the pak files. | 
| 414   ASSERT_EQ(file_util::WriteFile(data_path, kSamplePakContents, | 414   ASSERT_EQ(base::WriteFile(data_path, kSamplePakContents, | 
| 415       kSamplePakSize), static_cast<int>(kSamplePakSize)); | 415       kSamplePakSize), static_cast<int>(kSamplePakSize)); | 
| 416   ASSERT_EQ(file_util::WriteFile(data_2x_path, kSamplePakContents2x, | 416   ASSERT_EQ(base::WriteFile(data_2x_path, kSamplePakContents2x, | 
| 417       kSamplePakSize2x), static_cast<int>(kSamplePakSize2x)); | 417       kSamplePakSize2x), static_cast<int>(kSamplePakSize2x)); | 
| 418 | 418 | 
| 419   // Load the regular and 2x pak files. | 419   // Load the regular and 2x pak files. | 
| 420   ResourceBundle* resource_bundle = CreateResourceBundleWithEmptyLocalePak(); | 420   ResourceBundle* resource_bundle = CreateResourceBundleWithEmptyLocalePak(); | 
| 421   resource_bundle->AddDataPackFromPath(data_path, SCALE_FACTOR_100P); | 421   resource_bundle->AddDataPackFromPath(data_path, SCALE_FACTOR_100P); | 
| 422   resource_bundle->AddDataPackFromPath(data_2x_path, SCALE_FACTOR_200P); | 422   resource_bundle->AddDataPackFromPath(data_2x_path, SCALE_FACTOR_200P); | 
| 423 | 423 | 
| 424   // Resource ID 4 exists in both 1x and 2x paks, so we expect a different | 424   // Resource ID 4 exists in both 1x and 2x paks, so we expect a different | 
| 425   // result when requesting the 2x scale. | 425   // result when requesting the 2x scale. | 
| 426   EXPECT_EQ("this is id 4", resource_bundle->GetRawDataResourceForScale(4, | 426   EXPECT_EQ("this is id 4", resource_bundle->GetRawDataResourceForScale(4, | 
| (...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 577   ResourceBundle* resource_bundle = CreateResourceBundleWithEmptyLocalePak(); | 577   ResourceBundle* resource_bundle = CreateResourceBundleWithEmptyLocalePak(); | 
| 578   resource_bundle->AddDataPackFromPath(data_default_path, SCALE_FACTOR_NONE); | 578   resource_bundle->AddDataPackFromPath(data_default_path, SCALE_FACTOR_NONE); | 
| 579 | 579 | 
| 580   gfx::ImageSkia* image_skia = resource_bundle->GetImageSkiaNamed(3); | 580   gfx::ImageSkia* image_skia = resource_bundle->GetImageSkiaNamed(3); | 
| 581   EXPECT_EQ(1u, image_skia->image_reps().size()); | 581   EXPECT_EQ(1u, image_skia->image_reps().size()); | 
| 582   EXPECT_EQ(ui::SCALE_FACTOR_100P, | 582   EXPECT_EQ(ui::SCALE_FACTOR_100P, | 
| 583             GetSupportedScaleFactor(image_skia->image_reps()[0].scale())); | 583             GetSupportedScaleFactor(image_skia->image_reps()[0].scale())); | 
| 584 } | 584 } | 
| 585 | 585 | 
| 586 }  // namespace ui | 586 }  // namespace ui | 
| OLD | NEW | 
|---|