summaryrefslogtreecommitdiffstats
path: root/chrome/browser/autofill/autofill_popup_unittest.cc
blob: ea3a97281570a22b9a69102b1096ddcf1cf90822 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/memory/scoped_ptr.h"
#include "chrome/browser/autofill/autofill_popup_view.h"
#include "chrome/browser/autofill/test_autofill_external_delegate.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/WebAutofillClient.h"

using ::testing::_;
using ::testing::AtLeast;
using WebKit::WebAutofillClient;

namespace {

class MockAutofillExternalDelegate : public TestAutofillExternalDelegate {
 public:
  MockAutofillExternalDelegate() : TestAutofillExternalDelegate(NULL, NULL) {};
  virtual ~MockAutofillExternalDelegate() {};

  virtual void SelectAutofillSuggestionAtIndex(int unique_id)
      OVERRIDE {}
  virtual void RemoveAutocompleteEntry(const string16& value) OVERRIDE {}
  virtual void RemoveAutofillProfileOrCreditCard(int unique_id) OVERRIDE {}
  virtual void ClearPreviewedForm() OVERRIDE {}
};

class TestAutofillPopupView : public AutofillPopupView {
 public:
  explicit TestAutofillPopupView(AutofillExternalDelegate* external_delegate) :
      AutofillPopupView(NULL, external_delegate) {}
  virtual ~TestAutofillPopupView() {}

  // Making protected functions public for testing
  const std::vector<string16>& autofill_values() const {
    return AutofillPopupView::autofill_values();
  }
  int selected_line() const {
    return AutofillPopupView::selected_line();
  }
  void SetSelectedLine(size_t selected_line) {
    AutofillPopupView::SetSelectedLine(selected_line);
  }
  void SelectNextLine() {
    AutofillPopupView::SelectNextLine();
  }
  void SelectPreviousLine() {
    AutofillPopupView::SelectPreviousLine();
  }
  bool RemoveSelectedLine() {
    return AutofillPopupView::RemoveSelectedLine();
  }

  MOCK_METHOD1(InvalidateRow, void(size_t));
  MOCK_METHOD0(HideInternal, void());

 private:
  virtual void ShowInternal() OVERRIDE {}

  virtual void ResizePopup() OVERRIDE {}
};

}  // namespace

class AutofillPopupViewUnitTest : public ::testing::Test {
 public:
  AutofillPopupViewUnitTest() {
    autofill_popup_view_.reset(new TestAutofillPopupView(&external_delegate_));
  }
  virtual ~AutofillPopupViewUnitTest() {}

  scoped_ptr<TestAutofillPopupView> autofill_popup_view_;

 private:
  MockAutofillExternalDelegate external_delegate_;
};

TEST_F(AutofillPopupViewUnitTest, ChangeSelectedLine) {
  // Set up the popup.
  std::vector<string16> autofill_values(2, string16());
  std::vector<int> autofill_ids(2, 0);
  autofill_popup_view_->Show(autofill_values, autofill_values, autofill_values,
                             autofill_ids);

  // To remove warnings.
  EXPECT_CALL(*autofill_popup_view_, InvalidateRow(_)).Times(AtLeast(0));

  EXPECT_LT(autofill_popup_view_->selected_line(), 0);
  // Check that there are at least 2 values so that the first and last selection
  // are different.
  EXPECT_GE(2,
            static_cast<int>(autofill_popup_view_->autofill_values().size()));

  // Test wrapping before the front.
  autofill_popup_view_->SelectPreviousLine();
  EXPECT_EQ(
      static_cast<int>(autofill_popup_view_->autofill_values().size() - 1),
      autofill_popup_view_->selected_line());

  // Test wrapping after the end.
  autofill_popup_view_->SelectNextLine();
  EXPECT_EQ(0, autofill_popup_view_->selected_line());
}

TEST_F(AutofillPopupViewUnitTest, RedrawSelectedLine) {
  // Set up the popup.
  std::vector<string16> autofill_values(2, string16());
  std::vector<int> autofill_ids(2, 0);
  autofill_popup_view_->Show(autofill_values, autofill_values, autofill_values,
                             autofill_ids);

  // Make sure that when a new line is selected, it is invalidated so it can
  // be updated to show it is selected.
  int selected_line = 0;
  EXPECT_CALL(*autofill_popup_view_, InvalidateRow(selected_line));
  autofill_popup_view_->SetSelectedLine(selected_line);

  // Ensure that the row isn't invalidated if it didn't change.
  EXPECT_CALL(*autofill_popup_view_, InvalidateRow(selected_line)).Times(0);
  autofill_popup_view_->SetSelectedLine(selected_line);

  // Change back to no selection.
  EXPECT_CALL(*autofill_popup_view_, InvalidateRow(selected_line));
  autofill_popup_view_->SetSelectedLine(-1);
}

TEST_F(AutofillPopupViewUnitTest, RemoveLine) {
  // Set up the popup.
  std::vector<string16> autofill_values(2, string16());
  std::vector<int> autofill_ids;
  autofill_ids.push_back(1);
  autofill_ids.push_back(WebAutofillClient::MenuItemIDAutofillOptions);
  autofill_popup_view_->Show(autofill_values, autofill_values, autofill_values,
                             autofill_ids);

  // To remove warnings.
  EXPECT_CALL(*autofill_popup_view_, InvalidateRow(_)).Times(AtLeast(0));

  // No line is selected so the removal should fail.
  EXPECT_FALSE(autofill_popup_view_->RemoveSelectedLine());

  // Try to remove the last entry and ensure it fails (it is an option).
  autofill_popup_view_->SetSelectedLine(
      autofill_popup_view_->autofill_values().size() - 1);
  EXPECT_FALSE(autofill_popup_view_->RemoveSelectedLine());
  EXPECT_LE(0, autofill_popup_view_->selected_line());

  // Remove the first (and only) entry. The popup should then be hidden since
  // there are no Autofill entries left.
  EXPECT_CALL(*autofill_popup_view_, HideInternal());
  autofill_popup_view_->SetSelectedLine(0);
  EXPECT_TRUE(autofill_popup_view_->RemoveSelectedLine());
}