Setup Wizard that asks for Doze Mode exception

Keep checking if we are whitelisted and request it if not
This commit is contained in:
Torsten Grote
2017-09-26 10:04:48 -03:00
parent 1c4f20f76f
commit 8a81171739
45 changed files with 897 additions and 382 deletions

View File

@@ -57,8 +57,6 @@ public class ChangePasswordActivityTest {
@Mock
private PasswordController passwordController;
@Mock
private SetupController setupController;
@Captor
private ArgumentCaptor<ResultHandler<Boolean>> resultCaptor;
@@ -71,14 +69,18 @@ public class ChangePasswordActivityTest {
.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);
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) {
@@ -110,12 +112,9 @@ public class ChangePasswordActivityTest {
@Test
public void testChangePasswordUI() {
PasswordController mockedPasswordController = this.passwordController;
SetupController mockedSetupController = this.setupController;
changePasswordActivity.setPasswordController(mockedPasswordController);
changePasswordActivity.setSetupController(mockedSetupController);
changePasswordActivity.setPasswordController(passwordController);
// Mock strong password strength answer
when(mockedSetupController.estimatePasswordStrength(anyString()))
when(passwordController.estimatePasswordStrength(anyString()))
.thenReturn(STRONG);
String curPass = "old.password";
String safePass = "really.safe.password";
@@ -127,7 +126,7 @@ public class ChangePasswordActivityTest {
changePasswordButton.performClick();
// Verify that the controller's method was called with the correct
// params and get the callback
verify(mockedPasswordController, times(1))
verify(passwordController, times(1))
.changePassword(eq(curPass), eq(safePass),
resultCaptor.capture());
// execute the callbacks
@@ -139,23 +138,24 @@ public class ChangePasswordActivityTest {
public void testPasswordChange() {
PasswordController passwordController =
changePasswordActivity.getPasswordController();
SetupController setupController =
changePasswordActivity.getSetupController();
TestSetupActivity setupActivity =
Robolectric.setupActivity(TestSetupActivity.class);
SetupController setupController = setupActivity.getController();
setupController.setAuthorName("nick");
setupController.setPassword("some.old.pass");
// 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
ResultHandler<Void> resultHandler = mock(ResultHandler.class);
setupController.createAccount(resultHandler);
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);
ResultHandler<Boolean> resultHandler2 = mock(ResultHandler.class);
passwordController.changePassword("some.old.pass", "some.strong.pass",
resultHandler2);
// blocking verification call with timeout that waits until the mocked
@@ -164,14 +164,14 @@ public class ChangePasswordActivityTest {
// Confirm database key
assertTrue(prefs.contains("key"));
assertNotEquals(oldKey, prefs.getString("key", null));
// Note that Robolectric uses its own persistant storage that it
// Note that Robolectric uses its own persistent storage that it
// wipes clean after each test run, no need to clean up manually.
}
@Test
public void testStrengthMeter() {
SetupController controller =
changePasswordActivity.getSetupController();
PasswordController controller =
changePasswordActivity.getPasswordController();
String strongPass = "very.strong.password.123";
String weakPass = "we";
@@ -188,9 +188,9 @@ public class ChangePasswordActivityTest {
@Test
public void testStrengthMeterUI() {
Assert.assertNotNull(changePasswordActivity);
// replace the setup controller with our mocked copy
SetupController mockedController = this.setupController;
changePasswordActivity.setSetupController(mockedController);
// replace the password controller with our mocked copy
PasswordController mockedController = this.passwordController;
changePasswordActivity.setPasswordController(mockedController);
// Mock answers for UI testing only
when(mockedController.estimatePasswordStrength("strong")).thenReturn(
STRONG);
@@ -220,4 +220,5 @@ public class ChangePasswordActivityTest {
Mockito.verify(mockedController, Mockito.times(1))
.estimatePasswordStrength(eq("empty"));
}
}

View File

@@ -19,30 +19,24 @@ 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 org.robolectric.shadows.ShadowLooper;
import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertNotNull;
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)
@@ -62,84 +56,15 @@ public class SetupActivityTest {
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());
createAccountButton = (Button) setupActivity.findViewById(R.id.next);
}
@Test
@@ -154,21 +79,65 @@ public class SetupActivityTest {
setupActivity.getString(R.string.name_too_long));
}
@Test
public void testPasswordMatchUI() {
proceedToPasswordFragment();
// 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));
// Passwords match, so button should be enabled
assertEquals(createAccountButton.isEnabled(), true);
}
@Test
public void testCreateAccountUI() {
proceedToPasswordFragment();
String safePass = "really.safe.password";
passwordEntry.setText(safePass);
passwordConfirmation.setText(safePass);
// Confirm that the create account button is clickable
assertEquals(createAccountButton.isEnabled(), true);
createAccountButton.performClick();
// wait a second since there's no easy way to get a callback
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
ShadowLooper.runUiThreadTasks();
// execute the callback
assertTrue(setupActivity.isFinishing());
// Confirm that the correct Activity has been started
ShadowActivity shadowActivity = shadowOf(setupActivity);
Intent intent = shadowActivity.peekNextStartedActivity();
assertNotNull(intent.getComponent());
assertEquals(intent.getComponent().getClassName(),
NavDrawerActivity.class.getName());
}
@Test
public void testAccountCreation() {
SetupController controller = setupActivity.getController();
controller.setAuthorName("nick");
controller.setPassword("password");
// 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
ResultHandler<Void> resultHandler = mock(ResultHandler.class);
controller.createAccount(resultHandler);
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
// Note that Robolectric uses its own persistent storage that it
// wipes clean after each test run, no need to clean up manually.
}
@@ -190,37 +159,37 @@ public class SetupActivityTest {
@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("qstrong")).thenReturn(
QUITE_STRONG);
when(mockedController.estimatePasswordStrength("qweak")).thenReturn(
QUITE_WEAK);
when(mockedController.estimatePasswordStrength("weak")).thenReturn(
WEAK);
when(mockedController.estimatePasswordStrength("empty")).thenReturn(
NONE);
proceedToPasswordFragment();
// Test the meters progress and color for several values
testStrengthMeter("strong", STRONG, StrengthMeter.GREEN);
Mockito.verify(mockedController, Mockito.times(1))
.estimatePasswordStrength(eq("strong"));
testStrengthMeter("qstrong", QUITE_STRONG, StrengthMeter.LIME);
Mockito.verify(mockedController, Mockito.times(1))
.estimatePasswordStrength(eq("qstrong"));
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"));
testStrengthMeter("1234567890ab", STRONG, StrengthMeter.GREEN);
testStrengthMeter("123456789", QUITE_STRONG, StrengthMeter.LIME);
testStrengthMeter("123456", QUITE_WEAK, StrengthMeter.YELLOW);
testStrengthMeter("123", WEAK, StrengthMeter.ORANGE);
testStrengthMeter("", NONE, StrengthMeter.RED);
}
private void proceedToPasswordFragment() {
// proceed to password fragment
nicknameEntry.setText("nick");
createAccountButton.performClick();
// find UI elements in new fragment
strengthMeter =
(StrengthMeter) setupActivity.findViewById(R.id.strength_meter);
passwordConfirmationWrapper = (TextInputLayout) setupActivity
.findViewById(R.id.password_confirm_wrapper);
passwordEntry =
(EditText) setupActivity.findViewById(R.id.password_entry);
passwordConfirmation =
(EditText) setupActivity.findViewById(R.id.password_confirm);
createAccountButton = (Button) setupActivity.findViewById(R.id.next);
}
private void testStrengthMeter(String pass, float strength, int color) {
passwordEntry.setText(pass);
assertEquals(strengthMeter.getProgress(),
(int) (strengthMeter.getMax() * strength));
assertEquals(color, strengthMeter.getColor());
}
}

View File

@@ -10,15 +10,8 @@ public class TestChangePasswordActivity extends ChangePasswordActivity {
return passwordController;
}
public SetupController getSetupController() {
return setupController;
}
public void setPasswordController(PasswordController passwordController) {
this.passwordController = passwordController;
}
public void setSetupController(SetupController setupController) {
this.setupController = setupController;
}
}