Chromium Code Reviews| Index: ui/base/resource/resource_bundle_unittest.cc |
| =================================================================== |
| --- ui/base/resource/resource_bundle_unittest.cc (revision 134688) |
| +++ ui/base/resource/resource_bundle_unittest.cc (working copy) |
| @@ -7,20 +7,236 @@ |
| #include "base/base_paths.h" |
| #include "base/file_path.h" |
| #include "base/file_util.h" |
| +#include "base/memory/ref_counted_memory.h" |
| #include "base/path_service.h" |
| #include "base/scoped_temp_dir.h" |
| +#include "base/utf_string_conversions.h" |
| +#include "testing/gmock/include/gmock/gmock.h" |
| #include "testing/gtest/include/gtest/gtest.h" |
| +#include "third_party/skia/include/core/SkBitmap.h" |
| +using ::testing::_; |
| +using ::testing::AtLeast; |
|
tony
2012/05/02 20:41:20
Nit: I don't see AtLeast used in this file.
Marshall
2012/05/03 19:10:54
Done.
|
| +using ::testing::Between; |
| +using ::testing::Property; |
| +using ::testing::Return; |
| + |
| namespace ui { |
| extern const char kSamplePakContents[]; |
| extern const size_t kSamplePakSize; |
| +namespace { |
| + |
| +// Mock for the ResourceBundle::Delegate class. |
| +class MockResourceBundleDelegate : public ui::ResourceBundle::Delegate { |
| + public: |
| + MockResourceBundleDelegate() { |
| + } |
| + virtual ~MockResourceBundleDelegate() { |
| + } |
| + |
| + MOCK_METHOD3(GetPathForResourcePack, bool(const std::string& pack_name, |
| + FilePath* pack_path, |
| + float scale_factor)); |
| + MOCK_METHOD2(GetPathForLocalePack, bool(const std::string& locale, |
| + FilePath* pack_path)); |
| + MOCK_METHOD1(GetImageNamedMock, gfx::Image*(int resource_id)); |
| + virtual scoped_ptr<gfx::Image> GetImageNamed(int resource_id) OVERRIDE { |
| + return scoped_ptr<gfx::Image>(GetImageNamedMock(resource_id)); |
| + } |
| + MOCK_METHOD2(GetNativeImageNamedMock, |
| + gfx::Image*(int resource_id, |
| + ui::ResourceBundle::ImageRTL rtl)); |
| + virtual scoped_ptr<gfx::Image> GetNativeImageNamed( |
| + int resource_id, |
| + ui::ResourceBundle::ImageRTL rtl) OVERRIDE { |
| + return scoped_ptr<gfx::Image>(GetNativeImageNamedMock(resource_id, rtl)); |
| + } |
| + MOCK_METHOD1(LoadDataResourceBytes, |
| + base::RefCountedStaticMemory*(int resource_id)); |
| + MOCK_METHOD1(GetRawDataResourceMock, base::StringPiece(int resource_id)); |
| + virtual bool GetRawDataResource(int resource_id, |
| + base::StringPiece* value) OVERRIDE { |
|
tony
2012/05/02 20:41:20
Nit: indented 2 spaces too far?
Marshall
2012/05/03 19:10:54
Done.
|
| + *value = GetRawDataResourceMock(resource_id); |
| + return true; |
| + } |
| + MOCK_METHOD1(GetLocalizedStringMock, string16(int message_id)); |
| + virtual bool GetLocalizedString(int message_id, string16* value) OVERRIDE { |
| + *value = GetLocalizedStringMock(message_id); |
| + return true; |
| + } |
| + MOCK_METHOD1(GetFontMock, gfx::Font*(ui::ResourceBundle::FontStyle style)); |
| + virtual scoped_ptr<gfx::Font> GetFont( |
| + ui::ResourceBundle::FontStyle style) OVERRIDE { |
| + return scoped_ptr<gfx::Font>(GetFontMock(style)); |
| + } |
| +}; |
| + |
| +} // namespace |
| + |
| +TEST(ResourceBundle, DelegateGetPathForResourcePack) { |
| + MockResourceBundleDelegate delegate; |
| + ResourceBundle resource_bundle(&delegate); |
| + |
| + std::string pack_name = "test_name"; |
| + FilePath pack_path(FILE_PATH_LITERAL("test_path")); |
| + double pack_scale_factor = 2.0; |
| + |
| + EXPECT_CALL(delegate, |
| + GetPathForResourcePack( |
| + pack_name, |
| + Property(&FilePath::value, pack_path.value()), |
| + pack_scale_factor)) |
| + .Times(1); |
| + |
| + resource_bundle.AddDataPack(pack_name, pack_path, pack_scale_factor); |
| +} |
| + |
| +TEST(ResourceBundle, DelegateGetPathForLocalePack) { |
| + MockResourceBundleDelegate delegate; |
| + ResourceBundle resource_bundle(&delegate); |
| + |
| + std::string locale = "en-US"; |
| + |
| + // Cancel the load. |
| + EXPECT_CALL(delegate, GetPathForLocalePack(locale, _)) |
| + .Times(2) |
| + .RetiresOnSaturation(); |
| + |
| + EXPECT_FALSE(resource_bundle.LocaleDataPakExists(locale)); |
| + EXPECT_EQ("", resource_bundle.LoadLocaleResources(locale)); |
| + |
| + // Allow the load to proceed. |
| + EXPECT_CALL(delegate, GetPathForLocalePack(locale, _)) |
| + .Times(2) |
| + .WillRepeatedly(Return(true)); |
| + |
| + EXPECT_TRUE(resource_bundle.LocaleDataPakExists(locale)); |
| + EXPECT_EQ(locale, resource_bundle.LoadLocaleResources(locale)); |
| +} |
| + |
| +TEST(ResourceBundle, DelegateGetImageNamed) { |
| + MockResourceBundleDelegate delegate; |
| + ResourceBundle resource_bundle(&delegate); |
| + |
| + // Create an empty image for testing purposes. |
| + SkBitmap* bitmap = new SkBitmap(); |
| + bitmap->setConfig(SkBitmap::kARGB_8888_Config, 32, 32); |
| + bitmap->allocPixels(); |
| + bitmap->eraseARGB(255, 255, 0, 0); |
| + gfx::Image* empty_image = new gfx::Image(bitmap); |
| + |
| + int resource_id = 5; |
| + |
| + EXPECT_CALL(delegate, GetImageNamedMock(resource_id)) |
| + .Times(1) |
| + .WillOnce(Return(empty_image)); |
| + |
| + gfx::Image* result = &resource_bundle.GetImageNamed(resource_id); |
| + EXPECT_EQ(empty_image, result); |
| +} |
| + |
| +TEST(ResourceBundle, DelegateGetNativeImageNamed) { |
| + MockResourceBundleDelegate delegate; |
| + ResourceBundle resource_bundle(&delegate); |
| + |
| + // Create an empty image for testing purposes. |
| + SkBitmap* bitmap = new SkBitmap(); |
| + bitmap->setConfig(SkBitmap::kARGB_8888_Config, 32, 32); |
| + bitmap->allocPixels(); |
| + bitmap->eraseARGB(255, 255, 0, 0); |
| + gfx::Image* empty_image = new gfx::Image(bitmap); |
| + |
| + int resource_id = 5; |
| + |
| + // Some platforms delegate GetNativeImageNamed calls to GetImageNamed. |
| + EXPECT_CALL(delegate, GetImageNamedMock(resource_id)) |
| + .Times(Between(0, 1)) |
| + .WillOnce(Return(empty_image)); |
| + EXPECT_CALL(delegate, |
| + GetNativeImageNamedMock(resource_id, ui::ResourceBundle::RTL_DISABLED)) |
| + .Times(Between(0, 1)) |
| + .WillOnce(Return(empty_image)); |
| + |
| + gfx::Image* result = &resource_bundle.GetNativeImageNamed(resource_id); |
| + EXPECT_EQ(empty_image, result); |
| +} |
| + |
| +TEST(ResourceBundle, DelegateLoadDataResourceBytes) { |
| + MockResourceBundleDelegate delegate; |
| + ResourceBundle resource_bundle(&delegate); |
| + |
| + // Create the data resource for testing purposes. |
| + unsigned char data[] = "My test data"; |
| + scoped_refptr<base::RefCountedStaticMemory> static_memory( |
| + new base::RefCountedStaticMemory(data, sizeof(data))); |
| + |
| + int resource_id = 5; |
| + |
| + EXPECT_CALL(delegate, LoadDataResourceBytes(resource_id)) |
| + .Times(1) |
| + .WillOnce(Return(static_memory)); |
| + |
| + scoped_refptr<base::RefCountedStaticMemory> result = |
| + resource_bundle.LoadDataResourceBytes(resource_id); |
| + EXPECT_EQ(static_memory, result); |
| +} |
| + |
| +TEST(ResourceBundle, DelegateGetRawDataResource) { |
| + MockResourceBundleDelegate delegate; |
| + ResourceBundle resource_bundle(&delegate); |
| + |
| + // Create the string piece for testing purposes. |
| + char data[] = "My test data"; |
| + base::StringPiece string_piece(data); |
| + |
| + int resource_id = 5; |
| + |
| + EXPECT_CALL(delegate, GetRawDataResourceMock(resource_id)) |
| + .Times(1) |
| + .WillOnce(Return(string_piece)); |
| + |
| + base::StringPiece result = resource_bundle.GetRawDataResource(resource_id); |
| + EXPECT_EQ(string_piece.data(), result.data()); |
| +} |
| + |
| +TEST(ResourceBundle, DelegateGetLocalizedString) { |
| + MockResourceBundleDelegate delegate; |
| + ResourceBundle resource_bundle(&delegate); |
| + |
| + string16 data = ASCIIToUTF16("My test data"); |
| + int resource_id = 5; |
| + |
| + EXPECT_CALL(delegate, GetLocalizedStringMock(resource_id)) |
| + .Times(1) |
| + .WillOnce(Return(data)); |
| + |
| + string16 result = resource_bundle.GetLocalizedString(resource_id); |
| + EXPECT_EQ(data, result); |
| +} |
| + |
| +TEST(ResourceBundle, DelegateGetFont) { |
| + MockResourceBundleDelegate delegate; |
| + ResourceBundle resource_bundle(&delegate); |
| + |
| + // Should be called once for each font type. When we return NULL the default |
| + // font will be created. |
| + gfx::Font* test_font = NULL; |
| + EXPECT_CALL(delegate, GetFontMock(_)) |
| + .Times(7) |
| + .WillRepeatedly(Return(test_font)); |
| + |
| + const gfx::Font* font = |
| + &resource_bundle.GetFont(ui::ResourceBundle::BaseFont); |
| + EXPECT_TRUE(font); |
| +} |
| + |
| TEST(ResourceBundle, LoadDataResourceBytes) { |
| // Verify that we don't crash when trying to load a resource that is not |
| // found. In some cases, we fail to mmap resources.pak, but try to keep |
| // going anyway. |
| - ResourceBundle resource_bundle; |
| + ResourceBundle resource_bundle(NULL); |
| // On Windows, the default data is compiled into the binary so this does |
| // nothing. |
| @@ -39,15 +255,19 @@ |
| EXPECT_EQ(NULL, resource_bundle.LoadDataResourceBytes(kUnfoundResourceId)); |
| // Give a .pak file that doesn't exist so we will fail to load it. |
| - resource_bundle.AddDataPack(FilePath( |
| - FILE_PATH_LITERAL("non-existant-file.pak")), 1.0); |
| + resource_bundle.AddDataPack( |
| + "non-existant-file", |
| + FilePath(FILE_PATH_LITERAL("non-existant-file.pak")), |
| + 1.0); |
| EXPECT_EQ(NULL, resource_bundle.LoadDataResourceBytes(kUnfoundResourceId)); |
| } |
| TEST(ResourceBundle, LocaleDataPakExists) { |
| + ResourceBundle resource_bundle(NULL); |
| + |
| // Check that ResourceBundle::LocaleDataPakExists returns the correct results. |
| - EXPECT_TRUE(ResourceBundle::LocaleDataPakExists("en-US")); |
| - EXPECT_FALSE(ResourceBundle::LocaleDataPakExists("not_a_real_locale")); |
| + EXPECT_TRUE(resource_bundle.LocaleDataPakExists("en-US")); |
| + EXPECT_FALSE(resource_bundle.LocaleDataPakExists("not_a_real_locale")); |
| } |
| } // namespace ui |