summaryrefslogtreecommitdiffstats
path: root/chromium/ash/first_run/first_run_helper_unittest.cc
blob: 4bac72371db0bf99458292a1f74ab2846fdae963 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ash/first_run/first_run_helper.h"

#include "ash/first_run/desktop_cleaner.h"
#include "ash/shell.h"
#include "ash/shell_window_ids.h"
#include "ash/test/ash_test_base.h"
#include "ui/aura/test/event_generator.h"
#include "ui/events/event_handler.h"
#include "ui/views/window/dialog_delegate.h"

namespace ash {
namespace test {

namespace {

class TestModalDialogDelegate : public views::DialogDelegateView {
 public:
  TestModalDialogDelegate() {}
  virtual ~TestModalDialogDelegate() {}

  // Overridden from views::WidgetDelegate:
  virtual ui::ModalType GetModalType() const OVERRIDE {
    return ui::MODAL_TYPE_SYSTEM;
  }

 private:
  DISALLOW_COPY_AND_ASSIGN(TestModalDialogDelegate);
};

class CountingEventHandler : public ui::EventHandler {
 public:
  // Handler resets |*mouse_events_registered_| during construction and updates
  // it after each registered event.
  explicit CountingEventHandler(int* mouse_events_registered)
      : mouse_events_registered_(mouse_events_registered) {
    *mouse_events_registered = 0;
  }

  virtual ~CountingEventHandler() {}

 private:
  // ui::EventHandler overrides.
  virtual void OnMouseEvent(ui::MouseEvent* event) OVERRIDE {
    ++*mouse_events_registered_;
  }

  int* mouse_events_registered_;

  DISALLOW_COPY_AND_ASSIGN(CountingEventHandler);
};

}  // namespace

class FirstRunHelperTest : public AshTestBase,
                           public FirstRunHelper::Observer {
 public:
  FirstRunHelperTest() : cancelled_times_(0) {}

  virtual ~FirstRunHelperTest() {}

  virtual void SetUp() OVERRIDE {
    AshTestBase::SetUp();
    CheckContainersAreVisible();
    helper_.reset(ash::Shell::GetInstance()->CreateFirstRunHelper());
    helper_->AddObserver(this);
    helper_->GetOverlayWidget()->Show();
  }

  virtual void TearDown() OVERRIDE {
    EXPECT_TRUE(helper_.get());
    helper_.reset();
    CheckContainersAreVisible();
    AshTestBase::TearDown();
  }

  void CheckContainersAreVisible() const {
    aura::Window* root_window = Shell::GetInstance()->GetPrimaryRootWindow();
    std::vector<int> containers_to_check =
        internal::DesktopCleaner::GetContainersToHideForTest();
    for (size_t i = 0; i < containers_to_check.size(); ++i) {
      aura::Window* container =
          Shell::GetContainer(root_window, containers_to_check[i]);
      EXPECT_TRUE(container->IsVisible());
    }
  }

  void CheckContainersAreHidden() const {
    aura::Window* root_window = Shell::GetInstance()->GetPrimaryRootWindow();
    std::vector<int> containers_to_check =
        internal::DesktopCleaner::GetContainersToHideForTest();
    for (size_t i = 0; i < containers_to_check.size(); ++i) {
      aura::Window* container =
          Shell::GetContainer(root_window, containers_to_check[i]);
      EXPECT_TRUE(!container->IsVisible());
    }
  }

  FirstRunHelper* helper() { return helper_.get(); }

  int cancelled_times() const { return cancelled_times_; }

 private:
  // FirstRunHelper::Observer overrides.
  virtual void OnCancelled() OVERRIDE {
    ++cancelled_times_;
  }

  scoped_ptr<FirstRunHelper> helper_;
  int cancelled_times_;

  DISALLOW_COPY_AND_ASSIGN(FirstRunHelperTest);
};

// This test creates helper, checks that containers are hidden and then
// destructs helper.
TEST_F(FirstRunHelperTest, ContainersAreHidden) {
  CheckContainersAreHidden();
}

// Tests that helper correctly handles Escape key press.
TEST_F(FirstRunHelperTest, Cancel) {
  GetEventGenerator().PressKey(ui::VKEY_ESCAPE, 0);
  EXPECT_EQ(cancelled_times(), 1);
}

// Tests that modal window doesn't block events for overlay window.
TEST_F(FirstRunHelperTest, ModalWindowDoesNotBlock) {
  views::Widget* modal_dialog = views::DialogDelegate::CreateDialogWidget(
      new TestModalDialogDelegate(), CurrentContext(), NULL);
  modal_dialog->Show();
  // TODO(dzhioev): modal window should not steal focus from overlay window.
  aura::Window* overlay_window = helper()->GetOverlayWidget()->GetNativeView();
  overlay_window->Focus();
  EXPECT_TRUE(overlay_window->HasFocus());
  int mouse_events;
  overlay_window->SetEventFilter(new CountingEventHandler(&mouse_events));
  GetEventGenerator().PressLeftButton();
  GetEventGenerator().ReleaseLeftButton();
  EXPECT_EQ(mouse_events, 2);
}

}  // namespace test
}  // namespace ash