forked tests

This commit is contained in:
Sergey Solovyev 2013-06-19 00:52:49 +04:00
parent bd85440c99
commit 03965333c2
12 changed files with 310 additions and 78 deletions

View File

@ -7,6 +7,8 @@
-->
<resources>
<string name="cpp_wizard_layout_optimized">Optimized</string>
<string name="cpp_wizard_layout_big_buttons">Big buttons</string>
<string name="cpp_wizard_mode_simple">Simple</string>
<string name="cpp_wizard_mode_simple_description">In simple mode only basic functionality will be available from the main screen.
Result is rounded up to 5 digits.</string>

View File

@ -0,0 +1,18 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:a="http://schemas.android.com/apk/res/android"
a:layout_width="match_parent"
a:layout_height="match_parent"
a:orientation="vertical">
<TextView
a:text="Do you want to use optimized layout for large screens or just big buttons?"
a:layout_height="wrap_content"
a:layout_width="match_parent" />
<Spinner
a:id="@+id/wizard_layout_spinner"
a:layout_height="wrap_content"
a:layout_width="match_parent" />
</LinearLayout>

View File

@ -5,6 +5,8 @@ import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.List;
import static org.solovyev.android.calculator.wizard.Wizard.DEFAULT_WIZARD_FLOW;
import static org.solovyev.android.calculator.wizard.Wizard.FIRST_TIME_WIZARD;
import static org.solovyev.android.calculator.wizard.WizardStep.welcome;
/**
@ -14,21 +16,36 @@ import static org.solovyev.android.calculator.wizard.WizardStep.welcome;
*/
final class AppWizardFlow implements WizardFlow {
public static final String NAME = "app-wizard";
@Nonnull
private final ListWizardFlow listWizardFlow;
AppWizardFlow() {
private AppWizardFlow(@Nonnull String name, @Nonnull List<WizardStep> wizardSteps) {
this.listWizardFlow = new ListWizardFlow(name, wizardSteps);
}
@Nonnull
static AppWizardFlow newDefaultWizardFlow() {
final List<WizardStep> wizardSteps = new ArrayList<WizardStep>();
for (WizardStep wizardStep : WizardStep.values()) {
if (wizardStep != welcome) {
if (wizardStep != welcome && wizardStep.isVisible()) {
wizardSteps.add(wizardStep);
}
}
this.listWizardFlow = new ListWizardFlow(NAME, wizardSteps);
return new AppWizardFlow(DEFAULT_WIZARD_FLOW, wizardSteps);
}
@Nonnull
static AppWizardFlow newFirstTimeWizardFlow() {
final List<WizardStep> wizardSteps = new ArrayList<WizardStep>();
for (WizardStep wizardStep : WizardStep.values()) {
if (wizardStep.isVisible()) {
wizardSteps.add(wizardStep);
}
}
return new AppWizardFlow(FIRST_TIME_WIZARD, wizardSteps);
}
@Nonnull
@Override
public String getName() {

View File

@ -0,0 +1,49 @@
package org.solovyev.android.calculator.wizard;
import android.content.SharedPreferences;
import org.solovyev.android.calculator.CalculatorPreferences;
import org.solovyev.android.calculator.R;
import javax.annotation.Nonnull;
import static org.solovyev.android.calculator.CalculatorPreferences.Gui.Layout.main_calculator;
import static org.solovyev.android.calculator.CalculatorPreferences.Gui.Layout.main_calculator_mobile;
/**
* User: serso
* Date: 6/19/13
* Time: 12:39 AM
*/
enum CalculatorLayout {
big_buttons(R.string.cpp_wizard_layout_big_buttons){
@Override
protected void apply(@Nonnull SharedPreferences preferences) {
CalculatorPreferences.Gui.layout.putPreference(preferences, main_calculator_mobile);
}
},
optimized(R.string.cpp_wizard_layout_optimized){
@Override
protected void apply(@Nonnull SharedPreferences preferences) {
CalculatorPreferences.Gui.layout.putPreference(preferences, main_calculator);
}
};
private final int nameResId;
CalculatorLayout(int nameResId) {
this.nameResId = nameResId;
}
int getNameResId() {
return nameResId;
}
protected abstract void apply(@Nonnull SharedPreferences preferences);
@Nonnull
static CalculatorLayout getDefaultMode(){
return big_buttons;
}
}

View File

@ -16,11 +16,13 @@ import static org.solovyev.android.calculator.CalculatorPreferences.Gui.Layout.m
* Time: 9:30 PM
*/
enum CalculatorMode {
simple(R.string.cpp_wizard_mode_simple) {
@Override
protected void apply(@Nonnull SharedPreferences preferences) {
CalculatorPreferences.Gui.layout.putPreference(preferences, CalculatorPreferences.Gui.Layout.simple);
CalculatorPreferences.Calculations.preferredAngleUnits.putPreference(preferences, AngleUnit.deg);
AndroidCalculatorEngine.Preferences.angleUnit.putPreference(preferences, AngleUnit.deg);
AndroidCalculatorEngine.Preferences.scienceNotation.putPreference(preferences, false);
AndroidCalculatorEngine.Preferences.roundResult.putPreference(preferences, true);
}
@ -31,6 +33,7 @@ enum CalculatorMode {
protected void apply(@Nonnull SharedPreferences preferences) {
CalculatorPreferences.Gui.layout.putPreference(preferences, main_calculator);
CalculatorPreferences.Calculations.preferredAngleUnits.putPreference(preferences, AngleUnit.rad);
AndroidCalculatorEngine.Preferences.angleUnit.putPreference(preferences, AngleUnit.rad);
AndroidCalculatorEngine.Preferences.scienceNotation.putPreference(preferences, true);
AndroidCalculatorEngine.Preferences.roundResult.putPreference(preferences, false);
}

View File

@ -64,7 +64,7 @@ public final class CalculatorWizardActivity extends SherlockFragmentActivity {
step = (WizardStep) savedInstanceState.getSerializable(STEP);
}
flow = Wizard.getWizardFlow(wizardName != null ? wizardName : FirstTimeWizardFlow.NAME);
flow = Wizard.getWizardFlow(wizardName != null ? wizardName : Wizard.FIRST_TIME_WIZARD);
if (step == null) {
step = flow.getFirstStep();

View File

@ -46,7 +46,7 @@ public final class ChooseModeWizardStep extends SherlockFragment {
*/
@Nullable
private Spinner layoutSpinner;
private Spinner modeSpinner;
private CalculatorMode mode;
@ -72,17 +72,17 @@ public final class ChooseModeWizardStep extends SherlockFragment {
public void onViewCreated(View root, Bundle savedInstanceState) {
super.onViewCreated(root, savedInstanceState);
layoutSpinner = (Spinner) root.findViewById(R.id.wizard_mode_spinner);
modeSpinner = (Spinner) root.findViewById(R.id.wizard_mode_spinner);
final List<ModeListItem> listItems = new ArrayList<ModeListItem>();
for (CalculatorMode mode : CalculatorMode.values()) {
listItems.add(new ModeListItem(mode));
}
layoutSpinner.setAdapter(ListItemAdapter.newInstance(getActivity(), listItems));
modeSpinner.setAdapter(ListItemAdapter.newInstance(getActivity(), listItems));
final int position = Arrays.binarySearch(CalculatorMode.values(), mode);
if (position >= 0) {
layoutSpinner.setSelection(position);
modeSpinner.setSelection(position);
}
}
@ -90,8 +90,8 @@ public final class ChooseModeWizardStep extends SherlockFragment {
CalculatorMode getSelectedMode() {
CalculatorMode mode = getDefaultMode();
if (layoutSpinner != null) {
final int position = layoutSpinner.getSelectedItemPosition();
if (modeSpinner != null) {
final int position = modeSpinner.getSelectedItemPosition();
if (position >= 0 && position < CalculatorMode.values().length) {
mode = CalculatorMode.values()[position];

View File

@ -1,54 +0,0 @@
package org.solovyev.android.calculator.wizard;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.List;
import static org.solovyev.android.calculator.wizard.WizardStep.choose_mode;
import static org.solovyev.android.calculator.wizard.WizardStep.welcome;
/**
* User: serso
* Date: 6/16/13
* Time: 9:25 PM
*/
final class FirstTimeWizardFlow implements WizardFlow {
public static final String NAME = "first-wizard";
@Nonnull
private final ListWizardFlow listWizardFlow;
FirstTimeWizardFlow() {
final List<WizardStep> wizardSteps = new ArrayList<WizardStep>();
for (WizardStep wizardStep : WizardStep.values()) {
wizardSteps.add(wizardStep);
}
this.listWizardFlow = new ListWizardFlow(NAME, wizardSteps);
}
@Nonnull
@Override
public String getName() {
return listWizardFlow.getName();
}
@Nullable
@Override
public WizardStep getNextStep(@Nonnull WizardStep step) {
return listWizardFlow.getNextStep(step);
}
@Nullable
@Override
public WizardStep getPrevStep(@Nonnull WizardStep step) {
return listWizardFlow.getPrevStep(step);
}
@Nonnull
@Override
public WizardStep getFirstStep() {
return listWizardFlow.getFirstStep();
}
}

View File

@ -0,0 +1,156 @@
package org.solovyev.android.calculator.wizard;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Spinner;
import android.widget.TextView;
import com.actionbarsherlock.app.SherlockFragment;
import org.solovyev.android.calculator.R;
import org.solovyev.android.list.ListItem;
import org.solovyev.android.list.ListItemAdapter;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import static org.solovyev.android.calculator.wizard.CalculatorLayout.getDefaultMode;
/**
* User: serso
* Date: 6/19/13
* Time: 12:33 AM
*/
final class TabletWizardStep extends SherlockFragment {
/*
**********************************************************************
*
* CONSTANTS
*
**********************************************************************
*/
static final String LAYOUT = "layout";
/*
**********************************************************************
*
* FIELDS
*
**********************************************************************
*/
@Nullable
private Spinner layoutSpinner;
private CalculatorLayout layout;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if(savedInstanceState != null) {
layout = (CalculatorLayout) savedInstanceState.getSerializable(LAYOUT);
}
if (layout == null) {
layout = (CalculatorLayout) getArguments().getSerializable(LAYOUT);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.cpp_wizard_step_tablet, null);
}
@Override
public void onViewCreated(View root, Bundle savedInstanceState) {
super.onViewCreated(root, savedInstanceState);
layoutSpinner = (Spinner) root.findViewById(R.id.wizard_layout_spinner);
final List<LayoutListItem> listItems = new ArrayList<LayoutListItem>();
for (CalculatorLayout layout : CalculatorLayout.values()) {
listItems.add(new LayoutListItem(layout));
}
layoutSpinner.setAdapter(ListItemAdapter.newInstance(getActivity(), listItems));
final int position = Arrays.binarySearch(CalculatorMode.values(), layout);
if (position >= 0) {
layoutSpinner.setSelection(position);
}
}
@Nonnull
CalculatorLayout getSelectedLayout() {
CalculatorLayout layout = getDefaultMode();
if (layoutSpinner != null) {
final int position = layoutSpinner.getSelectedItemPosition();
if (position >= 0 && position < CalculatorLayout.values().length) {
layout = CalculatorLayout.values()[position];
}
}
return layout;
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putSerializable(LAYOUT, layout);
}
/*
**********************************************************************
*
* STATIC/INNER
*
**********************************************************************
*/
private static final class LayoutListItem implements ListItem {
@Nonnull
private final CalculatorLayout layout;
private LayoutListItem(@Nonnull CalculatorLayout layout) {
this.layout = layout;
}
@Nullable
@Override
public OnClickAction getOnClickAction() {
return null;
}
@Nullable
@Override
public OnClickAction getOnLongClickAction() {
return null;
}
@Nonnull
@Override
public View updateView(@Nonnull Context context, @Nonnull View view) {
return build(context);
}
@Nonnull
@Override
public View build(@Nonnull Context context) {
final TextView textView = new TextView(context);
textView.setText(layout.getNameResId());
return textView;
}
}
}

View File

@ -4,7 +4,8 @@ import org.solovyev.android.prefs.Preference;
import javax.annotation.Nonnull;
import static org.solovyev.android.calculator.wizard.CalculatorMode.getDefaultMode;
import static org.solovyev.android.calculator.wizard.AppWizardFlow.newDefaultWizardFlow;
import static org.solovyev.android.calculator.wizard.AppWizardFlow.newFirstTimeWizardFlow;
import static org.solovyev.android.prefs.StringPreference.ofEnum;
/**
@ -14,16 +15,19 @@ import static org.solovyev.android.prefs.StringPreference.ofEnum;
*/
public final class Wizard {
public static final String FIRST_TIME_WIZARD = "first-wizard";
public static final String DEFAULT_WIZARD_FLOW = "app-wizard";
private Wizard() {
throw new AssertionError();
}
@Nonnull
public static WizardFlow getWizardFlow(@Nonnull String name) {
if(FirstTimeWizardFlow.NAME.equals(name)) {
return new FirstTimeWizardFlow();
} else if(AppWizardFlow.NAME.equals(name)) {
return new AppWizardFlow();
if(FIRST_TIME_WIZARD.equals(name)) {
return newFirstTimeWizardFlow();
} else if(DEFAULT_WIZARD_FLOW.equals(name)) {
return newDefaultWizardFlow();
} else {
throw new IllegalArgumentException("Wizard flow " + name + " is not supported");
}
@ -38,6 +42,7 @@ public final class Wizard {
*/
static final class Preferences {
static final Preference<CalculatorMode> mode = ofEnum("mode", getDefaultMode(), CalculatorMode.class);
static final Preference<CalculatorMode> mode = ofEnum("mode", CalculatorMode.getDefaultMode(), CalculatorMode.class);
static final Preference<CalculatorLayout> layout = ofEnum("layout", CalculatorLayout.getDefaultMode(), CalculatorLayout.class);
}
}

View File

@ -10,6 +10,7 @@ import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import static org.solovyev.android.calculator.wizard.ChooseModeWizardStep.MODE;
import static org.solovyev.android.calculator.wizard.TabletWizardStep.LAYOUT;
import static org.solovyev.android.calculator.wizard.Wizard.Preferences;
/**
@ -65,6 +66,36 @@ enum WizardStep {
bundle.putSerializable(MODE, Preferences.mode.getPreference(preferences));
return bundle;
}
},
tablet(TabletWizardStep.class) {
@Override
boolean onNext(@Nonnull Fragment f) {
final TabletWizardStep fragment = (TabletWizardStep) f;
final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(f.getActivity());
final CalculatorLayout layout = fragment.getSelectedLayout();
layout.apply(preferences);
Preferences.layout.putPreference(preferences, layout);
return true;
}
@Override
boolean onPrev(@Nonnull Fragment fragment) {
return true;
}
@Nullable
@Override
Bundle getFragmentArgs() {
final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(CalculatorApplication.getInstance());
final Bundle bundle = new Bundle();
bundle.putSerializable(LAYOUT, Preferences.layout.getPreference(preferences));
return bundle;
}
};
@Nonnull
@ -84,8 +115,13 @@ enum WizardStep {
}
abstract boolean onNext(@Nonnull Fragment fragment);
abstract boolean onPrev(@Nonnull Fragment fragment);
@Nullable
abstract Bundle getFragmentArgs();
public boolean isVisible() {
return true;
}
}

View File

@ -40,7 +40,7 @@ public class CalculatorWizardActivityTest {
controller.create();
assertNotNull(activity.getFlow());
assertEquals(FirstTimeWizardFlow.NAME, activity.getFlow().getName());
assertEquals(Wizard.FIRST_TIME_WIZARD, activity.getFlow().getName());
assertNotNull(activity.getStep());
assertEquals(activity.getFlow().getFirstStep(), activity.getStep());
@ -54,17 +54,17 @@ public class CalculatorWizardActivityTest {
activity = controller.get();
assertNotNull(activity.getFlow());
assertEquals(FirstTimeWizardFlow.NAME, activity.getFlow().getName());
assertEquals(Wizard.FIRST_TIME_WIZARD, 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);
intent.putExtra(CalculatorWizardActivity.FLOW, Wizard.DEFAULT_WIZARD_FLOW);
controller = Robolectric.buildActivity(CalculatorWizardActivity.class).withIntent(intent);
activity = controller.get();
controller.create(null);
assertEquals(AppWizardFlow.NAME, activity.getFlow().getName());
assertEquals(Wizard.DEFAULT_WIZARD_FLOW, activity.getFlow().getName());
assertEquals(activity.getFlow().getFirstStep(), activity.getStep());
final Bundle outState1 = new Bundle();
@ -73,7 +73,7 @@ public class CalculatorWizardActivityTest {
controller = Robolectric.buildActivity(CalculatorWizardActivity.class);
activity = controller.get();
controller.create(outState1);
assertEquals(AppWizardFlow.NAME, activity.getFlow().getName());
assertEquals(Wizard.DEFAULT_WIZARD_FLOW, activity.getFlow().getName());
assertEquals(activity.getFlow().getFirstStep(), activity.getStep());
}
@ -102,7 +102,7 @@ public class CalculatorWizardActivityTest {
@Test
public void testStartWizard() throws Exception {
final ShadowActivity shadowActivity = Robolectric.shadowOf(controller.get());
CalculatorWizardActivity.startWizard(AppWizardFlow.NAME, shadowActivity.getApplicationContext());
CalculatorWizardActivity.startWizard(Wizard.DEFAULT_WIZARD_FLOW, shadowActivity.getApplicationContext());
Assert.assertNotNull(shadowActivity.getNextStartedActivity());
}
}