Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(16)

Side by Side Diff: ui/base/resource/resource_bundle_unittest.cc

Issue 11301007: Load the resources for max scale factor first. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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"
(...skipping 229 matching lines...) Expand 10 before | Expand all | Expand 10 after
240 const gfx::Font* font = 240 const gfx::Font* font =
241 &resource_bundle.GetFont(ui::ResourceBundle::BaseFont); 241 &resource_bundle.GetFont(ui::ResourceBundle::BaseFont);
242 EXPECT_TRUE(font); 242 EXPECT_TRUE(font);
243 } 243 }
244 244
245 TEST(ResourceBundle, LoadDataResourceBytes) { 245 TEST(ResourceBundle, LoadDataResourceBytes) {
246 // On Windows, the default data is compiled into the binary so this does 246 // On Windows, the default data is compiled into the binary so this does
247 // nothing. 247 // nothing.
248 ScopedTempDir dir; 248 ScopedTempDir dir;
249 ASSERT_TRUE(dir.CreateUniqueTempDir()); 249 ASSERT_TRUE(dir.CreateUniqueTempDir());
250 FilePath data_path = dir.path().Append(FILE_PATH_LITERAL("sample.pak")); 250 FilePath data_path = dir.path().AppendASCII("sample.pak");
251 251
252 // Put the ResourceBundle in a different scope so that it's destroyed before 252 // Put the ResourceBundle in a different scope so that it's destroyed before
253 // the ScopedTempDir. 253 // the ScopedTempDir.
254 { 254 {
255 // Verify that we don't crash when trying to load a resource that is not 255 // Verify that we don't crash when trying to load a resource that is not
256 // found. In some cases, we fail to mmap resources.pak, but try to keep 256 // found. In some cases, we fail to mmap resources.pak, but try to keep
257 // going anyway. 257 // going anyway.
258 ResourceBundle resource_bundle(NULL); 258 ResourceBundle resource_bundle(NULL);
259 259
260 // Dump contents into the pak file. 260 // Dump contents into the pak file.
(...skipping 16 matching lines...) Expand all
277 kUnfoundResourceId)); 277 kUnfoundResourceId));
278 } 278 }
279 } 279 }
280 280
281 TEST(ResourceBundle, GetRawDataResource) { 281 TEST(ResourceBundle, GetRawDataResource) {
282 282
283 // On Windows, the default data is compiled into the binary so this does 283 // On Windows, the default data is compiled into the binary so this does
284 // nothing. 284 // nothing.
285 ScopedTempDir dir; 285 ScopedTempDir dir;
286 ASSERT_TRUE(dir.CreateUniqueTempDir()); 286 ASSERT_TRUE(dir.CreateUniqueTempDir());
287 FilePath locale_path = dir.path().Append(FILE_PATH_LITERAL("empty.pak")); 287 FilePath locale_path = dir.path().AppendASCII("empty.pak");
288 FilePath data_path = dir.path().Append(FILE_PATH_LITERAL("sample.pak")); 288 FilePath data_path = dir.path().AppendASCII("sample.pak");
289 FilePath data_2x_path = dir.path().Append(FILE_PATH_LITERAL("sample_2x.pak")); 289 FilePath data_2x_path = dir.path().AppendASCII("sample_2x.pak");
290 290
291 { 291 {
292 ResourceBundle resource_bundle(NULL); 292 ResourceBundle resource_bundle(NULL);
293 // Dump contents into the pak files. 293 // Dump contents into the pak files.
294 ASSERT_EQ(file_util::WriteFile(locale_path, kEmptyPakContents, 294 ASSERT_EQ(file_util::WriteFile(locale_path, kEmptyPakContents,
295 kEmptyPakSize), static_cast<int>(kEmptyPakSize)); 295 kEmptyPakSize), static_cast<int>(kEmptyPakSize));
296 ASSERT_EQ(file_util::WriteFile(data_path, kSamplePakContents, 296 ASSERT_EQ(file_util::WriteFile(data_path, kSamplePakContents,
297 kSamplePakSize), static_cast<int>(kSamplePakSize)); 297 kSamplePakSize), static_cast<int>(kSamplePakSize));
298 ASSERT_EQ(file_util::WriteFile(data_2x_path, kSamplePakContents2x, 298 ASSERT_EQ(file_util::WriteFile(data_2x_path, kSamplePakContents2x,
299 kSamplePakSize2x), static_cast<int>(kSamplePakSize2x)); 299 kSamplePakSize2x), static_cast<int>(kSamplePakSize2x));
(...skipping 27 matching lines...) Expand all
327 } 327 }
328 328
329 // Test requesting image reps at various scale factors from the image returned 329 // Test requesting image reps at various scale factors from the image returned
330 // via ResourceBundle::GetImageNamed(). 330 // via ResourceBundle::GetImageNamed().
331 TEST(ResourceBundle, GetImageNamed) { 331 TEST(ResourceBundle, GetImageNamed) {
332 // On Windows, the default data is compiled into the binary so this does 332 // On Windows, the default data is compiled into the binary so this does
333 // nothing. 333 // nothing.
334 ScopedTempDir dir; 334 ScopedTempDir dir;
335 ASSERT_TRUE(dir.CreateUniqueTempDir()); 335 ASSERT_TRUE(dir.CreateUniqueTempDir());
336 336
337 FilePath locale_path = dir.path().Append(FILE_PATH_LITERAL("empty.pak")); 337 FilePath locale_path = dir.path().AppendASCII("empty.pak");
338 FilePath data_path = dir.path().Append(FILE_PATH_LITERAL("sample.pak")); 338 FilePath data_default_path = dir.path().AppendASCII("sample.pak");
339 FilePath data_2x_path = dir.path().Append(FILE_PATH_LITERAL("sample_2x.pak")); 339 FilePath data_1x_path = dir.path().AppendASCII("sample_1x.pak");
340 FilePath data_2x_path = dir.path().AppendASCII("sample_2x.pak");
340 341
341 { 342 // Create the pak files.
342 // Create the pak files. 343 ASSERT_EQ(file_util::WriteFile(locale_path, kEmptyPakContents,
343 ASSERT_EQ(file_util::WriteFile(locale_path, kEmptyPakContents, 344 kEmptyPakSize), static_cast<int>(kEmptyPakSize));
344 kEmptyPakSize), static_cast<int>(kEmptyPakSize)); 345 CreateDataPackWithSingleBitmap(data_default_path, 10);
345 CreateDataPackWithSingleBitmap(data_path, 10); 346 CreateDataPackWithSingleBitmap(data_1x_path, 10);
346 CreateDataPackWithSingleBitmap(data_2x_path, 20); 347 CreateDataPackWithSingleBitmap(data_2x_path, 20);
347 348
348 // Load the regular and 2x pak files. 349 // Load the regular and 2x pak files.
349 ResourceBundle resource_bundle(NULL); 350 ResourceBundle resource_bundle(NULL);
350 resource_bundle.LoadTestResources(data_path, locale_path); 351 resource_bundle.LoadTestResources(data_default_path, locale_path);
351 resource_bundle.AddDataPackFromPath(data_2x_path, SCALE_FACTOR_200P); 352 resource_bundle.AddDataPackFromPath(data_1x_path, SCALE_FACTOR_100P);
353 resource_bundle.AddDataPackFromPath(data_2x_path, SCALE_FACTOR_200P);
352 354
353 gfx::ImageSkia* image_skia = resource_bundle.GetImageSkiaNamed(3); 355 gfx::ImageSkia* image_skia = resource_bundle.GetImageSkiaNamed(3);
354 356
355 // Resource ID 3 exists in both 1x and 2x paks. Image reps should be 357 EXPECT_EQ(ui::GetMaxScaleFactor(),
356 // available for both scale factors in |image_skia|. 358 image_skia->image_reps()[0].scale_factor());
pkotwicz 2012/11/07 06:40:44 Shouldn't you call ui::test::SetSupportedScaleFact
oshima 2012/11/07 22:35:30 Current code is a bit broken as we included 200P e
357 gfx::ImageSkiaRep image_rep =
358 image_skia->GetRepresentation(ui::SCALE_FACTOR_100P);
359 EXPECT_EQ(ui::SCALE_FACTOR_100P, image_rep.scale_factor());
360 image_rep = image_skia->GetRepresentation(ui::SCALE_FACTOR_200P);
361 EXPECT_EQ(ui::SCALE_FACTOR_200P, image_rep.scale_factor());
362 359
363 // The 1.4x pack was not loaded. Requesting the 1.4x resource should return 360 // Resource ID 3 exists in both 1x and 2x paks. Image reps should be
364 // either the 1x or the 2x resource. 361 // available for both scale factors in |image_skia|.
365 image_rep = image_skia->GetRepresentation(ui::SCALE_FACTOR_140P); 362 gfx::ImageSkiaRep image_rep =
366 EXPECT_TRUE(image_rep.scale_factor() == ui::SCALE_FACTOR_100P || 363 image_skia->GetRepresentation(ui::SCALE_FACTOR_100P);
367 image_rep.scale_factor() == ui::SCALE_FACTOR_200P); 364 EXPECT_EQ(ui::SCALE_FACTOR_100P, image_rep.scale_factor());
368 } 365 image_rep = image_skia->GetRepresentation(ui::SCALE_FACTOR_200P);
366 EXPECT_EQ(ui::SCALE_FACTOR_200P, image_rep.scale_factor());
367
368 // The 1.4x pack was not loaded. Requesting the 1.4x resource should return
369 // either the 1x or the 2x resource.
370 image_rep = image_skia->GetRepresentation(ui::SCALE_FACTOR_140P);
371 EXPECT_TRUE(image_rep.scale_factor() == ui::SCALE_FACTOR_100P ||
372 image_rep.scale_factor() == ui::SCALE_FACTOR_200P);
373 }
374
375 TEST(ResourceBundle, FallbackToNone) {
376 // On Windows, the default data is compiled into the binary so this does
377 // nothing.
378 ScopedTempDir dir;
379 ASSERT_TRUE(dir.CreateUniqueTempDir());
380
381 FilePath locale_path = dir.path().AppendASCII("empty.pak");
382 FilePath data_default_path = dir.path().AppendASCII("sample.pak");
383
384 // Create the pak files.
385 ASSERT_EQ(file_util::WriteFile(locale_path, kEmptyPakContents,
386 kEmptyPakSize), static_cast<int>(kEmptyPakSize));
387 CreateDataPackWithSingleBitmap(data_default_path, 10);
388
389 // Load the regular pak files only.
390 ResourceBundle resource_bundle(NULL);
391 resource_bundle.LoadTestResources(data_default_path, locale_path);
392
393 gfx::ImageSkia* image_skia = resource_bundle.GetImageSkiaNamed(3);
394 EXPECT_EQ(1u, image_skia->image_reps().size());
395 EXPECT_EQ(ui::SCALE_FACTOR_100P,
396 image_skia->image_reps()[0].scale_factor());
369 } 397 }
370 398
371 } // namespace ui 399 } // namespace ui
OLDNEW
« ui/base/resource/resource_bundle.h ('K') | « ui/base/resource/resource_bundle_mac.mm ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698