mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-19 06:09:55 +01:00
finished Activity mock
This commit is contained in:
@@ -27,8 +27,8 @@ import javax.inject.Inject;
|
|||||||
import static android.content.Intent.FLAG_ACTIVITY_NEW_TASK;
|
import static android.content.Intent.FLAG_ACTIVITY_NEW_TASK;
|
||||||
import static android.view.View.INVISIBLE;
|
import static android.view.View.INVISIBLE;
|
||||||
import static android.view.View.VISIBLE;
|
import static android.view.View.VISIBLE;
|
||||||
import static org.briarproject.android.TestingConstants.PREVENT_SCREENSHOTS;
|
|
||||||
import static android.view.WindowManager.LayoutParams.FLAG_SECURE;
|
import static android.view.WindowManager.LayoutParams.FLAG_SECURE;
|
||||||
|
import static org.briarproject.android.TestingConstants.PREVENT_SCREENSHOTS;
|
||||||
import static org.briarproject.api.crypto.PasswordStrengthEstimator.WEAK;
|
import static org.briarproject.api.crypto.PasswordStrengthEstimator.WEAK;
|
||||||
import static org.briarproject.api.identity.AuthorConstants.MAX_AUTHOR_NAME_LENGTH;
|
import static org.briarproject.api.identity.AuthorConstants.MAX_AUTHOR_NAME_LENGTH;
|
||||||
|
|
||||||
|
|||||||
@@ -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,85 @@
|
|||||||
|
package briarproject.activity;
|
||||||
|
|
||||||
|
import org.briarproject.android.ActivityModule;
|
||||||
|
import org.briarproject.android.SetupActivity;
|
||||||
|
import org.briarproject.android.controller.SetupController;
|
||||||
|
import org.briarproject.android.controller.SetupControllerImp;
|
||||||
|
import org.briarproject.android.controller.handler.ResultHandler;
|
||||||
|
import org.mockito.Mockito;
|
||||||
|
import org.mockito.invocation.InvocationOnMock;
|
||||||
|
import org.mockito.stubbing.Answer;
|
||||||
|
|
||||||
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
|
import static org.briarproject.api.crypto.PasswordStrengthEstimator.NONE;
|
||||||
|
import static org.briarproject.api.crypto.PasswordStrengthEstimator.QUITE_STRONG;
|
||||||
|
import static org.briarproject.api.crypto.PasswordStrengthEstimator.QUITE_WEAK;
|
||||||
|
import static org.briarproject.api.crypto.PasswordStrengthEstimator.STRONG;
|
||||||
|
import static org.briarproject.api.crypto.PasswordStrengthEstimator.WEAK;
|
||||||
|
import static org.mockito.Matchers.any;
|
||||||
|
import static org.mockito.Matchers.anyString;
|
||||||
|
|
||||||
|
public class MockedSetupActivity extends SetupActivity {
|
||||||
|
|
||||||
|
private static final Logger LOG =
|
||||||
|
Logger.getLogger(MockedSetupActivity.class.getName());
|
||||||
|
|
||||||
|
final static String STRONG_PASS = "strong";
|
||||||
|
final static String QSTRONG_PASS = "qstrong";
|
||||||
|
final static String QWEAK_PASS = "qweak";
|
||||||
|
final static String WEAK_PASS = "weak";
|
||||||
|
final static String NO_PASS = "none";
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected ActivityModule getActivityModule() {
|
||||||
|
return new ActivityModule(this) {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected SetupController provideSetupController(
|
||||||
|
SetupControllerImp setupControllerImp) {
|
||||||
|
SetupController setupController =
|
||||||
|
Mockito.mock(SetupControllerImp.class);
|
||||||
|
|
||||||
|
Mockito.doAnswer(new Answer<Void>() {
|
||||||
|
@Override
|
||||||
|
public Void answer(InvocationOnMock invocation)
|
||||||
|
throws Throwable {
|
||||||
|
((ResultHandler<Long>) invocation.getArguments()[2])
|
||||||
|
.onResult(1L);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}).when(setupController)
|
||||||
|
.createIdentity(anyString(), anyString(),
|
||||||
|
(ResultHandler<Long>) any());
|
||||||
|
Mockito.when(
|
||||||
|
setupController
|
||||||
|
.estimatePasswordStrength(anyString()))
|
||||||
|
.thenAnswer(new Answer<Float>() {
|
||||||
|
@Override
|
||||||
|
public Float answer(
|
||||||
|
InvocationOnMock invocation)
|
||||||
|
throws Throwable {
|
||||||
|
String p = (String) invocation
|
||||||
|
.getArguments()[0];
|
||||||
|
LOG.info("p = " + p);
|
||||||
|
if (p.equals(STRONG_PASS)) {
|
||||||
|
return STRONG;
|
||||||
|
} else if (p.equals(QSTRONG_PASS)) {
|
||||||
|
return QUITE_STRONG;
|
||||||
|
} else if (p.equals(QWEAK_PASS)) {
|
||||||
|
return QUITE_WEAK;
|
||||||
|
} else if (p.equals(WEAK_PASS)) {
|
||||||
|
return WEAK;
|
||||||
|
} else if (p.equals(NO_PASS)) {
|
||||||
|
return NONE;
|
||||||
|
} else {
|
||||||
|
return STRONG;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
return setupController;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,5 +1,6 @@
|
|||||||
package briarproject.activity;
|
package briarproject.activity;
|
||||||
|
|
||||||
|
import android.content.Intent;
|
||||||
import android.support.design.widget.TextInputLayout;
|
import android.support.design.widget.TextInputLayout;
|
||||||
import android.widget.Button;
|
import android.widget.Button;
|
||||||
import android.widget.EditText;
|
import android.widget.EditText;
|
||||||
@@ -8,27 +9,47 @@ import com.google.common.base.Strings;
|
|||||||
|
|
||||||
import org.briarproject.BuildConfig;
|
import org.briarproject.BuildConfig;
|
||||||
import org.briarproject.R;
|
import org.briarproject.R;
|
||||||
import org.briarproject.android.ActivityModule;
|
import org.briarproject.android.NavDrawerActivity;
|
||||||
import org.briarproject.android.SetupActivity;
|
import org.briarproject.android.SetupActivity;
|
||||||
import org.briarproject.android.controller.SetupController;
|
|
||||||
import org.briarproject.android.controller.SetupControllerImp;
|
|
||||||
import org.briarproject.android.util.StrengthMeter;
|
import org.briarproject.android.util.StrengthMeter;
|
||||||
import org.briarproject.api.crypto.PasswordStrengthEstimator;
|
|
||||||
import org.briarproject.api.identity.AuthorConstants;
|
import org.briarproject.api.identity.AuthorConstants;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
import org.mockito.Mockito;
|
|
||||||
import org.robolectric.Robolectric;
|
import org.robolectric.Robolectric;
|
||||||
import org.robolectric.RobolectricGradleTestRunner;
|
import org.robolectric.RobolectricGradleTestRunner;
|
||||||
import org.robolectric.annotation.Config;
|
import org.robolectric.annotation.Config;
|
||||||
|
import org.robolectric.shadows.ShadowActivity;
|
||||||
|
|
||||||
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
|
import static briarproject.activity.MockedSetupActivity.NO_PASS;
|
||||||
|
import static briarproject.activity.MockedSetupActivity.QSTRONG_PASS;
|
||||||
|
import static briarproject.activity.MockedSetupActivity.QWEAK_PASS;
|
||||||
|
import static briarproject.activity.MockedSetupActivity.STRONG_PASS;
|
||||||
|
import static briarproject.activity.MockedSetupActivity.WEAK_PASS;
|
||||||
import static junit.framework.Assert.assertEquals;
|
import static junit.framework.Assert.assertEquals;
|
||||||
|
import static org.briarproject.android.util.StrengthMeter.GREEN;
|
||||||
|
import static org.briarproject.android.util.StrengthMeter.LIME;
|
||||||
|
import static org.briarproject.android.util.StrengthMeter.ORANGE;
|
||||||
|
import static org.briarproject.android.util.StrengthMeter.RED;
|
||||||
|
import static org.briarproject.android.util.StrengthMeter.YELLOW;
|
||||||
|
import static org.briarproject.api.crypto.PasswordStrengthEstimator.NONE;
|
||||||
|
import static org.briarproject.api.crypto.PasswordStrengthEstimator.QUITE_STRONG;
|
||||||
|
import static org.briarproject.api.crypto.PasswordStrengthEstimator.QUITE_WEAK;
|
||||||
|
import static org.briarproject.api.crypto.PasswordStrengthEstimator.STRONG;
|
||||||
|
import static org.briarproject.api.crypto.PasswordStrengthEstimator.WEAK;
|
||||||
|
import static org.junit.Assert.assertNotEquals;
|
||||||
|
import static org.robolectric.Shadows.shadowOf;
|
||||||
|
|
||||||
@RunWith(RobolectricGradleTestRunner.class)
|
@RunWith(RobolectricGradleTestRunner.class)
|
||||||
@Config(constants = BuildConfig.class, sdk = 21)
|
@Config(constants = BuildConfig.class, sdk = 21)
|
||||||
public class SetupActivityTest {
|
public class SetupActivityTest {
|
||||||
|
|
||||||
|
private static final Logger LOG =
|
||||||
|
Logger.getLogger(SetupActivityTest.class.getName());
|
||||||
|
|
||||||
|
|
||||||
private SetupActivity setupActivity;
|
private SetupActivity setupActivity;
|
||||||
TextInputLayout nicknameEntryWrapper;
|
TextInputLayout nicknameEntryWrapper;
|
||||||
TextInputLayout passwordEntryWrapper;
|
TextInputLayout passwordEntryWrapper;
|
||||||
@@ -39,32 +60,9 @@ public class SetupActivityTest {
|
|||||||
StrengthMeter strengthMeter;
|
StrengthMeter strengthMeter;
|
||||||
Button createAccountButton;
|
Button createAccountButton;
|
||||||
|
|
||||||
class TestSetupActivity extends SetupActivity {
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected ActivityModule getActivityModule() {
|
|
||||||
return new ActivityModule(this) {
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected SetupController provideSetupController(
|
|
||||||
SetupControllerImp setupControllerImp) {
|
|
||||||
SetupController setupController =
|
|
||||||
Mockito.mock(SetupControllerImp.class);
|
|
||||||
Mockito.when(
|
|
||||||
setupController.estimatePasswordStrength("strong"))
|
|
||||||
.thenReturn(PasswordStrengthEstimator.STRONG);
|
|
||||||
// Mockito.when(
|
|
||||||
// setupController.estimatePasswordStrength("qstrong"))
|
|
||||||
// .thenReturn(PasswordStrengthEstimator.QUITE_STRONG);
|
|
||||||
return setupController;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void setUp() {
|
public void setUp() {
|
||||||
setupActivity = Robolectric.setupActivity(SetupActivity.class);
|
setupActivity = Robolectric.setupActivity(MockedSetupActivity.class);
|
||||||
nicknameEntryWrapper = (TextInputLayout) setupActivity
|
nicknameEntryWrapper = (TextInputLayout) setupActivity
|
||||||
.findViewById(R.id.nickname_entry_wrapper);
|
.findViewById(R.id.nickname_entry_wrapper);
|
||||||
passwordEntryWrapper = (TextInputLayout) setupActivity
|
passwordEntryWrapper = (TextInputLayout) setupActivity
|
||||||
@@ -83,21 +81,54 @@ public class SetupActivityTest {
|
|||||||
(Button) setupActivity.findViewById(R.id.create_account);
|
(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
|
@Test
|
||||||
public void test() {
|
public void testUI() {
|
||||||
|
// Nick
|
||||||
String longNick =
|
String longNick =
|
||||||
Strings.padEnd("*", AuthorConstants.MAX_AUTHOR_NAME_LENGTH + 1,
|
Strings.padEnd("*", AuthorConstants.MAX_AUTHOR_NAME_LENGTH + 1,
|
||||||
'*');
|
'*');
|
||||||
nicknameEntry.setText(longNick);
|
nicknameEntry.setText(longNick);
|
||||||
assertEquals(nicknameEntryWrapper.getError(),
|
assertEquals(nicknameEntryWrapper.getError(),
|
||||||
setupActivity.getString(R.string.name_too_long));
|
setupActivity.getString(R.string.name_too_long));
|
||||||
|
assertEquals(createAccountButton.isEnabled(), false);
|
||||||
|
// strength estimator
|
||||||
|
testStrengthMeter(STRONG_PASS, STRONG, GREEN);
|
||||||
|
assertEquals(createAccountButton.isEnabled(), false);
|
||||||
|
testStrengthMeter(QSTRONG_PASS, QUITE_STRONG, LIME);
|
||||||
|
assertEquals(createAccountButton.isEnabled(), false);
|
||||||
|
testStrengthMeter(QWEAK_PASS, QUITE_WEAK, YELLOW);
|
||||||
|
assertEquals(createAccountButton.isEnabled(), false);
|
||||||
|
testStrengthMeter(WEAK_PASS, WEAK, ORANGE);
|
||||||
|
assertEquals(createAccountButton.isEnabled(), false);
|
||||||
|
testStrengthMeter(NO_PASS, NONE, RED);
|
||||||
|
assertEquals(createAccountButton.isEnabled(), false);
|
||||||
|
|
||||||
passwordEntry.setText("strong");
|
// pass confirmation
|
||||||
assertEquals(strengthMeter.getProgress(),
|
nicknameEntry.setText("nick.nickerton");
|
||||||
strengthMeter.getMax() * PasswordStrengthEstimator.STRONG);
|
passwordEntry.setText("really.safe.password");
|
||||||
|
passwordConfirmation.setText("really.safe.pass");
|
||||||
// passwordEntry.setText("strong");
|
assertEquals(createAccountButton.isEnabled(), false);
|
||||||
// assertEquals(StrengthMeter.GREEN, strengthMeter.getColor());
|
assertEquals(passwordConfirmationWrapper.getError(),
|
||||||
// setupActivity.
|
setupActivity.getString(R.string.passwords_do_not_match));
|
||||||
|
passwordEntry.setText("really.safe.pass");
|
||||||
|
passwordConfirmation.setText("really.safe.pass");
|
||||||
|
assertNotEquals(passwordConfirmationWrapper.getError(),
|
||||||
|
setupActivity.getString(R.string.passwords_do_not_match));
|
||||||
|
assertEquals(createAccountButton.isEnabled(), true);
|
||||||
|
// confirm correct Activity started
|
||||||
|
createAccountButton.performClick();
|
||||||
|
assertEquals(setupActivity.isFinishing(), true);
|
||||||
|
ShadowActivity shadowActivity = shadowOf(setupActivity);
|
||||||
|
Intent intent = shadowActivity.peekNextStartedActivity();
|
||||||
|
assertEquals(intent.getComponent().getClassName(),
|
||||||
|
NavDrawerActivity.class.getName());
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user