Skip to content

Make tests run/pass #260

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Aug 19, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ android:
components:
- platform-tools
- tools
- build-tools-24.0.1
- android-24
- build-tools-23.0.3
- android-23
- extra-google-google_play_services
- extra-google-m2repository
- extra-android-m2repository
Expand All @@ -16,4 +16,4 @@ android:
- 'android-sdk-license-.+'
- 'google-gdk-license-.+'
script:
- ./gradlew clean :library:prepareArtifacts
- ./gradlew clean :library:testAll :library:prepareArtifacts
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ apply plugin: 'com.neenbedankt.android-apt'
apply from: "../common/constants.gradle"

android {
compileSdkVersion 24
compileSdkVersion project.ext.compileSdk
buildToolsVersion "${project.ext.buildtools}"

defaultConfig {
applicationId "com.firebase.uidemo"
minSdkVersion 16
targetSdkVersion 24
targetSdkVersion project.ext.targetSdk
versionCode 1
versionName "1.0"
}
Expand Down
6 changes: 3 additions & 3 deletions auth/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ apply plugin: 'com.android.library'
apply plugin: 'checkstyle'

android {
compileSdkVersion 24
compileSdkVersion project.ext.compileSdk
buildToolsVersion "${project.ext.buildtools}"

defaultConfig {
minSdkVersion 16
targetSdkVersion 24
targetSdkVersion project.ext.targetSdk
versionCode 1
versionName "1.0"
}
Expand All @@ -22,7 +22,7 @@ android {
dependencies {
testCompile 'junit:junit:4.12'
testCompile 'org.mockito:mockito-core:1.10.19'
testCompile "org.robolectric:robolectric:3.1-rc1"
testCompile "org.robolectric:robolectric:3.1.1"
compile "com.android.support:appcompat-v7:${project.ext.support_library_version}"
compile 'com.facebook.android:facebook-android-sdk:4.14.1'
compile "com.android.support:design:${project.ext.support_library_version}"
Expand Down
5 changes: 5 additions & 0 deletions auth/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
package="com.firebase.ui.auth">

<application>

<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />

<activity
android:name="com.firebase.ui.auth.ui.email.ConfirmRecoverPasswordActivity"
android:label="@string/title_confirm_recover_password_activity"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,20 +63,23 @@ public class ChooseAccountActivity extends ActivityBase {
private static final int RC_PLAY_SERVICES = 6;

private CredentialsAPI mCredentialsApi;
private PlayServicesHelper mPlayServicesHelper;

@Override
protected void onCreate(Bundle savedInstance) {
super.onCreate(savedInstance);

// Make Google Play Services available at the correct version, if possible
boolean madeAvailable = PlayServicesHelper.makePlayServicesAvailable(this, RC_PLAY_SERVICES,
new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialogInterface) {
Log.w(TAG, "playServices:dialog.onCancel()");
finish(RESULT_CANCELED, new Intent());
}
});
mPlayServicesHelper = PlayServicesHelper.getInstance(this);
boolean madeAvailable = mPlayServicesHelper
.makePlayServicesAvailable(this, RC_PLAY_SERVICES,
new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialogInterface) {
Log.w(TAG, "playServices:dialog.onCancel()");
finish(RESULT_CANCELED, new Intent());
}
});

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

if (flowParams.smartLockEnabled
&& PlayServicesHelper.isPlayServicesAvailable(this)
&& mPlayServicesHelper.isPlayServicesAvailable()
&& credentialsApi.isCredentialsAvailable()) {

// Attempt auto-sign in using SmartLock
if (credentialsApi.isAutoSignInAvailable()) {
credentialsApi.googleSilentSignIn();
if (!TextUtils.isEmpty(password)) {
// Sign in with the email/password retrieved from SmartLock
signInWithEmailAndPassword(email, password);
signInWithEmailAndPassword(activityHelper, email, password);
} else {
// log in with id/provider
redirectToIdpSignIn(email, accountType);
Expand Down Expand Up @@ -177,7 +180,7 @@ private void logInWithCredential(
&& !mCredentialsApi.isSignInResolutionNeeded()) {
if (password != null && !password.isEmpty()) {
// email/password combination
signInWithEmailAndPassword(email, password);
signInWithEmailAndPassword(mActivityHelper, email, password);
} else {
// identifier/provider combination
redirectToIdpSignIn(email, accountType);
Expand Down Expand Up @@ -231,8 +234,8 @@ public void onActivityResult(int requestCode, int resultCode, Intent data) {
* On failure, delete the credential from SmartLock (if applicable) and then launch the
* auth method picker flow.
*/
private void signInWithEmailAndPassword(String email, String password) {
mActivityHelper.getFirebaseAuth()
private void signInWithEmailAndPassword(ActivityHelper helper, String email, String password) {
helper.getFirebaseAuth()
.signInWithEmailAndPassword(email, password)
.addOnFailureListener(new TaskFailureLogger(
TAG, "Error signing in with email and password"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public class CredentialsAPI implements
private ProgressDialog mProgressDialog;
private Credential mCredential;
private final CallbackInterface mCallback;
private PlayServicesHelper mPlayServicesHelper;

public interface CallbackInterface {
void onAsyncTaskFinished();
Expand All @@ -59,13 +60,14 @@ public CredentialsAPI(Activity activity, CallbackInterface callback) {
mSignInResolutionNeeded = false;
mActivity = activity;
mCallback = callback;
mPlayServicesHelper = PlayServicesHelper.getInstance(mActivity);

initGoogleApiClient(null);
requestCredentials(true /* shouldResolve */, false /* onlyPasswords */);
}

public boolean isPlayServicesAvailable() {
return PlayServicesHelper.isPlayServicesAvailable(mActivity);
return mPlayServicesHelper.isPlayServicesAvailable();
}

public boolean isCredentialsAvailable() {
Expand Down Expand Up @@ -167,7 +169,7 @@ public void handleCredential(Credential credential) {
}

public void requestCredentials(final boolean shouldResolve, boolean onlyPasswords) {
if (!PlayServicesHelper.isPlayServicesAvailable(mActivity)) {
if (!mPlayServicesHelper.isPlayServicesAvailable()) {
// TODO(samstern): it would probably be better to not actually call the method
// in this case.
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.VisibleForTesting;
import android.util.Log;

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

private static final String TAG = "PlayServicesHelper";

private static final GoogleApiAvailability sApiAvailability =
GoogleApiAvailability.getInstance();
@VisibleForTesting
public static GoogleApiAvailability sApiAvailability;

private final Context mContext;

public static PlayServicesHelper getInstance(Context context) {
return new PlayServicesHelper(context);
}

private PlayServicesHelper(Context context) {
this.mContext = context;
}

/**
* Returns {@code true} if Google Play Services is available and at the correct version,
* false otherwise.
*/
public static boolean isPlayServicesAvailable(Context context) {
int playServicesAvailable = sApiAvailability.isGooglePlayServicesAvailable(context);
public boolean isPlayServicesAvailable() {
int playServicesAvailable = sApiAvailability.isGooglePlayServicesAvailable(mContext);
return playServicesAvailable == ConnectionResult.SUCCESS;
}

/**
* Returns {@code true} if Google Play Services is either already available or can be made
* available with user action, {@code false} otherwise.
* @param context the calling context.
*/
public static boolean canMakePlayServicesAvailable(Context context) {
public boolean canMakePlayServicesAvailable() {
// Check if already available
if (isPlayServicesAvailable(context)) {
if (isPlayServicesAvailable()) {
return true;
}

// Check if error is resolvable
int availabilityCode = sApiAvailability.isGooglePlayServicesAvailable(context);
int availabilityCode = sApiAvailability.isGooglePlayServicesAvailable(mContext);
boolean isUserResolvable = sApiAvailability.isUserResolvableError(availabilityCode);

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

// Check if already available
if (isPlayServicesAvailable(activity)) {
if (isPlayServicesAvailable()) {
return true;
}

// Check if error is resolvable
if (!canMakePlayServicesAvailable(activity)) {
if (!canMakePlayServicesAvailable()) {
return false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,7 @@ public static void saveCredentialOrFinish(Activity activity,
}

// If Play Services is not available, finish the Activity

if(!FirebaseAuthWrapperFactory
.getFirebaseAuthWrapper(parameters.appName)
.isPlayServicesAvailable(activity)) {
if(!PlayServicesHelper.getInstance(activity).isPlayServicesAvailable()) {
finishActivity(activity);
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,15 @@
import com.firebase.ui.auth.AuthUI;
import com.firebase.ui.auth.ui.FlowParameters;
import com.firebase.ui.auth.util.ProviderHelper;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GoogleApiAvailability;
import com.google.firebase.FirebaseApp;
import com.google.firebase.FirebaseOptions;
import com.google.firebase.auth.FirebaseUser;

import java.util.List;

import static org.mockito.Matchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

Expand Down Expand Up @@ -64,4 +67,12 @@ public static FirebaseUser makeMockFirebaseUser() {
when(mockFirebaseUser.getPhotoUrl()).thenReturn(TestConstants.PHOTO_URI);
return mockFirebaseUser;
}

public static GoogleApiAvailability makeMockGoogleApiAvailability() {
GoogleApiAvailability availability = mock(GoogleApiAvailability.class);
when(availability.isGooglePlayServicesAvailable(any(Context.class)))
.thenReturn(ConnectionResult.SUCCESS);

return availability;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,6 @@

package com.firebase.ui.auth.ui;

import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import android.content.Intent;

import com.firebase.ui.auth.AuthUI;
Expand Down Expand Up @@ -51,6 +45,12 @@
import org.robolectric.annotation.Config;
import org.robolectric.shadows.ShadowActivity;

import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

@RunWith(CustomRobolectricGradleTestRunner.class)
@Config(constants = BuildConfig.class, shadows = {ActivityHelperShadow.class}, sdk = 21)
public class ChooseAccountActivityTest {
Expand All @@ -66,27 +66,32 @@ public void setUp() {
when(mCredentialsAPI.isPlayServicesAvailable()).thenReturn(true);
when(mCredentialsAPI.isCredentialsAvailable()).thenReturn(true);
when(mCredentialsAPI.isAutoSignInAvailable()).thenReturn(true);

}

private Intent createStartIntent() {
return AuthUI
.getInstance(mFirebaseApp).createSignInIntentBuilder()
return AuthUI.getInstance(mFirebaseApp)
.createSignInIntentBuilder()
.setProviders(AuthUI.EMAIL_PROVIDER, AuthUI.GOOGLE_PROVIDER)
.setIsSmartLockEnabled(true)
.build();
}

@Test
public void testAutoSignInWithSavedUsernameAndPassword_signsIn() {
Intent startIntent = createStartIntent();
ChooseAccountActivity chooseAccountActivity =
Robolectric.buildActivity(ChooseAccountActivity.class)
.withIntent(createStartIntent()).create().get();

when(mCredentialsAPI.getEmailFromCredential()).thenReturn(TestConstants.EMAIL);
when(mCredentialsAPI.getPasswordFromCredential()).thenReturn(TestConstants.PASSWORD);
when(mCredentialsAPI.getAccountTypeFromCredential()).thenReturn(
EmailAuthProvider.PROVIDER_ID);

when(mActivityHelper.getFirebaseAuth()).thenReturn(mFirebaseAuth);
when(mActivityHelper.getFlowParams()).thenReturn(
(FlowParameters) startIntent.getParcelableExtra(ExtraConstants.EXTRA_FLOW_PARAMS));

when(mFirebaseAuth.signInWithEmailAndPassword(
TestConstants.EMAIL,
TestConstants.PASSWORD))
Expand Down
Loading