Skip to content

Commit 710cdef

Browse files
authored
Merge pull request #260 from samtstern/master
Make tests run/pass
2 parents 243616f + 2d9145d commit 710cdef

18 files changed

+165
-81
lines changed

.travis.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ android:
55
components:
66
- platform-tools
77
- tools
8-
- build-tools-24.0.1
9-
- android-24
8+
- build-tools-23.0.3
9+
- android-23
1010
- extra-google-google_play_services
1111
- extra-google-m2repository
1212
- extra-android-m2repository
@@ -16,4 +16,4 @@ android:
1616
- 'android-sdk-license-.+'
1717
- 'google-gdk-license-.+'
1818
script:
19-
- ./gradlew clean :library:prepareArtifacts
19+
- ./gradlew clean :library:testAll :library:prepareArtifacts

app/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@ apply plugin: 'com.neenbedankt.android-apt'
33
apply from: "../common/constants.gradle"
44

55
android {
6-
compileSdkVersion 24
6+
compileSdkVersion project.ext.compileSdk
77
buildToolsVersion "${project.ext.buildtools}"
88

99
defaultConfig {
1010
applicationId "com.firebase.uidemo"
1111
minSdkVersion 16
12-
targetSdkVersion 24
12+
targetSdkVersion project.ext.targetSdk
1313
versionCode 1
1414
versionName "1.0"
1515
}

auth/build.gradle

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ apply plugin: 'com.android.library'
22
apply plugin: 'checkstyle'
33

44
android {
5-
compileSdkVersion 24
5+
compileSdkVersion project.ext.compileSdk
66
buildToolsVersion "${project.ext.buildtools}"
77

88
defaultConfig {
99
minSdkVersion 16
10-
targetSdkVersion 24
10+
targetSdkVersion project.ext.targetSdk
1111
versionCode 1
1212
versionName "1.0"
1313
}
@@ -22,7 +22,7 @@ android {
2222
dependencies {
2323
testCompile 'junit:junit:4.12'
2424
testCompile 'org.mockito:mockito-core:1.10.19'
25-
testCompile "org.robolectric:robolectric:3.1-rc1"
25+
testCompile "org.robolectric:robolectric:3.1.1"
2626
compile "com.android.support:appcompat-v7:${project.ext.support_library_version}"
2727
compile 'com.facebook.android:facebook-android-sdk:4.14.1'
2828
compile "com.android.support:design:${project.ext.support_library_version}"

auth/src/main/AndroidManifest.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@
33
package="com.firebase.ui.auth">
44

55
<application>
6+
7+
<meta-data
8+
android:name="com.google.android.gms.version"
9+
android:value="@integer/google_play_services_version" />
10+
611
<activity
712
android:name="com.firebase.ui.auth.ui.email.ConfirmRecoverPasswordActivity"
813
android:label="@string/title_confirm_recover_password_activity"

auth/src/main/java/com/firebase/ui/auth/ui/ChooseAccountActivity.java

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -63,20 +63,23 @@ public class ChooseAccountActivity extends ActivityBase {
6363
private static final int RC_PLAY_SERVICES = 6;
6464

6565
private CredentialsAPI mCredentialsApi;
66+
private PlayServicesHelper mPlayServicesHelper;
6667

6768
@Override
6869
protected void onCreate(Bundle savedInstance) {
6970
super.onCreate(savedInstance);
7071

7172
// Make Google Play Services available at the correct version, if possible
72-
boolean madeAvailable = PlayServicesHelper.makePlayServicesAvailable(this, RC_PLAY_SERVICES,
73-
new DialogInterface.OnCancelListener() {
74-
@Override
75-
public void onCancel(DialogInterface dialogInterface) {
76-
Log.w(TAG, "playServices:dialog.onCancel()");
77-
finish(RESULT_CANCELED, new Intent());
78-
}
79-
});
73+
mPlayServicesHelper = PlayServicesHelper.getInstance(this);
74+
boolean madeAvailable = mPlayServicesHelper
75+
.makePlayServicesAvailable(this, RC_PLAY_SERVICES,
76+
new DialogInterface.OnCancelListener() {
77+
@Override
78+
public void onCancel(DialogInterface dialogInterface) {
79+
Log.w(TAG, "playServices:dialog.onCancel()");
80+
finish(RESULT_CANCELED, new Intent());
81+
}
82+
});
8083

8184
if (!madeAvailable) {
8285
Log.w(TAG, "playServices: could not make available.");
@@ -122,15 +125,15 @@ public void onCredentialsApiConnected(
122125
FlowParameters flowParams = activityHelper.getFlowParams();
123126

124127
if (flowParams.smartLockEnabled
125-
&& PlayServicesHelper.isPlayServicesAvailable(this)
128+
&& mPlayServicesHelper.isPlayServicesAvailable()
126129
&& credentialsApi.isCredentialsAvailable()) {
127130

128131
// Attempt auto-sign in using SmartLock
129132
if (credentialsApi.isAutoSignInAvailable()) {
130133
credentialsApi.googleSilentSignIn();
131134
if (!TextUtils.isEmpty(password)) {
132135
// Sign in with the email/password retrieved from SmartLock
133-
signInWithEmailAndPassword(email, password);
136+
signInWithEmailAndPassword(activityHelper, email, password);
134137
} else {
135138
// log in with id/provider
136139
redirectToIdpSignIn(email, accountType);
@@ -177,7 +180,7 @@ private void logInWithCredential(
177180
&& !mCredentialsApi.isSignInResolutionNeeded()) {
178181
if (password != null && !password.isEmpty()) {
179182
// email/password combination
180-
signInWithEmailAndPassword(email, password);
183+
signInWithEmailAndPassword(mActivityHelper, email, password);
181184
} else {
182185
// identifier/provider combination
183186
redirectToIdpSignIn(email, accountType);
@@ -231,8 +234,8 @@ public void onActivityResult(int requestCode, int resultCode, Intent data) {
231234
* On failure, delete the credential from SmartLock (if applicable) and then launch the
232235
* auth method picker flow.
233236
*/
234-
private void signInWithEmailAndPassword(String email, String password) {
235-
mActivityHelper.getFirebaseAuth()
237+
private void signInWithEmailAndPassword(ActivityHelper helper, String email, String password) {
238+
helper.getFirebaseAuth()
236239
.signInWithEmailAndPassword(email, password)
237240
.addOnFailureListener(new TaskFailureLogger(
238241
TAG, "Error signing in with email and password"))

auth/src/main/java/com/firebase/ui/auth/util/CredentialsAPI.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ public class CredentialsAPI implements
4949
private ProgressDialog mProgressDialog;
5050
private Credential mCredential;
5151
private final CallbackInterface mCallback;
52+
private PlayServicesHelper mPlayServicesHelper;
5253

5354
public interface CallbackInterface {
5455
void onAsyncTaskFinished();
@@ -59,13 +60,14 @@ public CredentialsAPI(Activity activity, CallbackInterface callback) {
5960
mSignInResolutionNeeded = false;
6061
mActivity = activity;
6162
mCallback = callback;
63+
mPlayServicesHelper = PlayServicesHelper.getInstance(mActivity);
6264

6365
initGoogleApiClient(null);
6466
requestCredentials(true /* shouldResolve */, false /* onlyPasswords */);
6567
}
6668

6769
public boolean isPlayServicesAvailable() {
68-
return PlayServicesHelper.isPlayServicesAvailable(mActivity);
70+
return mPlayServicesHelper.isPlayServicesAvailable();
6971
}
7072

7173
public boolean isCredentialsAvailable() {
@@ -167,7 +169,7 @@ public void handleCredential(Credential credential) {
167169
}
168170

169171
public void requestCredentials(final boolean shouldResolve, boolean onlyPasswords) {
170-
if (!PlayServicesHelper.isPlayServicesAvailable(mActivity)) {
172+
if (!mPlayServicesHelper.isPlayServicesAvailable()) {
171173
// TODO(samstern): it would probably be better to not actually call the method
172174
// in this case.
173175
return;

auth/src/main/java/com/firebase/ui/auth/util/PlayServicesHelper.java

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import android.content.Context;
66
import android.support.annotation.NonNull;
77
import android.support.annotation.Nullable;
8+
import android.support.annotation.VisibleForTesting;
89
import android.util.Log;
910

1011
import com.google.android.gms.common.ConnectionResult;
@@ -18,31 +19,40 @@ public class PlayServicesHelper {
1819

1920
private static final String TAG = "PlayServicesHelper";
2021

21-
private static final GoogleApiAvailability sApiAvailability =
22-
GoogleApiAvailability.getInstance();
22+
@VisibleForTesting
23+
public static GoogleApiAvailability sApiAvailability;
24+
25+
private final Context mContext;
26+
27+
public static PlayServicesHelper getInstance(Context context) {
28+
return new PlayServicesHelper(context);
29+
}
30+
31+
private PlayServicesHelper(Context context) {
32+
this.mContext = context;
33+
}
2334

2435
/**
2536
* Returns {@code true} if Google Play Services is available and at the correct version,
2637
* false otherwise.
2738
*/
28-
public static boolean isPlayServicesAvailable(Context context) {
29-
int playServicesAvailable = sApiAvailability.isGooglePlayServicesAvailable(context);
39+
public boolean isPlayServicesAvailable() {
40+
int playServicesAvailable = sApiAvailability.isGooglePlayServicesAvailable(mContext);
3041
return playServicesAvailable == ConnectionResult.SUCCESS;
3142
}
3243

3344
/**
3445
* Returns {@code true} if Google Play Services is either already available or can be made
3546
* available with user action, {@code false} otherwise.
36-
* @param context the calling context.
3747
*/
38-
public static boolean canMakePlayServicesAvailable(Context context) {
48+
public boolean canMakePlayServicesAvailable() {
3949
// Check if already available
40-
if (isPlayServicesAvailable(context)) {
50+
if (isPlayServicesAvailable()) {
4151
return true;
4252
}
4353

4454
// Check if error is resolvable
45-
int availabilityCode = sApiAvailability.isGooglePlayServicesAvailable(context);
55+
int availabilityCode = sApiAvailability.isGooglePlayServicesAvailable(mContext);
4656
boolean isUserResolvable = sApiAvailability.isUserResolvableError(availabilityCode);
4757

4858
// Although the API considers SERVICE_INVALID to be resolvable, it can cause crashes
@@ -59,18 +69,18 @@ public static boolean canMakePlayServicesAvailable(Context context) {
5969
* @return {@code true} if a resolution is launched or if a resolution was not necessary,
6070
* {@code false otherwise}.
6171
*/
62-
public static boolean makePlayServicesAvailable(
72+
public boolean makePlayServicesAvailable(
6373
@NonNull Activity activity,
6474
int requestCode,
6575
@Nullable Dialog.OnCancelListener cancelListener) {
6676

6777
// Check if already available
68-
if (isPlayServicesAvailable(activity)) {
78+
if (isPlayServicesAvailable()) {
6979
return true;
7080
}
7181

7282
// Check if error is resolvable
73-
if (!canMakePlayServicesAvailable(activity)) {
83+
if (!canMakePlayServicesAvailable()) {
7484
return false;
7585
}
7686

auth/src/main/java/com/firebase/ui/auth/util/SmartlockUtil.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,7 @@ public static void saveCredentialOrFinish(Activity activity,
3737
}
3838

3939
// If Play Services is not available, finish the Activity
40-
41-
if(!FirebaseAuthWrapperFactory
42-
.getFirebaseAuthWrapper(parameters.appName)
43-
.isPlayServicesAvailable(activity)) {
40+
if(!PlayServicesHelper.getInstance(activity).isPlayServicesAvailable()) {
4441
finishActivity(activity);
4542
return;
4643
}

auth/src/test/java/com/firebase/ui/auth/test_helpers/TestHelper.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,15 @@
1919
import com.firebase.ui.auth.AuthUI;
2020
import com.firebase.ui.auth.ui.FlowParameters;
2121
import com.firebase.ui.auth.util.ProviderHelper;
22+
import com.google.android.gms.common.ConnectionResult;
23+
import com.google.android.gms.common.GoogleApiAvailability;
2224
import com.google.firebase.FirebaseApp;
2325
import com.google.firebase.FirebaseOptions;
2426
import com.google.firebase.auth.FirebaseUser;
2527

2628
import java.util.List;
2729

30+
import static org.mockito.Matchers.any;
2831
import static org.mockito.Mockito.mock;
2932
import static org.mockito.Mockito.when;
3033

@@ -64,4 +67,12 @@ public static FirebaseUser makeMockFirebaseUser() {
6467
when(mockFirebaseUser.getPhotoUrl()).thenReturn(TestConstants.PHOTO_URI);
6568
return mockFirebaseUser;
6669
}
70+
71+
public static GoogleApiAvailability makeMockGoogleApiAvailability() {
72+
GoogleApiAvailability availability = mock(GoogleApiAvailability.class);
73+
when(availability.isGooglePlayServicesAvailable(any(Context.class)))
74+
.thenReturn(ConnectionResult.SUCCESS);
75+
76+
return availability;
77+
}
6778
}

auth/src/test/java/com/firebase/ui/auth/ui/ChooseAccountActivityTest.java

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,6 @@
1414

1515
package com.firebase.ui.auth.ui;
1616

17-
import static junit.framework.Assert.assertEquals;
18-
import static junit.framework.Assert.assertTrue;
19-
import static org.mockito.Mockito.mock;
20-
import static org.mockito.Mockito.verify;
21-
import static org.mockito.Mockito.when;
22-
2317
import android.content.Intent;
2418

2519
import com.firebase.ui.auth.AuthUI;
@@ -51,6 +45,12 @@
5145
import org.robolectric.annotation.Config;
5246
import org.robolectric.shadows.ShadowActivity;
5347

48+
import static junit.framework.Assert.assertEquals;
49+
import static junit.framework.Assert.assertTrue;
50+
import static org.mockito.Mockito.mock;
51+
import static org.mockito.Mockito.verify;
52+
import static org.mockito.Mockito.when;
53+
5454
@RunWith(CustomRobolectricGradleTestRunner.class)
5555
@Config(constants = BuildConfig.class, shadows = {ActivityHelperShadow.class}, sdk = 21)
5656
public class ChooseAccountActivityTest {
@@ -66,27 +66,32 @@ public void setUp() {
6666
when(mCredentialsAPI.isPlayServicesAvailable()).thenReturn(true);
6767
when(mCredentialsAPI.isCredentialsAvailable()).thenReturn(true);
6868
when(mCredentialsAPI.isAutoSignInAvailable()).thenReturn(true);
69-
7069
}
7170

7271
private Intent createStartIntent() {
73-
return AuthUI
74-
.getInstance(mFirebaseApp).createSignInIntentBuilder()
72+
return AuthUI.getInstance(mFirebaseApp)
73+
.createSignInIntentBuilder()
7574
.setProviders(AuthUI.EMAIL_PROVIDER, AuthUI.GOOGLE_PROVIDER)
75+
.setIsSmartLockEnabled(true)
7676
.build();
7777
}
7878

7979
@Test
8080
public void testAutoSignInWithSavedUsernameAndPassword_signsIn() {
81+
Intent startIntent = createStartIntent();
8182
ChooseAccountActivity chooseAccountActivity =
8283
Robolectric.buildActivity(ChooseAccountActivity.class)
8384
.withIntent(createStartIntent()).create().get();
85+
8486
when(mCredentialsAPI.getEmailFromCredential()).thenReturn(TestConstants.EMAIL);
8587
when(mCredentialsAPI.getPasswordFromCredential()).thenReturn(TestConstants.PASSWORD);
8688
when(mCredentialsAPI.getAccountTypeFromCredential()).thenReturn(
8789
EmailAuthProvider.PROVIDER_ID);
8890

8991
when(mActivityHelper.getFirebaseAuth()).thenReturn(mFirebaseAuth);
92+
when(mActivityHelper.getFlowParams()).thenReturn(
93+
(FlowParameters) startIntent.getParcelableExtra(ExtraConstants.EXTRA_FLOW_PARAMS));
94+
9095
when(mFirebaseAuth.signInWithEmailAndPassword(
9196
TestConstants.EMAIL,
9297
TestConstants.PASSWORD))

0 commit comments

Comments
 (0)