OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/autofill/autofill_popup_view.h" | 5 #include "chrome/browser/autofill/autofill_popup_view.h" |
6 | 6 |
7 #include "base/memory/scoped_ptr.h" | 7 #include "base/memory/scoped_ptr.h" |
| 8 #include "chrome/browser/autofill/test_autofill_external_delegate.h" |
8 #include "chrome/browser/ui/browser.h" | 9 #include "chrome/browser/ui/browser.h" |
9 #include "chrome/test/base/ui_test_utils.h" | 10 #include "chrome/test/base/ui_test_utils.h" |
10 #include "content/public/browser/navigation_controller.h" | 11 #include "content/public/browser/navigation_controller.h" |
11 #include "content/public/browser/notification_service.h" | 12 #include "content/public/browser/notification_service.h" |
12 #include "content/public/browser/notification_types.h" | 13 #include "content/public/browser/notification_types.h" |
13 #include "content/public/browser/page_navigator.h" | 14 #include "content/public/browser/page_navigator.h" |
14 #include "content/public/browser/web_contents.h" | 15 #include "content/public/browser/web_contents.h" |
15 #include "content/public/common/url_constants.h" | 16 #include "content/public/common/url_constants.h" |
16 #include "content/test/browser_test.h" | 17 #include "content/test/browser_test.h" |
17 #include "testing/gmock/include/gmock/gmock.h" | 18 #include "testing/gmock/include/gmock/gmock.h" |
18 #include "testing/gtest/include/gtest/gtest.h" | 19 #include "testing/gtest/include/gtest/gtest.h" |
19 | 20 |
20 using ::testing::AtLeast; | 21 using ::testing::AtLeast; |
| 22 using testing::_; |
| 23 |
| 24 namespace { |
| 25 |
| 26 class MockAutofillExternalDelegate : public TestAutofillExternalDelegate { |
| 27 public: |
| 28 MockAutofillExternalDelegate() : TestAutofillExternalDelegate(NULL, NULL) {} |
| 29 ~MockAutofillExternalDelegate() {} |
| 30 |
| 31 virtual void SelectAutofillSuggestionAtIndex(int unique_id, int list_index) |
| 32 OVERRIDE {} |
| 33 }; |
21 | 34 |
22 class TestAutofillPopupView : public AutofillPopupView { | 35 class TestAutofillPopupView : public AutofillPopupView { |
23 public: | 36 public: |
24 explicit TestAutofillPopupView(content::WebContents* web_contents) | 37 explicit TestAutofillPopupView( |
25 : AutofillPopupView(web_contents) {} | 38 content::WebContents* web_contents, |
| 39 AutofillExternalDelegate* autofill_external_delegate) |
| 40 : AutofillPopupView(web_contents, autofill_external_delegate) {} |
26 virtual ~TestAutofillPopupView() {} | 41 virtual ~TestAutofillPopupView() {} |
27 | 42 |
28 MOCK_METHOD0(Hide, void()); | 43 MOCK_METHOD0(Hide, void()); |
29 | 44 |
| 45 MOCK_METHOD1(InvalidateRow, void(size_t)); |
| 46 |
| 47 void SetSelectedLine(size_t selected_line) { |
| 48 AutofillPopupView::SetSelectedLine(selected_line); |
| 49 } |
| 50 |
| 51 protected: |
30 virtual void ShowInternal() OVERRIDE {} | 52 virtual void ShowInternal() OVERRIDE {} |
| 53 |
| 54 virtual void HideInternal() OVERRIDE {} |
31 }; | 55 }; |
32 | 56 |
| 57 } // namespace |
| 58 |
33 class AutofillPopupViewBrowserTest : public InProcessBrowserTest { | 59 class AutofillPopupViewBrowserTest : public InProcessBrowserTest { |
34 public: | 60 public: |
35 AutofillPopupViewBrowserTest() {} | 61 AutofillPopupViewBrowserTest() {} |
36 virtual ~AutofillPopupViewBrowserTest() {} | 62 virtual ~AutofillPopupViewBrowserTest() {} |
37 | 63 |
| 64 virtual void SetUpOnMainThread() OVERRIDE { |
| 65 web_contents_ = browser()->GetSelectedWebContents(); |
| 66 ASSERT_TRUE(web_contents_ != NULL); |
| 67 |
| 68 autofill_popup_view_.reset(new TestAutofillPopupView( |
| 69 web_contents_, |
| 70 &autofill_external_delegate_)); |
| 71 } |
| 72 |
38 protected: | 73 protected: |
| 74 content::WebContents* web_contents_; |
39 scoped_ptr<TestAutofillPopupView> autofill_popup_view_; | 75 scoped_ptr<TestAutofillPopupView> autofill_popup_view_; |
| 76 MockAutofillExternalDelegate autofill_external_delegate_; |
40 }; | 77 }; |
41 | 78 |
42 IN_PROC_BROWSER_TEST_F(InProcessBrowserTest, SwitchTabAndHideAutofillPopup) { | 79 IN_PROC_BROWSER_TEST_F(AutofillPopupViewBrowserTest, |
43 content::WebContents* web_contents = browser()->GetSelectedWebContents(); | 80 SwitchTabAndHideAutofillPopup) { |
44 TestAutofillPopupView autofill_popup_view(web_contents); | 81 EXPECT_CALL(*autofill_popup_view_, Hide()).Times(AtLeast(1)); |
45 | |
46 // Using AtLeast here because current Hide is called once on Linux and Mac, | |
47 // and three times on Windows and ChromeOS. http://crbug.com/109269 | |
48 EXPECT_CALL(autofill_popup_view, Hide()).Times(AtLeast(1)); | |
49 | 82 |
50 ui_test_utils::WindowedNotificationObserver observer( | 83 ui_test_utils::WindowedNotificationObserver observer( |
51 content::NOTIFICATION_WEB_CONTENTS_HIDDEN, | 84 content::NOTIFICATION_WEB_CONTENTS_HIDDEN, |
52 content::Source<content::WebContents>(web_contents)); | 85 content::Source<content::WebContents>(web_contents_)); |
53 browser()->AddSelectedTabWithURL(GURL(chrome::kAboutBlankURL), | 86 browser()->AddSelectedTabWithURL(GURL(chrome::kAboutBlankURL), |
54 content::PAGE_TRANSITION_START_PAGE); | 87 content::PAGE_TRANSITION_START_PAGE); |
55 observer.Wait(); | 88 observer.Wait(); |
56 | 89 |
57 // The mock verifies that the call was made. | 90 // The mock verifies that the call was made. |
58 } | 91 } |
59 | 92 |
60 IN_PROC_BROWSER_TEST_F(InProcessBrowserTest, | 93 IN_PROC_BROWSER_TEST_F(AutofillPopupViewBrowserTest, |
61 TestPageNavigationHidingAutofillPopup) { | 94 TestPageNavigationHidingAutofillPopup) { |
62 content::WebContents* web_contents = browser()->GetSelectedWebContents(); | 95 EXPECT_CALL(*autofill_popup_view_, Hide()).Times(AtLeast(1)); |
63 TestAutofillPopupView autofill_popup_view(web_contents); | |
64 EXPECT_CALL(autofill_popup_view, Hide()); | |
65 | 96 |
66 ui_test_utils::WindowedNotificationObserver observer( | 97 ui_test_utils::WindowedNotificationObserver observer( |
67 content::NOTIFICATION_NAV_ENTRY_COMMITTED, | 98 content::NOTIFICATION_NAV_ENTRY_COMMITTED, |
68 content::Source<content::NavigationController>( | 99 content::Source<content::NavigationController>( |
69 &(web_contents->GetController()))); | 100 &(web_contents_->GetController()))); |
70 browser()->OpenURL(content::OpenURLParams( | 101 browser()->OpenURL(content::OpenURLParams( |
71 GURL(chrome::kAboutBlankURL), content::Referrer(), | 102 GURL(chrome::kAboutBlankURL), content::Referrer(), |
72 CURRENT_TAB, content::PAGE_TRANSITION_TYPED, false)); | 103 CURRENT_TAB, content::PAGE_TRANSITION_TYPED, false)); |
73 browser()->OpenURL(content::OpenURLParams( | 104 browser()->OpenURL(content::OpenURLParams( |
74 GURL(chrome::kAboutCrashURL), content::Referrer(), | 105 GURL(chrome::kAboutCrashURL), content::Referrer(), |
75 CURRENT_TAB, content::PAGE_TRANSITION_TYPED, false)); | 106 CURRENT_TAB, content::PAGE_TRANSITION_TYPED, false)); |
76 observer.Wait(); | 107 observer.Wait(); |
77 | 108 |
78 // The mock verifies that the call was made. | 109 // The mock verifies that the call was made. |
79 } | 110 } |
| 111 |
| 112 IN_PROC_BROWSER_TEST_F(AutofillPopupViewBrowserTest, |
| 113 SetSelectedAutofillLineAndCallInvalidate) { |
| 114 std::vector<string16> autofill_values; |
| 115 autofill_values.push_back(string16()); |
| 116 std::vector<int> autofill_ids; |
| 117 autofill_ids.push_back(0); |
| 118 autofill_popup_view_->Show( |
| 119 autofill_values, autofill_values, autofill_values, autofill_ids, 0); |
| 120 |
| 121 // Make sure that when a new line is selected, it is invalidated so it can |
| 122 // be updated to show it is selected. |
| 123 int selected_line = 0; |
| 124 EXPECT_CALL(*autofill_popup_view_, InvalidateRow(selected_line)); |
| 125 autofill_popup_view_->SetSelectedLine(selected_line); |
| 126 |
| 127 // Ensure that the row isn't invalidated if it didn't change. |
| 128 EXPECT_CALL(*autofill_popup_view_, InvalidateRow(selected_line)).Times(0); |
| 129 autofill_popup_view_->SetSelectedLine(selected_line); |
| 130 |
| 131 // Change back to no selection. |
| 132 EXPECT_CALL(*autofill_popup_view_, InvalidateRow(selected_line)); |
| 133 autofill_popup_view_->SetSelectedLine(-1); |
| 134 } |
OLD | NEW |