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
3 // found in the LICENSE file.
5 #include "ui/wm/core/shadow.h"
7 #include "base/memory/scoped_ptr.h"
8 #include "base/path_service.h"
9 #include "third_party/skia/include/core/SkBitmap.h"
10 #include "ui/aura/test/aura_test_base.h"
11 #include "ui/aura/test/test_windows.h"
12 #include "ui/aura/window.h"
13 #include "ui/base/resource/resource_bundle.h"
14 #include "ui/base/ui_base_paths.h"
15 #include "ui/compositor/layer.h"
16 #include "ui/compositor/layer_tree_owner.h"
17 #include "ui/resources/grit/ui_resources.h"
23 const int kSmallBitmapSize
= 129;
24 const int kLargeBitmapSize
= 269;
26 // Mock for the ResourceBundle::Delegate class.
27 class MockResourceBundleDelegate
: public ui::ResourceBundle::Delegate
{
29 MockResourceBundleDelegate() : last_resource_id_(0) {
30 SkBitmap bitmap_small
, bitmap_large
;
31 bitmap_small
.allocPixels(
32 SkImageInfo::MakeN32Premul(kSmallBitmapSize
, kSmallBitmapSize
));
33 bitmap_large
.allocPixels(
34 SkImageInfo::MakeN32Premul(kLargeBitmapSize
, kLargeBitmapSize
));
35 image_small_
= gfx::Image::CreateFrom1xBitmap(bitmap_small
);
36 image_large_
= gfx::Image::CreateFrom1xBitmap(bitmap_large
);
38 ~MockResourceBundleDelegate() override
{}
40 // ResourceBundle::Delegate:
41 base::FilePath
GetPathForResourcePack(const base::FilePath
& pack_path
,
42 ui::ScaleFactor scale_factor
) override
{
43 return base::FilePath();
45 base::FilePath
GetPathForLocalePack(const base::FilePath
& pack_path
,
46 const std::string
& locale
) override
{
47 return base::FilePath();
49 gfx::Image
GetImageNamed(int resource_id
) override
{
50 last_resource_id_
= resource_id
;
51 switch (resource_id
) {
52 case IDR_WINDOW_BUBBLE_SHADOW_SMALL
:
54 case IDR_AURA_SHADOW_ACTIVE
:
55 case IDR_AURA_SHADOW_INACTIVE
:
62 gfx::Image
GetNativeImageNamed(int resource_id
,
63 ui::ResourceBundle::ImageRTL rtl
) override
{
66 base::RefCountedStaticMemory
* LoadDataResourceBytes(
68 ui::ScaleFactor scale_factor
) override
{
71 bool GetRawDataResource(int resource_id
,
72 ui::ScaleFactor scale_factor
,
73 base::StringPiece
* value
) override
{
76 bool GetLocalizedString(int message_id
, base::string16
* value
) override
{
79 scoped_ptr
<gfx::Font
> GetFont(ui::ResourceBundle::FontStyle style
) override
{
80 return scoped_ptr
<gfx::Font
>();
83 int last_resource_id() const { return last_resource_id_
; }
86 gfx::Image image_small_
;
87 gfx::Image image_large_
;
88 int last_resource_id_
;
90 DISALLOW_COPY_AND_ASSIGN(MockResourceBundleDelegate
);
95 class ShadowTest
: public aura::test::AuraTestBase
{
98 ~ShadowTest() override
{}
100 MockResourceBundleDelegate
* delegate() { return delegate_
.get(); }
102 // aura::testAuraBase:
103 void SetUp() override
{
104 aura::test::AuraTestBase::SetUp();
105 delegate_
.reset(new MockResourceBundleDelegate());
106 if (ResourceBundle::HasSharedInstance())
107 ui::ResourceBundle::CleanupSharedInstance();
108 ui::ResourceBundle::InitSharedInstanceWithLocale(
109 "en-US", delegate(), ui::ResourceBundle::LOAD_COMMON_RESOURCES
);
111 void TearDown() override
{
112 ui::ResourceBundle::CleanupSharedInstance();
113 base::FilePath ui_test_pak_path
;
114 ASSERT_TRUE(PathService::Get(ui::UI_TEST_PAK
, &ui_test_pak_path
));
115 ui::ResourceBundle::InitSharedInstanceWithPakPath(ui_test_pak_path
);
116 aura::test::AuraTestBase::TearDown();
119 scoped_ptr
<MockResourceBundleDelegate
> delegate_
;
120 DISALLOW_COPY_AND_ASSIGN(ShadowTest
);
123 // Test if the proper image is set for the specified style.
124 TEST_F(ShadowTest
, UpdateImagesForStyle
) {
127 shadow
.Init(Shadow::STYLE_SMALL
);
128 EXPECT_EQ(delegate()->last_resource_id(), IDR_WINDOW_BUBBLE_SHADOW_SMALL
);
129 shadow
.SetStyle(Shadow::STYLE_ACTIVE
);
130 EXPECT_EQ(delegate()->last_resource_id(), IDR_AURA_SHADOW_ACTIVE
);
131 shadow
.SetStyle(Shadow::STYLE_INACTIVE
);
132 EXPECT_EQ(delegate()->last_resource_id(), IDR_AURA_SHADOW_INACTIVE
);
135 // Test if the proper content bounds is calculated based on the current style.
136 TEST_F(ShadowTest
, SetContentBounds
) {
139 // Verify that layer bounds are inset from content bounds.
140 shadow
.Init(Shadow::STYLE_ACTIVE
);
141 gfx::Rect
content_bounds(100, 100, 300, 300);
142 shadow
.SetContentBounds(content_bounds
);
143 EXPECT_EQ(shadow
.content_bounds(), content_bounds
);
144 EXPECT_EQ(shadow
.layer()->bounds(), gfx::Rect(36, 36, 428, 428));
146 shadow
.SetStyle(Shadow::STYLE_SMALL
);
147 EXPECT_EQ(shadow
.content_bounds(), content_bounds
);
148 EXPECT_EQ(shadow
.layer()->bounds(), gfx::Rect(96, 96, 308, 308));