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 "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
16 #include "third_party/skia/include/core/SkBitmap.h" | 16 #include "third_party/skia/include/core/SkBitmap.h" |
| 17 #include "ui/base/layout.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 ui::ScaleFactor 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, |
49 MOCK_METHOD1(GetRawDataResourceMock, base::StringPiece(int resource_id)); | 54 ui::ScaleFactor scale_factor)); |
| 55 MOCK_METHOD2(GetRawDataResourceMock, base::StringPiece( |
| 56 int resource_id, |
| 57 ui::ScaleFactor scale_factor)); |
50 virtual bool GetRawDataResource(int resource_id, | 58 virtual bool GetRawDataResource(int resource_id, |
| 59 ui::ScaleFactor scale_factor, |
51 base::StringPiece* value) OVERRIDE { | 60 base::StringPiece* value) OVERRIDE { |
52 *value = GetRawDataResourceMock(resource_id); | 61 *value = GetRawDataResourceMock(resource_id, scale_factor); |
53 return true; | 62 return true; |
54 } | 63 } |
55 MOCK_METHOD1(GetLocalizedStringMock, string16(int message_id)); | 64 MOCK_METHOD1(GetLocalizedStringMock, string16(int message_id)); |
56 virtual bool GetLocalizedString(int message_id, string16* value) OVERRIDE { | 65 virtual bool GetLocalizedString(int message_id, string16* value) OVERRIDE { |
57 *value = GetLocalizedStringMock(message_id); | 66 *value = GetLocalizedStringMock(message_id); |
58 return true; | 67 return true; |
59 } | 68 } |
60 MOCK_METHOD1(GetFontMock, gfx::Font*(ui::ResourceBundle::FontStyle style)); | 69 MOCK_METHOD1(GetFontMock, gfx::Font*(ui::ResourceBundle::FontStyle style)); |
61 virtual scoped_ptr<gfx::Font> GetFont( | 70 virtual scoped_ptr<gfx::Font> GetFont( |
62 ui::ResourceBundle::FontStyle style) OVERRIDE { | 71 ui::ResourceBundle::FontStyle style) OVERRIDE { |
63 return scoped_ptr<gfx::Font>(GetFontMock(style)); | 72 return scoped_ptr<gfx::Font>(GetFontMock(style)); |
64 } | 73 } |
65 }; | 74 }; |
66 | 75 |
67 } // namespace | 76 } // namespace |
68 | 77 |
69 TEST(ResourceBundle, DelegateGetPathForResourcePack) { | 78 TEST(ResourceBundle, DelegateGetPathForResourcePack) { |
70 MockResourceBundleDelegate delegate; | 79 MockResourceBundleDelegate delegate; |
71 ResourceBundle resource_bundle(&delegate); | 80 ResourceBundle resource_bundle(&delegate); |
72 | 81 |
73 FilePath pack_path(FILE_PATH_LITERAL("/path/to/test_path.pak")); | 82 FilePath pack_path(FILE_PATH_LITERAL("/path/to/test_path.pak")); |
74 double pack_scale_factor = 2.0; | 83 ui::ScaleFactor pack_scale_factor = ui::SCALE_FACTOR_200P; |
75 | 84 |
76 EXPECT_CALL(delegate, | 85 EXPECT_CALL(delegate, |
77 GetPathForResourcePack( | 86 GetPathForResourcePack( |
78 Property(&FilePath::value, pack_path.value()), | 87 Property(&FilePath::value, pack_path.value()), |
79 pack_scale_factor)) | 88 pack_scale_factor)) |
80 .Times(1) | 89 .Times(1) |
81 .WillOnce(Return(pack_path)); | 90 .WillOnce(Return(pack_path)); |
82 | 91 |
83 resource_bundle.AddDataPack(pack_path, pack_scale_factor); | 92 resource_bundle.AddDataPack(pack_path, pack_scale_factor); |
84 } | 93 } |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
145 TEST(ResourceBundle, DelegateLoadDataResourceBytes) { | 154 TEST(ResourceBundle, DelegateLoadDataResourceBytes) { |
146 MockResourceBundleDelegate delegate; | 155 MockResourceBundleDelegate delegate; |
147 ResourceBundle resource_bundle(&delegate); | 156 ResourceBundle resource_bundle(&delegate); |
148 | 157 |
149 // Create the data resource for testing purposes. | 158 // Create the data resource for testing purposes. |
150 unsigned char data[] = "My test data"; | 159 unsigned char data[] = "My test data"; |
151 scoped_refptr<base::RefCountedStaticMemory> static_memory( | 160 scoped_refptr<base::RefCountedStaticMemory> static_memory( |
152 new base::RefCountedStaticMemory(data, sizeof(data))); | 161 new base::RefCountedStaticMemory(data, sizeof(data))); |
153 | 162 |
154 int resource_id = 5; | 163 int resource_id = 5; |
| 164 ui::ScaleFactor scale_factor = ui::SCALE_FACTOR_NONE; |
155 | 165 |
156 EXPECT_CALL(delegate, LoadDataResourceBytes(resource_id)) | 166 EXPECT_CALL(delegate, LoadDataResourceBytes(resource_id, scale_factor)) |
157 .Times(1) | 167 .Times(1) |
158 .WillOnce(Return(static_memory)); | 168 .WillOnce(Return(static_memory)); |
159 | 169 |
160 scoped_refptr<base::RefCountedStaticMemory> result = | 170 scoped_refptr<base::RefCountedStaticMemory> result = |
161 resource_bundle.LoadDataResourceBytes(resource_id); | 171 resource_bundle.LoadDataResourceBytes(resource_id, scale_factor); |
162 EXPECT_EQ(static_memory, result); | 172 EXPECT_EQ(static_memory, result); |
163 } | 173 } |
164 | 174 |
165 TEST(ResourceBundle, DelegateGetRawDataResource) { | 175 TEST(ResourceBundle, DelegateGetRawDataResource) { |
166 MockResourceBundleDelegate delegate; | 176 MockResourceBundleDelegate delegate; |
167 ResourceBundle resource_bundle(&delegate); | 177 ResourceBundle resource_bundle(&delegate); |
168 | 178 |
169 // Create the string piece for testing purposes. | 179 // Create the string piece for testing purposes. |
170 char data[] = "My test data"; | 180 char data[] = "My test data"; |
171 base::StringPiece string_piece(data); | 181 base::StringPiece string_piece(data); |
172 | 182 |
173 int resource_id = 5; | 183 int resource_id = 5; |
174 | 184 |
175 EXPECT_CALL(delegate, GetRawDataResourceMock(resource_id)) | 185 EXPECT_CALL(delegate, GetRawDataResourceMock( |
| 186 resource_id, ui::SCALE_FACTOR_NONE)) |
176 .Times(1) | 187 .Times(1) |
177 .WillOnce(Return(string_piece)); | 188 .WillOnce(Return(string_piece)); |
178 | 189 |
179 base::StringPiece result = resource_bundle.GetRawDataResource(resource_id); | 190 base::StringPiece result = resource_bundle.GetRawDataResource( |
| 191 resource_id, ui::SCALE_FACTOR_NONE); |
180 EXPECT_EQ(string_piece.data(), result.data()); | 192 EXPECT_EQ(string_piece.data(), result.data()); |
181 } | 193 } |
182 | 194 |
183 TEST(ResourceBundle, DelegateGetLocalizedString) { | 195 TEST(ResourceBundle, DelegateGetLocalizedString) { |
184 MockResourceBundleDelegate delegate; | 196 MockResourceBundleDelegate delegate; |
185 ResourceBundle resource_bundle(&delegate); | 197 ResourceBundle resource_bundle(&delegate); |
186 | 198 |
187 string16 data = ASCIIToUTF16("My test data"); | 199 string16 data = ASCIIToUTF16("My test data"); |
188 int resource_id = 5; | 200 int resource_id = 5; |
189 | 201 |
(...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 | 237 // found. In some cases, we fail to mmap resources.pak, but try to keep |
226 // going anyway. | 238 // going anyway. |
227 ResourceBundle resource_bundle(NULL); | 239 ResourceBundle resource_bundle(NULL); |
228 | 240 |
229 // Dump contents into the pak file. | 241 // Dump contents into the pak file. |
230 ASSERT_EQ(file_util::WriteFile(data_path, kSamplePakContents, | 242 ASSERT_EQ(file_util::WriteFile(data_path, kSamplePakContents, |
231 kSamplePakSize), | 243 kSamplePakSize), |
232 static_cast<int>(kSamplePakSize)); | 244 static_cast<int>(kSamplePakSize)); |
233 | 245 |
234 // Create a resource bundle from the file. | 246 // Create a resource bundle from the file. |
235 resource_bundle.LoadTestResources(data_path); | 247 resource_bundle.LoadTestResources(data_path, data_path); |
236 | 248 |
237 const int kUnfoundResourceId = 10000; | 249 const int kUnfoundResourceId = 10000; |
238 EXPECT_EQ(NULL, resource_bundle.LoadDataResourceBytes(kUnfoundResourceId)); | 250 EXPECT_EQ(NULL, resource_bundle.LoadDataResourceBytes( |
| 251 kUnfoundResourceId, ui::SCALE_FACTOR_NONE)); |
239 | 252 |
240 // Give a .pak file that doesn't exist so we will fail to load it. | 253 // Give a .pak file that doesn't exist so we will fail to load it. |
241 resource_bundle.AddDataPack( | 254 resource_bundle.AddDataPack( |
242 FilePath(FILE_PATH_LITERAL("non-existant-file.pak")), | 255 FilePath(FILE_PATH_LITERAL("non-existant-file.pak")), |
243 1.0); | 256 ui::SCALE_FACTOR_NONE); |
244 EXPECT_EQ(NULL, resource_bundle.LoadDataResourceBytes(kUnfoundResourceId)); | 257 EXPECT_EQ(NULL, resource_bundle.LoadDataResourceBytes( |
| 258 kUnfoundResourceId, ui::SCALE_FACTOR_NONE)); |
245 } | 259 } |
246 } | 260 } |
247 | 261 |
| 262 TEST(ResourceBundle, GetRawDataResource) { |
| 263 |
| 264 // On Windows, the default data is compiled into the binary so this does |
| 265 // nothing. |
| 266 ScopedTempDir dir; |
| 267 ASSERT_TRUE(dir.CreateUniqueTempDir()); |
| 268 FilePath locale_path = dir.path().Append(FILE_PATH_LITERAL("empty.pak")); |
| 269 FilePath data_path = dir.path().Append(FILE_PATH_LITERAL("sample.pak")); |
| 270 FilePath data_2x_path = dir.path().Append(FILE_PATH_LITERAL("sample_2x.pak")); |
| 271 |
| 272 { |
| 273 ResourceBundle resource_bundle(NULL); |
| 274 // Dump contents into the pak files. |
| 275 ASSERT_EQ(file_util::WriteFile(locale_path, kEmptyPakContents, |
| 276 kEmptyPakSize), static_cast<int>(kEmptyPakSize)); |
| 277 ASSERT_EQ(file_util::WriteFile(data_path, kSamplePakContents, |
| 278 kSamplePakSize), static_cast<int>(kSamplePakSize)); |
| 279 ASSERT_EQ(file_util::WriteFile(data_2x_path, kSamplePakContents2x, |
| 280 kSamplePakSize2x), static_cast<int>(kSamplePakSize2x)); |
| 281 |
| 282 // Load the regular and 2x pak files. |
| 283 resource_bundle.LoadTestResources(data_path, locale_path); |
| 284 resource_bundle.AddDataPack(data_2x_path, SCALE_FACTOR_200P); |
| 285 |
| 286 // Resource ID 4 exists in both 1x and 2x paks, so we expect a different |
| 287 // result when requesting the 2x scale. |
| 288 EXPECT_EQ("this is id 4", resource_bundle.GetRawDataResource(4, |
| 289 SCALE_FACTOR_100P)); |
| 290 EXPECT_EQ("this is id 4 2x", resource_bundle.GetRawDataResource(4, |
| 291 SCALE_FACTOR_200P)); |
| 292 |
| 293 // Resource ID 6 only exists in the 1x pak so we expect the same resource |
| 294 // for both scale factor requests. |
| 295 EXPECT_EQ("this is id 6", resource_bundle.GetRawDataResource(6, |
| 296 SCALE_FACTOR_100P)); |
| 297 EXPECT_EQ("this is id 6", resource_bundle.GetRawDataResource(6, |
| 298 SCALE_FACTOR_200P)); |
| 299 } |
| 300 } |
| 301 |
248 TEST(ResourceBundle, LocaleDataPakExists) { | 302 TEST(ResourceBundle, LocaleDataPakExists) { |
249 ResourceBundle resource_bundle(NULL); | 303 ResourceBundle resource_bundle(NULL); |
250 | 304 |
251 // Check that ResourceBundle::LocaleDataPakExists returns the correct results. | 305 // Check that ResourceBundle::LocaleDataPakExists returns the correct results. |
252 EXPECT_TRUE(resource_bundle.LocaleDataPakExists("en-US")); | 306 EXPECT_TRUE(resource_bundle.LocaleDataPakExists("en-US")); |
253 EXPECT_FALSE(resource_bundle.LocaleDataPakExists("not_a_real_locale")); | 307 EXPECT_FALSE(resource_bundle.LocaleDataPakExists("not_a_real_locale")); |
254 } | 308 } |
255 | 309 |
256 } // namespace ui | 310 } // namespace ui |
OLD | NEW |