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;
}
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;
}
Aggregations