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

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

Issue 24175004: Remove dependency on ui::ScaleFactor from ui/gfx (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Rename methods and vars to make image_scale more clear Created 7 years, 3 months 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_util.h" 8 #include "base/file_util.h"
9 #include "base/files/file_path.h" 9 #include "base/files/file_path.h"
10 #include "base/files/scoped_temp_dir.h" 10 #include "base/files/scoped_temp_dir.h"
(...skipping 415 matching lines...) Expand 10 before | Expand all | Expand 10 after
426 // for both scale factor requests. 426 // for both scale factor requests.
427 EXPECT_EQ("this is id 6", resource_bundle->GetRawDataResourceForScale(6, 427 EXPECT_EQ("this is id 6", resource_bundle->GetRawDataResourceForScale(6,
428 SCALE_FACTOR_100P)); 428 SCALE_FACTOR_100P));
429 EXPECT_EQ("this is id 6", resource_bundle->GetRawDataResourceForScale(6, 429 EXPECT_EQ("this is id 6", resource_bundle->GetRawDataResourceForScale(6,
430 SCALE_FACTOR_200P)); 430 SCALE_FACTOR_200P));
431 } 431 }
432 432
433 // Test requesting image reps at various scale factors from the image returned 433 // Test requesting image reps at various scale factors from the image returned
434 // via ResourceBundle::GetImageNamed(). 434 // via ResourceBundle::GetImageNamed().
435 TEST_F(ResourceBundleImageTest, GetImageNamed) { 435 TEST_F(ResourceBundleImageTest, GetImageNamed) {
436 std::vector<ScaleFactor> supported_factors;
437 supported_factors.push_back(SCALE_FACTOR_100P);
438 supported_factors.push_back(SCALE_FACTOR_200P);
439 test::ScopedSetSupportedScaleFactors scoped_supported(supported_factors);
436 base::FilePath data_1x_path = dir_path().AppendASCII("sample_1x.pak"); 440 base::FilePath data_1x_path = dir_path().AppendASCII("sample_1x.pak");
437 base::FilePath data_2x_path = dir_path().AppendASCII("sample_2x.pak"); 441 base::FilePath data_2x_path = dir_path().AppendASCII("sample_2x.pak");
438 442
439 // Create the pak files. 443 // Create the pak files.
440 CreateDataPackWithSingleBitmap(data_1x_path, 10, base::StringPiece()); 444 CreateDataPackWithSingleBitmap(data_1x_path, 10, base::StringPiece());
441 CreateDataPackWithSingleBitmap(data_2x_path, 20, base::StringPiece()); 445 CreateDataPackWithSingleBitmap(data_2x_path, 20, base::StringPiece());
442 446
443 // Load the regular and 2x pak files. 447 // Load the regular and 2x pak files.
444 ResourceBundle* resource_bundle = CreateResourceBundleWithEmptyLocalePak(); 448 ResourceBundle* resource_bundle = CreateResourceBundleWithEmptyLocalePak();
445 resource_bundle->AddDataPackFromPath(data_1x_path, SCALE_FACTOR_100P); 449 resource_bundle->AddDataPackFromPath(data_1x_path, SCALE_FACTOR_100P);
446 resource_bundle->AddDataPackFromPath(data_2x_path, SCALE_FACTOR_200P); 450 resource_bundle->AddDataPackFromPath(data_2x_path, SCALE_FACTOR_200P);
447 451
448 EXPECT_EQ(SCALE_FACTOR_200P, resource_bundle->max_scale_factor()); 452 EXPECT_EQ(SCALE_FACTOR_200P, resource_bundle->max_scale_factor());
449 453
450 gfx::ImageSkia* image_skia = resource_bundle->GetImageSkiaNamed(3); 454 gfx::ImageSkia* image_skia = resource_bundle->GetImageSkiaNamed(3);
451 455
452 #if defined(OS_CHROMEOS) 456 #if defined(OS_CHROMEOS)
453 // ChromeOS loads highest scale factor first. 457 // ChromeOS loads highest scale factor first.
454 EXPECT_EQ(ui::SCALE_FACTOR_200P, image_skia->image_reps()[0].scale_factor()); 458 EXPECT_EQ(ui::SCALE_FACTOR_200P,
459 GetSupportedScaleFactor(image_skia->image_reps()[0].scale()));
455 #else 460 #else
456 EXPECT_EQ(ui::SCALE_FACTOR_100P, image_skia->image_reps()[0].scale_factor()); 461 EXPECT_EQ(ui::SCALE_FACTOR_100P,
462 GetSupportedScaleFactor(image_skia->image_reps()[0].scale()));
457 #endif 463 #endif
458 464
459 // Resource ID 3 exists in both 1x and 2x paks. Image reps should be 465 // Resource ID 3 exists in both 1x and 2x paks. Image reps should be
460 // available for both scale factors in |image_skia|. 466 // available for both scale factors in |image_skia|.
461 gfx::ImageSkiaRep image_rep = 467 gfx::ImageSkiaRep image_rep =
462 image_skia->GetRepresentation(ui::SCALE_FACTOR_100P); 468 image_skia->GetRepresentation(GetImageScale(ui::SCALE_FACTOR_100P));
463 EXPECT_EQ(ui::SCALE_FACTOR_100P, image_rep.scale_factor()); 469 EXPECT_EQ(ui::SCALE_FACTOR_100P, GetSupportedScaleFactor(image_rep.scale()));
464 image_rep = image_skia->GetRepresentation(ui::SCALE_FACTOR_200P); 470 image_rep =
465 EXPECT_EQ(ui::SCALE_FACTOR_200P, image_rep.scale_factor()); 471 image_skia->GetRepresentation(GetImageScale(ui::SCALE_FACTOR_200P));
472 EXPECT_EQ(ui::SCALE_FACTOR_200P, GetSupportedScaleFactor(image_rep.scale()));
466 473
467 // The 1.4x pack was not loaded. Requesting the 1.4x resource should return 474 // The 1.4x pack was not loaded. Requesting the 1.4x resource should return
468 // either the 1x or the 2x resource. 475 // either the 1x or the 2x resource.
469 image_rep = image_skia->GetRepresentation(ui::SCALE_FACTOR_140P); 476 image_rep = image_skia->GetRepresentation(
470 EXPECT_TRUE(image_rep.scale_factor() == ui::SCALE_FACTOR_100P || 477 ui::GetImageScale(ui::SCALE_FACTOR_140P));
471 image_rep.scale_factor() == ui::SCALE_FACTOR_200P); 478 ui::ScaleFactor scale_factor = GetSupportedScaleFactor(image_rep.scale());
479 EXPECT_TRUE(scale_factor == ui::SCALE_FACTOR_100P ||
480 scale_factor == ui::SCALE_FACTOR_200P);
472 } 481 }
473 482
474 // Test that GetImageNamed() behaves properly for images which GRIT has 483 // Test that GetImageNamed() behaves properly for images which GRIT has
475 // annotated as having fallen back to 1x. 484 // annotated as having fallen back to 1x.
476 TEST_F(ResourceBundleImageTest, GetImageNamedFallback1x) { 485 TEST_F(ResourceBundleImageTest, GetImageNamedFallback1x) {
486 std::vector<ScaleFactor> supported_factors;
487 supported_factors.push_back(SCALE_FACTOR_100P);
488 supported_factors.push_back(SCALE_FACTOR_200P);
489 test::ScopedSetSupportedScaleFactors scoped_supported(supported_factors);
477 base::FilePath data_path = dir_path().AppendASCII("sample.pak"); 490 base::FilePath data_path = dir_path().AppendASCII("sample.pak");
478 base::FilePath data_2x_path = dir_path().AppendASCII("sample_2x.pak"); 491 base::FilePath data_2x_path = dir_path().AppendASCII("sample_2x.pak");
479 492
480 // Create the pak files. 493 // Create the pak files.
481 CreateDataPackWithSingleBitmap(data_path, 10, base::StringPiece()); 494 CreateDataPackWithSingleBitmap(data_path, 10, base::StringPiece());
482 // 2x data pack bitmap has custom chunk to indicate that the 2x bitmap is not 495 // 2x data pack bitmap has custom chunk to indicate that the 2x bitmap is not
483 // available and that GRIT fell back to 1x. 496 // available and that GRIT fell back to 1x.
484 CreateDataPackWithSingleBitmap(data_2x_path, 10, base::StringPiece( 497 CreateDataPackWithSingleBitmap(data_2x_path, 10, base::StringPiece(
485 reinterpret_cast<const char*>(kPngScaleChunk), 498 reinterpret_cast<const char*>(kPngScaleChunk),
486 arraysize(kPngScaleChunk))); 499 arraysize(kPngScaleChunk)));
487 500
488 // Load the regular and 2x pak files. 501 // Load the regular and 2x pak files.
489 ResourceBundle* resource_bundle = CreateResourceBundleWithEmptyLocalePak(); 502 ResourceBundle* resource_bundle = CreateResourceBundleWithEmptyLocalePak();
490 resource_bundle->AddDataPackFromPath(data_path, SCALE_FACTOR_100P); 503 resource_bundle->AddDataPackFromPath(data_path, SCALE_FACTOR_100P);
491 resource_bundle->AddDataPackFromPath(data_2x_path, SCALE_FACTOR_200P); 504 resource_bundle->AddDataPackFromPath(data_2x_path, SCALE_FACTOR_200P);
492 505
493 gfx::ImageSkia* image_skia = resource_bundle->GetImageSkiaNamed(3); 506 gfx::ImageSkia* image_skia = resource_bundle->GetImageSkiaNamed(3);
494 507
495 // The image rep for 2x should be available. It should be resized to the 508 // The image rep for 2x should be available. It should be resized to the
496 // proper 2x size. 509 // proper 2x size.
497 gfx::ImageSkiaRep image_rep = 510 gfx::ImageSkiaRep image_rep =
498 image_skia->GetRepresentation(ui::SCALE_FACTOR_200P); 511 image_skia->GetRepresentation(GetImageScale(ui::SCALE_FACTOR_200P));
499 EXPECT_EQ(ui::SCALE_FACTOR_200P, image_rep.scale_factor()); 512 EXPECT_EQ(ui::SCALE_FACTOR_200P, GetSupportedScaleFactor(image_rep.scale()));
500 EXPECT_EQ(20, image_rep.pixel_width()); 513 EXPECT_EQ(20, image_rep.pixel_width());
501 EXPECT_EQ(20, image_rep.pixel_height()); 514 EXPECT_EQ(20, image_rep.pixel_height());
502 } 515 }
503 516
504 #if defined(OS_WIN) 517 #if defined(OS_WIN)
505 // Tests GetImageNamed() behaves properly when the size of a scaled image 518 // Tests GetImageNamed() behaves properly when the size of a scaled image
506 // requires rounding as a result of using a non-integer scale factor. 519 // requires rounding as a result of using a non-integer scale factor.
507 // Scale factors of 140 and 1805 are Windows specific. 520 // Scale factors of 140 and 1805 are Windows specific.
508 TEST_F(ResourceBundleImageTest, GetImageNamedFallback1xRounding) { 521 TEST_F(ResourceBundleImageTest, GetImageNamedFallback1xRounding) {
509 base::FilePath data_path = dir_path().AppendASCII("sample.pak"); 522 base::FilePath data_path = dir_path().AppendASCII("sample.pak");
(...skipping 10 matching lines...) Expand all
520 arraysize(kPngScaleChunk))); 533 arraysize(kPngScaleChunk)));
521 534
522 ResourceBundle* resource_bundle = CreateResourceBundleWithEmptyLocalePak(); 535 ResourceBundle* resource_bundle = CreateResourceBundleWithEmptyLocalePak();
523 resource_bundle->AddDataPackFromPath(data_path, SCALE_FACTOR_100P); 536 resource_bundle->AddDataPackFromPath(data_path, SCALE_FACTOR_100P);
524 resource_bundle->AddDataPackFromPath(data_140P_path, SCALE_FACTOR_140P); 537 resource_bundle->AddDataPackFromPath(data_140P_path, SCALE_FACTOR_140P);
525 resource_bundle->AddDataPackFromPath(data_180P_path, SCALE_FACTOR_180P); 538 resource_bundle->AddDataPackFromPath(data_180P_path, SCALE_FACTOR_180P);
526 539
527 // Non-integer dimensions should be rounded up. 540 // Non-integer dimensions should be rounded up.
528 gfx::ImageSkia* image_skia = resource_bundle->GetImageSkiaNamed(3); 541 gfx::ImageSkia* image_skia = resource_bundle->GetImageSkiaNamed(3);
529 gfx::ImageSkiaRep image_rep = 542 gfx::ImageSkiaRep image_rep =
530 image_skia->GetRepresentation(ui::SCALE_FACTOR_140P); 543 image_skia->GetRepresentation(
544 GetImageScale(ui::SCALE_FACTOR_140P));
531 EXPECT_EQ(12, image_rep.pixel_width()); 545 EXPECT_EQ(12, image_rep.pixel_width());
532 image_rep = image_skia->GetRepresentation(ui::SCALE_FACTOR_180P); 546 image_rep = image_skia->GetRepresentation(
547 GetImageScale(ui::SCALE_FACTOR_180P));
533 EXPECT_EQ(15, image_rep.pixel_width()); 548 EXPECT_EQ(15, image_rep.pixel_width());
534 } 549 }
535 #endif 550 #endif
536 551
537 TEST_F(ResourceBundleImageTest, FallbackToNone) { 552 TEST_F(ResourceBundleImageTest, FallbackToNone) {
538 base::FilePath data_default_path = dir_path().AppendASCII("sample.pak"); 553 base::FilePath data_default_path = dir_path().AppendASCII("sample.pak");
539 554
540 // Create the pak files. 555 // Create the pak files.
541 CreateDataPackWithSingleBitmap(data_default_path, 10, base::StringPiece()); 556 CreateDataPackWithSingleBitmap(data_default_path, 10, base::StringPiece());
542 557
543 // Load the regular pak files only. 558 // Load the regular pak files only.
544 ResourceBundle* resource_bundle = CreateResourceBundleWithEmptyLocalePak(); 559 ResourceBundle* resource_bundle = CreateResourceBundleWithEmptyLocalePak();
545 resource_bundle->AddDataPackFromPath(data_default_path, SCALE_FACTOR_NONE); 560 resource_bundle->AddDataPackFromPath(data_default_path, SCALE_FACTOR_NONE);
546 561
547 gfx::ImageSkia* image_skia = resource_bundle->GetImageSkiaNamed(3); 562 gfx::ImageSkia* image_skia = resource_bundle->GetImageSkiaNamed(3);
548 EXPECT_EQ(1u, image_skia->image_reps().size()); 563 EXPECT_EQ(1u, image_skia->image_reps().size());
549 EXPECT_EQ(ui::SCALE_FACTOR_100P, 564 EXPECT_EQ(ui::SCALE_FACTOR_100P,
550 image_skia->image_reps()[0].scale_factor()); 565 GetSupportedScaleFactor(image_skia->image_reps()[0].scale()));
551 } 566 }
552 567
553 } // namespace ui 568 } // namespace ui
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698