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/launcher/launcher_view.h" | 5 #include "ash/launcher/launcher_view.h" |
6 | 6 |
7 #include "ash/ash_switches.h" | |
8 #include "ash/launcher/launcher.h" | 7 #include "ash/launcher/launcher.h" |
| 8 #include "ash/launcher/launcher_button.h" |
| 9 #include "ash/launcher/launcher_model.h" |
9 #include "ash/launcher/launcher_icon_observer.h" | 10 #include "ash/launcher/launcher_icon_observer.h" |
10 #include "ash/shell.h" | 11 #include "ash/shell.h" |
11 #include "ash/shell_window_ids.h" | |
12 #include "ash/test/ash_test_base.h" | 12 #include "ash/test/ash_test_base.h" |
| 13 #include "ash/test/launcher_view_test_api.h" |
13 #include "ash/test/test_launcher_delegate.h" | 14 #include "ash/test/test_launcher_delegate.h" |
14 #include "base/basictypes.h" | 15 #include "base/basictypes.h" |
15 #include "base/command_line.h" | |
16 #include "base/compiler_specific.h" | 16 #include "base/compiler_specific.h" |
| 17 #include "base/memory/scoped_ptr.h" |
17 #include "ui/aura/window.h" | 18 #include "ui/aura/window.h" |
| 19 #include "ui/aura/test/aura_test_base.h" |
| 20 #include "ui/gfx/compositor/layer.h" |
18 #include "ui/views/widget/widget.h" | 21 #include "ui/views/widget/widget.h" |
19 #include "ui/views/widget/widget_delegate.h" | 22 #include "ui/views/widget/widget_delegate.h" |
20 | 23 |
21 namespace ash { | 24 namespace ash { |
| 25 namespace test { |
22 | 26 |
23 namespace { | 27 //////////////////////////////////////////////////////////////////////////////// |
| 28 // LauncherIconObserver tests. |
24 | 29 |
25 class TestLauncherIconObserver : public LauncherIconObserver { | 30 class TestLauncherIconObserver : public LauncherIconObserver { |
26 public: | 31 public: |
27 TestLauncherIconObserver() : count_(0) { | 32 TestLauncherIconObserver() : count_(0) { |
28 Shell::GetInstance()->launcher()->AddIconObserver(this); | 33 Shell::GetInstance()->launcher()->AddIconObserver(this); |
29 } | 34 } |
30 | 35 |
31 virtual ~TestLauncherIconObserver() { | 36 virtual ~TestLauncherIconObserver() { |
32 Shell::GetInstance()->launcher()->RemoveIconObserver(this); | 37 Shell::GetInstance()->launcher()->RemoveIconObserver(this); |
33 } | 38 } |
34 | 39 |
35 // LauncherIconObserver implementation. | 40 // LauncherIconObserver implementation. |
36 void OnLauncherIconPositionsChanged() OVERRIDE { | 41 void OnLauncherIconPositionsChanged() OVERRIDE { |
37 count_++; | 42 ++count_; |
38 } | 43 } |
39 | 44 |
40 int count() const { return count_; } | 45 int count() const { return count_; } |
41 void Reset() { count_ = 0; } | 46 void Reset() { count_ = 0; } |
42 | 47 |
43 private: | 48 private: |
44 int count_; | 49 int count_; |
45 | 50 |
46 DISALLOW_COPY_AND_ASSIGN(TestLauncherIconObserver); | 51 DISALLOW_COPY_AND_ASSIGN(TestLauncherIconObserver); |
47 }; | 52 }; |
48 | 53 |
49 class LauncherViewTest : public ash::test::AshTestBase { | 54 class LauncherViewIconObserverTest : public ash::test::AshTestBase { |
50 public: | 55 public: |
51 LauncherViewTest() {} | 56 LauncherViewIconObserverTest() {} |
52 virtual ~LauncherViewTest() {} | 57 virtual ~LauncherViewIconObserverTest() {} |
53 | 58 |
54 virtual void SetUp() OVERRIDE { | 59 virtual void SetUp() OVERRIDE { |
55 AshTestBase::SetUp(); | 60 AshTestBase::SetUp(); |
56 observer_.reset(new TestLauncherIconObserver); | 61 observer_.reset(new TestLauncherIconObserver); |
57 } | 62 } |
58 | 63 |
59 virtual void TearDown() OVERRIDE { | 64 virtual void TearDown() OVERRIDE { |
60 observer_.reset(); | 65 observer_.reset(); |
61 AshTestBase::TearDown(); | 66 AshTestBase::TearDown(); |
62 } | 67 } |
63 | 68 |
64 TestLauncherIconObserver* observer() { return observer_.get(); } | 69 TestLauncherIconObserver* observer() { return observer_.get(); } |
65 | 70 |
66 private: | 71 private: |
67 scoped_ptr<TestLauncherIconObserver> observer_; | 72 scoped_ptr<TestLauncherIconObserver> observer_; |
68 | 73 |
69 DISALLOW_COPY_AND_ASSIGN(LauncherViewTest); | 74 DISALLOW_COPY_AND_ASSIGN(LauncherViewIconObserverTest); |
70 }; | 75 }; |
71 | 76 |
72 TEST_F(LauncherViewTest, AddRemove) { | 77 TEST_F(LauncherViewIconObserverTest, AddRemove) { |
73 ash::test::TestLauncherDelegate* launcher_delegate = | 78 ash::test::TestLauncherDelegate* launcher_delegate = |
74 ash::test::TestLauncherDelegate::instance(); | 79 ash::test::TestLauncherDelegate::instance(); |
75 ASSERT_TRUE(launcher_delegate); | 80 ASSERT_TRUE(launcher_delegate); |
76 | 81 |
77 views::Widget::InitParams params(views::Widget::InitParams::TYPE_WINDOW); | 82 views::Widget::InitParams params(views::Widget::InitParams::TYPE_WINDOW); |
78 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; | 83 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; |
79 params.bounds = gfx::Rect(0, 0, 200, 200); | 84 params.bounds = gfx::Rect(0, 0, 200, 200); |
80 | 85 |
81 scoped_ptr<views::Widget> widget(new views::Widget()); | 86 scoped_ptr<views::Widget> widget(new views::Widget()); |
82 widget->Init(params); | 87 widget->Init(params); |
83 launcher_delegate->AddLauncherItem(widget->GetNativeWindow()); | 88 launcher_delegate->AddLauncherItem(widget->GetNativeWindow()); |
84 EXPECT_EQ(1, observer()->count()); | 89 EXPECT_EQ(1, observer()->count()); |
85 observer()->Reset(); | 90 observer()->Reset(); |
86 | 91 |
87 widget->Show(); | 92 widget->Show(); |
88 widget->GetNativeWindow()->parent()->RemoveChild(widget->GetNativeWindow()); | 93 widget->GetNativeWindow()->parent()->RemoveChild(widget->GetNativeWindow()); |
89 EXPECT_EQ(1, observer()->count()); | 94 EXPECT_EQ(1, observer()->count()); |
90 observer()->Reset(); | 95 observer()->Reset(); |
91 } | 96 } |
92 | 97 |
93 TEST_F(LauncherViewTest, BoundsChanged) { | 98 TEST_F(LauncherViewIconObserverTest, BoundsChanged) { |
94 Launcher* launcher = Shell::GetInstance()->launcher(); | 99 Launcher* launcher = Shell::GetInstance()->launcher(); |
95 int total_width = launcher->widget()->GetWindowScreenBounds().width(); | 100 int total_width = launcher->widget()->GetWindowScreenBounds().width(); |
96 ASSERT_GT(total_width, 0); | 101 ASSERT_GT(total_width, 0); |
97 launcher->SetStatusWidth(total_width / 2); | 102 launcher->SetStatusWidth(total_width / 2); |
98 EXPECT_EQ(1, observer()->count()); | 103 EXPECT_EQ(1, observer()->count()); |
99 observer()->Reset(); | 104 observer()->Reset(); |
100 } | 105 } |
101 | 106 |
102 } // namespace | 107 //////////////////////////////////////////////////////////////////////////////// |
| 108 // LauncherView tests. |
103 | 109 |
| 110 class LauncherViewTest : public aura::test::AuraTestBase { |
| 111 public: |
| 112 LauncherViewTest() {} |
| 113 virtual ~LauncherViewTest() {} |
| 114 |
| 115 virtual void SetUp() OVERRIDE { |
| 116 aura::test::AuraTestBase::SetUp(); |
| 117 |
| 118 model_.reset(new LauncherModel); |
| 119 |
| 120 launcher_view_.reset(new internal::LauncherView(model_.get(), NULL)); |
| 121 launcher_view_->Init(); |
| 122 // The bounds should be big enough for 4 buttons + overflow chevron. |
| 123 launcher_view_->SetBounds(0, 0, 500, 50); |
| 124 |
| 125 test_api_.reset(new LauncherViewTestAPI(launcher_view_.get())); |
| 126 test_api_->SetAnimationDuration(1); // Speeds up animation for test. |
| 127 } |
| 128 |
| 129 protected: |
| 130 LauncherID AddAppShortcut() { |
| 131 LauncherItem item; |
| 132 item.type = TYPE_APP_SHORTCUT; |
| 133 item.status = STATUS_CLOSED; |
| 134 |
| 135 int id = model_->next_id(); |
| 136 model_->Add(item); |
| 137 test_api_->RunMessageLoopUntilAnimationsDone(); |
| 138 return id; |
| 139 } |
| 140 |
| 141 LauncherID AddTabbedBrowser() { |
| 142 LauncherItem item; |
| 143 item.type = TYPE_TABBED; |
| 144 item.status = STATUS_RUNNING; |
| 145 |
| 146 int id = model_->next_id(); |
| 147 model_->Add(item); |
| 148 test_api_->RunMessageLoopUntilAnimationsDone(); |
| 149 return id; |
| 150 } |
| 151 |
| 152 void RemoveByID(LauncherID id) { |
| 153 model_->RemoveItemAt(model_->ItemIndexByID(id)); |
| 154 test_api_->RunMessageLoopUntilAnimationsDone(); |
| 155 } |
| 156 |
| 157 internal::LauncherButton* GetButtonByID(LauncherID id) { |
| 158 int index = model_->ItemIndexByID(id); |
| 159 return test_api_->GetButton(index); |
| 160 } |
| 161 |
| 162 scoped_ptr<LauncherModel> model_; |
| 163 scoped_ptr<internal::LauncherView> launcher_view_; |
| 164 scoped_ptr<LauncherViewTestAPI> test_api_; |
| 165 |
| 166 private: |
| 167 DISALLOW_COPY_AND_ASSIGN(LauncherViewTest); |
| 168 }; |
| 169 |
| 170 // Adds browser button until overflow and verifies that the last added browser |
| 171 // button is hidden. |
| 172 TEST_F(LauncherViewTest, AddBrowserUntilOverflow) { |
| 173 // All buttons should be visible. |
| 174 ASSERT_EQ(test_api_->GetLastVisibleIndex() + 1, |
| 175 test_api_->GetButtonCount()); |
| 176 |
| 177 // Add tabbed browser until overflow. |
| 178 LauncherID last_added = AddTabbedBrowser(); |
| 179 while (!test_api_->IsOverflowButtonVisible()) { |
| 180 // Added button is visible after animation while in this loop. |
| 181 EXPECT_TRUE(GetButtonByID(last_added)->visible()); |
| 182 |
| 183 last_added = AddTabbedBrowser(); |
| 184 } |
| 185 |
| 186 // The last added button should be invisible. |
| 187 EXPECT_FALSE(GetButtonByID(last_added)->visible()); |
| 188 } |
| 189 |
| 190 // Adds one browser button then adds app shortcut until overflow. Verifies that |
| 191 // the browser button gets hidden on overflow and last added app shortcut is |
| 192 // still visible. |
| 193 TEST_F(LauncherViewTest, AddAppShortcutWithBrowserButtonUntilOverflow) { |
| 194 // All buttons should be visible. |
| 195 ASSERT_EQ(test_api_->GetLastVisibleIndex() + 1, |
| 196 test_api_->GetButtonCount()); |
| 197 |
| 198 LauncherID browser_button_id = AddTabbedBrowser(); |
| 199 |
| 200 // Add app shortcut until overflow. |
| 201 LauncherID last_added = AddAppShortcut(); |
| 202 while (!test_api_->IsOverflowButtonVisible()) { |
| 203 // Added button is visible after animation while in this loop. |
| 204 EXPECT_TRUE(GetButtonByID(last_added)->visible()); |
| 205 |
| 206 last_added = AddAppShortcut(); |
| 207 } |
| 208 |
| 209 // The last added app short button should be visible. |
| 210 EXPECT_TRUE(GetButtonByID(last_added)->visible()); |
| 211 // And the browser button is invisible. |
| 212 EXPECT_FALSE(GetButtonByID(browser_button_id)->visible()); |
| 213 } |
| 214 |
| 215 // Adds button until overflow then removes first added one. Verifies that |
| 216 // the last added one changes from invisible to visible and overflow |
| 217 // chevron is gone. |
| 218 TEST_F(LauncherViewTest, RemoveButtonRevealsOverflowed) { |
| 219 // All buttons should be visible. |
| 220 ASSERT_EQ(test_api_->GetLastVisibleIndex() + 1, |
| 221 test_api_->GetButtonCount()); |
| 222 |
| 223 // Add tabbed browser until overflow. |
| 224 LauncherID first_added= AddTabbedBrowser(); |
| 225 LauncherID last_added = first_added; |
| 226 while (!test_api_->IsOverflowButtonVisible()) |
| 227 last_added = AddTabbedBrowser(); |
| 228 |
| 229 // Expect add more than 1 button. First added is visible and last is not. |
| 230 EXPECT_NE(first_added, last_added); |
| 231 EXPECT_TRUE(GetButtonByID(first_added)->visible()); |
| 232 EXPECT_FALSE(GetButtonByID(last_added)->visible()); |
| 233 |
| 234 // Remove first added. |
| 235 RemoveByID(first_added); |
| 236 |
| 237 // Last added button becomes visible and overflow chevron is gone. |
| 238 EXPECT_TRUE(GetButtonByID(last_added)->visible()); |
| 239 EXPECT_EQ(1.0f, GetButtonByID(last_added)->layer()->opacity()); |
| 240 EXPECT_FALSE(test_api_->IsOverflowButtonVisible()); |
| 241 } |
| 242 |
| 243 // Verifies that remove last overflowed button should hide overflow chevron. |
| 244 TEST_F(LauncherViewTest, RemoveLastOverflowed) { |
| 245 // All buttons should be visible. |
| 246 ASSERT_EQ(test_api_->GetLastVisibleIndex() + 1, |
| 247 test_api_->GetButtonCount()); |
| 248 |
| 249 // Add tabbed browser until overflow. |
| 250 LauncherID last_added= AddTabbedBrowser(); |
| 251 while (!test_api_->IsOverflowButtonVisible()) |
| 252 last_added = AddTabbedBrowser(); |
| 253 |
| 254 RemoveByID(last_added); |
| 255 EXPECT_FALSE(test_api_->IsOverflowButtonVisible()); |
| 256 } |
| 257 |
| 258 } // namespace test |
104 } // namespace ash | 259 } // namespace ash |
OLD | NEW |