Migrate SetupController to a ViewModel

Solves #1865
This commit is contained in:
Daniel Lublin
2021-01-12 11:14:08 +01:00
parent 5aa24414c6
commit a349bd146c
18 changed files with 370 additions and 289 deletions

View File

@@ -1,63 +0,0 @@
package org.briarproject.briar.android.account;
import org.briarproject.bramble.api.account.AccountManager;
import org.briarproject.bramble.api.crypto.PasswordStrengthEstimator;
import org.briarproject.bramble.test.BrambleMockTestCase;
import org.briarproject.bramble.test.ImmediateExecutor;
import org.jmock.Expectations;
import org.jmock.lib.legacy.ClassImposteriser;
import org.junit.Test;
import java.util.concurrent.Executor;
import java.util.concurrent.atomic.AtomicBoolean;
import static junit.framework.Assert.assertTrue;
import static org.briarproject.bramble.api.identity.AuthorConstants.MAX_AUTHOR_NAME_LENGTH;
import static org.briarproject.bramble.util.StringUtils.getRandomString;
public class SetupControllerImplTest extends BrambleMockTestCase {
private final AccountManager accountManager =
context.mock(AccountManager.class);
private final PasswordStrengthEstimator estimator =
context.mock(PasswordStrengthEstimator.class);
private final SetupActivity setupActivity;
private final Executor ioExecutor = new ImmediateExecutor();
private final String authorName = getRandomString(MAX_AUTHOR_NAME_LENGTH);
private final String password = getRandomString(10);
public SetupControllerImplTest() {
context.setImposteriser(ClassImposteriser.INSTANCE);
setupActivity = context.mock(SetupActivity.class);
}
@Test
@SuppressWarnings("ResultOfMethodCallIgnored")
public void testCreateAccount() {
context.checking(new Expectations() {{
// Set the author name and password
oneOf(setupActivity).setAuthorName(authorName);
oneOf(setupActivity).setPassword(password);
// Get the author name and password
oneOf(setupActivity).getAuthorName();
will(returnValue(authorName));
oneOf(setupActivity).getPassword();
will(returnValue(password));
// Create the account
oneOf(accountManager).createAccount(authorName, password);
will(returnValue(true));
}});
SetupControllerImpl s = new SetupControllerImpl(accountManager,
ioExecutor, estimator);
s.setSetupActivity(setupActivity);
AtomicBoolean called = new AtomicBoolean(false);
s.setAuthorName(authorName);
s.setPassword(password);
s.createAccount(result -> called.set(true));
assertTrue(called.get());
}
}

View File

@@ -0,0 +1,73 @@
package org.briarproject.briar.android.account;
import android.app.Application;
import android.content.Context;
import org.briarproject.bramble.api.account.AccountManager;
import org.briarproject.bramble.api.crypto.PasswordStrengthEstimator;
import org.briarproject.bramble.test.BrambleMockTestCase;
import org.briarproject.bramble.test.ImmediateExecutor;
import org.briarproject.briar.android.account.SetupViewModel.State;
import org.jmock.Expectations;
import org.jmock.lib.legacy.ClassImposteriser;
import org.junit.Rule;
import org.junit.Test;
import androidx.arch.core.executor.testing.InstantTaskExecutorRule;
import static junit.framework.Assert.assertEquals;
import static org.briarproject.bramble.api.identity.AuthorConstants.MAX_AUTHOR_NAME_LENGTH;
import static org.briarproject.bramble.util.StringUtils.getRandomString;
import static org.briarproject.briar.android.account.SetupViewModel.State.CREATED;
import static org.briarproject.briar.android.viewmodel.LiveEventTestUtil.getOrAwaitValue;
public class SetupViewModelTest extends BrambleMockTestCase {
@Rule
public final InstantTaskExecutorRule testRule =
new InstantTaskExecutorRule();
private final String authorName = getRandomString(MAX_AUTHOR_NAME_LENGTH);
private final String password = getRandomString(10);
private final Application app;
private final Context appContext;
private final AccountManager accountManager;
private final DozeHelper dozeHelper;
public SetupViewModelTest() {
context.setImposteriser(ClassImposteriser.INSTANCE);
app = context.mock(Application.class);
appContext = context.mock(Context.class);
accountManager = context.mock(AccountManager.class);
dozeHelper = context.mock(DozeHelper.class);
}
@Test
public void testCreateAccount() throws Exception {
context.checking(new Expectations() {{
oneOf(accountManager).accountExists();
will(returnValue(false));
allowing(dozeHelper).needToShowDozeFragment(app);
allowing(app).getApplicationContext();
will(returnValue(appContext));
allowing(appContext).getPackageManager();
// Create the account
oneOf(accountManager).createAccount(authorName, password);
will(returnValue(true));
}});
SetupViewModel viewModel = new SetupViewModel(app,
accountManager,
new ImmediateExecutor(),
context.mock(PasswordStrengthEstimator.class),
dozeHelper);
viewModel.setAuthorName(authorName);
viewModel.setPassword(password);
State state = getOrAwaitValue(viewModel.getState());
assertEquals(CREATED, state);
}
}

View File

@@ -0,0 +1,34 @@
/* Copyright 2019 Google LLC.
SPDX-License-Identifier: Apache-2.0
https://gist.github.com/JoseAlcerreca/1e9ee05dcdd6a6a6fa1cbfc125559bba
*/
package org.briarproject.briar.android.viewmodel;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;
public class LiveEventTestUtil {
public static <T> T getOrAwaitValue(final LiveEvent<T> liveEvent)
throws InterruptedException {
final AtomicReference<T> data = new AtomicReference<>();
final CountDownLatch latch = new CountDownLatch(1);
liveEvent.observeEventForever(new LiveEvent.LiveEventHandler<T>() {
@Override
public void onEvent(T o) {
data.set(o);
latch.countDown();
// LiveEventHandler is wrapped internally in an observer that we
// don't have a reference to. Skip trying to remove the observer
// for now; all is torn down at the end of testing anyway.
}
});
// Don't wait indefinitely if the LiveEvent is not set.
if (!latch.await(2, TimeUnit.SECONDS)) {
throw new RuntimeException("LiveEvent value was never set.");
}
return data.get();
}
}