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.
5 #include "ui/views/controls/prefix_selector.h"
9 #include "base/strings/string_util.h"
10 #include "base/strings/utf_string_conversions.h"
11 #include "ui/views/controls/prefix_delegate.h"
12 #include "ui/views/test/views_test_base.h"
14 using base::ASCIIToUTF16
;
18 class TestPrefixDelegate
: public PrefixDelegate
{
20 TestPrefixDelegate() : selected_row_(0) {
21 rows_
.push_back(ASCIIToUTF16("aardvark"));
22 rows_
.push_back(ASCIIToUTF16("antelope"));
23 rows_
.push_back(ASCIIToUTF16("badger"));
24 rows_
.push_back(ASCIIToUTF16("gnu"));
27 ~TestPrefixDelegate() override
{}
29 int GetRowCount() override
{ return static_cast<int>(rows_
.size()); }
31 int GetSelectedRow() override
{ return selected_row_
; }
33 void SetSelectedRow(int row
) override
{ selected_row_
= row
; }
35 base::string16
GetTextForRow(int row
) override
{ return rows_
[row
]; }
38 std::vector
<base::string16
> rows_
;
41 DISALLOW_COPY_AND_ASSIGN(TestPrefixDelegate
);
44 class PrefixSelectorTest
: public ViewsTestBase
{
46 PrefixSelectorTest() {
47 selector_
.reset(new PrefixSelector(&delegate_
));
49 ~PrefixSelectorTest() override
{
50 // Explicitly release |selector_| here which can happen before releasing
56 scoped_ptr
<PrefixSelector
> selector_
;
57 TestPrefixDelegate delegate_
;
60 DISALLOW_COPY_AND_ASSIGN(PrefixSelectorTest
);
63 TEST_F(PrefixSelectorTest
, PrefixSelect
) {
64 selector_
->InsertText(ASCIIToUTF16("an"));
65 EXPECT_EQ(1, delegate_
.GetSelectedRow());
67 // Invoke OnViewBlur() to reset time.
68 selector_
->OnViewBlur();
69 selector_
->InsertText(ASCIIToUTF16("a"));
70 EXPECT_EQ(0, delegate_
.GetSelectedRow());
72 selector_
->OnViewBlur();
73 selector_
->InsertText(ASCIIToUTF16("g"));
74 EXPECT_EQ(3, delegate_
.GetSelectedRow());
76 selector_
->OnViewBlur();
77 selector_
->InsertText(ASCIIToUTF16("b"));
78 selector_
->InsertText(ASCIIToUTF16("a"));
79 EXPECT_EQ(2, delegate_
.GetSelectedRow());
81 selector_
->OnViewBlur();
82 selector_
->InsertText(ASCIIToUTF16("\t"));
83 selector_
->InsertText(ASCIIToUTF16("b"));
84 selector_
->InsertText(ASCIIToUTF16("a"));
85 EXPECT_EQ(2, delegate_
.GetSelectedRow());