From 46e2a1c736c257349962b905506322c594339f13 Mon Sep 17 00:00:00 2001 From: Sergey Solovyev Date: Tue, 25 Jun 2013 12:11:04 +0400 Subject: [PATCH] tests added --- .../wizard/CalculatorWizardActivity.java | 14 ++++- .../android/calculator/wizard/Wizard.java | 4 +- .../wizard/CalculatorWizardActivityTest.java | 59 ++++++++++++++++++- 3 files changed, 72 insertions(+), 5 deletions(-) diff --git a/android-app/src/main/java/org/solovyev/android/calculator/wizard/CalculatorWizardActivity.java b/android-app/src/main/java/org/solovyev/android/calculator/wizard/CalculatorWizardActivity.java index 147b3095..c7d18dd5 100644 --- a/android-app/src/main/java/org/solovyev/android/calculator/wizard/CalculatorWizardActivity.java +++ b/android-app/src/main/java/org/solovyev/android/calculator/wizard/CalculatorWizardActivity.java @@ -142,7 +142,7 @@ public final class CalculatorWizardActivity extends SherlockFragmentActivity { } } - private void finishFlow() { + void finishFlow() { if (flow != null && step != null) { Wizard.saveWizardFinished(flow, step); } @@ -237,6 +237,18 @@ public final class CalculatorWizardActivity extends SherlockFragmentActivity { return flow; } + View getPrevButton() { + return prevButton; + } + + View getNextButton() { + return nextButton; + } + + View getFinishButton() { + return finishButton; + } + /* ********************************************************************** * diff --git a/android-app/src/main/java/org/solovyev/android/calculator/wizard/Wizard.java b/android-app/src/main/java/org/solovyev/android/calculator/wizard/Wizard.java index 046ac0ff..739763e2 100644 --- a/android-app/src/main/java/org/solovyev/android/calculator/wizard/Wizard.java +++ b/android-app/src/main/java/org/solovyev/android/calculator/wizard/Wizard.java @@ -39,8 +39,8 @@ public final class Wizard { } } - public static boolean isWizardFinished(@Nonnull String name, @Nonnull Context context) { - final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context); + public static boolean isWizardFinished(@Nonnull String name) { + final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(CalculatorApplication.getInstance()); return preferences.getBoolean(makeFlowFinishedPreferenceKey(name), false); } diff --git a/android-app/src/test/java/org/solovyev/android/calculator/wizard/CalculatorWizardActivityTest.java b/android-app/src/test/java/org/solovyev/android/calculator/wizard/CalculatorWizardActivityTest.java index 206470ed..e3db8abc 100644 --- a/android-app/src/test/java/org/solovyev/android/calculator/wizard/CalculatorWizardActivityTest.java +++ b/android-app/src/test/java/org/solovyev/android/calculator/wizard/CalculatorWizardActivityTest.java @@ -4,6 +4,7 @@ import android.content.Intent; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; + import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -12,7 +13,13 @@ import org.robolectric.RobolectricTestRunner; import org.robolectric.shadows.ShadowActivity; import org.robolectric.util.ActivityController; -import static org.junit.Assert.*; +import static android.view.View.GONE; +import static android.view.View.VISIBLE; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; import static org.solovyev.android.calculator.wizard.CalculatorWizardActivity.startWizard; import static org.solovyev.android.calculator.wizard.WizardStep.choose_mode; @@ -69,8 +76,8 @@ public class CalculatorWizardActivityTest { intent.setClass(activity, CalculatorWizardActivity.class); intent.putExtra(Wizard.FLOW, Wizard.DEFAULT_WIZARD_FLOW); controller = Robolectric.buildActivity(CalculatorWizardActivity.class).withIntent(intent); + controller.create(); activity = controller.get(); - controller.create(null); assertEquals(Wizard.DEFAULT_WIZARD_FLOW, activity.getFlow().getName()); assertEquals(activity.getFlow().getFirstStep(), activity.getStep()); @@ -125,4 +132,52 @@ public class CalculatorWizardActivityTest { activity.setStep(choose_mode); assertEquals(activity.getString(choose_mode.getTitleResId()), activity.getTitle().toString()); } + + @Test + public void testNextButtonShouldNotBeShownAtTheEnd() throws Exception { + setLastStep(); + assertEquals(VISIBLE, activity.getPrevButton().getVisibility()); + assertEquals(GONE, activity.getNextButton().getVisibility()); + assertEquals(VISIBLE, activity.getFinishButton().getVisibility()); + } + + private void setLastStep() { + activity.setStep(WizardStep.values()[WizardStep.values().length - 1]); + } + + @Test + public void testPrevButtonShouldNotBeShownAtTheStart() throws Exception { + setFirstStep(); + assertEquals(VISIBLE, activity.getNextButton().getVisibility()); + assertEquals(GONE, activity.getFinishButton().getVisibility()); + assertEquals(GONE, activity.getPrevButton().getVisibility()); + } + + private void setFirstStep() { + activity.setStep(WizardStep.values()[0]); + } + + @Test + public void testShouldSaveLastWizardStateOnPause() throws Exception { + assertNull(Wizard.getLastSavedWizardStepName(activity.getFlow().getName())); + activity.setStep(WizardStep.drag_button_step); + activity.onPause(); + assertEquals(WizardStep.drag_button_step.getName(), Wizard.getLastSavedWizardStepName(activity.getFlow().getName())); + } + + @Test + public void testShouldSaveFinishedIfLastStep() throws Exception { + assertFalse(Wizard.isWizardFinished(activity.getFlow().getName())); + setLastStep(); + activity.finishFlow(); + assertTrue(Wizard.isWizardFinished(activity.getFlow().getName())); + } + + @Test + public void testShouldNotSaveFinishedIfNotLastStep() throws Exception { + assertFalse(Wizard.isWizardFinished(activity.getFlow().getName())); + setFirstStep(); + activity.finishFlow(); + assertFalse(Wizard.isWizardFinished(activity.getFlow().getName())); + } }