Search in sources :

Example 6 with HomeServerConnectionConfig

use of org.matrix.androidsdk.HomeServerConnectionConfig in project matrix-android-sdk by matrix-org.

the class CommonTestHelper method createNewSession.

/**
 * Clone a session.
 * It simulate that the user launches again the application with the same Credentials, contrary to login which will create a new DeviceId
 *
 * @param from the session to clone
 * @return the duplicated session
 */
@NonNull
public MXSession createNewSession(@NonNull MXSession from, SessionTestParams sessionTestParams) throws InterruptedException {
    final Context context = InstrumentationRegistry.getContext();
    Credentials credentials = from.getCredentials();
    HomeServerConnectionConfig hs = createHomeServerConfig(credentials);
    MXFileStore store = new MXFileStore(hs, false, context);
    MXDataHandler dataHandler = new MXDataHandler(store, credentials);
    dataHandler.setLazyLoadingEnabled(sessionTestParams.getWithLazyLoading());
    store.setDataHandler(dataHandler);
    MXSession session2 = new MXSession.Builder(hs, dataHandler, context).withLegacyCryptoStore(sessionTestParams.getWithLegacyCryptoStore()).build();
    final Map<String, Object> results = new HashMap<>();
    final CountDownLatch lock = new CountDownLatch(1);
    MXStoreListener listener = new MXStoreListener() {

        @Override
        public void postProcess(String accountId) {
            results.put("postProcess", "postProcess " + accountId);
        }

        @Override
        public void onStoreReady(String accountId) {
            results.put("onStoreReady", "onStoreReady");
            lock.countDown();
        }

        @Override
        public void onStoreCorrupted(String accountId, String description) {
            results.put("onStoreCorrupted", description);
            lock.countDown();
        }

        @Override
        public void onStoreOOM(String accountId, String description) {
            results.put("onStoreOOM", "onStoreOOM");
            lock.countDown();
        }
    };
    store.addMXStoreListener(listener);
    store.open();
    await(lock);
    Assert.assertTrue(results.toString(), results.containsKey("onStoreReady"));
    return session2;
}
Also used : Context(android.content.Context) MXStoreListener(org.matrix.androidsdk.data.store.MXStoreListener) HashMap(java.util.HashMap) MXFileStore(org.matrix.androidsdk.data.store.MXFileStore) CountDownLatch(java.util.concurrent.CountDownLatch) HomeServerConnectionConfig(org.matrix.androidsdk.HomeServerConnectionConfig) MXSession(org.matrix.androidsdk.MXSession) MXDataHandler(org.matrix.androidsdk.MXDataHandler) Credentials(org.matrix.androidsdk.rest.model.login.Credentials) NonNull(androidx.annotation.NonNull)

Example 7 with HomeServerConnectionConfig

use of org.matrix.androidsdk.HomeServerConnectionConfig in project matrix-android-sdk by matrix-org.

the class CommonTestHelper method createAccountAndSync.

/**
 * Create an account and a dedicated session
 *
 * @param context           the context
 * @param userName          the account username
 * @param password          the password
 * @param sessionTestParams parameters for the test
 */
