Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / ui / chromeos / ime / candidate_view_unittest.cc
blobe7ce9cd852b2f971045c2a86f3e853fcc7f10e13
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 "ui/chromeos/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"
17 namespace ui {
18 namespace ime {
19 namespace {
21 const char* const kDummyCandidates[] = {
22 "candidate1",
23 "candidate2",
24 "candidate3",
27 } // namespace
29 class CandidateViewTest : public views::ViewsTestBase,
30 public views::ButtonListener {
31 public:
32 CandidateViewTest() : widget_(NULL), last_pressed_(NULL) {}
33 ~CandidateViewTest() override {}
35 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);
57 widget_->Show();
59 aura::Window* native_window = widget_->GetNativeWindow();
60 event_generator_.reset(new ui::test::EventGenerator(
61 native_window->GetRootWindow(), native_window));
64 void TearDown() override {
65 widget_->Close();
67 views::ViewsTestBase::TearDown();
70 protected:
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)++;
81 last_highlighted = i;
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)) {
90 last_pressed_ = NULL;
91 return i;
95 DCHECK(last_pressed_ == NULL);
96 last_pressed_ = NULL;
97 return -1;
100 ui::test::EventGenerator* event_generator() { return event_generator_.get(); }
102 private:
103 void ButtonPressed(views::Button* sender, const ui::Event& event) override {
104 last_pressed_ = sender;
107 views::Widget* widget_;
108 views::View* container_;
109 scoped_ptr<ui::test::EventGenerator> event_generator_;
110 views::View* last_pressed_;
112 DISALLOW_COPY_AND_ASSIGN(CandidateViewTest);
115 TEST_F(CandidateViewTest, MouseHovers) {
116 GetCandidateAt(0)->SetHighlighted(true);
118 int highlighted_count = 0;
119 EXPECT_EQ(0, GetHighlightedIndex(&highlighted_count));
120 EXPECT_EQ(1, highlighted_count);
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);
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);
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);
141 TEST_F(CandidateViewTest, MouseClick) {
142 event_generator()->MoveMouseTo(
143 GetCandidateAt(1)->GetBoundsInScreen().CenterPoint());
144 event_generator()->ClickLeftButton();
145 EXPECT_EQ(1, GetLastPressedIndexAndReset());
148 TEST_F(CandidateViewTest, ClickAndMove) {
149 GetCandidateAt(0)->SetHighlighted(true);
151 int highlighted_count = 0;
152 EXPECT_EQ(0, GetHighlightedIndex(&highlighted_count));
153 EXPECT_EQ(1, highlighted_count);
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);
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);
167 event_generator()->MoveMouseTo(
168 GetCandidateAt(0)->GetBoundsInScreen().CenterPoint());
169 EXPECT_EQ(0, GetHighlightedIndex(&highlighted_count));
170 EXPECT_EQ(1, highlighted_count);
172 event_generator()->MoveMouseTo(
173 GetCandidateAt(1)->GetBoundsInScreen().CenterPoint());
174 EXPECT_EQ(1, GetHighlightedIndex(&highlighted_count));
175 EXPECT_EQ(1, highlighted_count);
177 event_generator()->ReleaseLeftButton();
178 EXPECT_EQ(1, GetLastPressedIndexAndReset());
181 } // namespace ime
182 } // namespace ui