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