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/memory/ref_counted_memory.h" |
11 #include "base/path_service.h" | 11 #include "base/path_service.h" |
12 #include "base/scoped_temp_dir.h" | 12 #include "base/scoped_temp_dir.h" |
13 #include "base/utf_string_conversions.h" | 13 #include "base/utf_string_conversions.h" |
14 #include "testing/gmock/include/gmock/gmock.h" | 14 #include "testing/gmock/include/gmock/gmock.h" |
| 15 #include "ui/base/resource/resource_handle.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 | 18 |
18 using ::testing::_; | 19 using ::testing::_; |
19 using ::testing::Between; | 20 using ::testing::Between; |
20 using ::testing::Property; | 21 using ::testing::Property; |
21 using ::testing::Return; | 22 using ::testing::Return; |
22 using ::testing::ReturnArg; | 23 using ::testing::ReturnArg; |
23 | 24 |
24 namespace ui { | 25 namespace ui { |
25 | 26 |
26 extern const char kSamplePakContents[]; | 27 extern const char kSamplePakContents[]; |
27 extern const size_t kSamplePakSize; | 28 extern const size_t kSamplePakSize; |
| 29 extern const char kSamplePakContents2x[]; |
| 30 extern const size_t kSamplePakSize2x; |
| 31 extern const char kEmptyPakContents[]; |
| 32 extern const size_t kEmptyPakSize; |
28 | 33 |
29 namespace { | 34 namespace { |
30 | 35 |
31 // Mock for the ResourceBundle::Delegate class. | 36 // Mock for the ResourceBundle::Delegate class. |
32 class MockResourceBundleDelegate : public ui::ResourceBundle::Delegate { | 37 class MockResourceBundleDelegate : public ui::ResourceBundle::Delegate { |
33 public: | 38 public: |
34 MockResourceBundleDelegate() { | 39 MockResourceBundleDelegate() { |
35 } | 40 } |
36 virtual ~MockResourceBundleDelegate() { | 41 virtual ~MockResourceBundleDelegate() { |
37 } | 42 } |
38 | 43 |
39 MOCK_METHOD2(GetPathForResourcePack, FilePath(const FilePath& pack_path, | 44 MOCK_METHOD2(GetPathForResourcePack, FilePath(const FilePath& pack_path, |
40 float scale_factor)); | 45 float scale_factor)); |
41 MOCK_METHOD2(GetPathForLocalePack, FilePath(const FilePath& pack_path, | 46 MOCK_METHOD2(GetPathForLocalePack, FilePath(const FilePath& pack_path, |
42 const std::string& locale)); | 47 const std::string& locale)); |
43 MOCK_METHOD1(GetImageNamed, gfx::Image(int resource_id)); | 48 MOCK_METHOD1(GetImageNamed, gfx::Image(int resource_id)); |
44 MOCK_METHOD2(GetNativeImageNamed, | 49 MOCK_METHOD2(GetNativeImageNamed, |
45 gfx::Image(int resource_id, | 50 gfx::Image(int resource_id, |
46 ui::ResourceBundle::ImageRTL rtl)); | 51 ui::ResourceBundle::ImageRTL rtl)); |
47 MOCK_METHOD1(LoadDataResourceBytes, | 52 MOCK_METHOD2(LoadDataResourceBytes, |
48 base::RefCountedStaticMemory*(int resource_id)); | 53 base::RefCountedStaticMemory*(int resource_id, float scale_factor)); |
49 MOCK_METHOD1(GetRawDataResourceMock, base::StringPiece(int resource_id)); | 54 MOCK_METHOD2(GetRawDataResourceMock, base::StringPiece(int resource_id, |
| 55 float scale_factor)); |
50 virtual bool GetRawDataResource(int resource_id, | 56 virtual bool GetRawDataResource(int resource_id, |
| 57 float scale_factor, |
51 base::StringPiece* value) OVERRIDE { | 58 base::StringPiece* value) OVERRIDE { |
52 *value = GetRawDataResourceMock(resource_id); | 59 *value = GetRawDataResourceMock(resource_id, scale_factor); |
53 return true; | 60 return true; |
54 } | 61 } |
55 MOCK_METHOD1(GetLocalizedStringMock, string16(int message_id)); | 62 MOCK_METHOD1(GetLocalizedStringMock, string16(int message_id)); |
56 virtual bool GetLocalizedString(int message_id, string16* value) OVERRIDE { | 63 virtual bool GetLocalizedString(int message_id, string16* value) OVERRIDE { |
57 *value = GetLocalizedStringMock(message_id); | 64 *value = GetLocalizedStringMock(message_id); |
58 return true; | 65 return true; |
59 } | 66 } |
60 MOCK_METHOD1(GetFontMock, gfx::Font*(ui::ResourceBundle::FontStyle style)); | 67 MOCK_METHOD1(GetFontMock, gfx::Font*(ui::ResourceBundle::FontStyle style)); |
61 virtual scoped_ptr<gfx::Font> GetFont( | 68 virtual scoped_ptr<gfx::Font> GetFont( |
62 ui::ResourceBundle::FontStyle style) OVERRIDE { | 69 ui::ResourceBundle::FontStyle style) OVERRIDE { |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
145 TEST(ResourceBundle, DelegateLoadDataResourceBytes) { | 152 TEST(ResourceBundle, DelegateLoadDataResourceBytes) { |
146 MockResourceBundleDelegate delegate; | 153 MockResourceBundleDelegate delegate; |
147 ResourceBundle resource_bundle(&delegate); | 154 ResourceBundle resource_bundle(&delegate); |
148 | 155 |
149 // Create the data resource for testing purposes. | 156 // Create the data resource for testing purposes. |
150 unsigned char data[] = "My test data"; | 157 unsigned char data[] = "My test data"; |
151 scoped_refptr<base::RefCountedStaticMemory> static_memory( | 158 scoped_refptr<base::RefCountedStaticMemory> static_memory( |
152 new base::RefCountedStaticMemory(data, sizeof(data))); | 159 new base::RefCountedStaticMemory(data, sizeof(data))); |
153 | 160 |
154 int resource_id = 5; | 161 int resource_id = 5; |
| 162 float scale_factor = ResourceHandle::kScaleFactorNone; |
155 | 163 |
156 EXPECT_CALL(delegate, LoadDataResourceBytes(resource_id)) | 164 EXPECT_CALL(delegate, LoadDataResourceBytes(resource_id, scale_factor)) |
157 .Times(1) | 165 .Times(1) |
158 .WillOnce(Return(static_memory)); | 166 .WillOnce(Return(static_memory)); |
159 | 167 |
160 scoped_refptr<base::RefCountedStaticMemory> result = | 168 scoped_refptr<base::RefCountedStaticMemory> result = |
161 resource_bundle.LoadDataResourceBytes(resource_id); | 169 resource_bundle.LoadDataResourceBytes(resource_id, scale_factor); |
162 EXPECT_EQ(static_memory, result); | 170 EXPECT_EQ(static_memory, result); |
163 } | 171 } |
164 | 172 |
165 TEST(ResourceBundle, DelegateGetRawDataResource) { | 173 TEST(ResourceBundle, DelegateGetRawDataResource) { |
166 MockResourceBundleDelegate delegate; | 174 MockResourceBundleDelegate delegate; |
167 ResourceBundle resource_bundle(&delegate); | 175 ResourceBundle resource_bundle(&delegate); |
168 | 176 |
169 // Create the string piece for testing purposes. | 177 // Create the string piece for testing purposes. |
170 char data[] = "My test data"; | 178 char data[] = "My test data"; |
171 base::StringPiece string_piece(data); | 179 base::StringPiece string_piece(data); |
172 | 180 |
173 int resource_id = 5; | 181 int resource_id = 5; |
174 | 182 |
175 EXPECT_CALL(delegate, GetRawDataResourceMock(resource_id)) | 183 EXPECT_CALL(delegate, GetRawDataResourceMock( |
| 184 resource_id, ResourceHandle::kScaleFactorNone)) |
176 .Times(1) | 185 .Times(1) |
177 .WillOnce(Return(string_piece)); | 186 .WillOnce(Return(string_piece)); |
178 | 187 |
179 base::StringPiece result = resource_bundle.GetRawDataResource(resource_id); | 188 base::StringPiece result = resource_bundle.GetRawDataResource( |
| 189 resource_id, ResourceHandle::kScaleFactorNone); |
180 EXPECT_EQ(string_piece.data(), result.data()); | 190 EXPECT_EQ(string_piece.data(), result.data()); |
181 } | 191 } |
182 | 192 |
183 TEST(ResourceBundle, DelegateGetLocalizedString) { | 193 TEST(ResourceBundle, DelegateGetLocalizedString) { |
184 MockResourceBundleDelegate delegate; | 194 MockResourceBundleDelegate delegate; |
185 ResourceBundle resource_bundle(&delegate); | 195 ResourceBundle resource_bundle(&delegate); |
186 | 196 |
187 string16 data = ASCIIToUTF16("My test data"); | 197 string16 data = ASCIIToUTF16("My test data"); |
188 int resource_id = 5; | 198 int resource_id = 5; |
189 | 199 |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
225 // found. In some cases, we fail to mmap resources.pak, but try to keep | 235 // found. In some cases, we fail to mmap resources.pak, but try to keep |
226 // going anyway. | 236 // going anyway. |
227 ResourceBundle resource_bundle(NULL); | 237 ResourceBundle resource_bundle(NULL); |
228 | 238 |
229 // Dump contents into the pak file. | 239 // Dump contents into the pak file. |
230 ASSERT_EQ(file_util::WriteFile(data_path, kSamplePakContents, | 240 ASSERT_EQ(file_util::WriteFile(data_path, kSamplePakContents, |
231 kSamplePakSize), | 241 kSamplePakSize), |
232 static_cast<int>(kSamplePakSize)); | 242 static_cast<int>(kSamplePakSize)); |
233 | 243 |
234 // Create a resource bundle from the file. | 244 // Create a resource bundle from the file. |
235 resource_bundle.LoadTestResources(data_path); | 245 resource_bundle.LoadTestResources(data_path, data_path); |
236 | 246 |
237 const int kUnfoundResourceId = 10000; | 247 const int kUnfoundResourceId = 10000; |
238 EXPECT_EQ(NULL, resource_bundle.LoadDataResourceBytes(kUnfoundResourceId)); | 248 EXPECT_EQ(NULL, resource_bundle.LoadDataResourceBytes( |
| 249 kUnfoundResourceId, ResourceHandle::kScaleFactorNone)); |
239 | 250 |
240 // Give a .pak file that doesn't exist so we will fail to load it. | 251 // Give a .pak file that doesn't exist so we will fail to load it. |
241 resource_bundle.AddDataPack( | 252 resource_bundle.AddDataPack( |
242 FilePath(FILE_PATH_LITERAL("non-existant-file.pak")), | 253 FilePath(FILE_PATH_LITERAL("non-existant-file.pak")), |
243 1.0); | 254 1.0); |
244 EXPECT_EQ(NULL, resource_bundle.LoadDataResourceBytes(kUnfoundResourceId)); | 255 EXPECT_EQ(NULL, resource_bundle.LoadDataResourceBytes( |
| 256 kUnfoundResourceId, ResourceHandle::kScaleFactorNone)); |
245 } | 257 } |
246 } | 258 } |
247 | 259 |
| 260 TEST(ResourceBundle, GetRawDataResource) { |
| 261 |
| 262 // On Windows, the default data is compiled into the binary so this does |
| 263 // nothing. |
| 264 ScopedTempDir dir; |
| 265 ASSERT_TRUE(dir.CreateUniqueTempDir()); |
| 266 FilePath locale_path = dir.path().Append(FILE_PATH_LITERAL("empty.pak")); |
| 267 FilePath data_path = dir.path().Append(FILE_PATH_LITERAL("sample.pak")); |
| 268 FilePath data_2x_path = dir.path().Append(FILE_PATH_LITERAL("sample_2x.pak")); |
| 269 |
| 270 { |
| 271 ResourceBundle resource_bundle(NULL); |
| 272 // Dump contents into the pak files. |
| 273 ASSERT_EQ(file_util::WriteFile(locale_path, kEmptyPakContents, |
| 274 kEmptyPakSize), static_cast<int>(kEmptyPakSize)); |
| 275 ASSERT_EQ(file_util::WriteFile(data_path, kSamplePakContents, |
| 276 kSamplePakSize), static_cast<int>(kSamplePakSize)); |
| 277 ASSERT_EQ(file_util::WriteFile(data_2x_path, kSamplePakContents2x, |
| 278 kSamplePakSize2x), static_cast<int>(kSamplePakSize2x)); |
| 279 |
| 280 // Load the regular and 2x pak files. |
| 281 resource_bundle.LoadTestResources(data_path, locale_path); |
| 282 resource_bundle.AddDataPack(data_2x_path, ResourceHandle::kScaleFactor200x); |
| 283 |
| 284 // Resource ID 4 exists in both 1x and 2x paks, so we expect a different |
| 285 // result when requesting the 2x scale. |
| 286 EXPECT_EQ("this is id 4", resource_bundle.GetRawDataResource(4, |
| 287 ResourceHandle::kScaleFactor100x)); |
| 288 EXPECT_EQ("this is id 4 2x", resource_bundle.GetRawDataResource(4, |
| 289 ResourceHandle::kScaleFactor200x)); |
| 290 |
| 291 // Resource ID 6 only exists in the 1x pak so we expect the same resource |
| 292 // for both scale factor requests. |
| 293 EXPECT_EQ("this is id 6", resource_bundle.GetRawDataResource(6, |
| 294 ResourceHandle::kScaleFactor100x)); |
| 295 EXPECT_EQ("this is id 6", resource_bundle.GetRawDataResource(6, |
| 296 ResourceHandle::kScaleFactor200x)); |
| 297 } |
| 298 } |
| 299 |
248 TEST(ResourceBundle, LocaleDataPakExists) { | 300 TEST(ResourceBundle, LocaleDataPakExists) { |
249 ResourceBundle resource_bundle(NULL); | 301 ResourceBundle resource_bundle(NULL); |
250 | 302 |
251 // Check that ResourceBundle::LocaleDataPakExists returns the correct results. | 303 // Check that ResourceBundle::LocaleDataPakExists returns the correct results. |
252 EXPECT_TRUE(resource_bundle.LocaleDataPakExists("en-US")); | 304 EXPECT_TRUE(resource_bundle.LocaleDataPakExists("en-US")); |
253 EXPECT_FALSE(resource_bundle.LocaleDataPakExists("not_a_real_locale")); | 305 EXPECT_FALSE(resource_bundle.LocaleDataPakExists("not_a_real_locale")); |
254 } | 306 } |
255 | 307 |
256 } // namespace ui | 308 } // namespace ui |
OLD | NEW |