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 "ios/chrome/browser/ui/authentication/re_signin_infobar_delegate.h" |
| 6 |
| 7 #include <memory> |
| 8 |
| 9 #include "base/mac/objc_property_releaser.h" |
| 10 #include "base/mac/scoped_nsobject.h" |
| 11 #include "base/memory/ptr_util.h" |
| 12 #include "components/sync_preferences/testing_pref_service_syncable.h" |
| 13 #include "ios/chrome/browser/browser_state/test_chrome_browser_state.h" |
| 14 #include "ios/chrome/browser/infobars/confirm_infobar_controller.h" |
| 15 #include "ios/chrome/browser/infobars/infobar.h" |
| 16 #include "ios/chrome/browser/infobars/infobar_utils.h" |
| 17 #include "ios/chrome/browser/signin/authentication_service.h" |
| 18 #include "ios/chrome/browser/signin/authentication_service_factory.h" |
| 19 #include "ios/chrome/browser/signin/authentication_service_fake.h" |
| 20 #include "ios/chrome/browser/ui/commands/UIKit+ChromeExecuteCommand.h" |
| 21 #import "ios/chrome/browser/ui/commands/show_signin_command.h" |
| 22 #include "ios/public/provider/chrome/browser/signin/fake_chrome_identity.h" |
| 23 #include "ios/web/public/test/test_web_thread_bundle.h" |
| 24 #include "testing/gtest_mac.h" |
| 25 #include "testing/platform_test.h" |
| 26 |
| 27 // View that intercepts and stores chrome commands sent up the responder chain. |
| 28 @interface CatchExecuteCommandView : UIView { |
| 29 base::mac::ObjCPropertyReleaser propertyReleaser_CatchExecuteCommandView_; |
| 30 } |
| 31 // Command sent up the responder chain and intercepted by this view. |
| 32 @property(nonatomic, retain) id command; |
| 33 @end |
| 34 |
| 35 @implementation CatchExecuteCommandView |
| 36 |
| 37 @synthesize command = _command; |
| 38 |
| 39 - (instancetype)initWithFrame:(CGRect)frame { |
| 40 self = [super initWithFrame:frame]; |
| 41 if (self) { |
| 42 propertyReleaser_CatchExecuteCommandView_.Init( |
| 43 self, [CatchExecuteCommandView class]); |
| 44 } |
| 45 return self; |
| 46 } |
| 47 |
| 48 - (void)chromeExecuteCommand:(id)command { |
| 49 DCHECK(command); |
| 50 DCHECK(!self.command); |
| 51 self.command = command; |
| 52 } |
| 53 |
| 54 @end |
| 55 |
| 56 namespace { |
| 57 |
| 58 class ReSignInInfoBarDelegateTest : public PlatformTest { |
| 59 public: |
| 60 ReSignInInfoBarDelegateTest() {} |
| 61 |
| 62 protected: |
| 63 void SetUp() override { |
| 64 mainBrowserStateBuilder_.AddTestingFactory( |
| 65 AuthenticationServiceFactory::GetInstance(), |
| 66 AuthenticationServiceFake::CreateAuthenticationService); |
| 67 } |
| 68 |
| 69 void SetUpMainChromeBrowserStateNotSignedIn() { |
| 70 chrome_browser_state_ = mainBrowserStateBuilder_.Build(); |
| 71 } |
| 72 |
| 73 void SetUpMainChromeBrowserStateWithSignedInUser() { |
| 74 ChromeIdentity* chrome_identity = |
| 75 [FakeChromeIdentity identityWithEmail:@"john.appleseed@gmail.com" |
| 76 gaiaID:@"1234" |
| 77 name:@"John"]; |
| 78 |
| 79 chrome_browser_state_ = mainBrowserStateBuilder_.Build(); |
| 80 AuthenticationService* authService = |
| 81 AuthenticationServiceFactory::GetForBrowserState( |
| 82 chrome_browser_state_.get()); |
| 83 authService->SignIn(chrome_identity, std::string()); |
| 84 } |
| 85 |
| 86 void SetUpMainChromeBrowserStateWithIncognito() { |
| 87 SetUpMainChromeBrowserStateNotSignedIn(); |
| 88 // Sets up an incognito ChromeBrowserState and attaches it to the main one. |
| 89 TestChromeBrowserState::Builder test_cbs_builder; |
| 90 chrome_browser_state_ = test_cbs_builder.Build(); |
| 91 } |
| 92 |
| 93 web::TestWebThreadBundle thread_bundle_; |
| 94 |
| 95 TestChromeBrowserState::Builder mainBrowserStateBuilder_; |
| 96 std::unique_ptr<TestChromeBrowserState> chrome_browser_state_; |
| 97 }; |
| 98 |
| 99 TEST_F(ReSignInInfoBarDelegateTest, TestCreateWhenNotPrompting) { |
| 100 // User is not signed in, but the "prompt" flag is not set. |
| 101 SetUpMainChromeBrowserStateNotSignedIn(); |
| 102 AuthenticationService* authService = |
| 103 AuthenticationServiceFactory::GetForBrowserState( |
| 104 chrome_browser_state_.get()); |
| 105 authService->SetPromptForSignIn(false); |
| 106 std::unique_ptr<ReSignInInfoBarDelegate> infobar_delegate = |
| 107 ReSignInInfoBarDelegate::CreateInfoBarDelegate( |
| 108 chrome_browser_state_.get()); |
| 109 // Infobar delegate should not be created. |
| 110 EXPECT_FALSE(infobar_delegate.get()); |
| 111 EXPECT_FALSE(authService->ShouldPromptForSignIn()); |
| 112 } |
| 113 |
| 114 TEST_F(ReSignInInfoBarDelegateTest, TestCreateWhenNotSignedIn) { |
| 115 // User is not signed in, but the "prompt" flag is set. |
| 116 SetUpMainChromeBrowserStateNotSignedIn(); |
| 117 AuthenticationService* authService = |
| 118 AuthenticationServiceFactory::GetForBrowserState( |
| 119 chrome_browser_state_.get()); |
| 120 authService->SetPromptForSignIn(true); |
| 121 std::unique_ptr<ReSignInInfoBarDelegate> infobar_delegate = |
| 122 ReSignInInfoBarDelegate::CreateInfoBarDelegate( |
| 123 chrome_browser_state_.get()); |
| 124 // Infobar delegate should be created. |
| 125 EXPECT_TRUE(infobar_delegate.get()); |
| 126 EXPECT_TRUE(authService->ShouldPromptForSignIn()); |
| 127 } |
| 128 |
| 129 TEST_F(ReSignInInfoBarDelegateTest, TestCreateWhenAlreadySignedIn) { |
| 130 // User is signed in and the "prompt" flag is set. |
| 131 SetUpMainChromeBrowserStateWithSignedInUser(); |
| 132 AuthenticationService* authService = |
| 133 AuthenticationServiceFactory::GetForBrowserState( |
| 134 chrome_browser_state_.get()); |
| 135 authService->SetPromptForSignIn(true); |
| 136 std::unique_ptr<ReSignInInfoBarDelegate> infobar_delegate = |
| 137 ReSignInInfoBarDelegate::CreateInfoBarDelegate( |
| 138 chrome_browser_state_.get()); |
| 139 // Infobar delegate should not be created. |
| 140 EXPECT_FALSE(infobar_delegate.get()); |
| 141 EXPECT_FALSE(authService->ShouldPromptForSignIn()); |
| 142 } |
| 143 |
| 144 TEST_F(ReSignInInfoBarDelegateTest, TestCreateWhenIncognito) { |
| 145 // Tab is incognito, and the "prompt" flag is set. |
| 146 SetUpMainChromeBrowserStateWithIncognito(); |
| 147 AuthenticationService* authService = |
| 148 AuthenticationServiceFactory::GetForBrowserState( |
| 149 chrome_browser_state_.get()); |
| 150 authService->SetPromptForSignIn(true); |
| 151 std::unique_ptr<ReSignInInfoBarDelegate> infobar_delegate = |
| 152 ReSignInInfoBarDelegate::CreateInfoBarDelegate( |
| 153 chrome_browser_state_->GetOffTheRecordChromeBrowserState()); |
| 154 // Infobar delegate should not be created. |
| 155 EXPECT_FALSE(infobar_delegate.get()); |
| 156 EXPECT_TRUE(authService->ShouldPromptForSignIn()); |
| 157 } |
| 158 |
| 159 TEST_F(ReSignInInfoBarDelegateTest, TestMessages) { |
| 160 SetUpMainChromeBrowserStateNotSignedIn(); |
| 161 std::unique_ptr<ReSignInInfoBarDelegate> delegate( |
| 162 new ReSignInInfoBarDelegate(chrome_browser_state_.get())); |
| 163 EXPECT_EQ(ConfirmInfoBarDelegate::BUTTON_OK, delegate->GetButtons()); |
| 164 base::string16 message_text = delegate->GetMessageText(); |
| 165 EXPECT_GT(message_text.length(), 0U); |
| 166 base::string16 button_label = |
| 167 delegate->GetButtonLabel(ConfirmInfoBarDelegate::BUTTON_OK); |
| 168 EXPECT_GT(button_label.length(), 0U); |
| 169 } |
| 170 |
| 171 TEST_F(ReSignInInfoBarDelegateTest, TestAccept) { |
| 172 SetUpMainChromeBrowserStateNotSignedIn(); |
| 173 AuthenticationService* authService = |
| 174 AuthenticationServiceFactory::GetForBrowserState( |
| 175 chrome_browser_state_.get()); |
| 176 authService->SetPromptForSignIn(true); |
| 177 std::unique_ptr<infobars::InfoBar> infobar( |
| 178 CreateConfirmInfoBar(ReSignInInfoBarDelegate::CreateInfoBarDelegate( |
| 179 chrome_browser_state_.get()))); |
| 180 InfoBarIOS* infobarIOS = static_cast<InfoBarIOS*>(infobar.get()); |
| 181 infobarIOS->Layout(CGRectZero); |
| 182 base::scoped_nsobject<CatchExecuteCommandView> view( |
| 183 [[CatchExecuteCommandView alloc] initWithFrame:CGRectZero]); |
| 184 [view.get() addSubview:infobarIOS->view()]; |
| 185 |
| 186 ReSignInInfoBarDelegate* delegate = |
| 187 static_cast<ReSignInInfoBarDelegate*>(infobarIOS->delegate()); |
| 188 EXPECT_TRUE(delegate->Accept()); |
| 189 EXPECT_FALSE(authService->ShouldPromptForSignIn()); |
| 190 EXPECT_TRUE([view command]); |
| 191 EXPECT_TRUE([[view command] isKindOfClass:[ShowSigninCommand class]]); |
| 192 EXPECT_EQ(AUTHENTICATION_OPERATION_REAUTHENTICATE, |
| 193 static_cast<ShowSigninCommand*>([view command]).operation); |
| 194 } |
| 195 |
| 196 TEST_F(ReSignInInfoBarDelegateTest, TestInfoBarDismissed) { |
| 197 SetUpMainChromeBrowserStateNotSignedIn(); |
| 198 AuthenticationService* authService = |
| 199 AuthenticationServiceFactory::GetForBrowserState( |
| 200 chrome_browser_state_.get()); |
| 201 authService->SetPromptForSignIn(true); |
| 202 std::unique_ptr<infobars::InfoBar> infobar( |
| 203 CreateConfirmInfoBar(ReSignInInfoBarDelegate::CreateInfoBarDelegate( |
| 204 chrome_browser_state_.get()))); |
| 205 InfoBarIOS* infobarIOS = static_cast<InfoBarIOS*>(infobar.get()); |
| 206 infobarIOS->Layout(CGRectZero); |
| 207 base::scoped_nsobject<CatchExecuteCommandView> view( |
| 208 [[CatchExecuteCommandView alloc] initWithFrame:CGRectZero]); |
| 209 [view.get() addSubview:infobarIOS->view()]; |
| 210 |
| 211 ReSignInInfoBarDelegate* delegate = |
| 212 static_cast<ReSignInInfoBarDelegate*>(infobarIOS->delegate()); |
| 213 delegate->InfoBarDismissed(); |
| 214 EXPECT_FALSE(authService->ShouldPromptForSignIn()); |
| 215 EXPECT_FALSE([view command]); |
| 216 } |
| 217 |
| 218 } // namespace |
OLD | NEW |