tests added

This commit is contained in:
Sergey Solovyev 2013-06-18 16:34:18 +04:00
parent 6ea156457c
commit 12da9d332c
4 changed files with 106 additions and 10 deletions

View File

@ -1,5 +1,7 @@
package org.solovyev.android.calculator.wizard; package org.solovyev.android.calculator.wizard;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle; import android.os.Bundle;
import android.support.v4.app.Fragment; import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentManager;
@ -20,8 +22,8 @@ import static android.view.View.VISIBLE;
*/ */
public final class CalculatorWizardActivity extends SherlockFragmentActivity { public final class CalculatorWizardActivity extends SherlockFragmentActivity {
private static final String FLOW = "flow"; static final String FLOW = "flow";
private static final String STEP = "step"; static final String STEP = "step";
/* /*
********************************************************************** **********************************************************************
@ -55,8 +57,8 @@ public final class CalculatorWizardActivity extends SherlockFragmentActivity {
nextButton = findViewById(R.id.wizard_next_button); nextButton = findViewById(R.id.wizard_next_button);
finishButton = findViewById(R.id.wizard_finish_button); finishButton = findViewById(R.id.wizard_finish_button);
String wizardName = null; String wizardName = getIntent().getStringExtra(FLOW);
WizardStep step = null; WizardStep step = (WizardStep) getIntent().getSerializableExtra(STEP);
if (savedInstanceState != null) { if (savedInstanceState != null) {
wizardName = savedInstanceState.getString(FLOW); wizardName = savedInstanceState.getString(FLOW);
step = (WizardStep) savedInstanceState.getSerializable(STEP); step = (WizardStep) savedInstanceState.getSerializable(STEP);
@ -71,7 +73,7 @@ public final class CalculatorWizardActivity extends SherlockFragmentActivity {
setStep(step); setStep(step);
} }
private void setStep(@Nonnull WizardStep step) { void setStep(@Nonnull WizardStep step) {
if (this.step == null || !this.step.equals(step)) { if (this.step == null || !this.step.equals(step)) {
final FragmentManager fm = getSupportFragmentManager(); final FragmentManager fm = getSupportFragmentManager();
final FragmentTransaction ft = fm.beginTransaction(); final FragmentTransaction ft = fm.beginTransaction();
@ -191,6 +193,7 @@ public final class CalculatorWizardActivity extends SherlockFragmentActivity {
protected void onSaveInstanceState(Bundle out) { protected void onSaveInstanceState(Bundle out) {
super.onSaveInstanceState(out); super.onSaveInstanceState(out);
out.putString(FLOW, flow.getName());
out.putSerializable(STEP, step); out.putSerializable(STEP, step);
} }
@ -201,4 +204,18 @@ public final class CalculatorWizardActivity extends SherlockFragmentActivity {
WizardFlow getFlow() { WizardFlow getFlow() {
return flow; return flow;
} }
/*
**********************************************************************
*
* STATIC/INNER
*
**********************************************************************
*/
public static void startWizard(@Nonnull String name, @Nonnull Context context) {
final Intent intent = new Intent(context, CalculatorWizardActivity.class);
intent.putExtra(FLOW, name);
context.startActivity(intent);
}
} }

View File

