OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include "ash/ime/candidate_view.h" | |
6 | |
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" | |
16 | |
17 namespace ash { | |
18 namespace ime { | |
19 namespace { | |
20 | |
21 const char* const kDummyCandidates[] = { | |
22 "candidate1", | |
23 "candidate2", | |
24 "candidate3", | |
25 }; | |
26 | |
27 } // namespace | |
28 | |
29 class CandidateViewTest : public views::ViewsTestBase, | |
30 public views::ButtonListener { | |
31 public: | |
32 CandidateViewTest() : widget_(NULL), last_pressed_(NULL) {} | |
33 ~CandidateViewTest() override {} | |
34 | |
35 void SetUp() override { | |
36 views::ViewsTestBase::SetUp(); | |
37 | |
38 views::Widget::InitParams init_params(CreateParams( | |
39 views::Widget::InitParams::TYPE_WINDOW)); | |
40 | |
41 init_params.delegate = new views::WidgetDelegateView(); | |
42 | |
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); | |
53 } | |
54 | |
55 widget_ = new views::Widget(); | |
56 widget_->Init(init_params); | |
57 widget_->Show(); | |
58 | |
59 aura::Window* native_window = widget_->GetNativeWindow(); | |
60 event_generator_.reset(new ui::test::EventGenerator( | |
61 native_window->GetRootWindow(), native_window)); | |
62 } | |
63 | |
64 void TearDown() override { | |
65 widget_->Close(); | |
66 | |
67 views::ViewsTestBase::TearDown(); | |
68 } | |
69 | |
70 protected: | |
71 CandidateView* GetCandidateAt(int index) { | |
72 return static_cast<CandidateView*>(container_->child_at(index)); | |
73 } | |
74 | |
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)++; | |
81 last_highlighted = i; | |
82 } | |
83 } | |
84 return last_highlighted; | |
85 } | |
86 | |
87 int GetLastPressedIndexAndReset() { | |
88 for (int i = 0; i < container_->child_count(); ++i) { | |
89 if (last_pressed_ == container_->child_at(i)) { | |
90 last_pressed_ = NULL; | |
91 return i; | |
92 } | |
93 } | |
94 | |
95 DCHECK(last_pressed_ == NULL); | |
96 last_pressed_ = NULL; | |
97 return -1; | |
98 } | |
99 | |
100 ui::test::EventGenerator* event_generator() { return event_generator_.get(); } | |
101 | |
102 private: | |
103 void ButtonPressed(views::Button* sender, const ui::Event& event) override { | |
104 last_pressed_ = sender; | |
105 } | |
106 | |
107 views::Widget* widget_; | |
108 views::View* container_; | |
109 scoped_ptr<ui::test::EventGenerator> event_generator_; | |
110 views::View* last_pressed_; | |
111 | |
112 DISALLOW_COPY_AND_ASSIGN(CandidateViewTest); | |
113 }; | |
114 | |
115 TEST_F(CandidateViewTest, MouseHovers) { | |
116 GetCandidateAt(0)->SetHighlighted(true); | |
117 | |
118 int highlighted_count = 0; | |
119 EXPECT_EQ(0, GetHighlightedIndex(&highlighted_count)); | |
120 EXPECT_EQ(1, highlighted_count); | |
121 | |
122 // Mouse hover shouldn't change the background. | |
123 event_generator()->MoveMouseTo( | |
124 GetCandidateAt(0)->GetBoundsInScreen().CenterPoint()); | |
125 EXPECT_EQ(0, GetHighlightedIndex(&highlighted_count)); | |
126 EXPECT_EQ(1, highlighted_count); | |
127 | |
128 // Mouse hover shouldn't change the background. | |
129 event_generator()->MoveMouseTo( | |
130 GetCandidateAt(1)->GetBoundsInScreen().CenterPoint()); | |
131 EXPECT_EQ(0, GetHighlightedIndex(&highlighted_count)); | |
132 EXPECT_EQ(1, highlighted_count); | |
133 | |
134 // Mouse hover shouldn't change the background. | |
135 event_generator()->MoveMouseTo( | |
136 GetCandidateAt(2)->GetBoundsInScreen().CenterPoint()); | |
137 EXPECT_EQ(0, GetHighlightedIndex(&highlighted_count)); | |
138 EXPECT_EQ(1, highlighted_count); | |
139 } | |
140 | |
141 TEST_F(CandidateViewTest, MouseClick) { | |
142 event_generator()->MoveMouseTo( | |
143 GetCandidateAt(1)->GetBoundsInScreen().CenterPoint()); | |
144 event_generator()->ClickLeftButton(); | |
145 EXPECT_EQ(1, GetLastPressedIndexAndReset()); | |
146 } | |
147 | |
148 TEST_F(CandidateViewTest, ClickAndMove) { | |
149 GetCandidateAt(0)->SetHighlighted(true); | |
150 | |
151 int highlighted_count = 0; | |
152 EXPECT_EQ(0, GetHighlightedIndex(&highlighted_count)); | |
153 EXPECT_EQ(1, highlighted_count); | |
154 | |
155 event_generator()->MoveMouseTo( | |
156 GetCandidateAt(2)->GetBoundsInScreen().CenterPoint()); | |
157 event_generator()->PressLeftButton(); | |
158 EXPECT_EQ(2, GetHighlightedIndex(&highlighted_count)); | |
159 EXPECT_EQ(1, highlighted_count); | |
160 | |
161 // Highlight follows the drag. | |
162 event_generator()->MoveMouseTo( | |
163 GetCandidateAt(1)->GetBoundsInScreen().CenterPoint()); | |
164 EXPECT_EQ(1, GetHighlightedIndex(&highlighted_count)); | |
165 EXPECT_EQ(1, highlighted_count); | |
166 | |
167 event_generator()->MoveMouseTo( | |
168 GetCandidateAt(0)->GetBoundsInScreen().CenterPoint()); | |
169 EXPECT_EQ(0, GetHighlightedIndex(&highlighted_count)); | |
170 EXPECT_EQ(1, highlighted_count); | |
171 | |
172 event_generator()->MoveMouseTo( | |
173 GetCandidateAt(1)->GetBoundsInScreen().CenterPoint()); | |
174 EXPECT_EQ(1, GetHighlightedIndex(&highlighted_count)); | |
175 EXPECT_EQ(1, highlighted_count); | |
176 | |
177 event_generator()->ReleaseLeftButton(); | |
178 EXPECT_EQ(1, GetLastPressedIndexAndReset()); | |
179 } | |
180 | |
181 } // namespace ime | |
182 } // namespace ash | |
OLD | NEW |