com.facebook
Class TestSession

java.lang.Object
  extended by com.facebook.Session
      extended by com.facebook.TestSession
All Implemented Interfaces:
Serializable

public class TestSession
extends Session

Implements an subclass of Session that knows about test users for a particular application. This should never be used from a real application, but may be useful for writing unit tests, etc.

Facebook allows developers to create test accounts for testing their applications' Facebook integration (see https://developers.facebook.com/docs/test_users/). This class simplifies use of these accounts for writing unit tests. It is not designed for use in production application code.

The main use case for this class is using createSessionWithPrivateUser(android.app.Activity, java.util.List) or createSessionWithSharedUser(android.app.Activity, java.util.List) to create a session for a test user. Two modes are supported. In "shared" mode, an attempt is made to find an existing test user that has the required permissions. If no such user is available, a new one is created with the required permissions. In "private" mode, designed for scenarios which require a new user in a known clean state, a new test user will always be created, and it will be automatically deleted when the TestSession is closed. The session obeys the same lifecycle as a regular Session, meaning it must be opened after creation before it can be used to make calls to the Facebook API.

Prior to creating a TestSession, two static methods must be called to initialize the application ID and application Secret to be used for managing test users. These methods are setTestApplicationId(String) and setTestApplicationSecret(String).

Note that the shared test user functionality depends on a naming convention for the test users. It is important that any testing of functionality which will mutate the permissions for a test user NOT use a shared test user, or this scheme will break down. If a shared test user seems to be in an invalid state, it can be deleted manually via the Web interface at https://developers.facebook.com/apps/APP_ID/permissions?role=test+users.

See Also:
Serialized Form

Nested Class Summary
 
Nested classes/interfaces inherited from class com.facebook.Session
Session.AuthorizationRequest, Session.Builder, Session.NewPermissionsRequest, Session.OpenRequest, Session.StatusCallback
 
Field Summary
 
Fields inherited from class com.facebook.Session
ACTION_ACTIVE_SESSION_CLOSED, ACTION_ACTIVE_SESSION_OPENED, ACTION_ACTIVE_SESSION_SET, ACTION_ACTIVE_SESSION_UNSET, APPLICATION_ID_PROPERTY, DEFAULT_AUTHORIZE_ACTIVITY_CODE, TAG, WEB_VIEW_ERROR_CODE_KEY, WEB_VIEW_FAILING_URL_KEY
 
Method Summary
static TestSession createSessionWithPrivateUser(Activity activity, List<String> permissions)
          Constructs a TestSession which creates a test user on open, and destroys the user on close; This method should not be used in application code -- but is useful for creating unit tests that use the Facebook SDK.
static TestSession createSessionWithSharedUser(Activity activity, List<String> permissions)
          Constructs a TestSession which uses a shared test user with the right permissions, creating one if necessary on open (but not deleting it on close, so it can be re-used in later tests).
static TestSession createSessionWithSharedUser(Activity activity, List<String> permissions, String sessionUniqueUserTag)
          Constructs a TestSession which uses a shared test user with the right permissions, creating one if necessary on open (but not deleting it on close, so it can be re-used in later tests).
static String getTestApplicationId()
          Gets the Facebook Application ID for the application under test.
static String getTestApplicationSecret()
          Gets the Facebook Application Secret for the application under test.
 String getTestUserId()
          Gets the ID of the test user that this TestSession is authenticated as.
static void setTestApplicationId(String applicationId)
          Sets the Facebook Application ID for the application under test.
static void setTestApplicationSecret(String applicationSecret)
          Sets the Facebook Application Secret for the application under test.
 String toString()
           
 
Methods inherited from class com.facebook.Session
addCallback, close, closeAndClearTokenInformation, equals, getAccessToken, getActiveSession, getApplicationId, getAuthorizationBundle, getExpirationDate, getPermissions, getState, hashCode, isClosed, isOpened, onActivityResult, open, openActiveSession, openActiveSession, openActiveSessionFromCache, openActiveSessionWithAccessToken, openForPublish, openForRead, removeCallback, requestNewPublishPermissions, requestNewReadPermissions, restoreSession, saveSession, setActiveSession
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Method Detail

createSessionWithPrivateUser

public static TestSession createSessionWithPrivateUser(Activity activity,
                                                       List<String> permissions)
Constructs a TestSession which creates a test user on open, and destroys the user on close; This method should not be used in application code -- but is useful for creating unit tests that use the Facebook SDK.

Parameters:
activity - the Activity to use for opening the session
permissions - list of strings containing permissions to request; nil will result in a common set of permissions (email, publish_actions) being requested
Returns:
a new TestSession that is in the CREATED state, ready to be opened

createSessionWithSharedUser

public static TestSession createSessionWithSharedUser(Activity activity,
                                                      List<String> permissions)
Constructs a TestSession which uses a shared test user with the right permissions, creating one if necessary on open (but not deleting it on close, so it can be re-used in later tests).

This method should not be used in application code -- but is useful for creating unit tests that use the Facebook SDK.

Parameters:
activity - the Activity to use for opening the session
permissions - list of strings containing permissions to request; nil will result in a common set of permissions (email, publish_actions) being requested
Returns:
a new TestSession that is in the CREATED state, ready to be opened

createSessionWithSharedUser

public static TestSession createSessionWithSharedUser(Activity activity,
                                                      List<String> permissions,
                                                      String sessionUniqueUserTag)
Constructs a TestSession which uses a shared test user with the right permissions, creating one if necessary on open (but not deleting it on close, so it can be re-used in later tests).

This method should not be used in application code -- but is useful for creating unit tests that use the Facebook SDK.

Parameters:
activity - the Activity to use for opening the session
permissions - list of strings containing permissions to request; nil will result in a common set of permissions (email, publish_actions) being requested
sessionUniqueUserTag - a string which will be used to make this user unique among other users with the same permissions. Useful for tests which require two or more users to interact with each other, and which therefore must have sessions associated with different users.
Returns:
a new TestSession that is in the CREATED state, ready to be opened

getTestApplicationId

public static String getTestApplicationId()
Gets the Facebook Application ID for the application under test.

Returns:
the application ID

setTestApplicationId

public static void setTestApplicationId(String applicationId)
Sets the Facebook Application ID for the application under test. This must be specified prior to creating a TestSession.

Parameters:
applicationId - the application ID

getTestApplicationSecret

public static String getTestApplicationSecret()
Gets the Facebook Application Secret for the application under test.

Returns:
the application secret

setTestApplicationSecret

public static void setTestApplicationSecret(String applicationSecret)
Sets the Facebook Application Secret for the application under test. This must be specified prior to creating a TestSession.

Parameters:
applicationSecret - the application secret

getTestUserId

public final String getTestUserId()
Gets the ID of the test user that this TestSession is authenticated as.

Returns:
the Facebook user ID of the test user

toString

public final String toString()
Overrides:
toString in class Session