| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2012 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 "base/memory/scoped_ptr.h" |
| 6 #include "chrome/browser/autofill/autofill_popup_view.h" |
| 7 #include "chrome/browser/autofill/test_autofill_external_delegate.h" |
| 8 #include "testing/gtest/include/gtest/gtest.h" |
| 9 #include "testing/gmock/include/gmock/gmock.h" |
| 10 |
| 11 namespace { |
| 12 |
| 13 class MockAutofillExternalDelegate : public TestAutofillExternalDelegate { |
| 14 public: |
| 15 MockAutofillExternalDelegate() : TestAutofillExternalDelegate(NULL, NULL) {}; |
| 16 virtual ~MockAutofillExternalDelegate() {}; |
| 17 |
| 18 virtual void SelectAutofillSuggestionAtIndex(int unique_id, int list_index) |
| 19 OVERRIDE {} |
| 20 }; |
| 21 |
| 22 class TestAutofillPopupView : public AutofillPopupView { |
| 23 public: |
| 24 explicit TestAutofillPopupView(AutofillExternalDelegate* external_delegate) : |
| 25 AutofillPopupView(NULL, external_delegate) { |
| 26 std::vector<string16> autofill_values; |
| 27 autofill_values.push_back(string16()); |
| 28 autofill_values.push_back(string16()); |
| 29 |
| 30 std::vector<int> autofill_ids; |
| 31 autofill_ids.push_back(0); |
| 32 autofill_ids.push_back(1); |
| 33 |
| 34 Show(autofill_values, autofill_values, autofill_values, autofill_ids, 0); |
| 35 } |
| 36 virtual ~TestAutofillPopupView() {} |
| 37 |
| 38 // Making protected functions public for testing |
| 39 const std::vector<string16>& autofill_values() const { |
| 40 return AutofillPopupView::autofill_values(); |
| 41 } |
| 42 int selected_line() const { |
| 43 return AutofillPopupView::selected_line(); |
| 44 } |
| 45 void SetSelectedLine(size_t selected_line) { |
| 46 AutofillPopupView::SetSelectedLine(selected_line); |
| 47 } |
| 48 void SelectNextLine() { |
| 49 AutofillPopupView::SelectNextLine(); |
| 50 } |
| 51 void SelectPreviousLine() { |
| 52 AutofillPopupView::SelectPreviousLine(); |
| 53 } |
| 54 |
| 55 MOCK_METHOD1(InvalidateRow, void(size_t)); |
| 56 |
| 57 private: |
| 58 virtual void ShowInternal() OVERRIDE {} |
| 59 virtual void HideInternal() OVERRIDE {} |
| 60 }; |
| 61 |
| 62 } // namespace |
| 63 |
| 64 class AutofillPopupViewUnitTest : public ::testing::Test { |
| 65 public: |
| 66 AutofillPopupViewUnitTest() { |
| 67 autofill_popup_view_.reset(new TestAutofillPopupView(&external_delegate_)); |
| 68 } |
| 69 virtual ~AutofillPopupViewUnitTest() {} |
| 70 |
| 71 scoped_ptr<TestAutofillPopupView> autofill_popup_view_; |
| 72 |
| 73 private: |
| 74 MockAutofillExternalDelegate external_delegate_; |
| 75 }; |
| 76 |
| 77 TEST_F(AutofillPopupViewUnitTest, ChangeSelectedLine) { |
| 78 EXPECT_LT(autofill_popup_view_->selected_line(), 0); |
| 79 // Check that there are at least 2 values so that the first and last selection |
| 80 // are different. |
| 81 EXPECT_GE(2, |
| 82 static_cast<int>(autofill_popup_view_->autofill_values().size())); |
| 83 |
| 84 // Test wrapping before the front. |
| 85 autofill_popup_view_->SelectPreviousLine(); |
| 86 EXPECT_EQ( |
| 87 static_cast<int>(autofill_popup_view_->autofill_values().size() - 1), |
| 88 autofill_popup_view_->selected_line()); |
| 89 |
| 90 // Test wrapping after the end. |
| 91 autofill_popup_view_->SelectNextLine(); |
| 92 EXPECT_EQ(0, autofill_popup_view_->selected_line()); |
| 93 } |
| 94 |
| 95 TEST_F(AutofillPopupViewUnitTest, RedrawSelectedLine) { |
| 96 // Make sure that when a new line is selected, it is invalidated so it can |
| 97 // be updated to show it is selected. |
| 98 int selected_line = 0; |
| 99 EXPECT_CALL(*autofill_popup_view_, InvalidateRow(selected_line)); |
| 100 autofill_popup_view_->SetSelectedLine(selected_line); |
| 101 |
| 102 // Ensure that the row isn't invalidated if it didn't change. |
| 103 EXPECT_CALL(*autofill_popup_view_, InvalidateRow(selected_line)).Times(0); |
| 104 autofill_popup_view_->SetSelectedLine(selected_line); |
| 105 |
| 106 // Change back to no selection. |
| 107 EXPECT_CALL(*autofill_popup_view_, InvalidateRow(selected_line)); |
| 108 autofill_popup_view_->SetSelectedLine(-1); |
| 109 } |
| OLD | NEW |