screen_tray_item_unittest.cc revision bb1529ce867d8845a77ec7cdf3e3003ef1771a40
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.
4
5#include "ash/system/chromeos/screen_security/screen_tray_item.h"
6
7#include "ash/system/chromeos/screen_security/screen_capture_tray_item.h"
8#include "ash/system/chromeos/screen_security/screen_share_tray_item.h"
9#include "ash/system/tray/tray_item_view.h"
10#include "ash/test/ash_test_base.h"
11#include "base/callback.h"
12#include "base/strings/utf_string_conversions.h"
13#include "ui/base/events/event.h"
14#include "ui/gfx/point.h"
15#include "ui/message_center/message_center.h"
16#include "ui/views/view.h"
17
18namespace ash {
19namespace internal {
20
21// Test with unicode strings.
22const char kTestScreenCaptureAppName[] =
23    "\xE0\xB2\xA0\x5F\xE0\xB2\xA0 (Screen Capture Test)";
24const char kTestScreenShareHelperName[] =
25    "\xE5\xAE\x8B\xE8\x85\xBE (Screen Share Test)";
26
27SystemTray* GetSystemTray() {
28  return Shell::GetInstance()->GetPrimarySystemTray();
29}
30
31SystemTrayNotifier* GetSystemTrayNotifier() {
32  return Shell::GetInstance()->system_tray_notifier();
33}
34
35void ClickViewCenter(views::View* view) {
36  gfx::Point click_location_in_local =
37      gfx::Point(view->width() / 2, view->height() / 2);
38  view->OnMousePressed(ui::MouseEvent(ui::ET_MOUSE_PRESSED,
39                                      click_location_in_local,
40                                      click_location_in_local,
41                                      ui::EF_NONE));
42}
43
44class ScreenTrayItemTest : public ash::test::AshTestBase {
45 public:
46  ScreenTrayItemTest()
47      : tray_item_(NULL), stop_callback_hit_count_(0) {}
48  virtual ~ScreenTrayItemTest() {}
49
50  ScreenTrayItem* tray_item() { return tray_item_; }
51  void set_tray_item(ScreenTrayItem* tray_item) { tray_item_ = tray_item; }
52
53  int stop_callback_hit_count() const { return stop_callback_hit_count_; }
54
55  virtual void SetUp() OVERRIDE {
56    test::AshTestBase::SetUp();
57    TrayItemView::DisableAnimationsForTest();
58  }
59
60  void StartSession() {
61    tray_item_->Start(
62        base::Bind(&ScreenTrayItemTest::StopCallback, base::Unretained(this)));
63  }
64
65  void StopSession() {
66    tray_item_->Stop();
67  }
68
69  void StopCallback() {
70    stop_callback_hit_count_++;
71  }
72
73 private:
74  ScreenTrayItem* tray_item_;
75  int stop_callback_hit_count_;
76
77  DISALLOW_COPY_AND_ASSIGN(ScreenTrayItemTest);
78};
79
80class ScreenCaptureTest : public ScreenTrayItemTest {
81 public:
82  ScreenCaptureTest() {}
83  virtual ~ScreenCaptureTest() {}
84
85  virtual void SetUp() OVERRIDE {
86    ScreenTrayItemTest::SetUp();
87    // This tray item is owned by its parent system tray view and will
88    // be deleted automatically when its parent is destroyed in AshTestBase.
89    ScreenTrayItem* tray_item = new ScreenCaptureTrayItem(GetSystemTray());
90    GetSystemTray()->AddTrayItem(tray_item);
91    set_tray_item(tray_item);
92  }
93
94  DISALLOW_COPY_AND_ASSIGN(ScreenCaptureTest);
95};
96
97class ScreenShareTest : public ScreenTrayItemTest {
98 public:
99  ScreenShareTest() {}
100  virtual ~ScreenShareTest() {}
101
102  virtual void SetUp() OVERRIDE {
103    ScreenTrayItemTest::SetUp();
104    // This tray item is owned by its parent system tray view and will
105    // be deleted automatically when its parent is destroyed in AshTestBase.
106    ScreenTrayItem* tray_item = new ScreenShareTrayItem(GetSystemTray());
107    GetSystemTray()->AddTrayItem(tray_item);
108    set_tray_item(tray_item);
109  }
110
111  DISALLOW_COPY_AND_ASSIGN(ScreenShareTest);
112};
113
114void TestStartAndStop(ScreenTrayItemTest* test) {
115  ScreenTrayItem* tray_item = test->tray_item();
116
117  EXPECT_FALSE(tray_item->is_started());
118  EXPECT_EQ(0, test->stop_callback_hit_count());
119
120  test->StartSession();
121  EXPECT_TRUE(tray_item->is_started());
122
123  test->StopSession();
124  EXPECT_FALSE(tray_item->is_started());
125  EXPECT_EQ(1, test->stop_callback_hit_count());
126}
127
128TEST_F(ScreenCaptureTest, StartAndStop) { TestStartAndStop(this); }
129TEST_F(ScreenShareTest, StartAndStop) { TestStartAndStop(this); }
130
131void TestNotificationStartAndStop(ScreenTrayItemTest* test,
132                                  const base::Closure& start_function,
133                                  const base::Closure& stop_function) {
134  ScreenTrayItem* tray_item = test->tray_item();
135  EXPECT_FALSE(tray_item->is_started());
136
137  start_function.Run();
138  EXPECT_TRUE(tray_item->is_started());
139
140  // The stop callback shouldn't be called because we stopped
141  // through the notification system.
142  stop_function.Run();
143  EXPECT_FALSE(tray_item->is_started());
144  EXPECT_EQ(0, test->stop_callback_hit_count());
145}
146
147TEST_F(ScreenCaptureTest, NotificationStartAndStop) {
148  base::Closure start_function =
149      base::Bind(&SystemTrayNotifier::NotifyScreenCaptureStart,
150          base::Unretained(GetSystemTrayNotifier()),
151          base::Bind(&ScreenTrayItemTest::StopCallback,
152                     base::Unretained(this)),
153                     base::UTF8ToUTF16(kTestScreenCaptureAppName));
154
155  base::Closure stop_function =
156      base::Bind(&SystemTrayNotifier::NotifyScreenCaptureStop,
157          base::Unretained(GetSystemTrayNotifier()));
158
159  TestNotificationStartAndStop(this, start_function, stop_function);
160}
161
162TEST_F(ScreenShareTest, NotificationStartAndStop) {
163  base::Closure start_func =
164      base::Bind(&SystemTrayNotifier::NotifyScreenShareStart,
165          base::Unretained(GetSystemTrayNotifier()),
166          base::Bind(&ScreenTrayItemTest::StopCallback,
167                     base::Unretained(this)),
168                     base::UTF8ToUTF16(kTestScreenShareHelperName));
169
170  base::Closure stop_func =
171      base::Bind(&SystemTrayNotifier::NotifyScreenShareStop,
172          base::Unretained(GetSystemTrayNotifier()));
173
174  TestNotificationStartAndStop(this, start_func, stop_func);
175}
176
177void TestNotificationView(ScreenTrayItemTest* test) {
178  ScreenTrayItem* tray_item = test->tray_item();
179
180  test->StartSession();
181  message_center::MessageCenter* message_center =
182      message_center::MessageCenter::Get();
183  EXPECT_TRUE(message_center->HasNotification(tray_item->GetNotificationId()));
184  test->StopSession();
185}
186
187TEST_F(ScreenCaptureTest, NotificationView) { TestNotificationView(this); }
188TEST_F(ScreenShareTest, NotificationView) { TestNotificationView(this); }
189
190void TestSystemTrayInteraction(ScreenTrayItemTest* test) {
191  ScreenTrayItem* tray_item = test->tray_item();
192  EXPECT_FALSE(tray_item->tray_view()->visible());
193
194  const std::vector<SystemTrayItem*>& tray_items =
195      GetSystemTray()->GetTrayItems();
196  EXPECT_NE(std::find(tray_items.begin(), tray_items.end(), tray_item),
197            tray_items.end());
198
199  test->StartSession();
200  EXPECT_TRUE(tray_item->tray_view()->visible());
201
202  // The default view should be created in a new bubble.
203  GetSystemTray()->ShowDefaultView(BUBBLE_CREATE_NEW);
204  EXPECT_TRUE(tray_item->default_view());
205  GetSystemTray()->CloseSystemBubble();
206  EXPECT_FALSE(tray_item->default_view());
207
208  test->StopSession();
209  EXPECT_FALSE(tray_item->tray_view()->visible());
210
211  // The default view should not be visible because session is stopped.
212  GetSystemTray()->ShowDefaultView(BUBBLE_CREATE_NEW);
213  EXPECT_FALSE(tray_item->default_view()->visible());
214}
215
216TEST_F(ScreenCaptureTest, SystemTrayInteraction) {
217  TestSystemTrayInteraction(this);
218}
219
220TEST_F(ScreenShareTest, SystemTrayInteraction) {
221  TestSystemTrayInteraction(this);
222}
223
224} // namespace internal
225} // namespace ash
226