private MXSession createAccountAndSync(Context context, String userName, String password, SessionTestParams sessionTestParams) throws InterruptedException {
    final HomeServerConnectionConfig hs = createHomeServerConfig(null);
    final LoginRestClient loginRestClient = new LoginRestClient(hs);
    final Map<String, Object> params = new HashMap<>();
    final RegistrationParams registrationParams = new RegistrationParams();
    CountDownLatch lock = new CountDownLatch(1);
    // get the registration session id
    loginRestClient.register(registrationParams, new TestApiCallback<Credentials>(lock, false) {

        @Override
        public void onMatrixError(MatrixError e) {
            // detect if a parameter is expected
            RegistrationFlowResponse registrationFlowResponse = null;
            // when a response is not completed the server returns an error message
            if ((null != e.mStatus) && (e.mStatus == 401)) {
                try {
                    registrationFlowResponse = JsonUtils.toRegistrationFlowResponse(e.mErrorBodyAsString);
                } catch (Exception castExcept) {
                }
            }
            // check if the server response can be casted
            if (null != registrationFlowResponse) {
                params.put("session", registrationFlowResponse.session);
            }
            super.onMatrixError(e);
        }
    });
    await(lock);
    final String session = (String) params.get("session");
    Assert.assertNotNull(session);
    registrationParams.username = userName;
    registrationParams.password = password;
    AuthParams authParams = new AuthParams(LoginRestClient.LOGIN_FLOW_TYPE_DUMMY);
    authParams.session = session;
    registrationParams.auth = authParams;
    lock = new CountDownLatch(1);
    loginRestClient.register(registrationParams, new TestApiCallback<Credentials>(lock) {

        @Override
        public void onSuccess(Credentials credentials) {
            params.put("credentials", credentials);
            super.onSuccess(credentials);
        }
    });
    await(lock);
    Credentials credentials = (Credentials) params.get("credentials");
    Assert.assertNotNull(credentials);
    hs.setCredentials(credentials);
    IMXStore store = new MXFileStore(hs, false, context);
    MXDataHandler dataHandler = new MXDataHandler(store, credentials);
    dataHandler.setLazyLoadingEnabled(sessionTestParams.getWithLazyLoading());
    MXSession mxSession = new MXSession.Builder(hs, dataHandler, context).withLegacyCryptoStore(sessionTestParams.getWithLegacyCryptoStore()).build();
    if (sessionTestParams.getWithCryptoEnabled()) {
        mxSession.enableCryptoWhenStarting();
    }
    if (sessionTestParams.getWithInitialSync()) {
        syncSession(mxSession, sessionTestParams.getWithCryptoEnabled());
    }
    return mxSession;
}
Also used : AuthParams(org.matrix.androidsdk.rest.model.login.AuthParams) HashMap(java.util.HashMap) RegistrationFlowResponse(org.matrix.androidsdk.rest.model.login.RegistrationFlowResponse) IMXStore(org.matrix.androidsdk.data.store.IMXStore) MXFileStore(org.matrix.androidsdk.data.store.MXFileStore) CountDownLatch(java.util.concurrent.CountDownLatch) HomeServerConnectionConfig(org.matrix.androidsdk.HomeServerConnectionConfig) MXSession(org.matrix.androidsdk.MXSession) MXDataHandler(org.matrix.androidsdk.MXDataHandler) RegistrationParams(org.matrix.androidsdk.rest.model.login.RegistrationParams) LoginRestClient(org.matrix.androidsdk.rest.client.LoginRestClient) MatrixError(org.matrix.androidsdk.core.model.MatrixError) Credentials(org.matrix.androidsdk.rest.model.login.Credentials)

Aggregations

HashMap (java.util.HashMap)7 CountDownLatch (java.util.concurrent.CountDownLatch)7 HomeServerConnectionConfig (org.matrix.androidsdk.HomeServerConnectionConfig)7 MXDataHandler (org.matrix.androidsdk.MXDataHandler)7 MXSession (org.matrix.androidsdk.MXSession)7 MXFileStore (org.matrix.androidsdk.data.store.MXFileStore)7 Credentials (org.matrix.androidsdk.rest.model.login.Credentials)7 IMXStore (org.matrix.androidsdk.data.store.IMXStore)6 Context (android.content.Context)5 MXStoreListener (org.matrix.androidsdk.data.store.MXStoreListener)5 JsonObject (com.google.gson.JsonObject)4 Test (org.junit.Test)4 CryptoTestData (org.matrix.androidsdk.common.CryptoTestData)4 Room (org.matrix.androidsdk.data.Room)4 MXEventListener (org.matrix.androidsdk.listeners.MXEventListener)4 Event (org.matrix.androidsdk.rest.model.Event)4 RoomState (org.matrix.androidsdk.data.RoomState)2 LoginRestClient (org.matrix.androidsdk.rest.client.LoginRestClient)2 NonNull (androidx.annotation.NonNull)1 ArrayList (java.util.ArrayList)1