Chromium Code Reviews| 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/path_service.h" | 11 #include "base/path_service.h" |
| 11 #include "base/scoped_temp_dir.h" | 12 #include "base/scoped_temp_dir.h" |
| 13 #include "base/utf_string_conversions.h" | |
| 14 #include "testing/gmock/include/gmock/gmock.h" | |
| 12 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
| 16 #include "third_party/skia/include/core/SkBitmap.h" | |
| 17 | |
| 18 using ::testing::_; | |
| 19 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.
| |
| 20 using ::testing::Between; | |
| 21 using ::testing::Property; | |
| 22 using ::testing::Return; | |
| 13 | 23 |
| 14 namespace ui { | 24 namespace ui { |
| 15 | 25 |
| 16 extern const char kSamplePakContents[]; | 26 extern const char kSamplePakContents[]; |
| 17 extern const size_t kSamplePakSize; | 27 extern const size_t kSamplePakSize; |
| 18 | 28 |
| 29 namespace { | |
| 30 | |
| 31 // Mock for the ResourceBundle::Delegate class. | |
| 32 class MockResourceBundleDelegate : public ui::ResourceBundle::Delegate { | |
| 33 public: | |
| 34 MockResourceBundleDelegate() { | |
| 35 } | |
| 36 virtual ~MockResourceBundleDelegate() { | |
| 37 } | |
| 38 | |
| 39 MOCK_METHOD3(GetPathForResourcePack, bool(const std::string& pack_name, | |
| 40 FilePath* pack_path, | |
| 41 float scale_factor)); | |
| 42 MOCK_METHOD2(GetPathForLocalePack, bool(const std::string& locale, | |
| 43 FilePath* pack_path)); | |
| 44 MOCK_METHOD1(GetImageNamedMock, gfx::Image*(int resource_id)); | |
| 45 virtual scoped_ptr<gfx::Image> GetImageNamed(int resource_id) OVERRIDE { | |
| 46 return scoped_ptr<gfx::Image>(GetImageNamedMock(resource_id)); | |
| 47 } | |
| 48 MOCK_METHOD2(GetNativeImageNamedMock, | |
| 49 gfx::Image*(int resource_id, | |
| 50 ui::ResourceBundle::ImageRTL rtl)); | |
| 51 virtual scoped_ptr<gfx::Image> GetNativeImageNamed( | |
| 52 int resource_id, | |
| 53 ui::ResourceBundle::ImageRTL rtl) OVERRIDE { | |
| 54 return scoped_ptr<gfx::Image>(GetNativeImageNamedMock(resource_id, rtl)); | |
| 55 } | |
| 56 MOCK_METHOD1(LoadDataResourceBytes, | |
| 57 base::RefCountedStaticMemory*(int resource_id)); | |
| 58 MOCK_METHOD1(GetRawDataResourceMock, base::StringPiece(int resource_id)); | |
| 59 virtual bool GetRawDataResource(int resource_id, | |
| 60 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.
| |
| 61 *value = GetRawDataResourceMock(resource_id); | |
| 62 return true; | |
| 63 } | |
| 64 MOCK_METHOD1(GetLocalizedStringMock, string16(int message_id)); | |
| 65 virtual bool GetLocalizedString(int message_id, string16* value) OVERRIDE { | |
| 66 *value = GetLocalizedStringMock(message_id); | |
| 67 return true; | |
| 68 } | |
| 69 MOCK_METHOD1(GetFontMock, gfx::Font*(ui::ResourceBundle::FontStyle style)); | |
| 70 virtual scoped_ptr<gfx::Font> GetFont( | |
| 71 ui::ResourceBundle::FontStyle style) OVERRIDE { | |
| 72 return scoped_ptr<gfx::Font>(GetFontMock(style)); | |
| 73 } | |
| 74 }; | |
| 75 | |
| 76 } // namespace | |
| 77 | |
| 78 TEST(ResourceBundle, DelegateGetPathForResourcePack) { | |
| 79 MockResourceBundleDelegate delegate; | |
| 80 ResourceBundle resource_bundle(&delegate); | |
| 81 | |
| 82 std::string pack_name = "test_name"; | |
| 83 FilePath pack_path(FILE_PATH_LITERAL("test_path")); | |
| 84 double pack_scale_factor = 2.0; | |
| 85 | |
| 86 EXPECT_CALL(delegate, | |
| 87 GetPathForResourcePack( | |
| 88 pack_name, | |
| 89 Property(&FilePath::value, pack_path.value()), | |
| 90 pack_scale_factor)) | |
| 91 .Times(1); | |
| 92 | |
| 93 resource_bundle.AddDataPack(pack_name, pack_path, pack_scale_factor); | |
| 94 } | |
| 95 | |
| 96 TEST(ResourceBundle, DelegateGetPathForLocalePack) { | |
| 97 MockResourceBundleDelegate delegate; | |
| 98 ResourceBundle resource_bundle(&delegate); | |
| 99 | |
| 100 std::string locale = "en-US"; | |
| 101 | |
| 102 // Cancel the load. | |
| 103 EXPECT_CALL(delegate, GetPathForLocalePack(locale, _)) | |
| 104 .Times(2) | |
| 105 .RetiresOnSaturation(); | |
| 106 | |
| 107 EXPECT_FALSE(resource_bundle.LocaleDataPakExists(locale)); | |
| 108 EXPECT_EQ("", resource_bundle.LoadLocaleResources(locale)); | |
| 109 | |
| 110 // Allow the load to proceed. | |
| 111 EXPECT_CALL(delegate, GetPathForLocalePack(locale, _)) | |
| 112 .Times(2) | |
| 113 .WillRepeatedly(Return(true)); | |
| 114 | |
| 115 EXPECT_TRUE(resource_bundle.LocaleDataPakExists(locale)); | |
| 116 EXPECT_EQ(locale, resource_bundle.LoadLocaleResources(locale)); | |
| 117 } | |
| 118 | |
| 119 TEST(ResourceBundle, DelegateGetImageNamed) { | |
| 120 MockResourceBundleDelegate delegate; | |
| 121 ResourceBundle resource_bundle(&delegate); | |
| 122 | |
| 123 // Create an empty image for testing purposes. | |
| 124 SkBitmap* bitmap = new SkBitmap(); | |
| 125 bitmap->setConfig(SkBitmap::kARGB_8888_Config, 32, 32); | |
| 126 bitmap->allocPixels(); | |
| 127 bitmap->eraseARGB(255, 255, 0, 0); | |
| 128 gfx::Image* empty_image = new gfx::Image(bitmap); | |
| 129 | |
| 130 int resource_id = 5; | |
| 131 | |
| 132 EXPECT_CALL(delegate, GetImageNamedMock(resource_id)) | |
| 133 .Times(1) | |
| 134 .WillOnce(Return(empty_image)); | |
| 135 | |
| 136 gfx::Image* result = &resource_bundle.GetImageNamed(resource_id); | |
| 137 EXPECT_EQ(empty_image, result); | |
| 138 } | |
| 139 | |
| 140 TEST(ResourceBundle, DelegateGetNativeImageNamed) { | |
| 141 MockResourceBundleDelegate delegate; | |
| 142 ResourceBundle resource_bundle(&delegate); | |
| 143 | |
| 144 // Create an empty image for testing purposes. | |
| 145 SkBitmap* bitmap = new SkBitmap(); | |
| 146 bitmap->setConfig(SkBitmap::kARGB_8888_Config, 32, 32); | |
| 147 bitmap->allocPixels(); | |
| 148 bitmap->eraseARGB(255, 255, 0, 0); | |
| 149 gfx::Image* empty_image = new gfx::Image(bitmap); | |
| 150 | |
| 151 int resource_id = 5; | |
| 152 | |
| 153 // Some platforms delegate GetNativeImageNamed calls to GetImageNamed. | |
| 154 EXPECT_CALL(delegate, GetImageNamedMock(resource_id)) | |
| 155 .Times(Between(0, 1)) | |
| 156 .WillOnce(Return(empty_image)); | |
| 157 EXPECT_CALL(delegate, | |
| 158 GetNativeImageNamedMock(resource_id, ui::ResourceBundle::RTL_DISABLED)) | |
| 159 .Times(Between(0, 1)) | |
| 160 .WillOnce(Return(empty_image)); | |
| 161 | |
| 162 gfx::Image* result = &resource_bundle.GetNativeImageNamed(resource_id); | |
| 163 EXPECT_EQ(empty_image, result); | |
| 164 } | |
| 165 | |
| 166 TEST(ResourceBundle, DelegateLoadDataResourceBytes) { | |
| 167 MockResourceBundleDelegate delegate; | |
| 168 ResourceBundle resource_bundle(&delegate); | |
| 169 | |
| 170 // Create the data resource for testing purposes. | |
| 171 unsigned char data[] = "My test data"; | |
| 172 scoped_refptr<base::RefCountedStaticMemory> static_memory( | |
| 173 new base::RefCountedStaticMemory(data, sizeof(data))); | |
| 174 | |
| 175 int resource_id = 5; | |
| 176 | |
| 177 EXPECT_CALL(delegate, LoadDataResourceBytes(resource_id)) | |
| 178 .Times(1) | |
| 179 .WillOnce(Return(static_memory)); | |
| 180 | |
| 181 scoped_refptr<base::RefCountedStaticMemory> result = | |
| 182 resource_bundle.LoadDataResourceBytes(resource_id); | |
| 183 EXPECT_EQ(static_memory, result); | |
| 184 } | |
| 185 | |
| 186 TEST(ResourceBundle, DelegateGetRawDataResource) { | |
| 187 MockResourceBundleDelegate delegate; | |
| 188 ResourceBundle resource_bundle(&delegate); | |
| 189 | |
| 190 // Create the string piece for testing purposes. | |
| 191 char data[] = "My test data"; | |
| 192 base::StringPiece string_piece(data); | |
| 193 | |
| 194 int resource_id = 5; | |
| 195 | |
| 196 EXPECT_CALL(delegate, GetRawDataResourceMock(resource_id)) | |
| 197 .Times(1) | |
| 198 .WillOnce(Return(string_piece)); | |
| 199 | |
| 200 base::StringPiece result = resource_bundle.GetRawDataResource(resource_id); | |
| 201 EXPECT_EQ(string_piece.data(), result.data()); | |
| 202 } | |
| 203 | |
| 204 TEST(ResourceBundle, DelegateGetLocalizedString) { | |
| 205 MockResourceBundleDelegate delegate; | |
| 206 ResourceBundle resource_bundle(&delegate); | |
| 207 | |
| 208 string16 data = ASCIIToUTF16("My test data"); | |
| 209 int resource_id = 5; | |
| 210 | |
| 211 EXPECT_CALL(delegate, GetLocalizedStringMock(resource_id)) | |
| 212 .Times(1) | |
| 213 .WillOnce(Return(data)); | |
| 214 | |
| 215 string16 result = resource_bundle.GetLocalizedString(resource_id); | |
| 216 EXPECT_EQ(data, result); | |
| 217 } | |
| 218 | |
| 219 TEST(ResourceBundle, DelegateGetFont) { | |
| 220 MockResourceBundleDelegate delegate; | |
| 221 ResourceBundle resource_bundle(&delegate); | |
| 222 | |
| 223 // Should be called once for each font type. When we return NULL the default | |
| 224 // font will be created. | |
| 225 gfx::Font* test_font = NULL; | |
| 226 EXPECT_CALL(delegate, GetFontMock(_)) | |
| 227 .Times(7) | |
| 228 .WillRepeatedly(Return(test_font)); | |
| 229 | |
| 230 const gfx::Font* font = | |
| 231 &resource_bundle.GetFont(ui::ResourceBundle::BaseFont); | |
| 232 EXPECT_TRUE(font); | |
| 233 } | |
| 234 | |
| 19 TEST(ResourceBundle, LoadDataResourceBytes) { | 235 TEST(ResourceBundle, LoadDataResourceBytes) { |
| 20 // Verify that we don't crash when trying to load a resource that is not | 236 // Verify that we don't crash when trying to load a resource that is not |
| 21 // found. In some cases, we fail to mmap resources.pak, but try to keep | 237 // found. In some cases, we fail to mmap resources.pak, but try to keep |
| 22 // going anyway. | 238 // going anyway. |
| 23 ResourceBundle resource_bundle; | 239 ResourceBundle resource_bundle(NULL); |
| 24 | 240 |
| 25 // On Windows, the default data is compiled into the binary so this does | 241 // On Windows, the default data is compiled into the binary so this does |
| 26 // nothing. | 242 // nothing. |
| 27 ScopedTempDir dir; | 243 ScopedTempDir dir; |
| 28 ASSERT_TRUE(dir.CreateUniqueTempDir()); | 244 ASSERT_TRUE(dir.CreateUniqueTempDir()); |
| 29 FilePath data_path = dir.path().Append(FILE_PATH_LITERAL("sample.pak")); | 245 FilePath data_path = dir.path().Append(FILE_PATH_LITERAL("sample.pak")); |
| 30 | 246 |
| 31 // Dump contents into the pak file. | 247 // Dump contents into the pak file. |
| 32 ASSERT_EQ(file_util::WriteFile(data_path, kSamplePakContents, kSamplePakSize), | 248 ASSERT_EQ(file_util::WriteFile(data_path, kSamplePakContents, kSamplePakSize), |
| 33 static_cast<int>(kSamplePakSize)); | 249 static_cast<int>(kSamplePakSize)); |
| 34 | 250 |
| 35 // Create a resource bundle from the file. | 251 // Create a resource bundle from the file. |
| 36 resource_bundle.LoadTestResources(data_path); | 252 resource_bundle.LoadTestResources(data_path); |
| 37 | 253 |
| 38 const int kUnfoundResourceId = 10000; | 254 const int kUnfoundResourceId = 10000; |
| 39 EXPECT_EQ(NULL, resource_bundle.LoadDataResourceBytes(kUnfoundResourceId)); | 255 EXPECT_EQ(NULL, resource_bundle.LoadDataResourceBytes(kUnfoundResourceId)); |
| 40 | 256 |
| 41 // Give a .pak file that doesn't exist so we will fail to load it. | 257 // Give a .pak file that doesn't exist so we will fail to load it. |
| 42 resource_bundle.AddDataPack(FilePath( | 258 resource_bundle.AddDataPack( |
| 43 FILE_PATH_LITERAL("non-existant-file.pak")), 1.0); | 259 "non-existant-file", |
| 260 FilePath(FILE_PATH_LITERAL("non-existant-file.pak")), | |
| 261 1.0); | |
| 44 EXPECT_EQ(NULL, resource_bundle.LoadDataResourceBytes(kUnfoundResourceId)); | 262 EXPECT_EQ(NULL, resource_bundle.LoadDataResourceBytes(kUnfoundResourceId)); |
| 45 } | 263 } |
| 46 | 264 |
| 47 TEST(ResourceBundle, LocaleDataPakExists) { | 265 TEST(ResourceBundle, LocaleDataPakExists) { |
| 266 ResourceBundle resource_bundle(NULL); | |
| 267 | |
| 48 // Check that ResourceBundle::LocaleDataPakExists returns the correct results. | 268 // Check that ResourceBundle::LocaleDataPakExists returns the correct results. |
| 49 EXPECT_TRUE(ResourceBundle::LocaleDataPakExists("en-US")); | 269 EXPECT_TRUE(resource_bundle.LocaleDataPakExists("en-US")); |
| 50 EXPECT_FALSE(ResourceBundle::LocaleDataPakExists("not_a_real_locale")); | 270 EXPECT_FALSE(resource_bundle.LocaleDataPakExists("not_a_real_locale")); |
| 51 } | 271 } |
| 52 | 272 |
| 53 } // namespace ui | 273 } // namespace ui |
| OLD | NEW |