diff --git a/briar-android/src/org/briarproject/android/SetupActivity.java b/briar-android/src/org/briarproject/android/SetupActivity.java index a08d23be14251e8dfe3d7e1fd5cbe523aecbf4d0..1b101d2e0194bb12b876769d42333ab689ed5119 100644 --- a/briar-android/src/org/briarproject/android/SetupActivity.java +++ b/briar-android/src/org/briarproject/android/SetupActivity.java @@ -27,8 +27,8 @@ import javax.inject.Inject; import static android.content.Intent.FLAG_ACTIVITY_NEW_TASK; import static android.view.View.INVISIBLE; import static android.view.View.VISIBLE; -import static org.briarproject.android.TestingConstants.PREVENT_SCREENSHOTS; 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.identity.AuthorConstants.MAX_AUTHOR_NAME_LENGTH; diff --git a/briar-android/test/java/android/net/http/AndroidHttpClient.java b/briar-android/test/java/android/net/http/AndroidHttpClient.java new file mode 100644 index 0000000000000000000000000000000000000000..f2cf7a34632bbf4e3a5ef2220ac8a9d01e0da1da --- /dev/null +++ b/briar-android/test/java/android/net/http/AndroidHttpClient.java @@ -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 { +} \ No newline at end of file diff --git a/briar-android/test/java/briarproject/activity/MockedSetupActivity.java b/briar-android/test/java/briarproject/activity/MockedSetupActivity.java new file mode 100644 index 0000000000000000000000000000000000000000..77a830f32a347e5653ca90ebda9932f1d2fe55bb --- /dev/null +++ b/briar-android/test/java/briarproject/activity/MockedSetupActivity.java @@ -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; + } + }; + } +} diff --git a/briar-android/test/java/briarproject/activity/SetupActivityTest.java b/briar-android/test/java/briarproject/activity/SetupActivityTest.java index 86a640d27dac982c14487f09841774f7e27f23ec..1317c6180935cc278f03986b3a6f984e945efc13 100644 --- a/briar-android/test/java/briarproject/activity/SetupActivityTest.java +++ b/briar-android/test/java/briarproject/activity/SetupActivityTest.java @@ -1,5 +1,6 @@ package briarproject.activity; +import android.content.Intent; import android.support.design.widget.TextInputLayout; import android.widget.Button; import android.widget.EditText; @@ -8,27 +9,47 @@ import com.google.common.base.Strings; import org.briarproject.BuildConfig; import org.briarproject.R; -import org.briarproject.android.ActivityModule; +import org.briarproject.android.NavDrawerActivity; 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.api.crypto.PasswordStrengthEstimator; import org.briarproject.api.identity.AuthorConstants; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; -import org.mockito.Mockito; import org.robolectric.Robolectric; import org.robolectric.RobolectricGradleTestRunner; 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 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) @Config(constants = BuildConfig.class, sdk = 21) public class SetupActivityTest { + private static final Logger LOG = + Logger.getLogger(SetupActivityTest.class.getName()); + + private SetupActivity setupActivity; TextInputLayout nicknameEntryWrapper; TextInputLayout passwordEntryWrapper; @@ -39,32 +60,9 @@ public class SetupActivityTest { StrengthMeter strengthMeter; 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 public void setUp() { - setupActivity = Robolectric.setupActivity(SetupActivity.class); + setupActivity = Robolectric.setupActivity(MockedSetupActivity.class); nicknameEntryWrapper = (TextInputLayout) setupActivity .findViewById(R.id.nickname_entry_wrapper); passwordEntryWrapper = (TextInputLayout) setupActivity @@ -83,21 +81,54 @@ public class SetupActivityTest { (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 test() { + public void testUI() { + // Nick String longNick = Strings.padEnd("*", AuthorConstants.MAX_AUTHOR_NAME_LENGTH + 1, '*'); nicknameEntry.setText(longNick); assertEquals(nicknameEntryWrapper.getError(), 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"); - assertEquals(strengthMeter.getProgress(), - strengthMeter.getMax() * PasswordStrengthEstimator.STRONG); - -// passwordEntry.setText("strong"); -// assertEquals(StrengthMeter.GREEN, strengthMeter.getColor()); -// setupActivity. + // pass confirmation + nicknameEntry.setText("nick.nickerton"); + 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)); + 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()); } + }