@ -22,8 +22,9 @@ final class FirstTimeWizardFlow implements WizardFlow {
FirstTimeWizardFlow() { FirstTimeWizardFlow() {
final List<WizardStep> wizardSteps = new ArrayList<WizardStep>(); final List<WizardStep> wizardSteps = new ArrayList<WizardStep>();
wizardSteps.add(welcome); for (WizardStep wizardStep : WizardStep.values()) {
wizardSteps.add(choose_mode); wizardSteps.add(wizardStep);
}
this.listWizardFlow = new ListWizardFlow(NAME, wizardSteps); this.listWizardFlow = new ListWizardFlow(NAME, wizardSteps);
} }

View File

@ -17,6 +17,8 @@ public final class Wizard {
public static WizardFlow getWizardFlow(@Nonnull String name) { public static WizardFlow getWizardFlow(@Nonnull String name) {
if(FirstTimeWizardFlow.NAME.equals(name)) { if(FirstTimeWizardFlow.NAME.equals(name)) {
return new FirstTimeWizardFlow(); return new FirstTimeWizardFlow();
} else if(AppWizardFlow.NAME.equals(name)) {
return new AppWizardFlow();
} else { } else {
throw new IllegalArgumentException("Wizard flow " + name + " is not supported"); throw new IllegalArgumentException("Wizard flow " + name + " is not supported");
} }

View File

@ -1,9 +1,17 @@
package org.solovyev.android.calculator.wizard; package org.solovyev.android.calculator.wizard;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.robolectric.Robolectric; import org.robolectric.Robolectric;
import org.robolectric.RobolectricTestRunner; import org.robolectric.RobolectricTestRunner;
import org.robolectric.shadows.ShadowActivity;
import org.robolectric.util.ActivityController; import org.robolectric.util.ActivityController;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
@ -17,16 +25,84 @@ import static org.junit.Assert.assertNotNull;
@RunWith(value = RobolectricTestRunner.class) @RunWith(value = RobolectricTestRunner.class)
public class CalculatorWizardActivityTest { public class CalculatorWizardActivityTest {
private ActivityController<CalculatorWizardActivity> controller;
private CalculatorWizardActivity activity;
@Before
public void setUp() throws Exception {
controller = Robolectric.buildActivity(CalculatorWizardActivity.class);
activity = controller.get();
}
@Test @Test
public void testRestoreState() throws Exception { public void testCreate() throws Exception {
final ActivityController<CalculatorWizardActivity> controller = Robolectric.buildActivity(CalculatorWizardActivity.class);
controller.attach(); controller.attach();
controller.create(); controller.create();
final CalculatorWizardActivity activity = controller.get();
assertNotNull(activity.getFlow()); assertNotNull(activity.getFlow());
assertEquals(FirstTimeWizardFlow.NAME, activity.getFlow().getName()); assertEquals(FirstTimeWizardFlow.NAME, activity.getFlow().getName());
assertNotNull(activity.getStep()); assertNotNull(activity.getStep());
assertEquals(activity.getFlow().getFirstStep(), activity.getStep()); assertEquals(activity.getFlow().getFirstStep(), activity.getStep());
activity.setStep(WizardStep.choose_mode);
final Bundle outState = new Bundle();
controller.saveInstanceState(outState);
controller = Robolectric.buildActivity(CalculatorWizardActivity.class);
controller.create(outState);
activity = controller.get();
assertNotNull(activity.getFlow());
assertEquals(FirstTimeWizardFlow.NAME, activity.getFlow().getName());
assertNotNull(activity.getStep());
assertEquals(WizardStep.choose_mode, activity.getStep());
final Intent intent = new Intent();
intent.setClass(activity, CalculatorWizardActivity.class);
intent.putExtra(CalculatorWizardActivity.FLOW, AppWizardFlow.NAME);
controller = Robolectric.buildActivity(CalculatorWizardActivity.class).withIntent(intent);
activity = controller.get();
controller.create(null);
assertEquals(AppWizardFlow.NAME, activity.getFlow().getName());
assertEquals(activity.getFlow().getFirstStep(), activity.getStep());
final Bundle outState1 = new Bundle();
controller.saveInstanceState(outState1);
controller = Robolectric.buildActivity(CalculatorWizardActivity.class);
activity = controller.get();
controller.create(outState1);
assertEquals(AppWizardFlow.NAME, activity.getFlow().getName());
assertEquals(activity.getFlow().getFirstStep(), activity.getStep());
}
@Test
public void testFragment() throws Exception {
controller.create().start().resume();
final FragmentManager fm = activity.getSupportFragmentManager();
Fragment f = fm.findFragmentByTag(WizardStep.welcome.getFragmentTag());
Assert.assertNotNull(f);
Assert.assertTrue(f.isAdded());
activity.setStep(WizardStep.choose_mode);
f = fm.findFragmentByTag(WizardStep.choose_mode.getFragmentTag());
Assert.assertNotNull(f);
Assert.assertTrue(f.isAdded());
}
@Test
public void testSetStep() throws Exception {
controller.create();
activity.setStep(WizardStep.choose_mode);
}
@Test
public void testStartWizard() throws Exception {
final ShadowActivity shadowActivity = Robolectric.shadowOf(controller.get());
CalculatorWizardActivity.startWizard(AppWizardFlow.NAME, shadowActivity.getApplicationContext());
Assert.assertNotNull(shadowActivity.getNextStartedActivity());
} }
} }