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