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.
5 #include "chrome/browser/ui/views/autofill/autofill_dialog_views.h"
7 #include "base/basictypes.h"
8 #include "base/compiler_specific.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "chrome/browser/ui/autofill/mock_autofill_dialog_view_delegate.h"
11 #include "chrome/browser/ui/tabs/tab_strip_model.h"
12 #include "chrome/browser/ui/views/autofill/decorated_textfield.h"
13 #include "chrome/browser/ui/views/frame/browser_view.h"
14 #include "chrome/browser/ui/views/frame/test_with_browser_view.h"
15 #include "components/web_modal/test_web_contents_modal_dialog_host.h"
16 #include "components/web_modal/test_web_contents_modal_dialog_manager_delegate.h"
17 #include "components/web_modal/web_contents_modal_dialog_manager.h"
18 #include "content/public/common/url_constants.h"
19 #include "testing/gmock/include/gmock/gmock.h"
20 #include "testing/gtest/include/gtest/gtest.h"
21 #include "ui/views/controls/webview/webview.h"
22 #include "ui/views/widget/widget.h"
28 using testing::Return
;
29 using web_modal::WebContentsModalDialogManager
;
31 // A views implementation of the Autofill dialog with slightly more testability.
32 class TestAutofillDialogViews
: public AutofillDialogViews
{
34 explicit TestAutofillDialogViews(AutofillDialogViewDelegate
* delegate
)
35 : AutofillDialogViews(delegate
) {}
36 ~TestAutofillDialogViews() override
{}
38 using AutofillDialogViews::GetNotificationAreaForTesting
;
39 using AutofillDialogViews::GetScrollableAreaForTesting
;
42 DISALLOW_COPY_AND_ASSIGN(TestAutofillDialogViews
);
47 class AutofillDialogViewsTest
: public TestWithBrowserView
{
49 AutofillDialogViewsTest() {}
50 ~AutofillDialogViewsTest() override
{}
52 // TestWithBrowserView:
53 void SetUp() override
{
54 TestWithBrowserView::SetUp();
56 view_delegate_
.SetProfile(profile());
58 AddTab(browser(), GURL(url::kAboutBlankURL
));
59 TabStripModel
* tab_strip_model
= browser()->tab_strip_model();
60 content::WebContents
* contents
= tab_strip_model
->GetWebContentsAt(0);
61 ASSERT_TRUE(contents
);
62 view_delegate_
.SetWebContents(contents
);
64 BrowserView
* browser_view
=
65 BrowserView::GetBrowserViewForBrowser(browser());
66 dialog_host_
.reset(new web_modal::TestWebContentsModalDialogHost(
67 browser_view
->GetWidget()->GetNativeView()));
68 dialog_delegate_
.set_web_contents_modal_dialog_host(dialog_host_
.get());
70 WebContentsModalDialogManager
* dialog_manager
=
71 WebContentsModalDialogManager::FromWebContents(contents
);
72 ASSERT_TRUE(dialog_manager
);
73 dialog_manager
->SetDelegate(&dialog_delegate_
);
75 dialog_
.reset(new TestAutofillDialogViews(&view_delegate_
));
79 void TearDown() override
{
80 dialog_
->GetWidget()->CloseNow();
83 TestWithBrowserView::TearDown();
86 MockAutofillDialogViewDelegate
* delegate() { return &view_delegate_
; }
88 TestAutofillDialogViews
* dialog() { return dialog_
.get(); }
91 void SetSectionsFocusable() {
92 dialog()->GetNotificationAreaForTesting()->SetFocusable(true);
93 dialog()->GetScrollableAreaForTesting()->SetFocusable(true);
97 // Fake dialog delegate and host to isolate test behavior.
98 web_modal::TestWebContentsModalDialogManagerDelegate dialog_delegate_
;
99 scoped_ptr
<web_modal::TestWebContentsModalDialogHost
> dialog_host_
;
101 // Mock view delegate as this file only tests the view.
102 testing::NiceMock
<MockAutofillDialogViewDelegate
> view_delegate_
;
104 scoped_ptr
<TestAutofillDialogViews
> dialog_
;
107 TEST_F(AutofillDialogViewsTest
, InitialFocus
) {
108 views::FocusManager
* focus_manager
= dialog()->GetWidget()->GetFocusManager();
109 views::View
* focused_view
= focus_manager
->GetFocusedView();
110 EXPECT_STREQ(DecoratedTextfield::kViewClassName
,
111 focused_view
->GetClassName());
114 TEST_F(AutofillDialogViewsTest
, ImeEventDoesntCrash
) {
115 // IMEs create synthetic events with no backing native event.
116 views::FocusManager
* focus_manager
= dialog()->GetWidget()->GetFocusManager();
117 views::View
* focused_view
= focus_manager
->GetFocusedView();
118 ASSERT_STREQ(DecoratedTextfield::kViewClassName
,
119 focused_view
->GetClassName());
120 EXPECT_FALSE(dialog()->HandleKeyEvent(
121 static_cast<views::Textfield
*>(focused_view
),
122 ui::KeyEvent(ui::ET_KEY_PRESSED
, ui::VKEY_A
, ui::EF_NONE
)));
125 } // namespace autofill