summaryrefslogtreecommitdiffstats
path: root/ash/system/tray/tray_details_view_unittest.cc
blob: 0d83ae153a2c8fe489a0b430278353cc3c9b1fee (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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
// 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/system/tray/tray_details_view.h"

#include "ash/root_window_controller.h"
#include "ash/shelf/shelf_widget.h"
#include "ash/shell.h"
#include "ash/system/status_area_widget.h"
#include "ash/system/tray/hover_highlight_view.h"
#include "ash/system/tray/special_popup_row.h"
#include "ash/system/tray/system_tray.h"
#include "ash/system/tray/system_tray_item.h"
#include "ash/system/tray/tray_popup_header_button.h"
#include "ash/system/tray/view_click_listener.h"
#include "ash/test/ash_test_base.h"
#include "base/run_loop.h"
#include "grit/ash_resources.h"
#include "grit/ash_strings.h"
#include "ui/aura/window.h"
#include "ui/events/test/event_generator.h"
#include "ui/views/controls/button/button.h"
#include "ui/views/view.h"
#include "ui/views/widget/widget.h"

namespace ash {
namespace test {

namespace {

SystemTray* GetSystemTray() {
  return Shell::GetPrimaryRootWindowController()->shelf()->
      status_area_widget()->system_tray();
}

class TestDetailsView : public TrayDetailsView,
                        public ViewClickListener,
                        public views::ButtonListener {
 public:
  explicit TestDetailsView(SystemTrayItem* owner) : TrayDetailsView(owner) {
    // Uses bluetooth label for testing purpose. It can be changed to any
    // string_id.
    CreateSpecialRow(IDS_ASH_STATUS_TRAY_BLUETOOTH, this);
    tray_popup_header_button_ =
        new TrayPopupHeaderButton(this, IDR_AURA_UBER_TRAY_BLUETOOTH_ENABLED,
                                  IDR_AURA_UBER_TRAY_BLUETOOTH_DISABLED,
                                  IDR_AURA_UBER_TRAY_BLUETOOTH_ENABLED_HOVER,
                                  IDR_AURA_UBER_TRAY_BLUETOOTH_DISABLED_HOVER,
                                  IDS_ASH_STATUS_TRAY_BLUETOOTH);
    footer()->AddButton(tray_popup_header_button_);
  }

  ~TestDetailsView() override {}

  TrayPopupHeaderButton* tray_popup_header_button() {
    return tray_popup_header_button_;
  }

  void FocusFooter() {
    footer()->content()->RequestFocus();
  }

  // ViewClickListener:
  void OnViewClicked(views::View* sender) override {}

  // views::ButtonListener:
  void ButtonPressed(views::Button* sender, const ui::Event& event) override {}

 private:
  TrayPopupHeaderButton* tray_popup_header_button_;

  DISALLOW_COPY_AND_ASSIGN(TestDetailsView);
};

// Trivial item implementation that tracks its views for testing.
class TestItem : public SystemTrayItem {
 public:
  TestItem() : SystemTrayItem(GetSystemTray()), tray_view_(NULL) {}

  // Overridden from SystemTrayItem:
  views::View* CreateTrayView(user::LoginStatus status) override {
    tray_view_ = new views::View;
    return tray_view_;
  }
  views::View* CreateDefaultView(user::LoginStatus status) override {
    default_view_ = new views::View;
    default_view_->SetFocusable(true);
    return default_view_;
  }
  views::View* CreateDetailedView(user::LoginStatus status) override {
    detailed_view_ = new TestDetailsView(this);
    return detailed_view_;
  }
  void DestroyTrayView() override { tray_view_ = NULL; }
  void DestroyDefaultView() override { default_view_ = NULL; }
  void DestroyDetailedView() override { detailed_view_ = NULL; }

  views::View* tray_view() const { return tray_view_; }
  views::View* default_view() const { return default_view_; }
  TestDetailsView* detailed_view() const { return detailed_view_; }

 private:
  views::View* tray_view_;
  views::View* default_view_;
  TestDetailsView* detailed_view_;

  DISALLOW_COPY_AND_ASSIGN(TestItem);
};

}  // namespace

class TrayDetailsViewTest : public AshTestBase {
 public:
  TrayDetailsViewTest() {}
  ~TrayDetailsViewTest() override {}

  HoverHighlightView* CreateAndShowHoverHighlightView() {
    SystemTray* tray = GetSystemTray();
    TestItem* test_item = new TestItem;
    tray->AddTrayItem(test_item);
    tray->ShowDefaultView(BUBBLE_CREATE_NEW);
    RunAllPendingInMessageLoop();
    tray->ShowDetailedView(test_item, 0, true, BUBBLE_USE_EXISTING);
    RunAllPendingInMessageLoop();

    return static_cast<HoverHighlightView*>(test_item->detailed_view()->
        footer()->content());
  }

  TrayPopupHeaderButton* CreateAndShowTrayPopupHeaderButton() {
    SystemTray* tray = GetSystemTray();
    TestItem* test_item = new TestItem;
    tray->AddTrayItem(test_item);
    tray->ShowDefaultView(BUBBLE_CREATE_NEW);
    RunAllPendingInMessageLoop();
    tray->ShowDetailedView(test_item, 0, true, BUBBLE_USE_EXISTING);
    RunAllPendingInMessageLoop();

    return test_item->detailed_view()->tray_popup_header_button();
  }

 private:
  DISALLOW_COPY_AND_ASSIGN(TrayDetailsViewTest);
};

TEST_F(TrayDetailsViewTest, TransitionToDefaultViewTest) {
  SystemTray* tray = GetSystemTray();
  ASSERT_TRUE(tray->GetWidget());

  TestItem* test_item_1 = new TestItem;
  TestItem* test_item_2 = new TestItem;
  tray->AddTrayItem(test_item_1);
  tray->AddTrayItem(test_item_2);

  // Ensure the tray views are created.
  ASSERT_TRUE(test_item_1->tray_view() != NULL);
  ASSERT_TRUE(test_item_2->tray_view() != NULL);

  // Show the default view.
  tray->ShowDefaultView(BUBBLE_CREATE_NEW);
  RunAllPendingInMessageLoop();

  // Show the detailed view of item 2.
  tray->ShowDetailedView(test_item_2, 0, true, BUBBLE_USE_EXISTING);
  EXPECT_TRUE(test_item_2->detailed_view());
  RunAllPendingInMessageLoop();
  EXPECT_FALSE(test_item_2->default_view());

  // Transition back to default view, the default view of item 2 should have
  // focus.
  test_item_2->detailed_view()->FocusFooter();
  test_item_2->detailed_view()->TransitionToDefaultView();
  RunAllPendingInMessageLoop();

  EXPECT_TRUE(test_item_2->default_view());
  EXPECT_FALSE(test_item_2->detailed_view());
  EXPECT_TRUE(test_item_2->default_view()->HasFocus());

  // Show the detailed view of item 2 again.
  tray->ShowDetailedView(test_item_2, 0, true, BUBBLE_USE_EXISTING);
  EXPECT_TRUE(test_item_2->detailed_view());
  RunAllPendingInMessageLoop();
  EXPECT_FALSE(test_item_2->default_view());

  // Transition back to default view, the default view of item 2 should NOT have
  // focus.
  test_item_2->detailed_view()->TransitionToDefaultView();
  RunAllPendingInMessageLoop();

  EXPECT_TRUE(test_item_2->default_view());
  EXPECT_FALSE(test_item_2->detailed_view());
  EXPECT_FALSE(test_item_2->default_view()->HasFocus());
}

// Tests that HoverHighlightView enters hover state in response to touch.
TEST_F(TrayDetailsViewTest, HoverHighlightViewTouchFeedback) {
  HoverHighlightView* view = CreateAndShowHoverHighlightView();
  EXPECT_FALSE(view->hover());

  ui::test::EventGenerator generator(Shell::GetPrimaryRootWindow());
  generator.set_current_location(view->GetBoundsInScreen().CenterPoint());
  generator.PressTouch();
  EXPECT_TRUE(view->hover());

  generator.ReleaseTouch();
  EXPECT_FALSE(view->hover());
}

// Tests that touch events leaving HoverHighlightView cancel the hover state.
TEST_F(TrayDetailsViewTest, HoverHighlightViewTouchFeedbackCancellation) {
  HoverHighlightView* view = CreateAndShowHoverHighlightView();
  EXPECT_FALSE(view->hover());

  gfx::Rect view_bounds = view->GetBoundsInScreen();
  ui::test::EventGenerator generator(Shell::GetPrimaryRootWindow());
  generator.set_current_location(view_bounds.CenterPoint());
  generator.PressTouch();
  EXPECT_TRUE(view->hover());

  gfx::Point move_point(view_bounds.x(), view_bounds.CenterPoint().y());
  generator.MoveTouch(move_point);
  EXPECT_FALSE(view->hover());

  generator.set_current_location(move_point);
  generator.ReleaseTouch();
  EXPECT_FALSE(view->hover());
}

// Tests that TrayPopupHeaderButton renders a background in response to touch.
TEST_F(TrayDetailsViewTest, TrayPopupHeaderButtonTouchFeedback) {
  TrayPopupHeaderButton* button = CreateAndShowTrayPopupHeaderButton();
  EXPECT_FALSE(button->background());

  ui::test::EventGenerator generator(Shell::GetPrimaryRootWindow());
  generator.set_current_location(button->GetBoundsInScreen().CenterPoint());
  generator.PressTouch();
  EXPECT_TRUE(button->background());

  generator.ReleaseTouch();
  EXPECT_FALSE(button->background());
}

// Tests that touch events leaving TrayPopupHeaderButton cancel the touch
// feedback background.
TEST_F(TrayDetailsViewTest, TrayPopupHeaderButtonTouchFeedbackCancellation) {
  TrayPopupHeaderButton* button = CreateAndShowTrayPopupHeaderButton();
  EXPECT_FALSE(button->background());

  gfx::Rect view_bounds = button->GetBoundsInScreen();
  ui::test::EventGenerator generator(Shell::GetPrimaryRootWindow());
  generator.set_current_location(view_bounds.CenterPoint());
  generator.PressTouch();
  EXPECT_TRUE(button->background());

  gfx::Point move_point(view_bounds.x(), view_bounds.CenterPoint().y());
  generator.MoveTouch(move_point);
  EXPECT_FALSE(button->background());

  generator.set_current_location(move_point);
  generator.ReleaseTouch();
  EXPECT_FALSE(button->background());
}

// Tests that a mouse entering TrayPopupHeaderButton renders a background as
// visual feedback.
TEST_F(TrayDetailsViewTest, TrayPopupHeaderButtonMouseHoverFeedback) {
  TrayPopupHeaderButton* button = CreateAndShowTrayPopupHeaderButton();
  EXPECT_FALSE(button->background());

  ui::test::EventGenerator generator(Shell::GetPrimaryRootWindow());
  gfx::Rect bounds = button->GetBoundsInScreen();
  gfx::Point initial_point(bounds.x() - 1, bounds.y());
  generator.set_current_location(initial_point);
  generator.MoveMouseBy(1, 0);
  RunAllPendingInMessageLoop();
  EXPECT_TRUE(button->background());
}

}  // namespace test
}  // namespace ash