| 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 <stddef.h> | 7 #include <stddef.h> |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 #include <string.h> | 9 #include <string.h> |
| 10 | 10 |
| (...skipping 400 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 411 | 411 |
| 412 // Returns the path of temporary directory to write test data packs into. | 412 // Returns the path of temporary directory to write test data packs into. |
| 413 const base::FilePath& dir_path() { return dir_.path(); } | 413 const base::FilePath& dir_path() { return dir_.path(); } |
| 414 | 414 |
| 415 // Returns the number of DataPacks managed by |resource_bundle|. | 415 // Returns the number of DataPacks managed by |resource_bundle|. |
| 416 size_t NumDataPacksInResourceBundle(ResourceBundle* resource_bundle) { | 416 size_t NumDataPacksInResourceBundle(ResourceBundle* resource_bundle) { |
| 417 DCHECK(resource_bundle); | 417 DCHECK(resource_bundle); |
| 418 return resource_bundle->data_packs_.size(); | 418 return resource_bundle->data_packs_.size(); |
| 419 } | 419 } |
| 420 | 420 |
| 421 // Returns the number of DataPacks managed by |resource_bundle| which are | |
| 422 // flagged as containing only material design resources. | |
| 423 size_t NumMaterialDesignDataPacksInResourceBundle( | |
| 424 ResourceBundle* resource_bundle) { | |
| 425 DCHECK(resource_bundle); | |
| 426 size_t num_material_packs = 0; | |
| 427 for (size_t i = 0; i < resource_bundle->data_packs_.size(); i++) { | |
| 428 if (resource_bundle->data_packs_[i]->HasOnlyMaterialDesignAssets()) | |
| 429 num_material_packs++; | |
| 430 } | |
| 431 | |
| 432 return num_material_packs; | |
| 433 } | |
| 434 | |
| 435 private: | 421 private: |
| 436 std::unique_ptr<DataPack> locale_pack_; | 422 std::unique_ptr<DataPack> locale_pack_; |
| 437 base::ScopedTempDir dir_; | 423 base::ScopedTempDir dir_; |
| 438 | 424 |
| 439 DISALLOW_COPY_AND_ASSIGN(ResourceBundleImageTest); | 425 DISALLOW_COPY_AND_ASSIGN(ResourceBundleImageTest); |
| 440 }; | 426 }; |
| 441 | 427 |
| 442 // Verify that we don't crash when trying to load a resource that is not found. | 428 // Verify that we don't crash when trying to load a resource that is not found. |
| 443 // In some cases, we fail to mmap resources.pak, but try to keep going anyway. | 429 // In some cases, we fail to mmap resources.pak, but try to keep going anyway. |
| 444 TEST_F(ResourceBundleImageTest, LoadDataResourceBytes) { | 430 TEST_F(ResourceBundleImageTest, LoadDataResourceBytes) { |
| (...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 547 ui::GetScaleForScaleFactor(ui::SCALE_FACTOR_140P)); | 533 ui::GetScaleForScaleFactor(ui::SCALE_FACTOR_140P)); |
| 548 ui::ScaleFactor scale_factor = GetSupportedScaleFactor(image_rep.scale()); | 534 ui::ScaleFactor scale_factor = GetSupportedScaleFactor(image_rep.scale()); |
| 549 EXPECT_TRUE(scale_factor == ui::SCALE_FACTOR_100P || | 535 EXPECT_TRUE(scale_factor == ui::SCALE_FACTOR_100P || |
| 550 scale_factor == ui::SCALE_FACTOR_200P); | 536 scale_factor == ui::SCALE_FACTOR_200P); |
| 551 | 537 |
| 552 // ImageSkia scales image if the one for the requested scale factor is not | 538 // ImageSkia scales image if the one for the requested scale factor is not |
| 553 // available. | 539 // available. |
| 554 EXPECT_EQ(1.4f, image_skia->GetRepresentation(1.4f).scale()); | 540 EXPECT_EQ(1.4f, image_skia->GetRepresentation(1.4f).scale()); |
| 555 } | 541 } |
| 556 | 542 |
| 557 // Verifies that the correct number of DataPacks managed by ResourceBundle | |
| 558 // are flagged as containing only material design assets. | |
| 559 TEST_F(ResourceBundleImageTest, CountMaterialDesignDataPacksInResourceBundle) { | |
| 560 ResourceBundle* resource_bundle = CreateResourceBundle(nullptr); | |
| 561 EXPECT_EQ(0u, NumDataPacksInResourceBundle(resource_bundle)); | |
| 562 EXPECT_EQ(0u, NumMaterialDesignDataPacksInResourceBundle(resource_bundle)); | |
| 563 | |
| 564 // Add a non-material data pack. | |
| 565 base::FilePath default_path = dir_path().AppendASCII("default.pak"); | |
| 566 CreateDataPackWithSingleBitmap(default_path, 10, base::StringPiece()); | |
| 567 resource_bundle->AddDataPackFromPath(default_path, SCALE_FACTOR_100P); | |
| 568 EXPECT_EQ(1u, NumDataPacksInResourceBundle(resource_bundle)); | |
| 569 EXPECT_EQ(0u, NumMaterialDesignDataPacksInResourceBundle(resource_bundle)); | |
| 570 | |
| 571 // Add a material data pack. | |
| 572 base::FilePath material_path1 = dir_path().AppendASCII("material1.pak"); | |
| 573 CreateDataPackWithSingleBitmap(material_path1, 10, base::StringPiece()); | |
| 574 resource_bundle->AddMaterialDesignDataPackFromPath(material_path1, | |
| 575 SCALE_FACTOR_100P); | |
| 576 EXPECT_EQ(2u, NumDataPacksInResourceBundle(resource_bundle)); | |
| 577 EXPECT_EQ(1u, NumMaterialDesignDataPacksInResourceBundle(resource_bundle)); | |
| 578 } | |
| 579 | |
| 580 // Verifies that data packs containing material design resources are permitted | |
| 581 // to have resource IDs which are present within other data packs managed by | |
| 582 // ResourceBundle. This test passes if it does not trigger the DCHECK in | |
| 583 // DataPack::CheckForDuplicateResources(). | |
| 584 TEST_F(ResourceBundleImageTest, NoCrashWithDuplicateMaterialDesignResources) { | |
| 585 // Create two data packs, each containing a single asset with the same ID. | |
| 586 base::FilePath default_path = dir_path().AppendASCII("default.pak"); | |
| 587 base::FilePath material_path = dir_path().AppendASCII("material.pak"); | |
| 588 CreateDataPackWithSingleBitmap(default_path, 10, base::StringPiece()); | |
| 589 CreateDataPackWithSingleBitmap(material_path, 10, base::StringPiece()); | |
| 590 | |
| 591 // Should not crash. | |
| 592 ResourceBundle* resource_bundle = CreateResourceBundleWithEmptyLocalePak(); | |
| 593 resource_bundle->AddMaterialDesignDataPackFromPath(material_path, | |
| 594 SCALE_FACTOR_100P); | |
| 595 resource_bundle->AddDataPackFromPath(default_path, SCALE_FACTOR_100P); | |
| 596 } | |
| 597 | |
| 598 // Verifies that ResourceBundle searches data pack A before data pack B for | |
| 599 // an asset if A was added to the ResourceBundle before B. | |
| 600 TEST_F(ResourceBundleImageTest, DataPackSearchOrder) { | |
| 601 // Create two .pak files, each containing a single image with the | |
| 602 // same asset ID but different sizes (note that the images must be | |
| 603 // different sizes in this test in order to correctly determine | |
| 604 // from which data pack the asset was pulled). Note also that the value | |
| 605 // of |material_size| was chosen to be divisible by 3, since iOS may | |
| 606 // use this scale factor. | |
| 607 const int default_size = 10; | |
| 608 const int material_size = 48; | |
| 609 ASSERT_NE(default_size, material_size); | |
| 610 base::FilePath default_path = dir_path().AppendASCII("default.pak"); | |
| 611 base::FilePath material_path = dir_path().AppendASCII("material.pak"); | |
| 612 CreateDataPackWithSingleBitmap(default_path, | |
| 613 default_size, | |
| 614 base::StringPiece()); | |
| 615 CreateDataPackWithSingleBitmap(material_path, | |
| 616 material_size, | |
| 617 base::StringPiece()); | |
| 618 | |
| 619 ScaleFactor scale_factor = SCALE_FACTOR_100P; | |
| 620 int expected_size = material_size; | |
| 621 ResourceBundle* resource_bundle = CreateResourceBundleWithEmptyLocalePak(); | |
| 622 | |
| 623 #if defined(OS_IOS) | |
| 624 // iOS retina devices do not use 100P scaling. See crbug.com/298406. | |
| 625 scale_factor = resource_bundle->GetMaxScaleFactor(); | |
| 626 expected_size = material_size / GetScaleForScaleFactor(scale_factor); | |
| 627 #endif | |
| 628 | |
| 629 // Load the 'material' data pack into ResourceBundle first. | |
| 630 resource_bundle->AddMaterialDesignDataPackFromPath(material_path, | |
| 631 scale_factor); | |
| 632 resource_bundle->AddDataPackFromPath(default_path, scale_factor); | |
| 633 | |
| 634 // A request for the image with ID 3 should return the image from the material | |
| 635 // data pack. | |
| 636 gfx::ImageSkia* image_skia = resource_bundle->GetImageSkiaNamed(3); | |
| 637 const SkBitmap* bitmap = image_skia->bitmap(); | |
| 638 ASSERT_TRUE(bitmap); | |
| 639 EXPECT_EQ(expected_size, bitmap->width()); | |
| 640 EXPECT_EQ(expected_size, bitmap->height()); | |
| 641 | |
| 642 // A subsequent request for the image with ID 3 (i.e., after the image | |
| 643 // has been cached by ResourceBundle) should also return the image | |
| 644 // from the material data pack. | |
| 645 gfx::ImageSkia* image_skia2 = resource_bundle->GetImageSkiaNamed(3); | |
| 646 const SkBitmap* bitmap2 = image_skia2->bitmap(); | |
| 647 ASSERT_TRUE(bitmap2); | |
| 648 EXPECT_EQ(expected_size, bitmap2->width()); | |
| 649 EXPECT_EQ(expected_size, bitmap2->height()); | |
| 650 } | |
| 651 | |
| 652 // Test that GetImageNamed() behaves properly for images which GRIT has | 543 // Test that GetImageNamed() behaves properly for images which GRIT has |
| 653 // annotated as having fallen back to 1x. | 544 // annotated as having fallen back to 1x. |
| 654 TEST_F(ResourceBundleImageTest, GetImageNamedFallback1x) { | 545 TEST_F(ResourceBundleImageTest, GetImageNamedFallback1x) { |
| 655 std::vector<ScaleFactor> supported_factors; | 546 std::vector<ScaleFactor> supported_factors; |
| 656 supported_factors.push_back(SCALE_FACTOR_100P); | 547 supported_factors.push_back(SCALE_FACTOR_100P); |
| 657 supported_factors.push_back(SCALE_FACTOR_200P); | 548 supported_factors.push_back(SCALE_FACTOR_200P); |
| 658 test::ScopedSetSupportedScaleFactors scoped_supported(supported_factors); | 549 test::ScopedSetSupportedScaleFactors scoped_supported(supported_factors); |
| 659 base::FilePath data_path = dir_path().AppendASCII("sample.pak"); | 550 base::FilePath data_path = dir_path().AppendASCII("sample.pak"); |
| 660 base::FilePath data_2x_path = dir_path().AppendASCII("sample_2x.pak"); | 551 base::FilePath data_2x_path = dir_path().AppendASCII("sample_2x.pak"); |
| 661 | 552 |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 742 resource_bundle->AddDataPackFromPath(data_default_path, SCALE_FACTOR_NONE); | 633 resource_bundle->AddDataPackFromPath(data_default_path, SCALE_FACTOR_NONE); |
| 743 | 634 |
| 744 gfx::ImageSkia* image_skia = resource_bundle->GetImageSkiaNamed(3); | 635 gfx::ImageSkia* image_skia = resource_bundle->GetImageSkiaNamed(3); |
| 745 EXPECT_EQ(1u, image_skia->image_reps().size()); | 636 EXPECT_EQ(1u, image_skia->image_reps().size()); |
| 746 EXPECT_TRUE(image_skia->image_reps()[0].unscaled()); | 637 EXPECT_TRUE(image_skia->image_reps()[0].unscaled()); |
| 747 EXPECT_EQ(ui::SCALE_FACTOR_100P, | 638 EXPECT_EQ(ui::SCALE_FACTOR_100P, |
| 748 GetSupportedScaleFactor(image_skia->image_reps()[0].scale())); | 639 GetSupportedScaleFactor(image_skia->image_reps()[0].scale())); |
| 749 } | 640 } |
| 750 | 641 |
| 751 } // namespace ui | 642 } // namespace ui |
| OLD | NEW |