mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-12 18:59:06 +01:00
Updated java.library.path.
This commit is contained in:
@@ -0,0 +1,7 @@
|
||||
package android.net.http;
|
||||
|
||||
// This class is here to fix an issue with Robolectric.
|
||||
// https://github.com/robolectric/robolectric/issues/1862
|
||||
// TODO: Check if this class can be removed on next Robolectric update
|
||||
public class AndroidHttpClient {
|
||||
}
|
||||
@@ -0,0 +1,226 @@
|
||||
package org.briarproject.briar.android;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.SharedPreferences;
|
||||
import android.support.design.widget.TextInputLayout;
|
||||
import android.widget.Button;
|
||||
import android.widget.EditText;
|
||||
|
||||
import com.google.common.base.Strings;
|
||||
|
||||
import org.briarproject.bramble.api.identity.AuthorConstants;
|
||||
import org.briarproject.briar.BuildConfig;
|
||||
import org.briarproject.briar.R;
|
||||
import org.briarproject.briar.android.controller.handler.ResultHandler;
|
||||
import org.briarproject.briar.android.login.SetupController;
|
||||
import org.briarproject.briar.android.login.StrengthMeter;
|
||||
import org.briarproject.briar.android.navdrawer.NavDrawerActivity;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
import org.mockito.Captor;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.Mockito;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
import org.robolectric.Robolectric;
|
||||
import org.robolectric.RobolectricGradleTestRunner;
|
||||
import org.robolectric.annotation.Config;
|
||||
import org.robolectric.shadows.ShadowActivity;
|
||||
|
||||
import static junit.framework.Assert.assertEquals;
|
||||
import static org.briarproject.bramble.api.crypto.PasswordStrengthEstimator.NONE;
|
||||
import static org.briarproject.bramble.api.crypto.PasswordStrengthEstimator.QUITE_STRONG;
|
||||
import static org.briarproject.bramble.api.crypto.PasswordStrengthEstimator.QUITE_WEAK;
|
||||
import static org.briarproject.bramble.api.crypto.PasswordStrengthEstimator.STRONG;
|
||||
import static org.briarproject.bramble.api.crypto.PasswordStrengthEstimator.WEAK;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.mockito.Matchers.anyString;
|
||||
import static org.mockito.Matchers.eq;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.timeout;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
import static org.robolectric.Shadows.shadowOf;
|
||||
|
||||
@RunWith(RobolectricGradleTestRunner.class)
|
||||
@Config(constants = BuildConfig.class, sdk = 21,
|
||||
application = TestBriarApplication.class)
|
||||
public class SetupActivityTest {
|
||||
|
||||
private static final int TIMEOUT_MS = 10 * 1000;
|
||||
|
||||
private TestSetupActivity setupActivity;
|
||||
private TextInputLayout nicknameEntryWrapper;
|
||||
private TextInputLayout passwordConfirmationWrapper;
|
||||
private EditText nicknameEntry;
|
||||
private EditText passwordEntry;
|
||||
private EditText passwordConfirmation;
|
||||
private StrengthMeter strengthMeter;
|
||||
private Button createAccountButton;
|
||||
|
||||
@Mock
|
||||
private SetupController setupController;
|
||||
@Captor
|
||||
private ArgumentCaptor<ResultHandler<Void>> authorCaptor;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
setupActivity = Robolectric.setupActivity(TestSetupActivity.class);
|
||||
nicknameEntryWrapper = (TextInputLayout) setupActivity
|
||||
.findViewById(R.id.nickname_entry_wrapper);
|
||||
passwordConfirmationWrapper = (TextInputLayout) setupActivity
|
||||
.findViewById(R.id.password_confirm_wrapper);
|
||||
nicknameEntry =
|
||||
(EditText) setupActivity.findViewById(R.id.nickname_entry);
|
||||
passwordEntry =
|
||||
(EditText) setupActivity.findViewById(R.id.password_entry);
|
||||
passwordConfirmation =
|
||||
(EditText) setupActivity.findViewById(R.id.password_confirm);
|
||||
strengthMeter =
|
||||
(StrengthMeter) setupActivity.findViewById(R.id.strength_meter);
|
||||
createAccountButton =
|
||||
(Button) setupActivity.findViewById(R.id.create_account);
|
||||
}
|
||||
|
||||
private void testStrengthMeter(String pass, float strength, int color) {
|
||||
passwordEntry.setText(pass);
|
||||
assertEquals(strengthMeter.getProgress(),
|
||||
(int) (strengthMeter.getMax() * strength));
|
||||
assertEquals(color, strengthMeter.getColor());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPasswordMatchUI() {
|
||||
// Password mismatch
|
||||
passwordEntry.setText("really.safe.password");
|
||||
passwordConfirmation.setText("really.safe.pass");
|
||||
assertEquals(createAccountButton.isEnabled(), false);
|
||||
assertEquals(passwordConfirmationWrapper.getError(),
|
||||
setupActivity.getString(R.string.passwords_do_not_match));
|
||||
// Button enabled
|
||||
passwordEntry.setText("really.safe.pass");
|
||||
passwordConfirmation.setText("really.safe.pass");
|
||||
// Confirm that the password mismatch error message is not visible
|
||||
Assert.assertNotEquals(passwordConfirmationWrapper.getError(),
|
||||
setupActivity.getString(R.string.passwords_do_not_match));
|
||||
// Nick has not been set, expect the button to be disabled
|
||||
assertEquals(createAccountButton.isEnabled(), false);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCreateAccountUI() {
|
||||
SetupController mockedController = this.setupController;
|
||||
setupActivity.setController(mockedController);
|
||||
// Mock strong password strength answer
|
||||
when(mockedController.estimatePasswordStrength(anyString())).thenReturn(
|
||||
STRONG);
|
||||
String safePass = "really.safe.password";
|
||||
String nick = "nick.nickerton";
|
||||
passwordEntry.setText(safePass);
|
||||
passwordConfirmation.setText(safePass);
|
||||
nicknameEntry.setText(nick);
|
||||
// Confirm that the create account button is clickable
|
||||
assertEquals(createAccountButton.isEnabled(), true);
|
||||
createAccountButton.performClick();
|
||||
// Verify that the controller's method was called with the correct
|
||||
// params and get the callback
|
||||
verify(mockedController, times(1))
|
||||
.storeAuthorInfo(eq(nick), eq(safePass),
|
||||
authorCaptor.capture());
|
||||
authorCaptor.getValue().onResult(null);
|
||||
// execute the callback
|
||||
assertEquals(setupActivity.isFinishing(), true);
|
||||
// Confirm that the correct Activity has been started
|
||||
ShadowActivity shadowActivity = shadowOf(setupActivity);
|
||||
Intent intent = shadowActivity.peekNextStartedActivity();
|
||||
assertEquals(intent.getComponent().getClassName(),
|
||||
NavDrawerActivity.class.getName());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNicknameUI() {
|
||||
Assert.assertNotNull(setupActivity);
|
||||
String longNick =
|
||||
Strings.padEnd("*", AuthorConstants.MAX_AUTHOR_NAME_LENGTH + 1,
|
||||
'*');
|
||||
nicknameEntry.setText(longNick);
|
||||
// Nickname should be too long
|
||||
assertEquals(nicknameEntryWrapper.getError(),
|
||||
setupActivity.getString(R.string.name_too_long));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAccountCreation() {
|
||||
SetupController controller = setupActivity.getController();
|
||||
// mock a resulthandler
|
||||
ResultHandler<Void> resultHandler =
|
||||
(ResultHandler<Void>) mock(ResultHandler.class);
|
||||
controller.storeAuthorInfo("nick", "some.strong.pass", resultHandler);
|
||||
// blocking verification call with timeout that waits until the mocked
|
||||
// result gets called with handle 0L, the expected value
|
||||
verify(resultHandler, timeout(TIMEOUT_MS).times(1)).onResult(null);
|
||||
SharedPreferences prefs =
|
||||
setupActivity.getSharedPreferences("db", Context.MODE_PRIVATE);
|
||||
// Confirm database key
|
||||
assertTrue(prefs.contains("key"));
|
||||
// Note that Robolectric uses its own persistant storage that it
|
||||
// wipes clean after each test run, no need to clean up manually.
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStrengthMeter() {
|
||||
SetupController controller = setupActivity.getController();
|
||||
|
||||
String strongPass = "very.strong.password.123";
|
||||
String weakPass = "we";
|
||||
String quiteStrongPass = "quite.strong";
|
||||
|
||||
float val = controller.estimatePasswordStrength(strongPass);
|
||||
assertTrue(val == STRONG);
|
||||
val = controller.estimatePasswordStrength(weakPass);
|
||||
assertTrue(val < WEAK && val > NONE);
|
||||
val = controller.estimatePasswordStrength(quiteStrongPass);
|
||||
assertTrue(val < STRONG && val > QUITE_WEAK);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStrengthMeterUI() {
|
||||
Assert.assertNotNull(setupActivity);
|
||||
// replace the setup controller with our mocked copy
|
||||
SetupController mockedController = this.setupController;
|
||||
setupActivity.setController(mockedController);
|
||||
// Mock answers for UI testing only
|
||||
when(mockedController.estimatePasswordStrength("strong")).thenReturn(
|
||||
STRONG);
|
||||
when(mockedController.estimatePasswordStrength("qstring")).thenReturn(
|
||||
QUITE_STRONG);
|
||||
when(mockedController.estimatePasswordStrength("qweak")).thenReturn(
|
||||
QUITE_WEAK);
|
||||
when(mockedController.estimatePasswordStrength("weak")).thenReturn(
|
||||
WEAK);
|
||||
when(mockedController.estimatePasswordStrength("empty")).thenReturn(
|
||||
NONE);
|
||||
// Test the meters progress and color for several values
|
||||
testStrengthMeter("strong", STRONG, StrengthMeter.GREEN);
|
||||
Mockito.verify(mockedController, Mockito.times(1))
|
||||
.estimatePasswordStrength(eq("strong"));
|
||||
testStrengthMeter("qstring", QUITE_STRONG, StrengthMeter.LIME);
|
||||
Mockito.verify(mockedController, Mockito.times(1))
|
||||
.estimatePasswordStrength(eq("qstring"));
|
||||
testStrengthMeter("qweak", QUITE_WEAK, StrengthMeter.YELLOW);
|
||||
Mockito.verify(mockedController, Mockito.times(1))
|
||||
.estimatePasswordStrength(eq("qweak"));
|
||||
testStrengthMeter("weak", WEAK, StrengthMeter.ORANGE);
|
||||
Mockito.verify(mockedController, Mockito.times(1))
|
||||
.estimatePasswordStrength(eq("weak"));
|
||||
// Not sure this should be the correct behaviour on an empty input ?
|
||||
testStrengthMeter("empty", NONE, StrengthMeter.RED);
|
||||
Mockito.verify(mockedController, Mockito.times(1))
|
||||
.estimatePasswordStrength(eq("empty"));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package org.briarproject.briar.android;
|
||||
|
||||
import android.app.Application;
|
||||
|
||||
import org.briarproject.bramble.BrambleCoreModule;
|
||||
import org.briarproject.briar.BriarCoreModule;
|
||||
|
||||
import java.util.logging.Logger;
|
||||
|
||||
/**
|
||||
* This class only exists to avoid static initialisation of ACRA
|
||||
*/
|
||||
public class TestBriarApplication extends Application
|
||||
implements BriarApplication {
|
||||
|
||||
private static final Logger LOG =
|
||||
Logger.getLogger(TestBriarApplication.class.getName());
|
||||
|
||||
private AndroidComponent applicationComponent;
|
||||
|
||||
@Override
|
||||
public void onCreate() {
|
||||
super.onCreate();
|
||||
LOG.info("Created");
|
||||
|
||||
applicationComponent = DaggerAndroidComponent.builder()
|
||||
.appModule(new AppModule(this))
|
||||
.build();
|
||||
|
||||
// We need to load the eager singletons directly after making the
|
||||
// dependency graphs
|
||||
BrambleCoreModule.initEagerSingletons(applicationComponent);
|
||||
BriarCoreModule.initEagerSingletons(applicationComponent);
|
||||
AndroidEagerSingletons.initEagerSingletons(applicationComponent);
|
||||
}
|
||||
|
||||
@Override
|
||||
public AndroidComponent getApplicationComponent() {
|
||||
return applicationComponent;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package org.briarproject.briar.android;
|
||||
|
||||
import org.briarproject.briar.android.login.SetupActivity;
|
||||
import org.briarproject.briar.android.login.SetupController;
|
||||
|
||||
/**
|
||||
* This class exposes the SetupController and offers the possibility to
|
||||
* override it.
|
||||
*/
|
||||
public class TestSetupActivity extends SetupActivity {
|
||||
|
||||
public SetupController getController() {
|
||||
return setupController;
|
||||
}
|
||||
|
||||
public void setController(SetupController setupController) {
|
||||
this.setupController = setupController;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,141 @@
|
||||
package org.briarproject.briar.android.forum;
|
||||
|
||||
import android.content.Intent;
|
||||
|
||||
import junit.framework.Assert;
|
||||
|
||||
import org.briarproject.TestUtils;
|
||||
import org.briarproject.bramble.api.db.DbException;
|
||||
import org.briarproject.bramble.api.identity.Author;
|
||||
import org.briarproject.bramble.api.identity.AuthorId;
|
||||
import org.briarproject.bramble.api.sync.MessageId;
|
||||
import org.briarproject.briar.BuildConfig;
|
||||
import org.briarproject.briar.android.TestBriarApplication;
|
||||
import org.briarproject.briar.android.controller.handler.UiResultExceptionHandler;
|
||||
import org.briarproject.briar.android.threaded.ThreadItemAdapter;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
import org.mockito.Captor;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
import org.robolectric.Robolectric;
|
||||
import org.robolectric.RobolectricGradleTestRunner;
|
||||
import org.robolectric.annotation.Config;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
import static junit.framework.Assert.assertEquals;
|
||||
import static junit.framework.Assert.assertTrue;
|
||||
import static org.briarproject.bramble.api.identity.Author.Status.UNKNOWN;
|
||||
import static org.briarproject.bramble.api.identity.AuthorConstants.MAX_PUBLIC_KEY_LENGTH;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
|
||||
@RunWith(RobolectricGradleTestRunner.class)
|
||||
@Config(constants = BuildConfig.class, sdk = 21,
|
||||
application = TestBriarApplication.class)
|
||||
public class ForumActivityTest {
|
||||
|
||||
private final static String AUTHOR_1 = "Author 1";
|
||||
private final static String AUTHOR_2 = "Author 2";
|
||||
private final static String AUTHOR_3 = "Author 3";
|
||||
private final static String AUTHOR_4 = "Author 4";
|
||||
private final static String AUTHOR_5 = "Author 5";
|
||||
private final static String AUTHOR_6 = "Author 6";
|
||||
|
||||
private final static String[] AUTHORS = {
|
||||
AUTHOR_1, AUTHOR_2, AUTHOR_3, AUTHOR_4, AUTHOR_5, AUTHOR_6
|
||||
};
|
||||
|
||||
private final static MessageId[] AUTHOR_IDS = new MessageId[AUTHORS.length];
|
||||
|
||||
static {
|
||||
for (int i = 0; i < AUTHOR_IDS.length; i++)
|
||||
AUTHOR_IDS[i] = new MessageId(TestUtils.getRandomId());
|
||||
}
|
||||
|
||||
private final static MessageId[] PARENT_AUTHOR_IDS = {
|
||||
null,
|
||||
AUTHOR_IDS[0],
|
||||
AUTHOR_IDS[1],
|
||||
AUTHOR_IDS[2],
|
||||
AUTHOR_IDS[0],
|
||||
null
|
||||
};
|
||||
|
||||
/*
|
||||
1
|
||||
-> 2
|
||||
-> 3
|
||||
-> 4
|
||||
5
|
||||
6
|
||||
*/
|
||||
private final static int[] LEVELS = {
|
||||
0, 1, 2, 3, 1, 0
|
||||
};
|
||||
|
||||
private TestForumActivity forumActivity;
|
||||
@Captor
|
||||
private ArgumentCaptor<UiResultExceptionHandler<Collection<ForumItem>, DbException>>
|
||||
rc;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
Intent intent = new Intent();
|
||||
intent.putExtra("briar.GROUP_ID", TestUtils.getRandomId());
|
||||
forumActivity = Robolectric.buildActivity(TestForumActivity.class)
|
||||
.withIntent(intent).create().resume().get();
|
||||
}
|
||||
|
||||
private List<ForumItem> getDummyData() {
|
||||
ForumItem[] forumItems = new ForumItem[6];
|
||||
for (int i = 0; i < forumItems.length; i++) {
|
||||
AuthorId authorId = new AuthorId(TestUtils.getRandomId());
|
||||
byte[] publicKey = TestUtils.getRandomBytes(MAX_PUBLIC_KEY_LENGTH);
|
||||
Author author = new Author(authorId, AUTHORS[i], publicKey);
|
||||
forumItems[i] = new ForumItem(AUTHOR_IDS[i], PARENT_AUTHOR_IDS[i],
|
||||
AUTHORS[i], System.currentTimeMillis(), author, UNKNOWN);
|
||||
forumItems[i].setLevel(LEVELS[i]);
|
||||
}
|
||||
return new ArrayList<>(Arrays.asList(forumItems));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNestedEntries() {
|
||||
ForumController mc = forumActivity.getController();
|
||||
List<ForumItem> dummyData = getDummyData();
|
||||
verify(mc, times(1)).loadItems(rc.capture());
|
||||
rc.getValue().onResult(dummyData);
|
||||
ThreadItemAdapter<ForumItem> adapter = forumActivity.getAdapter();
|
||||
Assert.assertNotNull(adapter);
|
||||
// Cascade close
|
||||
assertEquals(6, adapter.getItemCount());
|
||||
adapter.hideDescendants(dummyData.get(2));
|
||||
assertEquals(5, adapter.getItemCount());
|
||||
adapter.hideDescendants(dummyData.get(1));
|
||||
assertEquals(4, adapter.getItemCount());
|
||||
adapter.hideDescendants(dummyData.get(0));
|
||||
assertEquals(2, adapter.getItemCount());
|
||||
assertTrue(dummyData.get(0).getText()
|
||||
.equals(adapter.getVisibleItem(0).getText()));
|
||||
assertTrue(dummyData.get(5).getText()
|
||||
.equals(adapter.getVisibleItem(1).getText()));
|
||||
// Cascade re-open
|
||||
adapter.showDescendants(dummyData.get(0));
|
||||
assertEquals(4, adapter.getItemCount());
|
||||
adapter.showDescendants(dummyData.get(1));
|
||||
assertEquals(5, adapter.getItemCount());
|
||||
adapter.showDescendants(dummyData.get(2));
|
||||
assertEquals(6, adapter.getItemCount());
|
||||
assertTrue(dummyData.get(2).getText()
|
||||
.equals(adapter.getVisibleItem(2).getText()));
|
||||
assertTrue(dummyData.get(4).getText()
|
||||
.equals(adapter.getVisibleItem(4).getText()));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
package org.briarproject.briar.android.forum;
|
||||
|
||||
import org.briarproject.bramble.api.nullsafety.MethodsNotNullByDefault;
|
||||
import org.briarproject.bramble.api.nullsafety.ParametersNotNullByDefault;
|
||||
import org.briarproject.briar.android.activity.ActivityModule;
|
||||
import org.briarproject.briar.android.activity.BaseActivity;
|
||||
import org.briarproject.briar.android.controller.BriarController;
|
||||
import org.briarproject.briar.android.controller.BriarControllerImpl;
|
||||
import org.briarproject.briar.android.threaded.ThreadItemAdapter;
|
||||
import org.mockito.Mockito;
|
||||
|
||||
/**
|
||||
* This class exposes the ForumController and offers the possibility to
|
||||
* override it.
|
||||
*/
|
||||
@MethodsNotNullByDefault
|
||||
@ParametersNotNullByDefault
|
||||
public class TestForumActivity extends ForumActivity {
|
||||
|
||||
@Override
|
||||
public ForumController getController() {
|
||||
return forumController;
|
||||
}
|
||||
|
||||
public ThreadItemAdapter<ForumItem> getAdapter() {
|
||||
return adapter;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected ActivityModule getActivityModule() {
|
||||
return new ActivityModule(this) {
|
||||
|
||||
@Override
|
||||
protected BriarController provideBriarController(
|
||||
BriarControllerImpl briarController) {
|
||||
BriarController c = Mockito.mock(BriarController.class);
|
||||
Mockito.when(c.hasEncryptionKey()).thenReturn(true);
|
||||
return c;
|
||||
}
|
||||
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
protected ForumModule getForumModule() {
|
||||
return new ForumModule() {
|
||||
@Override
|
||||
ForumController provideForumController(BaseActivity activity,
|
||||
ForumControllerImpl forumController) {
|
||||
return Mockito.mock(ForumController.class);
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,222 @@
|
||||
package org.briarproject.briar.android.login;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.SharedPreferences;
|
||||
import android.support.design.widget.TextInputLayout;
|
||||
import android.widget.Button;
|
||||
import android.widget.EditText;
|
||||
|
||||
import org.briarproject.briar.BuildConfig;
|
||||
import org.briarproject.briar.R;
|
||||
import org.briarproject.briar.android.TestBriarApplication;
|
||||
import org.briarproject.briar.android.controller.handler.ResultHandler;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
import org.mockito.Captor;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.Mockito;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
import org.robolectric.Robolectric;
|
||||
import org.robolectric.RobolectricGradleTestRunner;
|
||||
import org.robolectric.annotation.Config;
|
||||
|
||||
import static junit.framework.Assert.assertEquals;
|
||||
import static org.briarproject.bramble.api.crypto.PasswordStrengthEstimator.NONE;
|
||||
import static org.briarproject.bramble.api.crypto.PasswordStrengthEstimator.QUITE_STRONG;
|
||||
import static org.briarproject.bramble.api.crypto.PasswordStrengthEstimator.QUITE_WEAK;
|
||||
import static org.briarproject.bramble.api.crypto.PasswordStrengthEstimator.STRONG;
|
||||
import static org.briarproject.bramble.api.crypto.PasswordStrengthEstimator.WEAK;
|
||||
import static org.junit.Assert.assertNotEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.mockito.Matchers.anyString;
|
||||
import static org.mockito.Matchers.eq;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.timeout;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
@RunWith(RobolectricGradleTestRunner.class)
|
||||
@Config(constants = BuildConfig.class, sdk = 21,
|
||||
application = TestBriarApplication.class)
|
||||
public class ChangePasswordActivityTest {
|
||||
|
||||
private static final int TIMEOUT_MS = 10 * 1000;
|
||||
|
||||
private TestChangePasswordActivity changePasswordActivity;
|
||||
private TextInputLayout passwordConfirmationWrapper;
|
||||
private EditText currentPassword;
|
||||
private EditText newPassword;
|
||||
private EditText newPasswordConfirmation;
|
||||
private StrengthMeter strengthMeter;
|
||||
private Button changePasswordButton;
|
||||
|
||||
@Mock
|
||||
private PasswordController passwordController;
|
||||
@Mock
|
||||
private SetupController setupController;
|
||||
@Captor
|
||||
private ArgumentCaptor<ResultHandler<Boolean>> resultCaptor;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
changePasswordActivity =
|
||||
Robolectric.setupActivity(TestChangePasswordActivity.class);
|
||||
passwordConfirmationWrapper = (TextInputLayout) changePasswordActivity
|
||||
.findViewById(R.id.new_password_confirm_wrapper);
|
||||
currentPassword = (EditText) changePasswordActivity
|
||||
.findViewById(R.id.current_password_entry);
|
||||
newPassword = (EditText) changePasswordActivity
|
||||
.findViewById(R.id.new_password_entry);
|
||||
newPasswordConfirmation = (EditText) changePasswordActivity
|
||||
.findViewById(R.id.new_password_confirm);
|
||||
strengthMeter = (StrengthMeter) changePasswordActivity
|
||||
.findViewById(R.id.strength_meter);
|
||||
changePasswordButton = (Button) changePasswordActivity
|
||||
.findViewById(R.id.change_password);
|
||||
}
|
||||
|
||||
private void testStrengthMeter(String pass, float strength, int color) {
|
||||
newPassword.setText(pass);
|
||||
assertEquals(strengthMeter.getProgress(),
|
||||
(int) (strengthMeter.getMax() * strength));
|
||||
assertEquals(color, strengthMeter.getColor());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPasswordMatchUI() {
|
||||
// Password mismatch
|
||||
newPassword.setText("really.safe.password");
|
||||
newPasswordConfirmation.setText("really.safe.pass");
|
||||
assertEquals(changePasswordButton.isEnabled(), false);
|
||||
assertEquals(passwordConfirmationWrapper.getError(),
|
||||
changePasswordActivity
|
||||
.getString(R.string.passwords_do_not_match));
|
||||
// Button enabled
|
||||
newPassword.setText("really.safe.pass");
|
||||
newPasswordConfirmation.setText("really.safe.pass");
|
||||
// Confirm that the password mismatch error message is not visible
|
||||
Assert.assertNotEquals(passwordConfirmationWrapper.getError(),
|
||||
changePasswordActivity
|
||||
.getString(R.string.passwords_do_not_match));
|
||||
// Nick has not been set, expect the button to be disabled
|
||||
assertEquals(changePasswordButton.isEnabled(), false);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testChangePasswordUI() {
|
||||
PasswordController mockedPasswordController = this.passwordController;
|
||||
SetupController mockedSetupController = this.setupController;
|
||||
changePasswordActivity.setPasswordController(mockedPasswordController);
|
||||
changePasswordActivity.setSetupController(mockedSetupController);
|
||||
// Mock strong password strength answer
|
||||
when(mockedSetupController.estimatePasswordStrength(anyString()))
|
||||
.thenReturn(STRONG);
|
||||
String curPass = "old.password";
|
||||
String safePass = "really.safe.password";
|
||||
currentPassword.setText(curPass);
|
||||
newPassword.setText(safePass);
|
||||
newPasswordConfirmation.setText(safePass);
|
||||
// Confirm that the create account button is clickable
|
||||
assertEquals(changePasswordButton.isEnabled(), true);
|
||||
changePasswordButton.performClick();
|
||||
// Verify that the controller's method was called with the correct
|
||||
// params and get the callback
|
||||
verify(mockedPasswordController, times(1))
|
||||
.changePassword(eq(curPass), eq(safePass),
|
||||
resultCaptor.capture());
|
||||
// execute the callbacks
|
||||
resultCaptor.getValue().onResult(true);
|
||||
assertEquals(changePasswordActivity.isFinishing(), true);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPasswordChange() {
|
||||
PasswordController passwordController =
|
||||
changePasswordActivity.getPasswordController();
|
||||
SetupController setupController =
|
||||
changePasswordActivity.getSetupController();
|
||||
// mock a resulthandler
|
||||
ResultHandler<Void> resultHandler =
|
||||
(ResultHandler<Void>) mock(ResultHandler.class);
|
||||
setupController.storeAuthorInfo("nick", "some.old.pass", resultHandler);
|
||||
// blocking verification call with timeout that waits until the mocked
|
||||
// result gets called with handle 0L, the expected value
|
||||
verify(resultHandler, timeout(TIMEOUT_MS).times(1)).onResult(null);
|
||||
SharedPreferences prefs = changePasswordActivity
|
||||
.getSharedPreferences("db", Context.MODE_PRIVATE);
|
||||
// Confirm database key
|
||||
assertTrue(prefs.contains("key"));
|
||||
String oldKey = prefs.getString("key", null);
|
||||
// mock a resulthandler
|
||||
ResultHandler<Boolean> resultHandler2 =
|
||||
(ResultHandler<Boolean>) mock(ResultHandler.class);
|
||||
passwordController.changePassword("some.old.pass", "some.strong.pass",
|
||||
resultHandler2);
|
||||
// blocking verification call with timeout that waits until the mocked
|
||||
// result gets called with handle 0L, the expected value
|
||||
verify(resultHandler2, timeout(TIMEOUT_MS).times(1)).onResult(true);
|
||||
// Confirm database key
|
||||
assertTrue(prefs.contains("key"));
|
||||
assertNotEquals(oldKey, prefs.getString("key", null));
|
||||
// Note that Robolectric uses its own persistant storage that it
|
||||
// wipes clean after each test run, no need to clean up manually.
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStrengthMeter() {
|
||||
SetupController controller =
|
||||
changePasswordActivity.getSetupController();
|
||||
|
||||
String strongPass = "very.strong.password.123";
|
||||
String weakPass = "we";
|
||||
String quiteStrongPass = "quite.strong";
|
||||
|
||||
float val = controller.estimatePasswordStrength(strongPass);
|
||||
assertTrue(val == STRONG);
|
||||
val = controller.estimatePasswordStrength(weakPass);
|
||||
assertTrue(val < WEAK && val > NONE);
|
||||
val = controller.estimatePasswordStrength(quiteStrongPass);
|
||||
assertTrue(val < STRONG && val > QUITE_WEAK);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStrengthMeterUI() {
|
||||
Assert.assertNotNull(changePasswordActivity);
|
||||
// replace the setup controller with our mocked copy
|
||||
SetupController mockedController = this.setupController;
|
||||
changePasswordActivity.setSetupController(mockedController);
|
||||
// Mock answers for UI testing only
|
||||
when(mockedController.estimatePasswordStrength("strong")).thenReturn(
|
||||
STRONG);
|
||||
when(mockedController.estimatePasswordStrength("qstring")).thenReturn(
|
||||
QUITE_STRONG);
|
||||
when(mockedController.estimatePasswordStrength("qweak")).thenReturn(
|
||||
QUITE_WEAK);
|
||||
when(mockedController.estimatePasswordStrength("weak")).thenReturn(
|
||||
WEAK);
|
||||
when(mockedController.estimatePasswordStrength("empty")).thenReturn(
|
||||
NONE);
|
||||
// Test the meters progress and color for several values
|
||||
testStrengthMeter("strong", STRONG, StrengthMeter.GREEN);
|
||||
Mockito.verify(mockedController, Mockito.times(1))
|
||||
.estimatePasswordStrength(eq("strong"));
|
||||
testStrengthMeter("qstring", QUITE_STRONG, StrengthMeter.LIME);
|
||||
Mockito.verify(mockedController, Mockito.times(1))
|
||||
.estimatePasswordStrength(eq("qstring"));
|
||||
testStrengthMeter("qweak", QUITE_WEAK, StrengthMeter.YELLOW);
|
||||
Mockito.verify(mockedController, Mockito.times(1))
|
||||
.estimatePasswordStrength(eq("qweak"));
|
||||
testStrengthMeter("weak", WEAK, StrengthMeter.ORANGE);
|
||||
Mockito.verify(mockedController, Mockito.times(1))
|
||||
.estimatePasswordStrength(eq("weak"));
|
||||
// Not sure this should be the correct behaviour on an empty input ?
|
||||
testStrengthMeter("empty", NONE, StrengthMeter.RED);
|
||||
Mockito.verify(mockedController, Mockito.times(1))
|
||||
.estimatePasswordStrength(eq("empty"));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package org.briarproject.briar.android.login;
|
||||
|
||||
/**
|
||||
* This class exposes the PasswordController and SetupController and offers the
|
||||
* possibility to override them.
|
||||
*/
|
||||
public class TestChangePasswordActivity extends ChangePasswordActivity {
|
||||
|
||||
public PasswordController getPasswordController() {
|
||||
return passwordController;
|
||||
}
|
||||
|
||||
public SetupController getSetupController() {
|
||||
return setupController;
|
||||
}
|
||||
|
||||
public void setPasswordController(PasswordController passwordController) {
|
||||
this.passwordController = passwordController;
|
||||
}
|
||||
|
||||
public void setSetupController(SetupController setupController) {
|
||||
this.setupController = setupController;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user