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 virtual ~TestPrefixDelegate() {}
29 virtual int GetRowCount() OVERRIDE
{
30 return static_cast<int>(rows_
.size());
33 virtual int GetSelectedRow() OVERRIDE
{
37 virtual void SetSelectedRow(int row
) OVERRIDE
{
41 virtual base::string16
GetTextForRow(int row
) OVERRIDE
{
46 std::vector
<base::string16
> rows_
;
49 DISALLOW_COPY_AND_ASSIGN(TestPrefixDelegate
);
52 class PrefixSelectorTest
: public ViewsTestBase
{
54 PrefixSelectorTest() {
55 selector_
.reset(new PrefixSelector(&delegate_
));
59 scoped_ptr
<PrefixSelector
> selector_
;
60 TestPrefixDelegate delegate_
;
63 DISALLOW_COPY_AND_ASSIGN(PrefixSelectorTest
);
66 TEST_F(PrefixSelectorTest
, PrefixSelect
) {
67 selector_
->InsertText(ASCIIToUTF16("an"));
68 EXPECT_EQ(1, delegate_
.GetSelectedRow());
70 // Invoke OnViewBlur() to reset time.
71 selector_
->OnViewBlur();
72 selector_
->InsertText(ASCIIToUTF16("a"));
73 EXPECT_EQ(0, delegate_
.GetSelectedRow());
75 selector_
->OnViewBlur();
76 selector_
->InsertText(ASCIIToUTF16("g"));
77 EXPECT_EQ(3, delegate_
.GetSelectedRow());
79 selector_
->OnViewBlur();
80 selector_
->InsertText(ASCIIToUTF16("b"));
81 selector_
->InsertText(ASCIIToUTF16("a"));
82 EXPECT_EQ(2, delegate_
.GetSelectedRow());
84 selector_
->OnViewBlur();
85 selector_
->InsertText(ASCIIToUTF16("\t"));
86 selector_
->InsertText(ASCIIToUTF16("b"));
87 selector_
->InsertText(ASCIIToUTF16("a"));
88 EXPECT_EQ(2, delegate_
.GetSelectedRow());