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/big_endian.h" | 8 #include "base/big_endian.h" |
9 #include "base/file_util.h" | 9 #include "base/file_util.h" |
10 #include "base/files/file_path.h" | 10 #include "base/files/file_path.h" |
11 #include "base/files/scoped_temp_dir.h" | 11 #include "base/files/scoped_temp_dir.h" |
12 #include "base/logging.h" | 12 #include "base/logging.h" |
13 #include "base/memory/ref_counted_memory.h" | 13 #include "base/memory/ref_counted_memory.h" |
14 #include "base/path_service.h" | 14 #include "base/path_service.h" |
15 #include "base/strings/utf_string_conversions.h" | 15 #include "base/strings/utf_string_conversions.h" |
16 #include "grit/ui_resources.h" | 16 #include "grit/ui_resources.h" |
17 #include "testing/gmock/include/gmock/gmock.h" | 17 #include "testing/gmock/include/gmock/gmock.h" |
18 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
19 #include "third_party/skia/include/core/SkBitmap.h" | 19 #include "third_party/skia/include/core/SkBitmap.h" |
20 #include "ui/base/layout.h" | 20 #include "ui/base/layout.h" |
21 #include "ui/base/resource/data_pack.h" | 21 #include "ui/base/resource/data_pack.h" |
22 #include "ui/gfx/codec/png_codec.h" | 22 #include "ui/gfx/codec/png_codec.h" |
23 #include "ui/gfx/image/image_skia.h" | 23 #include "ui/gfx/image/image_skia.h" |
| 24 #if defined(OS_WIN) |
| 25 #include "ui/gfx/win/dpi.h" |
| 26 #endif |
24 | 27 |
25 using ::testing::_; | 28 using ::testing::_; |
26 using ::testing::Between; | 29 using ::testing::Between; |
27 using ::testing::Property; | 30 using ::testing::Property; |
28 using ::testing::Return; | 31 using ::testing::Return; |
29 using ::testing::ReturnArg; | 32 using ::testing::ReturnArg; |
30 | 33 |
31 namespace ui { | 34 namespace ui { |
32 | 35 |
33 extern const char kSamplePakContents[]; | 36 extern const char kSamplePakContents[]; |
(...skipping 398 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
432 // for both scale factor requests. | 435 // for both scale factor requests. |
433 EXPECT_EQ("this is id 6", resource_bundle->GetRawDataResourceForScale(6, | 436 EXPECT_EQ("this is id 6", resource_bundle->GetRawDataResourceForScale(6, |
434 SCALE_FACTOR_100P)); | 437 SCALE_FACTOR_100P)); |
435 EXPECT_EQ("this is id 6", resource_bundle->GetRawDataResourceForScale(6, | 438 EXPECT_EQ("this is id 6", resource_bundle->GetRawDataResourceForScale(6, |
436 SCALE_FACTOR_200P)); | 439 SCALE_FACTOR_200P)); |
437 } | 440 } |
438 | 441 |
439 // Test requesting image reps at various scale factors from the image returned | 442 // Test requesting image reps at various scale factors from the image returned |
440 // via ResourceBundle::GetImageNamed(). | 443 // via ResourceBundle::GetImageNamed(). |
441 TEST_F(ResourceBundleImageTest, GetImageNamed) { | 444 TEST_F(ResourceBundleImageTest, GetImageNamed) { |
| 445 #if defined(OS_WIN) |
| 446 gfx::ForceHighDPISupportForTesting(2.0); |
| 447 #endif |
442 std::vector<ScaleFactor> supported_factors; | 448 std::vector<ScaleFactor> supported_factors; |
443 supported_factors.push_back(SCALE_FACTOR_100P); | 449 supported_factors.push_back(SCALE_FACTOR_100P); |
444 supported_factors.push_back(SCALE_FACTOR_200P); | 450 supported_factors.push_back(SCALE_FACTOR_200P); |
445 test::ScopedSetSupportedScaleFactors scoped_supported(supported_factors); | 451 test::ScopedSetSupportedScaleFactors scoped_supported(supported_factors); |
446 base::FilePath data_1x_path = dir_path().AppendASCII("sample_1x.pak"); | 452 base::FilePath data_1x_path = dir_path().AppendASCII("sample_1x.pak"); |
447 base::FilePath data_2x_path = dir_path().AppendASCII("sample_2x.pak"); | 453 base::FilePath data_2x_path = dir_path().AppendASCII("sample_2x.pak"); |
448 | 454 |
449 // Create the pak files. | 455 // Create the pak files. |
450 CreateDataPackWithSingleBitmap(data_1x_path, 10, base::StringPiece()); | 456 CreateDataPackWithSingleBitmap(data_1x_path, 10, base::StringPiece()); |
451 CreateDataPackWithSingleBitmap(data_2x_path, 20, base::StringPiece()); | 457 CreateDataPackWithSingleBitmap(data_2x_path, 20, base::StringPiece()); |
(...skipping 12 matching lines...) Expand all Loading... |
464 EXPECT_EQ(ui::SCALE_FACTOR_200P, | 470 EXPECT_EQ(ui::SCALE_FACTOR_200P, |
465 GetSupportedScaleFactor(image_skia->image_reps()[0].scale())); | 471 GetSupportedScaleFactor(image_skia->image_reps()[0].scale())); |
466 #else | 472 #else |
467 EXPECT_EQ(ui::SCALE_FACTOR_100P, | 473 EXPECT_EQ(ui::SCALE_FACTOR_100P, |
468 GetSupportedScaleFactor(image_skia->image_reps()[0].scale())); | 474 GetSupportedScaleFactor(image_skia->image_reps()[0].scale())); |
469 #endif | 475 #endif |
470 | 476 |
471 // Resource ID 3 exists in both 1x and 2x paks. Image reps should be | 477 // Resource ID 3 exists in both 1x and 2x paks. Image reps should be |
472 // available for both scale factors in |image_skia|. | 478 // available for both scale factors in |image_skia|. |
473 gfx::ImageSkiaRep image_rep = | 479 gfx::ImageSkiaRep image_rep = |
474 image_skia->GetRepresentation(GetImageScale(ui::SCALE_FACTOR_100P)); | 480 image_skia->GetRepresentation( |
| 481 GetScaleForScaleFactor(ui::SCALE_FACTOR_100P)); |
475 EXPECT_EQ(ui::SCALE_FACTOR_100P, GetSupportedScaleFactor(image_rep.scale())); | 482 EXPECT_EQ(ui::SCALE_FACTOR_100P, GetSupportedScaleFactor(image_rep.scale())); |
476 image_rep = | 483 image_rep = |
477 image_skia->GetRepresentation(GetImageScale(ui::SCALE_FACTOR_200P)); | 484 image_skia->GetRepresentation( |
| 485 GetScaleForScaleFactor(ui::SCALE_FACTOR_200P)); |
478 EXPECT_EQ(ui::SCALE_FACTOR_200P, GetSupportedScaleFactor(image_rep.scale())); | 486 EXPECT_EQ(ui::SCALE_FACTOR_200P, GetSupportedScaleFactor(image_rep.scale())); |
479 | 487 |
480 // The 1.4x pack was not loaded. Requesting the 1.4x resource should return | 488 // The 1.4x pack was not loaded. Requesting the 1.4x resource should return |
481 // either the 1x or the 2x resource. | 489 // either the 1x or the 2x resource. |
482 image_rep = image_skia->GetRepresentation( | 490 image_rep = image_skia->GetRepresentation( |
483 ui::GetImageScale(ui::SCALE_FACTOR_140P)); | 491 ui::GetScaleForScaleFactor(ui::SCALE_FACTOR_140P)); |
484 ui::ScaleFactor scale_factor = GetSupportedScaleFactor(image_rep.scale()); | 492 ui::ScaleFactor scale_factor = GetSupportedScaleFactor(image_rep.scale()); |
485 EXPECT_TRUE(scale_factor == ui::SCALE_FACTOR_100P || | 493 EXPECT_TRUE(scale_factor == ui::SCALE_FACTOR_100P || |
486 scale_factor == ui::SCALE_FACTOR_200P); | 494 scale_factor == ui::SCALE_FACTOR_200P); |
487 #if !defined(OS_WIN) | 495 #if !defined(OS_WIN) |
488 // If the image is not found, the it should simply image rep should | 496 // If the image is not found, the it should simply image rep should |
489 // contain the scale of the image rep, not the requested scale. | 497 // contain the scale of the image rep, not the requested scale. |
490 EXPECT_EQ(1.0f, image_skia->GetRepresentation(1.4f).scale()); | 498 EXPECT_EQ(1.0f, image_skia->GetRepresentation(1.4f).scale()); |
491 #endif | 499 #endif |
492 } | 500 } |
493 | 501 |
(...skipping 18 matching lines...) Expand all Loading... |
512 // Load the regular and 2x pak files. | 520 // Load the regular and 2x pak files. |
513 ResourceBundle* resource_bundle = CreateResourceBundleWithEmptyLocalePak(); | 521 ResourceBundle* resource_bundle = CreateResourceBundleWithEmptyLocalePak(); |
514 resource_bundle->AddDataPackFromPath(data_path, SCALE_FACTOR_100P); | 522 resource_bundle->AddDataPackFromPath(data_path, SCALE_FACTOR_100P); |
515 resource_bundle->AddDataPackFromPath(data_2x_path, SCALE_FACTOR_200P); | 523 resource_bundle->AddDataPackFromPath(data_2x_path, SCALE_FACTOR_200P); |
516 | 524 |
517 gfx::ImageSkia* image_skia = resource_bundle->GetImageSkiaNamed(3); | 525 gfx::ImageSkia* image_skia = resource_bundle->GetImageSkiaNamed(3); |
518 | 526 |
519 // The image rep for 2x should be available. It should be resized to the | 527 // The image rep for 2x should be available. It should be resized to the |
520 // proper 2x size. | 528 // proper 2x size. |
521 gfx::ImageSkiaRep image_rep = | 529 gfx::ImageSkiaRep image_rep = |
522 image_skia->GetRepresentation(GetImageScale(ui::SCALE_FACTOR_200P)); | 530 image_skia->GetRepresentation(GetScaleForScaleFactor( |
| 531 ui::SCALE_FACTOR_200P)); |
523 EXPECT_EQ(ui::SCALE_FACTOR_200P, GetSupportedScaleFactor(image_rep.scale())); | 532 EXPECT_EQ(ui::SCALE_FACTOR_200P, GetSupportedScaleFactor(image_rep.scale())); |
524 EXPECT_EQ(20, image_rep.pixel_width()); | 533 EXPECT_EQ(20, image_rep.pixel_width()); |
525 EXPECT_EQ(20, image_rep.pixel_height()); | 534 EXPECT_EQ(20, image_rep.pixel_height()); |
526 } | 535 } |
527 | 536 |
528 #if defined(OS_WIN) | 537 #if defined(OS_WIN) |
529 // Tests GetImageNamed() behaves properly when the size of a scaled image | 538 // Tests GetImageNamed() behaves properly when the size of a scaled image |
530 // requires rounding as a result of using a non-integer scale factor. | 539 // requires rounding as a result of using a non-integer scale factor. |
531 // Scale factors of 140 and 1805 are Windows specific. | 540 // Scale factors of 140 and 1805 are Windows specific. |
532 TEST_F(ResourceBundleImageTest, GetImageNamedFallback1xRounding) { | 541 TEST_F(ResourceBundleImageTest, GetImageNamedFallback1xRounding) { |
(...skipping 18 matching lines...) Expand all Loading... |
551 | 560 |
552 ResourceBundle* resource_bundle = CreateResourceBundleWithEmptyLocalePak(); | 561 ResourceBundle* resource_bundle = CreateResourceBundleWithEmptyLocalePak(); |
553 resource_bundle->AddDataPackFromPath(data_path, SCALE_FACTOR_100P); | 562 resource_bundle->AddDataPackFromPath(data_path, SCALE_FACTOR_100P); |
554 resource_bundle->AddDataPackFromPath(data_140P_path, SCALE_FACTOR_140P); | 563 resource_bundle->AddDataPackFromPath(data_140P_path, SCALE_FACTOR_140P); |
555 resource_bundle->AddDataPackFromPath(data_180P_path, SCALE_FACTOR_180P); | 564 resource_bundle->AddDataPackFromPath(data_180P_path, SCALE_FACTOR_180P); |
556 | 565 |
557 // Non-integer dimensions should be rounded up. | 566 // Non-integer dimensions should be rounded up. |
558 gfx::ImageSkia* image_skia = resource_bundle->GetImageSkiaNamed(3); | 567 gfx::ImageSkia* image_skia = resource_bundle->GetImageSkiaNamed(3); |
559 gfx::ImageSkiaRep image_rep = | 568 gfx::ImageSkiaRep image_rep = |
560 image_skia->GetRepresentation( | 569 image_skia->GetRepresentation( |
561 GetImageScale(ui::SCALE_FACTOR_140P)); | 570 GetScaleForScaleFactor(ui::SCALE_FACTOR_140P)); |
562 EXPECT_EQ(12, image_rep.pixel_width()); | 571 EXPECT_EQ(12, image_rep.pixel_width()); |
563 image_rep = image_skia->GetRepresentation( | 572 image_rep = image_skia->GetRepresentation( |
564 GetImageScale(ui::SCALE_FACTOR_180P)); | 573 GetScaleForScaleFactor(ui::SCALE_FACTOR_180P)); |
565 EXPECT_EQ(15, image_rep.pixel_width()); | 574 EXPECT_EQ(15, image_rep.pixel_width()); |
566 } | 575 } |
567 #endif | 576 #endif |
568 | 577 |
569 #if defined(OS_IOS) | 578 #if defined(OS_IOS) |
570 // Fails on devices that have non-100P scaling. See crbug.com/298406 | 579 // Fails on devices that have non-100P scaling. See crbug.com/298406 |
571 #define MAYBE_FallbackToNone DISABLED_FallbackToNone | 580 #define MAYBE_FallbackToNone DISABLED_FallbackToNone |
572 #else | 581 #else |
573 #define MAYBE_FallbackToNone FallbackToNone | 582 #define MAYBE_FallbackToNone FallbackToNone |
574 #endif | 583 #endif |
575 TEST_F(ResourceBundleImageTest, MAYBE_FallbackToNone) { | 584 TEST_F(ResourceBundleImageTest, MAYBE_FallbackToNone) { |
576 base::FilePath data_default_path = dir_path().AppendASCII("sample.pak"); | 585 base::FilePath data_default_path = dir_path().AppendASCII("sample.pak"); |
577 | 586 |
578 // Create the pak files. | 587 // Create the pak files. |
579 CreateDataPackWithSingleBitmap(data_default_path, 10, base::StringPiece()); | 588 CreateDataPackWithSingleBitmap(data_default_path, 10, base::StringPiece()); |
580 | 589 |
581 // Load the regular pak files only. | 590 // Load the regular pak files only. |
582 ResourceBundle* resource_bundle = CreateResourceBundleWithEmptyLocalePak(); | 591 ResourceBundle* resource_bundle = CreateResourceBundleWithEmptyLocalePak(); |
583 resource_bundle->AddDataPackFromPath(data_default_path, SCALE_FACTOR_NONE); | 592 resource_bundle->AddDataPackFromPath(data_default_path, SCALE_FACTOR_NONE); |
584 | 593 |
585 gfx::ImageSkia* image_skia = resource_bundle->GetImageSkiaNamed(3); | 594 gfx::ImageSkia* image_skia = resource_bundle->GetImageSkiaNamed(3); |
586 EXPECT_EQ(1u, image_skia->image_reps().size()); | 595 EXPECT_EQ(1u, image_skia->image_reps().size()); |
587 EXPECT_TRUE(image_skia->image_reps()[0].unscaled()); | 596 EXPECT_TRUE(image_skia->image_reps()[0].unscaled()); |
588 EXPECT_EQ(ui::SCALE_FACTOR_100P, | 597 EXPECT_EQ(ui::SCALE_FACTOR_100P, |
589 GetSupportedScaleFactor(image_skia->image_reps()[0].scale())); | 598 GetSupportedScaleFactor(image_skia->image_reps()[0].scale())); |
590 } | 599 } |
591 | 600 |
592 } // namespace ui | 601 } // namespace ui |
OLD | NEW |