autofill_dialog_views_unittest.cc revision 8bcbed890bc3ce4d7a057a8f32cab53fa534672e
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 "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/frame/browser_view.h" 13#include "chrome/browser/ui/views/frame/test_with_browser_view.h" 14#include "components/web_modal/test_web_contents_modal_dialog_host.h" 15#include "components/web_modal/test_web_contents_modal_dialog_manager_delegate.h" 16#include "components/web_modal/web_contents_modal_dialog_manager.h" 17#include "testing/gmock/include/gmock/gmock.h" 18#include "testing/gtest/include/gtest/gtest.h" 19#include "ui/views/controls/webview/webview.h" 20#include "ui/views/widget/widget.h" 21 22namespace autofill { 23 24namespace { 25 26using web_modal::WebContentsModalDialogManager; 27 28// A views implementation of the Autofill dialog with slightly more testability. 29class TestAutofillDialogViews : public AutofillDialogViews { 30 public: 31 explicit TestAutofillDialogViews(AutofillDialogViewDelegate* delegate) 32 : AutofillDialogViews(delegate) {} 33 virtual ~TestAutofillDialogViews() {} 34 35 void ShowLoadingMode() { ShowDialogInMode(LOADING); } 36 void ShowSignInMode() { ShowDialogInMode(SIGN_IN); } 37 void ShowDetailInputMode() { ShowDialogInMode(DETAIL_INPUT); } 38 39 using AutofillDialogViews::GetLoadingShieldForTesting; 40 using AutofillDialogViews::GetSignInWebViewForTesting; 41 using AutofillDialogViews::GetNotificationAreaForTesting; 42 using AutofillDialogViews::GetScrollableAreaForTesting; 43 44 private: 45 DISALLOW_COPY_AND_ASSIGN(TestAutofillDialogViews); 46}; 47 48} // namespace 49 50class AutofillDialogViewsTest : public TestWithBrowserView { 51 public: 52 AutofillDialogViewsTest() {} 53 virtual ~AutofillDialogViewsTest() {} 54 55 // TestWithBrowserView: 56 virtual void SetUp() OVERRIDE { 57 TestWithBrowserView::SetUp(); 58 59 AddTab(browser(), GURL()); 60 TabStripModel* tab_strip_model = browser()->tab_strip_model(); 61 content::WebContents* contents = tab_strip_model->GetWebContentsAt(0); 62 ASSERT_TRUE(contents); 63 view_delegate_.SetWebContents(contents); 64 65 BrowserView* browser_view = 66 BrowserView::GetBrowserViewForBrowser(browser()); 67 dialog_host_.reset(new web_modal::TestWebContentsModalDialogHost( 68 browser_view->GetWidget()->GetNativeView())); 69 dialog_delegate_.set_web_contents_modal_dialog_host(dialog_host_.get()); 70 71 WebContentsModalDialogManager* dialog_manager = 72 WebContentsModalDialogManager::FromWebContents(contents); 73 ASSERT_TRUE(dialog_manager); 74 dialog_manager->SetDelegate(&dialog_delegate_); 75 76 dialog_.reset(new TestAutofillDialogViews(&view_delegate_)); 77 dialog_->Show(); 78 } 79 80 virtual void TearDown() OVERRIDE { 81 dialog_->GetWidget()->CloseNow(); 82 83 TestWithBrowserView::TearDown(); 84 } 85 86 TestAutofillDialogViews* dialog() { return dialog_.get(); } 87 88 protected: 89 // Allows each main section (e.g. loading shield, sign in web view, 90 // notification area, and scrollable area) to get focus. Used in focus-related 91 // tests. 92 void SetSectionsFocusable() { 93 dialog()->GetLoadingShieldForTesting()->set_focusable(true); 94 // The sign in web view has a different implementation of IsFocusable(). 95 dialog()->GetNotificationAreaForTesting()->set_focusable(true); 96 dialog()->GetScrollableAreaForTesting()->set_focusable(true); 97 } 98 99 private: 100 // Fake dialog delegate and host to isolate test behavior. 101 web_modal::TestWebContentsModalDialogManagerDelegate dialog_delegate_; 102 scoped_ptr<web_modal::TestWebContentsModalDialogHost> dialog_host_; 103 104 // Mock view delegate as this file only tests the view. 105 testing::NiceMock<MockAutofillDialogViewDelegate> view_delegate_; 106 107 scoped_ptr<TestAutofillDialogViews> dialog_; 108}; 109 110TEST_F(AutofillDialogViewsTest, ShowLoadingMode) { 111 SetSectionsFocusable(); 112 113 dialog()->ShowLoadingMode(); 114 115 views::View* loading_shield = dialog()->GetLoadingShieldForTesting(); 116 EXPECT_TRUE(loading_shield->visible()); 117 EXPECT_TRUE(loading_shield->IsFocusable()); 118 119 loading_shield->RequestFocus(); 120 EXPECT_TRUE(loading_shield->HasFocus()); 121 122 views::View* sign_in_web_view = dialog()->GetSignInWebViewForTesting(); 123 EXPECT_FALSE(sign_in_web_view->visible()); 124 EXPECT_FALSE(sign_in_web_view->IsFocusable()); 125 126 views::View* notification_area = dialog()->GetNotificationAreaForTesting(); 127 EXPECT_FALSE(notification_area->visible()); 128 EXPECT_FALSE(notification_area->IsFocusable()); 129 130 views::View* scrollable_area = dialog()->GetScrollableAreaForTesting(); 131 EXPECT_FALSE(scrollable_area->visible()); 132 EXPECT_FALSE(scrollable_area->IsFocusable()); 133} 134 135TEST_F(AutofillDialogViewsTest, ShowSignInMode) { 136 SetSectionsFocusable(); 137 138 dialog()->ShowSignInMode(); 139 140 views::View* loading_shield = dialog()->GetLoadingShieldForTesting(); 141 EXPECT_FALSE(loading_shield->visible()); 142 EXPECT_FALSE(loading_shield->IsFocusable()); 143 144 views::View* sign_in_web_view = dialog()->GetSignInWebViewForTesting(); 145 EXPECT_TRUE(sign_in_web_view->visible()); 146 // NOTE: |sign_in_web_view| is not focusable until a web contents is created. 147 // TODO(dbeam): figure out how to create a web contents on the right thread. 148 149 views::View* notification_area = dialog()->GetNotificationAreaForTesting(); 150 EXPECT_FALSE(notification_area->visible()); 151 EXPECT_FALSE(notification_area->IsFocusable()); 152 153 views::View* scrollable_area = dialog()->GetScrollableAreaForTesting(); 154 EXPECT_FALSE(scrollable_area->visible()); 155 EXPECT_FALSE(scrollable_area->IsFocusable()); 156} 157 158TEST_F(AutofillDialogViewsTest, ShowDetailInputMode) { 159 SetSectionsFocusable(); 160 161 dialog()->ShowDetailInputMode(); 162 163 views::View* loading_shield = dialog()->GetLoadingShieldForTesting(); 164 EXPECT_FALSE(loading_shield->visible()); 165 EXPECT_FALSE(loading_shield->IsFocusable()); 166 167 views::View* sign_in_web_view = dialog()->GetSignInWebViewForTesting(); 168 EXPECT_FALSE(sign_in_web_view->visible()); 169 EXPECT_FALSE(sign_in_web_view->IsFocusable()); 170 171 views::View* notification_area = dialog()->GetNotificationAreaForTesting(); 172 EXPECT_TRUE(notification_area->visible()); 173 EXPECT_TRUE(notification_area->IsFocusable()); 174 175 views::View* scrollable_area = dialog()->GetScrollableAreaForTesting(); 176 EXPECT_TRUE(scrollable_area->visible()); 177 EXPECT_TRUE(scrollable_area->IsFocusable()); 178 179 notification_area->RequestFocus(); 180 EXPECT_TRUE(notification_area->HasFocus()); 181 182 scrollable_area->RequestFocus(); 183 EXPECT_TRUE(scrollable_area->HasFocus()); 184} 185 186} // namespace autofill 187