Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/wm/core/shadow.h" | 5 #include "ui/wm/core/shadow.h" |
| 6 | 6 |
| 7 #include <memory> | |
| 8 | |
| 9 #include "base/macros.h" | 7 #include "base/macros.h" |
| 10 #include "base/path_service.h" | |
| 11 #include "third_party/skia/include/core/SkBitmap.h" | |
| 12 #include "ui/aura/test/aura_test_base.h" | 8 #include "ui/aura/test/aura_test_base.h" |
| 13 #include "ui/aura/test/test_windows.h" | |
| 14 #include "ui/aura/window.h" | |
| 15 #include "ui/base/resource/resource_bundle.h" | |
| 16 #include "ui/base/ui_base_paths.h" | |
| 17 #include "ui/compositor/layer.h" | |
| 18 #include "ui/compositor/layer_tree_owner.h" | |
| 19 #include "ui/resources/grit/ui_resources.h" | |
| 20 | 9 |
| 21 namespace wm { | 10 namespace wm { |
| 22 | |
| 23 namespace { | 11 namespace { |
| 24 | 12 |
| 25 const int kSmallBitmapSize = 129; | 13 class ShadowTest : public aura::test::AuraTestBase { |
|
James Cook
2016/12/05 23:38:36
using ShadowTest = aura::test::AuraTestBase?
Evan Stade
2016/12/06 00:53:21
Done.
| |
| 26 const int kLargeBitmapSize = 269; | |
| 27 | |
| 28 // Mock for the ResourceBundle::Delegate class. | |
| 29 class MockResourceBundleDelegate : public ui::ResourceBundle::Delegate { | |
| 30 public: | |
| 31 MockResourceBundleDelegate() : last_resource_id_(0) { | |
| 32 SkBitmap bitmap_small, bitmap_large; | |
| 33 bitmap_small.allocPixels( | |
| 34 SkImageInfo::MakeN32Premul(kSmallBitmapSize, kSmallBitmapSize)); | |
| 35 bitmap_large.allocPixels( | |
| 36 SkImageInfo::MakeN32Premul(kLargeBitmapSize, kLargeBitmapSize)); | |
| 37 image_small_ = gfx::Image::CreateFrom1xBitmap(bitmap_small); | |
| 38 image_large_ = gfx::Image::CreateFrom1xBitmap(bitmap_large); | |
| 39 } | |
| 40 ~MockResourceBundleDelegate() override {} | |
| 41 | |
| 42 // ResourceBundle::Delegate: | |
| 43 base::FilePath GetPathForResourcePack(const base::FilePath& pack_path, | |
| 44 ui::ScaleFactor scale_factor) override { | |
| 45 return base::FilePath(); | |
| 46 } | |
| 47 base::FilePath GetPathForLocalePack(const base::FilePath& pack_path, | |
| 48 const std::string& locale) override { | |
| 49 return base::FilePath(); | |
| 50 } | |
| 51 gfx::Image GetImageNamed(int resource_id) override { | |
| 52 last_resource_id_ = resource_id; | |
| 53 switch (resource_id) { | |
| 54 case IDR_WINDOW_BUBBLE_SHADOW_SMALL: | |
| 55 return image_small_; | |
| 56 case IDR_AURA_SHADOW_ACTIVE: | |
| 57 case IDR_AURA_SHADOW_INACTIVE: | |
| 58 return image_large_; | |
| 59 default: | |
| 60 NOTREACHED(); | |
| 61 return gfx::Image(); | |
| 62 } | |
| 63 } | |
| 64 gfx::Image GetNativeImageNamed(int resource_id) override { | |
| 65 return gfx::Image(); | |
| 66 } | |
| 67 base::RefCountedStaticMemory* LoadDataResourceBytes( | |
| 68 int resource_id, | |
| 69 ui::ScaleFactor scale_factor) override { | |
| 70 return NULL; | |
| 71 } | |
| 72 bool GetRawDataResource(int resource_id, | |
| 73 ui::ScaleFactor scale_factor, | |
| 74 base::StringPiece* value) override { | |
| 75 return false; | |
| 76 } | |
| 77 bool GetLocalizedString(int message_id, base::string16* value) override { | |
| 78 return false; | |
| 79 } | |
| 80 | |
| 81 int last_resource_id() const { return last_resource_id_; } | |
| 82 | |
| 83 private: | |
| 84 gfx::Image image_small_; | |
| 85 gfx::Image image_large_; | |
| 86 int last_resource_id_; | |
| 87 | |
| 88 DISALLOW_COPY_AND_ASSIGN(MockResourceBundleDelegate); | |
| 89 }; | |
| 90 | |
| 91 } // namespace | |
| 92 | |
| 93 class ShadowTest: public aura::test::AuraTestBase { | |
| 94 public: | 14 public: |
| 95 ShadowTest() {} | 15 ShadowTest() {} |
| 96 ~ShadowTest() override {} | 16 ~ShadowTest() override {} |
| 97 | |
| 98 MockResourceBundleDelegate* delegate() { return delegate_.get(); } | |
| 99 | |
| 100 // aura::testAuraBase: | |
| 101 void SetUp() override { | |
| 102 aura::test::AuraTestBase::SetUp(); | |
| 103 delegate_.reset(new MockResourceBundleDelegate()); | |
| 104 if (ResourceBundle::HasSharedInstance()) | |
| 105 ui::ResourceBundle::CleanupSharedInstance(); | |
| 106 ui::ResourceBundle::InitSharedInstanceWithLocale( | |
| 107 "en-US", delegate(), ui::ResourceBundle::LOAD_COMMON_RESOURCES); | |
| 108 } | |
| 109 void TearDown() override { | |
| 110 ui::ResourceBundle::CleanupSharedInstance(); | |
| 111 base::FilePath ui_test_pak_path; | |
| 112 ASSERT_TRUE(PathService::Get(ui::UI_TEST_PAK, &ui_test_pak_path)); | |
| 113 ui::ResourceBundle::InitSharedInstanceWithPakPath(ui_test_pak_path); | |
| 114 aura::test::AuraTestBase::TearDown(); | |
| 115 } | |
| 116 private: | 17 private: |
| 117 std::unique_ptr<MockResourceBundleDelegate> delegate_; | |
| 118 DISALLOW_COPY_AND_ASSIGN(ShadowTest); | 18 DISALLOW_COPY_AND_ASSIGN(ShadowTest); |
| 119 }; | 19 }; |
| 120 | 20 |
| 121 // Test if the proper image is set for the specified style. | |
| 122 TEST_F(ShadowTest, UpdateImagesForStyle) { | |
| 123 Shadow shadow; | |
| 124 | |
| 125 shadow.Init(Shadow::STYLE_SMALL); | |
| 126 EXPECT_EQ(delegate()->last_resource_id(), IDR_WINDOW_BUBBLE_SHADOW_SMALL); | |
| 127 shadow.SetStyle(Shadow::STYLE_ACTIVE); | |
| 128 EXPECT_EQ(delegate()->last_resource_id(), IDR_AURA_SHADOW_ACTIVE); | |
| 129 shadow.SetStyle(Shadow::STYLE_INACTIVE); | |
| 130 EXPECT_EQ(delegate()->last_resource_id(), IDR_AURA_SHADOW_INACTIVE); | |
| 131 } | |
| 132 | |
| 133 // Test if the proper content bounds is calculated based on the current style. | 21 // Test if the proper content bounds is calculated based on the current style. |
| 134 TEST_F(ShadowTest, SetContentBounds) { | 22 TEST_F(ShadowTest, SetContentBounds) { |
| 135 Shadow shadow; | 23 Shadow shadow; |
| 136 | 24 |
| 137 // Verify that layer bounds are inset from content bounds. | 25 // Verify that layer bounds are outset from content bounds. |
| 138 shadow.Init(Shadow::STYLE_ACTIVE); | 26 shadow.Init(Shadow::STYLE_ACTIVE); |
| 139 gfx::Rect content_bounds(100, 100, 300, 300); | 27 gfx::Rect content_bounds(100, 100, 300, 300); |
| 140 shadow.SetContentBounds(content_bounds); | 28 shadow.SetContentBounds(content_bounds); |
| 141 EXPECT_EQ(shadow.content_bounds(), content_bounds); | 29 EXPECT_EQ(content_bounds, shadow.content_bounds()); |
| 142 EXPECT_EQ(shadow.layer()->bounds(), gfx::Rect(36, 36, 428, 428)); | 30 gfx::Rect shadow_bounds(content_bounds); |
| 31 int elevation = 24; | |
| 32 shadow_bounds.Inset(-gfx::Insets(2 * elevation) + | |
|
James Cook
2016/12/05 23:38:35
Can you comment on what this is expecting/testing?
Evan Stade
2016/12/06 00:53:21
beyond the comments that already exist, what detai
James Cook
2016/12/06 18:17:51
Actually, this is fine.
| |
| 33 gfx::Insets(elevation, 0, -elevation, 0)); | |
| 34 | |
| 35 EXPECT_EQ(shadow_bounds, shadow.layer()->bounds()); | |
| 143 | 36 |
| 144 shadow.SetStyle(Shadow::STYLE_SMALL); | 37 shadow.SetStyle(Shadow::STYLE_SMALL); |
| 145 EXPECT_EQ(shadow.content_bounds(), content_bounds); | 38 EXPECT_EQ(content_bounds, shadow.content_bounds()); |
| 146 EXPECT_EQ(shadow.layer()->bounds(), gfx::Rect(96, 96, 308, 308)); | 39 shadow_bounds = content_bounds; |
|
James Cook
2016/12/05 23:38:35
nit: Either use a new variable, or split the test
Evan Stade
2016/12/06 00:53:21
Done.
| |
| 40 elevation = 6; | |
| 41 shadow_bounds.Inset(-gfx::Insets(2 * elevation) + | |
| 42 gfx::Insets(elevation, 0, -elevation, 0)); | |
| 43 EXPECT_EQ(shadow_bounds, shadow.layer()->bounds()); | |
| 147 } | 44 } |
| 45 | |
| 46 } // namespace | |
|
James Cook
2016/12/05 23:38:36
Nice. The linker thanks you.
Evan Stade
2016/12/06 00:53:21
it's very welcome
| |
| 148 } // namespace wm | 47 } // namespace wm |
| OLD | NEW |