1+ /*
2+ * Copyright 2016 Google Inc. All Rights Reserved.
3+ *
4+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5+ * in compliance with the License. You may obtain a copy of the License at
6+ *
7+ * http://www.apache.org/licenses/LICENSE-2.0
8+ *
9+ * Unless required by applicable law or agreed to in writing, software distributed under the
10+ * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
11+ * express or implied. See the License for the specific language governing permissions and
12+ * limitations under the License.
13+ */
14+
115package com .firebase .ui .auth .ui .idp ;
216
317import static junit .framework .Assert .assertEquals ;
822import android .app .Activity ;
923import android .content .Context ;
1024import android .content .Intent ;
25+ import android .os .Bundle ;
1126
1227import com .firebase .ui .auth .BuildConfig ;
1328import com .firebase .ui .auth .provider .IDPResponse ;
2540import com .firebase .ui .auth .ui .account_link .WelcomeBackIDPPrompt ;
2641import com .firebase .ui .auth .ui .account_link .WelcomeBackPasswordPrompt ;
2742import com .google .android .gms .tasks .Task ;
43+ import com .google .android .gms .tasks .Tasks ;
2844import com .google .firebase .auth .EmailAuthProvider ;
2945import com .google .firebase .auth .FacebookAuthProvider ;
3046import com .google .firebase .auth .FirebaseAuth ;
3147import com .google .firebase .auth .FirebaseAuthUserCollisionException ;
3248import com .google .firebase .auth .FirebaseUser ;
49+ import com .google .firebase .auth .GoogleAuthProvider ;
3350import com .google .firebase .auth .ProviderQueryResult ;
3451
3552import org .junit .Test ;
@@ -50,31 +67,29 @@ public class CredentialSignInHandlerTest {
5067
5168 @ Test
5269 public void testSignInSucceeded () {
53- Activity mockActivty = mock (Activity .class );
70+ Activity mockActivity = mock (Activity .class );
5471 ActivityHelper mockActivityHelper = mock (ActivityHelper .class );
5572 FirebaseUser mockFirebaseUser = TestHelper .makeMockFirebaseUser ();
56- IDPResponse mockIdpResponse = mock (IDPResponse .class );
57-
73+ IDPResponse idpResponse = new IDPResponse (
74+ GoogleAuthProvider .PROVIDER_ID ,
75+ TestConstants .EMAIL ,
76+ new Bundle ());
5877 CredentialSignInHandler credentialSignInHandler = new CredentialSignInHandler (
59- mockActivty ,
78+ mockActivity ,
6079 mockActivityHelper ,
6180 RC_ACCOUNT_LINK ,
6281 RC_SAVE_CREDENTIALS ,
63- mockIdpResponse );
64-
82+ idpResponse );
6583 Context mockContext = mock (Context .class );
6684 FlowParameters mockFlowParams = mock (FlowParameters .class );
67- Task mockTask = mock (Task .class );
68-
69- when (mockTask .isSuccessful ()).thenReturn (true );
70- when (mockTask .getResult ()).thenReturn (new FakeAuthResult (mockFirebaseUser ));
85+ Task signInTask = Tasks .forResult (new FakeAuthResult (mockFirebaseUser ));
7186 when (mockActivityHelper .getApplicationContext ()).thenReturn (mockContext );
7287 when (mockActivityHelper .getFlowParams ()).thenReturn (mockFlowParams );
73- credentialSignInHandler .onComplete (mockTask );
88+ credentialSignInHandler .onComplete (signInTask );
7489
7590 ArgumentCaptor <Intent > intentCaptor = ArgumentCaptor .forClass (Intent .class );
7691 ArgumentCaptor <Integer > intCaptor = ArgumentCaptor .forClass (Integer .class );
77- verify (mockActivty ).startActivityForResult (intentCaptor .capture (), intCaptor .capture ());
92+ verify (mockActivity ).startActivityForResult (intentCaptor .capture (), intCaptor .capture ());
7893 Intent capturedIntent = intentCaptor .getValue ();
7994 assertEquals (RC_SAVE_CREDENTIALS , (int ) intCaptor .getValue ());
8095 assertEquals (
@@ -93,24 +108,22 @@ public void testSignInSucceeded() {
93108
94109 @ Test
95110 public void testSignInFailed_withFacebookAlreadyLinked () {
96- Activity mockActivty = mock (Activity .class );
111+ Activity mockActivity = mock (Activity .class );
97112 ActivityHelper mockActivityHelper = mock (ActivityHelper .class );
98113 FirebaseAuth mockFirebaseAuth = mock (FirebaseAuth .class );
99- IDPResponse mockIdpResponse = mock (IDPResponse .class );
114+ IDPResponse idpResponse = new IDPResponse (
115+ GoogleAuthProvider .PROVIDER_ID ,
116+ TestConstants .EMAIL ,
117+ new Bundle ());
100118 CredentialSignInHandler credentialSignInHandler = new CredentialSignInHandler (
101- mockActivty ,
119+ mockActivity ,
102120 mockActivityHelper ,
103121 RC_ACCOUNT_LINK ,
104122 RC_SAVE_CREDENTIALS ,
105- mockIdpResponse );
123+ idpResponse );
106124
107125 Context mockContext = mock (Context .class );
108- Task mockTask = mock (Task .class );
109126 FlowParameters mockFlowParams = mock (FlowParameters .class );
110- // pretend there was already an account with this email
111- when (mockTask .getException ()).thenReturn (
112- new FirebaseAuthUserCollisionException (LINKING_ERROR , LINKING_EXPLANATION ));
113- when (mockIdpResponse .getEmail ()).thenReturn (TestConstants .EMAIL );
114127 when (mockActivityHelper .getFirebaseAuth ()).thenReturn (mockFirebaseAuth );
115128 when (mockActivityHelper .getApplicationContext ()).thenReturn (mockContext );
116129 when (mockActivityHelper .getFlowParams ()).thenReturn (mockFlowParams );
@@ -121,11 +134,13 @@ public void testSignInFailed_withFacebookAlreadyLinked() {
121134 new FakeProviderQueryResult (
122135 Arrays .asList (FacebookAuthProvider .PROVIDER_ID )), true , null ));
123136
124-
125- credentialSignInHandler .onComplete (mockTask );
137+ // pretend there was already an account with this email
138+ Task exceptionTask = Tasks .forException (
139+ new FirebaseAuthUserCollisionException (LINKING_ERROR , LINKING_EXPLANATION ));
140+ credentialSignInHandler .onComplete (exceptionTask );
126141 ArgumentCaptor <Intent > intentCaptor = ArgumentCaptor .forClass (Intent .class );
127142 ArgumentCaptor <Integer > intCaptor = ArgumentCaptor .forClass (Integer .class );
128- verify (mockActivty ).startActivityForResult (intentCaptor .capture (), intCaptor .capture ());
143+ verify (mockActivity ).startActivityForResult (intentCaptor .capture (), intCaptor .capture ());
129144 Intent capturedIntent = intentCaptor .getValue ();
130145 assertEquals (RC_ACCOUNT_LINK , (int ) intCaptor .getValue ());
131146 assertEquals (
@@ -143,17 +158,19 @@ public void testSignInFailed_withFacebookAlreadyLinked() {
143158
144159 @ Test
145160 public void testSignInFailed_withPasswordAccountAlreadyLinked () {
146- Activity mockActivty = mock (Activity .class );
161+ Activity mockActivity = mock (Activity .class );
147162 ActivityHelper mockActivityHelper = mock (ActivityHelper .class );
148163 FirebaseAuth mockFirebaseAuth = mock (FirebaseAuth .class );
149- IDPResponse mockIdpResponse = mock (IDPResponse .class );
150- when (mockIdpResponse .getEmail ()).thenReturn (TestConstants .EMAIL );
164+ IDPResponse idpResponse = new IDPResponse (
165+ GoogleAuthProvider .PROVIDER_ID ,
166+ TestConstants .EMAIL ,
167+ new Bundle ());
151168 CredentialSignInHandler credentialSignInHandler = new CredentialSignInHandler (
152- mockActivty ,
169+ mockActivity ,
153170 mockActivityHelper ,
154171 RC_ACCOUNT_LINK ,
155172 RC_SAVE_CREDENTIALS ,
156- mockIdpResponse );
173+ idpResponse );
157174
158175 Context mockContext = mock (Context .class );
159176 Task mockTask = mock (Task .class );
@@ -162,7 +179,6 @@ public void testSignInFailed_withPasswordAccountAlreadyLinked() {
162179 // pretend there was already an account with this email
163180 when (mockTask .getException ()).thenReturn (
164181 new FirebaseAuthUserCollisionException (LINKING_ERROR , LINKING_EXPLANATION ));
165- when (mockIdpResponse .getEmail ()).thenReturn (TestConstants .EMAIL );
166182 when (mockActivityHelper .getFirebaseAuth ()).thenReturn (mockFirebaseAuth );
167183 when (mockActivityHelper .getApplicationContext ()).thenReturn (mockContext );
168184 when (mockActivityHelper .getFlowParams ()).thenReturn (mockFlowParams );
@@ -177,7 +193,7 @@ public void testSignInFailed_withPasswordAccountAlreadyLinked() {
177193 credentialSignInHandler .onComplete (mockTask );
178194 ArgumentCaptor <Intent > intentCaptor = ArgumentCaptor .forClass (Intent .class );
179195 ArgumentCaptor <Integer > intCaptor = ArgumentCaptor .forClass (Integer .class );
180- verify (mockActivty ).startActivityForResult (intentCaptor .capture (), intCaptor .capture ());
196+ verify (mockActivity ).startActivityForResult (intentCaptor .capture (), intCaptor .capture ());
181197 Intent capturedIntent = intentCaptor .getValue ();
182198 assertEquals (RC_ACCOUNT_LINK , (int ) intCaptor .getValue ());
183199 assertEquals (
0 commit comments