OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include "chrome/browser/ui/views/autofill/autofill_dialog_views.h" | |
6 | |
7 #include <memory> | |
8 | |
9 #include "base/compiler_specific.h" | |
10 #include "base/macros.h" | |
11 #include "chrome/browser/ui/autofill/mock_autofill_dialog_view_delegate.h" | |
12 #include "chrome/browser/ui/tabs/tab_strip_model.h" | |
13 #include "chrome/browser/ui/views/autofill/decorated_textfield.h" | |
14 #include "chrome/browser/ui/views/frame/browser_view.h" | |
15 #include "chrome/browser/ui/views/frame/test_with_browser_view.h" | |
16 #include "components/web_modal/test_web_contents_modal_dialog_host.h" | |
17 #include "components/web_modal/test_web_contents_modal_dialog_manager_delegate.h
" | |
18 #include "components/web_modal/web_contents_modal_dialog_manager.h" | |
19 #include "content/public/browser/native_web_keyboard_event.h" | |
20 #include "content/public/common/url_constants.h" | |
21 #include "testing/gmock/include/gmock/gmock.h" | |
22 #include "testing/gtest/include/gtest/gtest.h" | |
23 #include "ui/views/controls/webview/webview.h" | |
24 #include "ui/views/widget/widget.h" | |
25 | |
26 namespace autofill { | |
27 | |
28 namespace { | |
29 | |
30 using testing::Return; | |
31 using web_modal::WebContentsModalDialogManager; | |
32 | |
33 // A views implementation of the Autofill dialog with slightly more testability. | |
34 class TestAutofillDialogViews : public AutofillDialogViews { | |
35 public: | |
36 explicit TestAutofillDialogViews(AutofillDialogViewDelegate* delegate) | |
37 : AutofillDialogViews(delegate) {} | |
38 ~TestAutofillDialogViews() override {} | |
39 | |
40 using AutofillDialogViews::GetNotificationAreaForTesting; | |
41 using AutofillDialogViews::GetScrollableAreaForTesting; | |
42 | |
43 private: | |
44 DISALLOW_COPY_AND_ASSIGN(TestAutofillDialogViews); | |
45 }; | |
46 | |
47 } // namespace | |
48 | |
49 class AutofillDialogViewsTest : public TestWithBrowserView { | |
50 public: | |
51 AutofillDialogViewsTest() {} | |
52 ~AutofillDialogViewsTest() override {} | |
53 | |
54 // TestWithBrowserView: | |
55 void SetUp() override { | |
56 TestWithBrowserView::SetUp(); | |
57 | |
58 view_delegate_.SetProfile(profile()); | |
59 | |
60 AddTab(browser(), GURL(url::kAboutBlankURL)); | |
61 TabStripModel* tab_strip_model = browser()->tab_strip_model(); | |
62 content::WebContents* contents = tab_strip_model->GetWebContentsAt(0); | |
63 ASSERT_TRUE(contents); | |
64 view_delegate_.SetWebContents(contents); | |
65 | |
66 BrowserView* browser_view = | |
67 BrowserView::GetBrowserViewForBrowser(browser()); | |
68 dialog_host_.reset(new web_modal::TestWebContentsModalDialogHost( | |
69 browser_view->GetWidget()->GetNativeView())); | |
70 dialog_delegate_.set_web_contents_modal_dialog_host(dialog_host_.get()); | |
71 | |
72 WebContentsModalDialogManager* dialog_manager = | |
73 WebContentsModalDialogManager::FromWebContents(contents); | |
74 ASSERT_TRUE(dialog_manager); | |
75 dialog_manager->SetDelegate(&dialog_delegate_); | |
76 | |
77 dialog_.reset(new TestAutofillDialogViews(&view_delegate_)); | |
78 dialog_->Show(); | |
79 } | |
80 | |
81 void TearDown() override { | |
82 dialog_->GetWidget()->CloseNow(); | |
83 dialog_.reset(); | |
84 | |
85 TestWithBrowserView::TearDown(); | |
86 } | |
87 | |
88 MockAutofillDialogViewDelegate* delegate() { return &view_delegate_; } | |
89 | |
90 TestAutofillDialogViews* dialog() { return dialog_.get(); } | |
91 | |
92 protected: | |
93 void SetSectionsFocusable() { | |
94 dialog()->GetNotificationAreaForTesting()->SetFocusBehavior( | |
95 views::View::FocusBehavior::ALWAYS); | |
96 dialog()->GetScrollableAreaForTesting()->SetFocusBehavior( | |
97 views::View::FocusBehavior::ALWAYS); | |
98 } | |
99 | |
100 private: | |
101 // Fake dialog delegate and host to isolate test behavior. | |
102 web_modal::TestWebContentsModalDialogManagerDelegate dialog_delegate_; | |
103 std::unique_ptr<web_modal::TestWebContentsModalDialogHost> dialog_host_; | |
104 | |
105 // Mock view delegate as this file only tests the view. | |
106 testing::NiceMock<MockAutofillDialogViewDelegate> view_delegate_; | |
107 | |
108 std::unique_ptr<TestAutofillDialogViews> dialog_; | |
109 }; | |
110 | |
111 TEST_F(AutofillDialogViewsTest, InitialFocus) { | |
112 views::FocusManager* focus_manager = dialog()->GetWidget()->GetFocusManager(); | |
113 views::View* focused_view = focus_manager->GetFocusedView(); | |
114 EXPECT_STREQ(DecoratedTextfield::kViewClassName, | |
115 focused_view->GetClassName()); | |
116 } | |
117 | |
118 TEST_F(AutofillDialogViewsTest, ImeEventDoesntCrash) { | |
119 // IMEs create synthetic events with no backing native event. | |
120 views::FocusManager* focus_manager = dialog()->GetWidget()->GetFocusManager(); | |
121 views::View* focused_view = focus_manager->GetFocusedView(); | |
122 ASSERT_STREQ(DecoratedTextfield::kViewClassName, | |
123 focused_view->GetClassName()); | |
124 EXPECT_FALSE(dialog()->HandleKeyEvent( | |
125 static_cast<views::Textfield*>(focused_view), | |
126 ui::KeyEvent(ui::ET_KEY_PRESSED, ui::VKEY_A, ui::EF_NONE))); | |
127 } | |
128 | |
129 } // namespace autofill | |
OLD | NEW |