1 // Copyright 2014 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/system/ime/tray_ime_chromeos.h"
7 #include "ash/accessibility_delegate.h"
9 #include "ash/system/status_area_widget.h"
10 #include "ash/system/tray/system_tray_notifier.h"
11 #include "ash/test/ash_test_base.h"
12 #include "ash/test/status_area_widget_test_helper.h"
13 #include "ash/test/virtual_keyboard_test_helper.h"
14 #include "base/command_line.h"
15 #include "ui/keyboard/keyboard_util.h"
19 class TrayIMETest
: public test::AshTestBase
{
22 ~TrayIMETest() override
{}
24 TrayIME
* tray() { return tray_
.get(); }
26 views::View
* default_view() { return default_view_
.get(); }
28 views::View
* detailed_view() { return detailed_view_
.get(); }
30 // Sets up a TrayIME and its default view.
31 void SetUpForStatusAreaWidget(StatusAreaWidget
* status_area_widget
);
33 // Mocks enabling the a11y virtual keyboard since the actual a11y manager
34 // is not created in ash tests.
35 void SetAccessibilityKeyboardEnabled(bool enabled
);
37 // Resets |tray_| and |default_view_| so that all components of
38 // TrayIME have been cleared. Tests may then call
39 // SetUpForStatusAreaWidget in order to initialize the components.
42 // Sets the current number of active IMEs.
43 void SetIMELength(int length
);
45 // Returns the view in the detailed views scroll content at the provided
47 views::View
* GetScrollChildView(int index
);
50 void SetUp() override
;
51 void TearDown() override
;
54 scoped_ptr
<TrayIME
> tray_
;
55 scoped_ptr
<views::View
> default_view_
;
56 scoped_ptr
<views::View
> detailed_view_
;
59 void TrayIMETest::SetUpForStatusAreaWidget(
60 StatusAreaWidget
* status_area_widget
) {
61 tray_
.reset(new TrayIME(status_area_widget
->system_tray()));
62 default_view_
.reset(tray_
->CreateDefaultView(
63 StatusAreaWidgetTestHelper::GetUserLoginStatus()));
64 detailed_view_
.reset(tray_
->CreateDetailedView(
65 StatusAreaWidgetTestHelper::GetUserLoginStatus()));
68 void TrayIMETest::SetAccessibilityKeyboardEnabled(bool enabled
) {
69 Shell::GetInstance()->accessibility_delegate()->SetVirtualKeyboardEnabled(
71 keyboard::SetAccessibilityKeyboardEnabled(enabled
);
72 ui::AccessibilityNotificationVisibility notification
=
73 enabled
? ui::AccessibilityNotificationVisibility::A11Y_NOTIFICATION_SHOW
74 : ui::AccessibilityNotificationVisibility::A11Y_NOTIFICATION_NONE
;
75 Shell::GetInstance()->system_tray_notifier()->NotifyAccessibilityModeChanged(
79 void TrayIMETest::TearDownViews() {
81 default_view_
.reset();
82 detailed_view_
.reset();
85 void TrayIMETest::SetIMELength(int length
) {
86 tray_
->ime_list_
.clear();
88 for (int i
= 0; i
< length
; i
++) {
89 tray_
->ime_list_
.push_back(ime
);
94 views::View
* TrayIMETest::GetScrollChildView(int index
) {
95 TrayDetailsView
* details
= static_cast<TrayDetailsView
*>(detailed_view());
96 views::View
* content
= details
->scroll_content();
98 EXPECT_GT(content
->child_count(), index
);
99 return content
->child_at(index
);
102 void TrayIMETest::SetUp() {
103 test::AshTestBase::SetUp();
104 SetUpForStatusAreaWidget(StatusAreaWidgetTestHelper::GetStatusAreaWidget());
107 void TrayIMETest::TearDown() {
108 SetAccessibilityKeyboardEnabled(false);
110 test::AshTestBase::TearDown();
113 // Tests that if the keyboard is not suppressed the default view is hidden
114 // if less than 2 IMEs are present.
115 TEST_F(TrayIMETest
, HiddenWithNoIMEs
) {
117 EXPECT_FALSE(default_view()->visible());
119 EXPECT_FALSE(default_view()->visible());
121 EXPECT_TRUE(default_view()->visible());
124 // Tests that if no IMEs are present the default view is hidden when a11y is
126 TEST_F(TrayIMETest
, HidesOnA11yEnabled
) {
128 test::VirtualKeyboardTestHelper::SuppressKeyboard();
129 EXPECT_TRUE(default_view()->visible());
130 // Enable a11y keyboard.
131 SetAccessibilityKeyboardEnabled(true);
132 EXPECT_FALSE(default_view()->visible());
133 // Disable the a11y keyboard.
134 SetAccessibilityKeyboardEnabled(false);
135 EXPECT_TRUE(default_view()->visible());
138 // Tests that clicking on the keyboard toggle causes the virtual keyboard
139 // to toggle between enabled and disabled.
140 TEST_F(TrayIMETest
, PerformActionOnDetailedView
) {
142 test::VirtualKeyboardTestHelper::SuppressKeyboard();
143 EXPECT_FALSE(keyboard::IsKeyboardEnabled());
144 views::View
* toggle
= GetScrollChildView(0);
145 ui::GestureEvent
tap(0, 0, 0, base::TimeDelta(),
146 ui::GestureEventDetails(ui::ET_GESTURE_TAP
));
147 // Enable the keyboard.
148 toggle
->OnGestureEvent(&tap
);
149 EXPECT_TRUE(keyboard::IsKeyboardEnabled());
150 EXPECT_TRUE(default_view()->visible());
151 // With no IMEs the toggle should be the first child.
152 toggle
= GetScrollChildView(0);
153 // Clicking again should disable the keyboard.
154 tap
= ui::GestureEvent(0, 0, 0, base::TimeDelta(),
155 ui::GestureEventDetails(ui::ET_GESTURE_TAP
));
156 toggle
->OnGestureEvent(&tap
);
157 EXPECT_FALSE(keyboard::IsKeyboardEnabled());
158 EXPECT_TRUE(default_view()->visible());