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.
4
5#include "ui/views/controls/prefix_selector.h"
6
7#include <string>
8
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"
13
14using base::ASCIIToUTF16;
15
16namespace views {
17
18class TestPrefixDelegate : public PrefixDelegate {
19 public:
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"));
25  }
26
27  virtual ~TestPrefixDelegate() {}
28
29  virtual int GetRowCount() OVERRIDE {
30    return static_cast<int>(rows_.size());
31  }
32
33  virtual int GetSelectedRow() OVERRIDE {
34    return selected_row_;
35  }
36
37  virtual void SetSelectedRow(int row) OVERRIDE {
38    selected_row_ = row;
39  }
40
41  virtual base::string16 GetTextForRow(int row) OVERRIDE {
42    return rows_[row];
43  }
44
45 private:
46  std::vector<base::string16> rows_;
47  int selected_row_;
48
49  DISALLOW_COPY_AND_ASSIGN(TestPrefixDelegate);
50};
51
52class PrefixSelectorTest : public ViewsTestBase {
53 public:
54  PrefixSelectorTest() {
55    selector_.reset(new PrefixSelector(&delegate_));
56  }
57
58 protected:
59  scoped_ptr<PrefixSelector> selector_;
60  TestPrefixDelegate delegate_;
61
62 private:
63    DISALLOW_COPY_AND_ASSIGN(PrefixSelectorTest);
64};
65
66TEST_F(PrefixSelectorTest, PrefixSelect) {
67  selector_->InsertText(ASCIIToUTF16("an"));
68  EXPECT_EQ(1, delegate_.GetSelectedRow());
69
70  // Invoke OnViewBlur() to reset time.
71  selector_->OnViewBlur();
72  selector_->InsertText(ASCIIToUTF16("a"));
73  EXPECT_EQ(0, delegate_.GetSelectedRow());
74
75  selector_->OnViewBlur();
76  selector_->InsertText(ASCIIToUTF16("g"));
77  EXPECT_EQ(3, delegate_.GetSelectedRow());
78
79  selector_->OnViewBlur();
80  selector_->InsertText(ASCIIToUTF16("b"));
81  selector_->InsertText(ASCIIToUTF16("a"));
82  EXPECT_EQ(2, delegate_.GetSelectedRow());
83
84  selector_->OnViewBlur();
85  selector_->InsertText(ASCIIToUTF16("\t"));
86  selector_->InsertText(ASCIIToUTF16("b"));
87  selector_->InsertText(ASCIIToUTF16("a"));
88  EXPECT_EQ(2, delegate_.GetSelectedRow());
89}
90
91}  // namespace views
92