Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / ui / views / controls / prefix_selector_unittest.cc
blobfe2651cc7e20cb2ba8ef1b7d2ce6f4053ffa393c
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"
7 #include <string>
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;
16 namespace views {
18 class 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"));
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]; }
37 private:
38 std::vector<base::string16> rows_;
39 int selected_row_;
41 DISALLOW_COPY_AND_ASSIGN(TestPrefixDelegate);
44 class PrefixSelectorTest : public ViewsTestBase {
45 public:
46 PrefixSelectorTest() {
47 selector_.reset(new PrefixSelector(&delegate_));
49 ~PrefixSelectorTest() override {
50 // Explicitly release |selector_| here which can happen before releasing
51 // |delegate_|.
52 selector_.reset();
55 protected:
56 scoped_ptr<PrefixSelector> selector_;
57 TestPrefixDelegate delegate_;
59 private:
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());
88 } // namespace views