Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
package com.facebook;
import java.util.ArrayList;
import java.util.Collection;
public class AccessTokenCreator {
public static AccessToken createToken(Collection<String> grantedPermissions) {
return new AccessToken("token", "appId", "userId", grantedPermissions,
new ArrayList<String>(), AccessTokenSource.WEB_VIEW, null, null);
}
}
package com.facebook.login;
import android.support.test.espresso.core.deps.guava.collect.Sets;
import com.facebook.AccessTokenCreator;
import com.facebook.login.LoginClient.Request;
import com.facebook.login.LoginClient.Result;
import java.util.HashSet;
public class LoginClientCreator {
public static Request createRequest() {
final HashSet<String> permissions = Sets.newHashSet("user_actions.music", "user_friends", "user_likes", "email");
return new Request(LoginBehavior.NATIVE_WITH_FALLBACK, permissions, DefaultAudience.EVERYONE, "appId", "authId");
}
public static Result createResult() {
final Request request = createRequest();
return new Result(request, Result.Code.SUCCESS, AccessTokenCreator.createToken(request.getPermissions()), null, null);
}
}
@Test
public void signupWithFacebook() {
Context context = InstrumentationRegistry.getInstrumentation().getContext();
final Intent resultData = new Intent();
resultData.putExtra("com.facebook.LoginFragment:Result", LoginClientCreator.createResult());
intending(hasComponent(FacebookActivity.class.getName()))
.respondWith(new Instrumentation.ActivityResult(Activity.RESULT_OK, resultData));
activityTestRule.launchActivity(LoginSignupActivity.buildIntent(context));
// click signup fb button
onView(withId(R.id.signup_with_facebook_button))
.perform(click());
// check facebook intent was hit
intended(hasComponent(FacebookActivity.class.getName()), times(1));
// check UI is showing the user as logged in
...
}

Worked great for me. Thanks for figuring this out.
I just inlined AccessTokenCreator class because it only has a dependency to AccessToken which is public. I thought 1 less class would be better.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment