summaryrefslogtreecommitdiffstats
path: root/ash/system/ime/tray_ime_chromeos_unittest.cc
blob: b17d4a53398e28bb07f6a24e3199a89dbbb600c2 (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
// Copyright 2014 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/ime/tray_ime_chromeos.h"

#include "ash/accessibility_delegate.h"
#include "ash/shell.h"
#include "ash/system/status_area_widget.h"
#include "ash/system/tray/system_tray_notifier.h"
#include "ash/test/ash_test_base.h"
#include "ash/test/status_area_widget_test_helper.h"
#include "ash/test/virtual_keyboard_test_helper.h"
#include "base/command_line.h"
#include "ui/keyboard/keyboard_util.h"

namespace ash {

class TrayIMETest : public test::AshTestBase {
 public:
  TrayIMETest() {}
  ~TrayIMETest() override {}

  TrayIME* tray() { return tray_.get(); }

  views::View* default_view() { return default_view_.get(); }

  views::View* detailed_view() { return detailed_view_.get(); }

  // Sets up a TrayIME and its default view.
  void SetUpForStatusAreaWidget(StatusAreaWidget* status_area_widget);

  // Mocks enabling the a11y virtual keyboard since the actual a11y manager
  // is not created in ash tests.
  void SetAccessibilityKeyboardEnabled(bool enabled);

  // Resets |tray_| and |default_view_| so that all components of
  // TrayIME have been cleared. Tests may then call
  // SetUpForStatusAreaWidget in order to initialize the components.
  void TearDownViews();

  // Sets the current number of active IMEs.
  void SetIMELength(int length);

  // Returns the view in the detailed views scroll content at the provided
  // index.
  views::View* GetScrollChildView(int index);

  // test::AshTestBase:
  void SetUp() override;
  void TearDown() override;

 private:
  scoped_ptr<TrayIME> tray_;
  scoped_ptr<views::View> default_view_;
  scoped_ptr<views::View> detailed_view_;
};

void TrayIMETest::SetUpForStatusAreaWidget(
    StatusAreaWidget* status_area_widget) {
  tray_.reset(new TrayIME(status_area_widget->system_tray()));
  default_view_.reset(tray_->CreateDefaultView(
      StatusAreaWidgetTestHelper::GetUserLoginStatus()));
  detailed_view_.reset(tray_->CreateDetailedView(
      StatusAreaWidgetTestHelper::GetUserLoginStatus()));
}

void TrayIMETest::SetAccessibilityKeyboardEnabled(bool enabled) {
  Shell::GetInstance()->accessibility_delegate()->SetVirtualKeyboardEnabled(
      enabled);
  keyboard::SetAccessibilityKeyboardEnabled(enabled);
  ui::AccessibilityNotificationVisibility notification =
      enabled ? ui::AccessibilityNotificationVisibility::A11Y_NOTIFICATION_SHOW
              : ui::AccessibilityNotificationVisibility::A11Y_NOTIFICATION_NONE;
  Shell::GetInstance()->system_tray_notifier()->NotifyAccessibilityModeChanged(
      notification);
}

void TrayIMETest::TearDownViews() {
  tray_.reset();
  default_view_.reset();
  detailed_view_.reset();
}

void TrayIMETest::SetIMELength(int length) {
  tray_->ime_list_.clear();
  IMEInfo ime;
  for (int i = 0; i < length; i++) {
    tray_->ime_list_.push_back(ime);
  }
  tray_->Update();
}

views::View* TrayIMETest::GetScrollChildView(int index) {
  TrayDetailsView* details = static_cast<TrayDetailsView*>(detailed_view());
  views::View* content = details->scroll_content();
  EXPECT_TRUE(content);
  EXPECT_GT(content->child_count(), index);
  return content->child_at(index);
}

void TrayIMETest::SetUp() {
  test::AshTestBase::SetUp();
  SetUpForStatusAreaWidget(StatusAreaWidgetTestHelper::GetStatusAreaWidget());
}

void TrayIMETest::TearDown() {
  SetAccessibilityKeyboardEnabled(false);
  TearDownViews();
  test::AshTestBase::TearDown();
}

// Tests that if the keyboard is not suppressed the default view is hidden
// if less than 2 IMEs are present.
TEST_F(TrayIMETest, HiddenWithNoIMEs) {
  SetIMELength(0);
  EXPECT_FALSE(default_view()->visible());
  SetIMELength(1);
  EXPECT_FALSE(default_view()->visible());
  SetIMELength(2);
  EXPECT_TRUE(default_view()->visible());
}

// Tests that if no IMEs are present the default view is hidden when a11y is
// enabled.
TEST_F(TrayIMETest, HidesOnA11yEnabled) {
  SetIMELength(0);
  test::VirtualKeyboardTestHelper::SuppressKeyboard();
  EXPECT_TRUE(default_view()->visible());
  // Enable a11y keyboard.
  SetAccessibilityKeyboardEnabled(true);
  EXPECT_FALSE(default_view()->visible());
  // Disable the a11y keyboard.
  SetAccessibilityKeyboardEnabled(false);
  EXPECT_TRUE(default_view()->visible());
}

// Tests that clicking on the keyboard toggle causes the virtual keyboard
// to toggle between enabled and disabled.
TEST_F(TrayIMETest, PerformActionOnDetailedView) {
  SetIMELength(0);
  test::VirtualKeyboardTestHelper::SuppressKeyboard();
  EXPECT_FALSE(keyboard::IsKeyboardEnabled());
  views::View* toggle = GetScrollChildView(0);
  ui::GestureEvent tap(0, 0, 0, base::TimeDelta(),
                       ui::GestureEventDetails(ui::ET_GESTURE_TAP));
  // Enable the keyboard.
  toggle->OnGestureEvent(&tap);
  EXPECT_TRUE(keyboard::IsKeyboardEnabled());
  EXPECT_TRUE(default_view()->visible());
  // With no IMEs the toggle should be the first child.
  toggle = GetScrollChildView(0);
  // Clicking again should disable the keyboard.
  tap = ui::GestureEvent(0, 0, 0, base::TimeDelta(),
                         ui::GestureEventDetails(ui::ET_GESTURE_TAP));
  toggle->OnGestureEvent(&tap);
  EXPECT_FALSE(keyboard::IsKeyboardEnabled());
  EXPECT_TRUE(default_view()->visible());
}

}  // namespace ash