Reland Double-Click on a Tab Close causes Maximize
[chromium-blink-merge.git] / ash / first_run / first_run_helper_unittest.cc
blob6952dd6152ce522d7bf480e9ab54c341e6c2d219
1 // Copyright 2013 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 "ash/first_run/first_run_helper.h"
7 #include "ash/first_run/desktop_cleaner.h"
8 #include "ash/shell.h"
9 #include "ash/shell_window_ids.h"
10 #include "ash/test/ash_test_base.h"
11 #include "ui/aura/test/event_generator.h"
12 #include "ui/events/event_handler.h"
13 #include "ui/views/window/dialog_delegate.h"
15 namespace ash {
16 namespace test {
18 namespace {
20 class TestModalDialogDelegate : public views::DialogDelegateView {
21 public:
22 TestModalDialogDelegate() {}
23 virtual ~TestModalDialogDelegate() {}
25 // Overridden from views::WidgetDelegate:
26 virtual ui::ModalType GetModalType() const OVERRIDE {
27 return ui::MODAL_TYPE_SYSTEM;
30 private:
31 DISALLOW_COPY_AND_ASSIGN(TestModalDialogDelegate);
34 class CountingEventHandler : public ui::EventHandler {
35 public:
36 // Handler resets |*mouse_events_registered_| during construction and updates
37 // it after each registered event.
38 explicit CountingEventHandler(int* mouse_events_registered)
39 : mouse_events_registered_(mouse_events_registered) {
40 *mouse_events_registered = 0;
43 virtual ~CountingEventHandler() {}
45 private:
46 // ui::EventHandler overrides.
47 virtual void OnMouseEvent(ui::MouseEvent* event) OVERRIDE {
48 ++*mouse_events_registered_;
51 int* mouse_events_registered_;
53 DISALLOW_COPY_AND_ASSIGN(CountingEventHandler);
56 } // namespace
58 class FirstRunHelperTest : public AshTestBase,
59 public FirstRunHelper::Observer {
60 public:
61 FirstRunHelperTest() : cancelled_times_(0) {}
63 virtual ~FirstRunHelperTest() {}
65 virtual void SetUp() OVERRIDE {
66 AshTestBase::SetUp();
67 CheckContainersAreVisible();
68 helper_.reset(ash::Shell::GetInstance()->CreateFirstRunHelper());
69 helper_->AddObserver(this);
70 helper_->GetOverlayWidget()->Show();
73 virtual void TearDown() OVERRIDE {
74 EXPECT_TRUE(helper_.get());
75 helper_.reset();
76 CheckContainersAreVisible();
77 AshTestBase::TearDown();
80 void CheckContainersAreVisible() const {
81 aura::Window* root_window = Shell::GetInstance()->GetPrimaryRootWindow();
82 std::vector<int> containers_to_check =
83 DesktopCleaner::GetContainersToHideForTest();
84 for (size_t i = 0; i < containers_to_check.size(); ++i) {
85 aura::Window* container =
86 Shell::GetContainer(root_window, containers_to_check[i]);
87 EXPECT_TRUE(container->IsVisible());
91 void CheckContainersAreHidden() const {
92 aura::Window* root_window = Shell::GetInstance()->GetPrimaryRootWindow();
93 std::vector<int> containers_to_check =
94 DesktopCleaner::GetContainersToHideForTest();
95 for (size_t i = 0; i < containers_to_check.size(); ++i) {
96 aura::Window* container =
97 Shell::GetContainer(root_window, containers_to_check[i]);
98 EXPECT_TRUE(!container->IsVisible());
102 FirstRunHelper* helper() { return helper_.get(); }
104 int cancelled_times() const { return cancelled_times_; }
106 private:
107 // FirstRunHelper::Observer overrides.
108 virtual void OnCancelled() OVERRIDE {
109 ++cancelled_times_;
112 scoped_ptr<FirstRunHelper> helper_;
113 int cancelled_times_;
115 DISALLOW_COPY_AND_ASSIGN(FirstRunHelperTest);
118 // This test creates helper, checks that containers are hidden and then
119 // destructs helper.
120 TEST_F(FirstRunHelperTest, ContainersAreHidden) {
121 CheckContainersAreHidden();
124 // Tests that helper correctly handles Escape key press.
125 TEST_F(FirstRunHelperTest, Cancel) {
126 GetEventGenerator().PressKey(ui::VKEY_ESCAPE, 0);
127 EXPECT_EQ(cancelled_times(), 1);
130 // Tests that modal window doesn't block events for overlay window.
131 TEST_F(FirstRunHelperTest, ModalWindowDoesNotBlock) {
132 views::Widget* modal_dialog = views::DialogDelegate::CreateDialogWidget(
133 new TestModalDialogDelegate(), CurrentContext(), NULL);
134 modal_dialog->Show();
135 // TODO(dzhioev): modal window should not steal focus from overlay window.
136 aura::Window* overlay_window = helper()->GetOverlayWidget()->GetNativeView();
137 overlay_window->Focus();
138 EXPECT_TRUE(overlay_window->HasFocus());
139 int mouse_events;
140 overlay_window->SetEventFilter(new CountingEventHandler(&mouse_events));
141 GetEventGenerator().PressLeftButton();
142 GetEventGenerator().ReleaseLeftButton();
143 EXPECT_EQ(mouse_events, 2);
146 } // namespace test
147 } // namespace ash