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.
8 #include "chrome/browser/chromeos/input_method/input_method_util.h"
9 #include "chrome/browser/chromeos/input_method/mode_indicator_controller.h"
10 #include "chrome/test/base/in_process_browser_test.h"
11 #include "content/public/test/browser_test_utils.h"
12 #include "content/public/test/test_utils.h"
13 #include "testing/gtest/include/gtest/gtest.h"
14 #include "ui/base/ime/chromeos/component_extension_ime_manager.h"
15 #include "ui/base/ime/chromeos/extension_ime_util.h"
16 #include "ui/base/ime/chromeos/ime_bridge.h"
17 #include "ui/base/ime/chromeos/input_method_manager.h"
18 #include "ui/base/ime/chromeos/input_method_whitelist.h"
19 #include "ui/base/ime/input_method_factory.h"
20 #include "ui/views/widget/widget.h"
21 #include "ui/views/widget/widget_observer.h"
24 namespace input_method
{
26 class ScopedModeIndicatorObserverForTesting
:
27 public ModeIndicatorObserverInterface
{
29 ScopedModeIndicatorObserverForTesting()
30 : max_widget_list_size_(0) {
31 ModeIndicatorController::SetModeIndicatorObserverForTesting(this);
34 ~ScopedModeIndicatorObserverForTesting() override
{
35 for (size_t i
= 0; i
< widget_list_
.size(); ++i
) {
36 widget_list_
[i
]->RemoveObserver(this);
38 ModeIndicatorController::SetModeIndicatorObserverForTesting(NULL
);
41 gfx::Rect
last_bounds() const {
45 bool is_displayed() const {
49 const std::vector
<views::Widget
*>& widget_list() const {
53 size_t widget_list_size() const {
54 return widget_list_
.size();
57 size_t max_widget_list_size() const {
58 return max_widget_list_size_
;
61 // ModeIndicatorObserverInterface override:
62 void AddModeIndicatorWidget(views::Widget
* widget
) override
{
63 widget_list_
.push_back(widget
);
64 max_widget_list_size_
=
65 std::max(max_widget_list_size_
, widget_list_
.size());
66 widget
->AddObserver(this);
69 // views::WidgetObserver override:
70 void OnWidgetDestroying(views::Widget
* widget
) override
{
71 std::vector
<views::Widget
*>::iterator it
=
72 std::find(widget_list_
.begin(), widget_list_
.end(), widget
);
73 if (it
!= widget_list_
.end())
74 widget_list_
.erase(it
);
77 // views::WidgetObserver override:
78 void OnWidgetVisibilityChanged(views::Widget
* widget
, bool visible
) override
{
79 last_bounds_
= widget
->GetWindowBoundsInScreen();
80 is_displayed_
|= visible
;
85 gfx::Rect last_bounds_
;
86 size_t max_widget_list_size_
;
87 std::vector
<views::Widget
*> widget_list_
;
90 class ModeIndicatorBrowserTest
: public InProcessBrowserTest
{
92 ModeIndicatorBrowserTest()
93 : InProcessBrowserTest() {}
94 ~ModeIndicatorBrowserTest() override
{}
96 void SetUpInProcessBrowserTestFixture() override
{
97 ui::SetUpInputMethodFactoryForTesting();
100 void InitializeIMF() {
101 InputMethodManager::Get()
102 ->GetInputMethodUtil()
103 ->InitXkbInputMethodsForTesting();
107 DISALLOW_COPY_AND_ASSIGN(ModeIndicatorBrowserTest
);
111 // 43 is the designed size of the inner contents.
112 // This value corresponds with kMinSize defined in
113 // mode_indicator_delegate_view.cc.
114 const int kInnerSize
= 43;
117 IN_PROC_BROWSER_TEST_F(ModeIndicatorBrowserTest
, Bounds
) {
120 InputMethodManager
* imm
= InputMethodManager::Get();
123 std::vector
<std::string
> keyboard_layouts
;
124 keyboard_layouts
.push_back(
125 extension_ime_util::GetInputMethodIDByEngineID("xkb:fr::fra"));
127 // Add keyboard layouts to enable the mode indicator.
128 imm
->GetActiveIMEState()->EnableLoginLayouts("fr", keyboard_layouts
);
129 ASSERT_LT(1UL, imm
->GetActiveIMEState()->GetNumActiveInputMethods());
130 EXPECT_TRUE(imm
->GetActiveIMEState()->CanCycleInputMethod());
132 chromeos::IMECandidateWindowHandlerInterface
* candidate_window
=
133 chromeos::IMEBridge::Get()->GetCandidateWindowHandler();
134 candidate_window
->FocusStateChanged(true);
136 // Check if the size of the mode indicator is expected.
137 gfx::Rect
cursor1_bounds(100, 100, 1, 20);
138 gfx::Rect mi1_bounds
;
140 ScopedModeIndicatorObserverForTesting observer
;
141 candidate_window
->SetCursorBounds(cursor1_bounds
, cursor1_bounds
);
142 imm
->GetActiveIMEState()->SwitchToNextInputMethod();
143 mi1_bounds
= observer
.last_bounds();
144 // The bounds should be bigger than the inner size.
145 EXPECT_LE(kInnerSize
, mi1_bounds
.width());
146 EXPECT_LE(kInnerSize
, mi1_bounds
.height());
147 EXPECT_TRUE(observer
.is_displayed());
150 // Check if the location of the mode indicator is coresponded to
151 // the cursor bounds.
152 gfx::Rect
cursor2_bounds(50, 200, 1, 20);
153 gfx::Rect mi2_bounds
;
155 ScopedModeIndicatorObserverForTesting observer
;
156 candidate_window
->SetCursorBounds(cursor2_bounds
, cursor2_bounds
);
157 imm
->GetActiveIMEState()->SwitchToNextInputMethod();
158 mi2_bounds
= observer
.last_bounds();
159 EXPECT_TRUE(observer
.is_displayed());
162 EXPECT_EQ(cursor1_bounds
.x() - cursor2_bounds
.x(),
163 mi1_bounds
.x() - mi2_bounds
.x());
164 EXPECT_EQ(cursor1_bounds
.y() - cursor2_bounds
.y(),
165 mi1_bounds
.y() - mi2_bounds
.y());
166 EXPECT_EQ(mi1_bounds
.width(), mi2_bounds
.width());
167 EXPECT_EQ(mi1_bounds
.height(), mi2_bounds
.height());
169 const gfx::Rect screen_bounds
=
170 ash::Shell::GetScreen()->GetDisplayMatching(cursor1_bounds
).work_area();
172 // Check if the location of the mode indicator is concidered with
174 const gfx::Rect
cursor3_bounds(100, screen_bounds
.bottom() - 25, 1, 20);
175 gfx::Rect mi3_bounds
;
177 ScopedModeIndicatorObserverForTesting observer
;
178 candidate_window
->SetCursorBounds(cursor3_bounds
, cursor3_bounds
);
179 imm
->GetActiveIMEState()->SwitchToNextInputMethod();
180 mi3_bounds
= observer
.last_bounds();
181 EXPECT_TRUE(observer
.is_displayed());
182 EXPECT_LT(mi3_bounds
.bottom(), screen_bounds
.bottom());
186 IN_PROC_BROWSER_TEST_F(ModeIndicatorBrowserTest
, NumOfWidgets
) {
189 InputMethodManager
* imm
= InputMethodManager::Get();
192 std::vector
<std::string
> keyboard_layouts
;
193 keyboard_layouts
.push_back(
194 extension_ime_util::GetInputMethodIDByEngineID("xkb:fr::fra"));
196 // Add keyboard layouts to enable the mode indicator.
197 imm
->GetActiveIMEState()->EnableLoginLayouts("fr", keyboard_layouts
);
198 ASSERT_LT(1UL, imm
->GetActiveIMEState()->GetNumActiveInputMethods());
199 EXPECT_TRUE(imm
->GetActiveIMEState()->CanCycleInputMethod());
201 chromeos::IMECandidateWindowHandlerInterface
* candidate_window
=
202 chromeos::IMEBridge::Get()->GetCandidateWindowHandler();
203 candidate_window
->FocusStateChanged(true);
206 ScopedModeIndicatorObserverForTesting observer
;
208 imm
->GetActiveIMEState()->SwitchToNextInputMethod();
209 EXPECT_EQ(1UL, observer
.max_widget_list_size());
210 const views::Widget
* widget1
= observer
.widget_list()[0];
212 imm
->GetActiveIMEState()->SwitchToNextInputMethod();
213 EXPECT_EQ(2UL, observer
.max_widget_list_size());
215 // When a new mode indicator is displayed, the previous one should be
217 content::RunAllPendingInMessageLoop();
218 EXPECT_EQ(1UL, observer
.widget_list_size());
219 const views::Widget
* widget2
= observer
.widget_list()[0];
220 EXPECT_NE(widget1
, widget2
);
223 } // namespace input_method
224 } // namespace chromeos