| 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 "ash/wm/shadow_controller.h" | 5 #include "ui/views/corewm/shadow_controller.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "ash/shell.h" | |
| 11 #include "ash/test/ash_test_base.h" | |
| 12 #include "ash/wm/shadow.h" | |
| 13 #include "ash/wm/shadow_types.h" | |
| 14 #include "ash/wm/window_properties.h" | |
| 15 #include "ash/wm/window_util.h" | |
| 16 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
| 17 #include "ui/aura/client/activation_client.h" | 11 #include "ui/aura/client/activation_client.h" |
| 18 #include "ui/aura/root_window.h" | 12 #include "ui/aura/root_window.h" |
| 13 #include "ui/aura/test/aura_test_base.h" |
| 19 #include "ui/aura/window.h" | 14 #include "ui/aura/window.h" |
| 20 #include "ui/compositor/layer.h" | 15 #include "ui/compositor/layer.h" |
| 16 #include "ui/views/corewm/shadow.h" |
| 17 #include "ui/views/corewm/shadow_types.h" |
| 21 | 18 |
| 22 namespace ash { | 19 namespace views { |
| 23 namespace internal { | 20 namespace corewm { |
| 24 | 21 |
| 25 typedef ash::test::AshTestBase ShadowControllerTest; | 22 class ShadowControllerTest : public aura::test::AuraTestBase { |
| 23 public: |
| 24 ShadowControllerTest() {} |
| 25 virtual ~ShadowControllerTest() {} |
| 26 |
| 27 virtual void SetUp() OVERRIDE { |
| 28 AuraTestBase::SetUp(); |
| 29 shadow_controller_.reset(new ShadowController(root_window())); |
| 30 } |
| 31 virtual void TearDown() OVERRIDE { |
| 32 shadow_controller_.reset(); |
| 33 AuraTestBase::TearDown(); |
| 34 } |
| 35 |
| 36 protected: |
| 37 ShadowController* shadow_controller() { return shadow_controller_.get(); } |
| 38 |
| 39 void ActivateWindow(aura::Window* window) { |
| 40 DCHECK(window); |
| 41 DCHECK(window->GetRootWindow()); |
| 42 aura::client::GetActivationClient(window->GetRootWindow())->ActivateWindow( |
| 43 window); |
| 44 } |
| 45 |
| 46 private: |
| 47 scoped_ptr<ShadowController> shadow_controller_; |
| 48 |
| 49 DISALLOW_COPY_AND_ASSIGN(ShadowControllerTest); |
| 50 }; |
| 26 | 51 |
| 27 // Tests that various methods in Window update the Shadow object as expected. | 52 // Tests that various methods in Window update the Shadow object as expected. |
| 28 TEST_F(ShadowControllerTest, Shadow) { | 53 TEST_F(ShadowControllerTest, Shadow) { |
| 29 scoped_ptr<aura::Window> window(new aura::Window(NULL)); | 54 scoped_ptr<aura::Window> window(new aura::Window(NULL)); |
| 30 window->SetType(aura::client::WINDOW_TYPE_NORMAL); | 55 window->SetType(aura::client::WINDOW_TYPE_NORMAL); |
| 31 window->Init(ui::LAYER_TEXTURED); | 56 window->Init(ui::LAYER_TEXTURED); |
| 32 window->SetParent(NULL); | 57 window->SetParent(NULL); |
| 33 | 58 |
| 34 // We should create the shadow before the window is visible (the shadow's | 59 // We should create the shadow before the window is visible (the shadow's |
| 35 // layer won't get drawn yet since it's a child of the window's layer). | 60 // layer won't get drawn yet since it's a child of the window's layer). |
| 36 internal::ShadowController::TestApi api( | 61 ShadowController::TestApi api(shadow_controller()); |
| 37 ash::Shell::GetInstance()->shadow_controller()); | 62 const Shadow* shadow = api.GetShadowForWindow(window.get()); |
| 38 const internal::Shadow* shadow = api.GetShadowForWindow(window.get()); | |
| 39 ASSERT_TRUE(shadow != NULL); | 63 ASSERT_TRUE(shadow != NULL); |
| 40 EXPECT_TRUE(shadow->layer()->visible()); | 64 EXPECT_TRUE(shadow->layer()->visible()); |
| 41 | 65 |
| 42 // The shadow should remain visible after window visibility changes. | 66 // The shadow should remain visible after window visibility changes. |
| 43 window->Show(); | 67 window->Show(); |
| 44 EXPECT_TRUE(shadow->layer()->visible()); | 68 EXPECT_TRUE(shadow->layer()->visible()); |
| 45 window->Hide(); | 69 window->Hide(); |
| 46 EXPECT_TRUE(shadow->layer()->visible()); | 70 EXPECT_TRUE(shadow->layer()->visible()); |
| 47 | 71 |
| 48 // If the shadow is disabled, it should be hidden. | 72 // If the shadow is disabled, it should be hidden. |
| 49 internal::SetShadowType(window.get(), internal::SHADOW_TYPE_NONE); | 73 SetShadowType(window.get(), SHADOW_TYPE_NONE); |
| 50 window->Show(); | 74 window->Show(); |
| 51 EXPECT_FALSE(shadow->layer()->visible()); | 75 EXPECT_FALSE(shadow->layer()->visible()); |
| 52 internal::SetShadowType(window.get(), internal::SHADOW_TYPE_RECTANGULAR); | 76 SetShadowType(window.get(), SHADOW_TYPE_RECTANGULAR); |
| 53 EXPECT_TRUE(shadow->layer()->visible()); | 77 EXPECT_TRUE(shadow->layer()->visible()); |
| 54 | 78 |
| 55 // The shadow's layer should be a child of the window's layer. | 79 // The shadow's layer should be a child of the window's layer. |
| 56 EXPECT_EQ(window->layer(), shadow->layer()->parent()); | 80 EXPECT_EQ(window->layer(), shadow->layer()->parent()); |
| 57 | 81 |
| 58 window->parent()->RemoveChild(window.get()); | 82 window->parent()->RemoveChild(window.get()); |
| 59 aura::Window* window_ptr = window.get(); | 83 aura::Window* window_ptr = window.get(); |
| 60 window.reset(); | 84 window.reset(); |
| 61 EXPECT_TRUE(api.GetShadowForWindow(window_ptr) == NULL); | 85 EXPECT_TRUE(api.GetShadowForWindow(window_ptr) == NULL); |
| 62 } | 86 } |
| 63 | 87 |
| 64 // Tests that the window's shadow's bounds are updated correctly. | 88 // Tests that the window's shadow's bounds are updated correctly. |
| 65 TEST_F(ShadowControllerTest, ShadowBounds) { | 89 TEST_F(ShadowControllerTest, ShadowBounds) { |
| 66 scoped_ptr<aura::Window> window(new aura::Window(NULL)); | 90 scoped_ptr<aura::Window> window(new aura::Window(NULL)); |
| 67 window->SetType(aura::client::WINDOW_TYPE_NORMAL); | 91 window->SetType(aura::client::WINDOW_TYPE_NORMAL); |
| 68 window->Init(ui::LAYER_TEXTURED); | 92 window->Init(ui::LAYER_TEXTURED); |
| 69 window->SetParent(NULL); | 93 window->SetParent(NULL); |
| 70 window->Show(); | 94 window->Show(); |
| 71 | 95 |
| 72 const gfx::Rect kOldBounds(20, 30, 400, 300); | 96 const gfx::Rect kOldBounds(20, 30, 400, 300); |
| 73 window->SetBounds(kOldBounds); | 97 window->SetBounds(kOldBounds); |
| 74 | 98 |
| 75 // When the shadow is first created, it should use the window's size (but | 99 // When the shadow is first created, it should use the window's size (but |
| 76 // remain at the origin, since it's a child of the window's layer). | 100 // remain at the origin, since it's a child of the window's layer). |
| 77 internal::SetShadowType(window.get(), internal::SHADOW_TYPE_RECTANGULAR); | 101 SetShadowType(window.get(), SHADOW_TYPE_RECTANGULAR); |
| 78 internal::ShadowController::TestApi api( | 102 ShadowController::TestApi api(shadow_controller()); |
| 79 ash::Shell::GetInstance()->shadow_controller()); | 103 const Shadow* shadow = api.GetShadowForWindow(window.get()); |
| 80 const internal::Shadow* shadow = api.GetShadowForWindow(window.get()); | |
| 81 ASSERT_TRUE(shadow != NULL); | 104 ASSERT_TRUE(shadow != NULL); |
| 82 EXPECT_EQ(gfx::Rect(kOldBounds.size()).ToString(), | 105 EXPECT_EQ(gfx::Rect(kOldBounds.size()).ToString(), |
| 83 shadow->content_bounds().ToString()); | 106 shadow->content_bounds().ToString()); |
| 84 | 107 |
| 85 // When we change the window's bounds, the shadow's should be updated too. | 108 // When we change the window's bounds, the shadow's should be updated too. |
| 86 gfx::Rect kNewBounds(50, 60, 500, 400); | 109 gfx::Rect kNewBounds(50, 60, 500, 400); |
| 87 window->SetBounds(kNewBounds); | 110 window->SetBounds(kNewBounds); |
| 88 EXPECT_EQ(gfx::Rect(kNewBounds.size()).ToString(), | 111 EXPECT_EQ(gfx::Rect(kNewBounds.size()).ToString(), |
| 89 shadow->content_bounds().ToString()); | 112 shadow->content_bounds().ToString()); |
| 90 } | 113 } |
| 91 | 114 |
| 92 // Tests that activating a window changes the shadow style. | 115 // Tests that activating a window changes the shadow style. |
| 93 TEST_F(ShadowControllerTest, ShadowStyle) { | 116 TEST_F(ShadowControllerTest, ShadowStyle) { |
| 94 ShadowController::TestApi api( | 117 ShadowController::TestApi api(shadow_controller()); |
| 95 ash::Shell::GetInstance()->shadow_controller()); | |
| 96 | 118 |
| 97 scoped_ptr<aura::Window> window1(new aura::Window(NULL)); | 119 scoped_ptr<aura::Window> window1(new aura::Window(NULL)); |
| 98 window1->SetType(aura::client::WINDOW_TYPE_NORMAL); | 120 window1->SetType(aura::client::WINDOW_TYPE_NORMAL); |
| 99 window1->Init(ui::LAYER_TEXTURED); | 121 window1->Init(ui::LAYER_TEXTURED); |
| 100 window1->SetParent(NULL); | 122 window1->SetParent(NULL); |
| 101 window1->SetBounds(gfx::Rect(10, 20, 300, 400)); | 123 window1->SetBounds(gfx::Rect(10, 20, 300, 400)); |
| 102 window1->Show(); | 124 window1->Show(); |
| 103 wm::ActivateWindow(window1.get()); | 125 ActivateWindow(window1.get()); |
| 104 | 126 |
| 105 // window1 is active, so style should have active appearance. | 127 // window1 is active, so style should have active appearance. |
| 106 Shadow* shadow1 = api.GetShadowForWindow(window1.get()); | 128 Shadow* shadow1 = api.GetShadowForWindow(window1.get()); |
| 107 ASSERT_TRUE(shadow1 != NULL); | 129 ASSERT_TRUE(shadow1 != NULL); |
| 108 EXPECT_EQ(Shadow::STYLE_ACTIVE, shadow1->style()); | 130 EXPECT_EQ(Shadow::STYLE_ACTIVE, shadow1->style()); |
| 109 | 131 |
| 110 // Create another window and activate it. | 132 // Create another window and activate it. |
| 111 scoped_ptr<aura::Window> window2(new aura::Window(NULL)); | 133 scoped_ptr<aura::Window> window2(new aura::Window(NULL)); |
| 112 window2->SetType(aura::client::WINDOW_TYPE_NORMAL); | 134 window2->SetType(aura::client::WINDOW_TYPE_NORMAL); |
| 113 window2->Init(ui::LAYER_TEXTURED); | 135 window2->Init(ui::LAYER_TEXTURED); |
| 114 window2->SetParent(NULL); | 136 window2->SetParent(NULL); |
| 115 window2->SetBounds(gfx::Rect(11, 21, 301, 401)); | 137 window2->SetBounds(gfx::Rect(11, 21, 301, 401)); |
| 116 window2->Show(); | 138 window2->Show(); |
| 117 wm::ActivateWindow(window2.get()); | 139 ActivateWindow(window2.get()); |
| 118 | 140 |
| 119 // window1 is now inactive, so shadow should go inactive. | 141 // window1 is now inactive, so shadow should go inactive. |
| 120 Shadow* shadow2 = api.GetShadowForWindow(window2.get()); | 142 Shadow* shadow2 = api.GetShadowForWindow(window2.get()); |
| 121 ASSERT_TRUE(shadow2 != NULL); | 143 ASSERT_TRUE(shadow2 != NULL); |
| 122 EXPECT_EQ(Shadow::STYLE_INACTIVE, shadow1->style()); | 144 EXPECT_EQ(Shadow::STYLE_INACTIVE, shadow1->style()); |
| 123 EXPECT_EQ(Shadow::STYLE_ACTIVE, shadow2->style()); | 145 EXPECT_EQ(Shadow::STYLE_ACTIVE, shadow2->style()); |
| 124 } | 146 } |
| 125 | 147 |
| 126 // Tests that we use smaller shadows for tooltips and menus. | 148 // Tests that we use smaller shadows for tooltips and menus. |
| 127 TEST_F(ShadowControllerTest, SmallShadowsForTooltipsAndMenus) { | 149 TEST_F(ShadowControllerTest, SmallShadowsForTooltipsAndMenus) { |
| 128 ShadowController::TestApi api( | 150 ShadowController::TestApi api(shadow_controller()); |
| 129 ash::Shell::GetInstance()->shadow_controller()); | |
| 130 | 151 |
| 131 scoped_ptr<aura::Window> tooltip_window(new aura::Window(NULL)); | 152 scoped_ptr<aura::Window> tooltip_window(new aura::Window(NULL)); |
| 132 tooltip_window->SetType(aura::client::WINDOW_TYPE_TOOLTIP); | 153 tooltip_window->SetType(aura::client::WINDOW_TYPE_TOOLTIP); |
| 133 tooltip_window->Init(ui::LAYER_TEXTURED); | 154 tooltip_window->Init(ui::LAYER_TEXTURED); |
| 134 tooltip_window->SetParent(NULL); | 155 tooltip_window->SetParent(NULL); |
| 135 tooltip_window->SetBounds(gfx::Rect(10, 20, 300, 400)); | 156 tooltip_window->SetBounds(gfx::Rect(10, 20, 300, 400)); |
| 136 tooltip_window->Show(); | 157 tooltip_window->Show(); |
| 137 | 158 |
| 138 Shadow* tooltip_shadow = api.GetShadowForWindow(tooltip_window.get()); | 159 Shadow* tooltip_shadow = api.GetShadowForWindow(tooltip_window.get()); |
| 139 ASSERT_TRUE(tooltip_shadow != NULL); | 160 ASSERT_TRUE(tooltip_shadow != NULL); |
| 140 EXPECT_EQ(Shadow::STYLE_SMALL, tooltip_shadow->style()); | 161 EXPECT_EQ(Shadow::STYLE_SMALL, tooltip_shadow->style()); |
| 141 | 162 |
| 142 scoped_ptr<aura::Window> menu_window(new aura::Window(NULL)); | 163 scoped_ptr<aura::Window> menu_window(new aura::Window(NULL)); |
| 143 menu_window->SetType(aura::client::WINDOW_TYPE_MENU); | 164 menu_window->SetType(aura::client::WINDOW_TYPE_MENU); |
| 144 menu_window->Init(ui::LAYER_TEXTURED); | 165 menu_window->Init(ui::LAYER_TEXTURED); |
| 145 menu_window->SetParent(NULL); | 166 menu_window->SetParent(NULL); |
| 146 menu_window->SetBounds(gfx::Rect(10, 20, 300, 400)); | 167 menu_window->SetBounds(gfx::Rect(10, 20, 300, 400)); |
| 147 menu_window->Show(); | 168 menu_window->Show(); |
| 148 | 169 |
| 149 Shadow* menu_shadow = api.GetShadowForWindow(tooltip_window.get()); | 170 Shadow* menu_shadow = api.GetShadowForWindow(tooltip_window.get()); |
| 150 ASSERT_TRUE(menu_shadow != NULL); | 171 ASSERT_TRUE(menu_shadow != NULL); |
| 151 EXPECT_EQ(Shadow::STYLE_SMALL, menu_shadow->style()); | 172 EXPECT_EQ(Shadow::STYLE_SMALL, menu_shadow->style()); |
| 152 } | 173 } |
| 153 | 174 |
| 154 // http://crbug.com/120210 - transient parents of certain types of transients | 175 // http://crbug.com/120210 - transient parents of certain types of transients |
| 155 // should not lose their shadow when they lose activation to the transient. | 176 // should not lose their shadow when they lose activation to the transient. |
| 156 TEST_F(ShadowControllerTest, TransientParentKeepsActiveShadow) { | 177 TEST_F(ShadowControllerTest, TransientParentKeepsActiveShadow) { |
| 157 ShadowController::TestApi api( | 178 ShadowController::TestApi api(shadow_controller()); |
| 158 ash::Shell::GetInstance()->shadow_controller()); | |
| 159 | 179 |
| 160 scoped_ptr<aura::Window> window1(new aura::Window(NULL)); | 180 scoped_ptr<aura::Window> window1(new aura::Window(NULL)); |
| 161 window1->SetType(aura::client::WINDOW_TYPE_NORMAL); | 181 window1->SetType(aura::client::WINDOW_TYPE_NORMAL); |
| 162 window1->Init(ui::LAYER_TEXTURED); | 182 window1->Init(ui::LAYER_TEXTURED); |
| 163 window1->SetParent(NULL); | 183 window1->SetParent(NULL); |
| 164 window1->SetBounds(gfx::Rect(10, 20, 300, 400)); | 184 window1->SetBounds(gfx::Rect(10, 20, 300, 400)); |
| 165 window1->Show(); | 185 window1->Show(); |
| 166 wm::ActivateWindow(window1.get()); | 186 ActivateWindow(window1.get()); |
| 167 | 187 |
| 168 // window1 is active, so style should have active appearance. | 188 // window1 is active, so style should have active appearance. |
| 169 Shadow* shadow1 = api.GetShadowForWindow(window1.get()); | 189 Shadow* shadow1 = api.GetShadowForWindow(window1.get()); |
| 170 ASSERT_TRUE(shadow1 != NULL); | 190 ASSERT_TRUE(shadow1 != NULL); |
| 171 EXPECT_EQ(Shadow::STYLE_ACTIVE, shadow1->style()); | 191 EXPECT_EQ(Shadow::STYLE_ACTIVE, shadow1->style()); |
| 172 | 192 |
| 173 // Create a window that is transient to window1, and that has the 'hide on | 193 // Create a window that is transient to window1, and that has the 'hide on |
| 174 // deactivate' property set. Upon activation, window1 should still have an | 194 // deactivate' property set. Upon activation, window1 should still have an |
| 175 // active shadow. | 195 // active shadow. |
| 176 scoped_ptr<aura::Window> window2(new aura::Window(NULL)); | 196 scoped_ptr<aura::Window> window2(new aura::Window(NULL)); |
| 177 window2->SetType(aura::client::WINDOW_TYPE_NORMAL); | 197 window2->SetType(aura::client::WINDOW_TYPE_NORMAL); |
| 178 window2->Init(ui::LAYER_TEXTURED); | 198 window2->Init(ui::LAYER_TEXTURED); |
| 179 window2->SetParent(NULL); | 199 window2->SetParent(NULL); |
| 180 window2->SetBounds(gfx::Rect(11, 21, 301, 401)); | 200 window2->SetBounds(gfx::Rect(11, 21, 301, 401)); |
| 181 window1->AddTransientChild(window2.get()); | 201 window1->AddTransientChild(window2.get()); |
| 182 aura::client::SetHideOnDeactivate(window2.get(), true); | 202 aura::client::SetHideOnDeactivate(window2.get(), true); |
| 183 window2->Show(); | 203 window2->Show(); |
| 184 wm::ActivateWindow(window2.get()); | 204 ActivateWindow(window2.get()); |
| 185 | 205 |
| 186 // window1 is now inactive, but its shadow should still appear active. | 206 // window1 is now inactive, but its shadow should still appear active. |
| 187 EXPECT_EQ(Shadow::STYLE_ACTIVE, shadow1->style()); | 207 EXPECT_EQ(Shadow::STYLE_ACTIVE, shadow1->style()); |
| 188 } | 208 } |
| 189 | 209 |
| 190 } // namespace internal | 210 } // namespace corewm |
| 191 } // namespace ash | 211 } // namespace views |
| OLD | NEW |