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
+
1
15
package com .firebase .ui .auth .ui .idp ;
2
16
3
17
import static junit .framework .Assert .assertEquals ;
8
22
import android .app .Activity ;
9
23
import android .content .Context ;
10
24
import android .content .Intent ;
25
+ import android .os .Bundle ;
11
26
12
27
import com .firebase .ui .auth .BuildConfig ;
13
28
import com .firebase .ui .auth .provider .IDPResponse ;
25
40
import com .firebase .ui .auth .ui .account_link .WelcomeBackIDPPrompt ;
26
41
import com .firebase .ui .auth .ui .account_link .WelcomeBackPasswordPrompt ;
27
42
import com .google .android .gms .tasks .Task ;
43
+ import com .google .android .gms .tasks .Tasks ;
28
44
import com .google .firebase .auth .EmailAuthProvider ;
29
45
import com .google .firebase .auth .FacebookAuthProvider ;
30
46
import com .google .firebase .auth .FirebaseAuth ;
31
47
import com .google .firebase .auth .FirebaseAuthUserCollisionException ;
32
48
import com .google .firebase .auth .FirebaseUser ;
49
+ import com .google .firebase .auth .GoogleAuthProvider ;
33
50
import com .google .firebase .auth .ProviderQueryResult ;
34
51
35
52
import org .junit .Test ;
@@ -50,31 +67,29 @@ public class CredentialSignInHandlerTest {
50
67
51
68
@ Test
52
69
public void testSignInSucceeded () {
53
- Activity mockActivty = mock (Activity .class );
70
+ Activity mockActivity = mock (Activity .class );
54
71
ActivityHelper mockActivityHelper = mock (ActivityHelper .class );
55
72
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 ());
58
77
CredentialSignInHandler credentialSignInHandler = new CredentialSignInHandler (
59
- mockActivty ,
78
+ mockActivity ,
60
79
mockActivityHelper ,
61
80
RC_ACCOUNT_LINK ,
62
81
RC_SAVE_CREDENTIALS ,
63
- mockIdpResponse );
64
-
82
+ idpResponse );
65
83
Context mockContext = mock (Context .class );
66
84
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 ));
71
86
when (mockActivityHelper .getApplicationContext ()).thenReturn (mockContext );
72
87
when (mockActivityHelper .getFlowParams ()).thenReturn (mockFlowParams );
73
- credentialSignInHandler .onComplete (mockTask );
88
+ credentialSignInHandler .onComplete (signInTask );
74
89
75
90
ArgumentCaptor <Intent > intentCaptor = ArgumentCaptor .forClass (Intent .class );
76
91
ArgumentCaptor <Integer > intCaptor = ArgumentCaptor .forClass (Integer .class );
77
- verify (mockActivty ).startActivityForResult (intentCaptor .capture (), intCaptor .capture ());
92
+ verify (mockActivity ).startActivityForResult (intentCaptor .capture (), intCaptor .capture ());
78
93
Intent capturedIntent = intentCaptor .getValue ();
79
94
assertEquals (RC_SAVE_CREDENTIALS , (int ) intCaptor .getValue ());
80
95
assertEquals (
@@ -93,24 +108,22 @@ public void testSignInSucceeded() {
93
108
94
109
@ Test
95
110
public void testSignInFailed_withFacebookAlreadyLinked () {
96
- Activity mockActivty = mock (Activity .class );
111
+ Activity mockActivity = mock (Activity .class );
97
112
ActivityHelper mockActivityHelper = mock (ActivityHelper .class );
98
113
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 ());
100
118
CredentialSignInHandler credentialSignInHandler = new CredentialSignInHandler (
101
- mockActivty ,
119
+ mockActivity ,
102
120
mockActivityHelper ,
103
121
RC_ACCOUNT_LINK ,
104
122
RC_SAVE_CREDENTIALS ,
105
- mockIdpResponse );
123
+ idpResponse );
106
124
107
125
Context mockContext = mock (Context .class );
108
- Task mockTask = mock (Task .class );
109
126
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 );
114
127
when (mockActivityHelper .getFirebaseAuth ()).thenReturn (mockFirebaseAuth );
115
128
when (mockActivityHelper .getApplicationContext ()).thenReturn (mockContext );
116
129
when (mockActivityHelper .getFlowParams ()).thenReturn (mockFlowParams );
@@ -121,11 +134,13 @@ public void testSignInFailed_withFacebookAlreadyLinked() {
121
134
new FakeProviderQueryResult (
122
135
Arrays .asList (FacebookAuthProvider .PROVIDER_ID )), true , null ));
123
136
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 );
126
141
ArgumentCaptor <Intent > intentCaptor = ArgumentCaptor .forClass (Intent .class );
127
142
ArgumentCaptor <Integer > intCaptor = ArgumentCaptor .forClass (Integer .class );
128
- verify (mockActivty ).startActivityForResult (intentCaptor .capture (), intCaptor .capture ());
143
+ verify (mockActivity ).startActivityForResult (intentCaptor .capture (), intCaptor .capture ());
129
144
Intent capturedIntent = intentCaptor .getValue ();
130
145
assertEquals (RC_ACCOUNT_LINK , (int ) intCaptor .getValue ());
131
146
assertEquals (
@@ -143,17 +158,19 @@ public void testSignInFailed_withFacebookAlreadyLinked() {
143
158
144
159
@ Test
145
160
public void testSignInFailed_withPasswordAccountAlreadyLinked () {
146
- Activity mockActivty = mock (Activity .class );
161
+ Activity mockActivity = mock (Activity .class );
147
162
ActivityHelper mockActivityHelper = mock (ActivityHelper .class );
148
163
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 ());
151
168
CredentialSignInHandler credentialSignInHandler = new CredentialSignInHandler (
152
- mockActivty ,
169
+ mockActivity ,
153
170
mockActivityHelper ,
154
171
RC_ACCOUNT_LINK ,
155
172
RC_SAVE_CREDENTIALS ,
156
- mockIdpResponse );
173
+ idpResponse );
157
174
158
175
Context mockContext = mock (Context .class );
159
176
Task mockTask = mock (Task .class );
@@ -162,7 +179,6 @@ public void testSignInFailed_withPasswordAccountAlreadyLinked() {
162
179
// pretend there was already an account with this email
163
180
when (mockTask .getException ()).thenReturn (
164
181
new FirebaseAuthUserCollisionException (LINKING_ERROR , LINKING_EXPLANATION ));
165
- when (mockIdpResponse .getEmail ()).thenReturn (TestConstants .EMAIL );
166
182
when (mockActivityHelper .getFirebaseAuth ()).thenReturn (mockFirebaseAuth );
167
183
when (mockActivityHelper .getApplicationContext ()).thenReturn (mockContext );
168
184
when (mockActivityHelper .getFlowParams ()).thenReturn (mockFlowParams );
@@ -177,7 +193,7 @@ public void testSignInFailed_withPasswordAccountAlreadyLinked() {
177
193
credentialSignInHandler .onComplete (mockTask );
178
194
ArgumentCaptor <Intent > intentCaptor = ArgumentCaptor .forClass (Intent .class );
179
195
ArgumentCaptor <Integer > intCaptor = ArgumentCaptor .forClass (Integer .class );
180
- verify (mockActivty ).startActivityForResult (intentCaptor .capture (), intCaptor .capture ());
196
+ verify (mockActivity ).startActivityForResult (intentCaptor .capture (), intCaptor .capture ());
181
197
Intent capturedIntent = intentCaptor .getValue ();
182
198
assertEquals (RC_ACCOUNT_LINK , (int ) intCaptor .getValue ());
183
199
assertEquals (
0 commit comments