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/ime/candidate_view.h"
7 #include "base/logging.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "ui/aura/window.h"
10 #include "ui/events/test/event_generator.h"
11 #include "ui/views/controls/button/button.h"
12 #include "ui/views/layout/box_layout.h"
13 #include "ui/views/layout/fill_layout.h"
14 #include "ui/views/test/views_test_base.h"
15 #include "ui/views/widget/widget_delegate.h"
21 const char* const kDummyCandidates
[] = {
29 class CandidateViewTest
: public views::ViewsTestBase
,
30 public views::ButtonListener
{
32 CandidateViewTest() : widget_(NULL
), last_pressed_(NULL
) {}
33 virtual ~CandidateViewTest() {}
35 virtual void SetUp() OVERRIDE
{
36 views::ViewsTestBase::SetUp();
38 views::Widget::InitParams
init_params(CreateParams(
39 views::Widget::InitParams::TYPE_WINDOW
));
41 init_params
.delegate
= new views::WidgetDelegateView();
43 container_
= init_params
.delegate
->GetContentsView();
44 container_
->SetLayoutManager(
45 new views::BoxLayout(views::BoxLayout::kVertical
, 0, 0, 0));
46 for (size_t i
= 0; i
< arraysize(kDummyCandidates
); ++i
) {
47 CandidateView
* candidate
= new CandidateView(
48 this, ui::CandidateWindow::VERTICAL
);
49 ui::CandidateWindow::Entry entry
;
50 entry
.value
= base::UTF8ToUTF16(kDummyCandidates
[i
]);
51 candidate
->SetEntry(entry
);
52 container_
->AddChildView(candidate
);
55 widget_
= new views::Widget();
56 widget_
->Init(init_params
);
59 aura::Window
* native_window
= widget_
->GetNativeWindow();
60 event_generator_
.reset(new ui::test::EventGenerator(
61 native_window
->GetRootWindow(), native_window
));
64 virtual void TearDown() OVERRIDE
{
67 views::ViewsTestBase::TearDown();
71 CandidateView
* GetCandidateAt(int index
) {
72 return static_cast<CandidateView
*>(container_
->child_at(index
));
75 int GetHighlightedIndex(int* highlighted_count
) const {
76 *highlighted_count
= 0;
77 int last_highlighted
= -1;
78 for (int i
= 0; i
< container_
->child_count(); ++i
) {
79 if (container_
->child_at(i
)->background() != NULL
) {
80 (*highlighted_count
)++;
84 return last_highlighted
;
87 int GetLastPressedIndexAndReset() {
88 for (int i
= 0; i
< container_
->child_count(); ++i
) {
89 if (last_pressed_
== container_
->child_at(i
)) {
95 DCHECK(last_pressed_
== NULL
);
100 ui::test::EventGenerator
* event_generator() { return event_generator_
.get(); }
103 virtual void ButtonPressed(views::Button
* sender
,
104 const ui::Event
& event
) OVERRIDE
{
105 last_pressed_
= sender
;
108 views::Widget
* widget_
;
109 views::View
* container_
;
110 scoped_ptr
<ui::test::EventGenerator
> event_generator_
;
111 views::View
* last_pressed_
;
113 DISALLOW_COPY_AND_ASSIGN(CandidateViewTest
);
116 TEST_F(CandidateViewTest
, MouseHovers
) {
117 GetCandidateAt(0)->SetHighlighted(true);
119 int highlighted_count
= 0;
120 EXPECT_EQ(0, GetHighlightedIndex(&highlighted_count
));
121 EXPECT_EQ(1, highlighted_count
);
123 // Mouse hover shouldn't change the background.
124 event_generator()->MoveMouseTo(
125 GetCandidateAt(0)->GetBoundsInScreen().CenterPoint());
126 EXPECT_EQ(0, GetHighlightedIndex(&highlighted_count
));
127 EXPECT_EQ(1, highlighted_count
);
129 // Mouse hover shouldn't change the background.
130 event_generator()->MoveMouseTo(
131 GetCandidateAt(1)->GetBoundsInScreen().CenterPoint());
132 EXPECT_EQ(0, GetHighlightedIndex(&highlighted_count
));
133 EXPECT_EQ(1, highlighted_count
);
135 // Mouse hover shouldn't change the background.
136 event_generator()->MoveMouseTo(
137 GetCandidateAt(2)->GetBoundsInScreen().CenterPoint());
138 EXPECT_EQ(0, GetHighlightedIndex(&highlighted_count
));
139 EXPECT_EQ(1, highlighted_count
);
142 TEST_F(CandidateViewTest
, MouseClick
) {
143 event_generator()->MoveMouseTo(
144 GetCandidateAt(1)->GetBoundsInScreen().CenterPoint());
145 event_generator()->ClickLeftButton();
146 EXPECT_EQ(1, GetLastPressedIndexAndReset());
149 TEST_F(CandidateViewTest
, ClickAndMove
) {
150 GetCandidateAt(0)->SetHighlighted(true);
152 int highlighted_count
= 0;
153 EXPECT_EQ(0, GetHighlightedIndex(&highlighted_count
));
154 EXPECT_EQ(1, highlighted_count
);
156 event_generator()->MoveMouseTo(
157 GetCandidateAt(2)->GetBoundsInScreen().CenterPoint());
158 event_generator()->PressLeftButton();
159 EXPECT_EQ(2, GetHighlightedIndex(&highlighted_count
));
160 EXPECT_EQ(1, highlighted_count
);
162 // Highlight follows the drag.
163 event_generator()->MoveMouseTo(
164 GetCandidateAt(1)->GetBoundsInScreen().CenterPoint());
165 EXPECT_EQ(1, GetHighlightedIndex(&highlighted_count
));
166 EXPECT_EQ(1, highlighted_count
);
168 event_generator()->MoveMouseTo(
169 GetCandidateAt(0)->GetBoundsInScreen().CenterPoint());
170 EXPECT_EQ(0, GetHighlightedIndex(&highlighted_count
));
171 EXPECT_EQ(1, highlighted_count
);
173 event_generator()->MoveMouseTo(
174 GetCandidateAt(1)->GetBoundsInScreen().CenterPoint());
175 EXPECT_EQ(1, GetHighlightedIndex(&highlighted_count
));
176 EXPECT_EQ(1, highlighted_count
);
178 event_generator()->ReleaseLeftButton();
179 EXPECT_EQ(1, GetLastPressedIndexAndReset());