wizard changes

This commit is contained in:
Sergey Solovyev 2013-06-24 14:26:59 +04:00
parent 61a405de10
commit cffee5a761
14 changed files with 376 additions and 224 deletions

View File

@ -130,8 +130,9 @@
<string name="c_calc_layout">Раскладка</string> <string name="c_calc_layout">Раскладка</string>
<string name="p_layout_cellphone">Научная (телефон)</string> <string name="p_layout_cellphone">Научная (телефон)</string>
<string name="p_layout_calculator">Научная</string> <string name="p_layout_calculator">Научная</string>
<string name="p_layout_calculator_mobile">Научная (большие кнопкм)</string> <string name="p_layout_calculator_mobile">Научная (большие кнопки)</string>
<string name="p_layout_simple">Простая</string> <string name="p_layout_simple">Простая</string>
<string name="p_layout_simple_mobile">Простая (большие кнопки)</string>
<string name="c_calc_layout_summary">Устанавливает раскладку кнопок</string> <string name="c_calc_layout_summary">Устанавливает раскладку кнопок</string>
<string name="c_calc_haptic_feedback_title">Отклик на нажатие</string> <string name="c_calc_haptic_feedback_title">Отклик на нажатие</string>

View File

@ -128,6 +128,7 @@
<string name="p_layout_calculator">Scientific</string> <string name="p_layout_calculator">Scientific</string>
<string name="p_layout_calculator_mobile">Scientific (Big buttons)</string> <string name="p_layout_calculator_mobile">Scientific (Big buttons)</string>
<string name="p_layout_simple">Simple</string> <string name="p_layout_simple">Simple</string>
<string name="p_layout_simple_mobile">Simple (Big buttons)</string>
<string name="c_calc_layout_summary">Sets layout of buttons</string> <string name="c_calc_layout_summary">Sets layout of buttons</string>
<string name="c_calc_haptic_feedback_title">Haptic feedback</string> <string name="c_calc_haptic_feedback_title">Haptic feedback</string>

View File

@ -106,21 +106,25 @@ public final class CalculatorPreferences {
} }
public static enum Layout { public static enum Layout {
main_calculator(R.layout.main_calculator, R.string.p_layout_calculator), main_calculator(R.layout.main_calculator, R.string.p_layout_calculator, true),
main_calculator_mobile(R.layout.main_calculator_mobile, R.string.p_layout_calculator_mobile), main_calculator_mobile(R.layout.main_calculator_mobile, R.string.p_layout_calculator_mobile, false),
// not used anymore // not used anymore
@Deprecated @Deprecated
main_cellphone(R.layout.main_calculator, 0), main_cellphone(R.layout.main_calculator, 0, true),
simple(R.layout.main_calculator, R.string.p_layout_simple); simple(R.layout.main_calculator, R.string.p_layout_simple, true),
simple_mobile(R.layout.main_calculator_mobile, R.string.p_layout_simple_mobile, false)
;
private final int layoutId; private final int layoutId;
private final int nameResId; private final int nameResId;
private final boolean optimized;
Layout(int layoutId, int nameResId) { Layout(int layoutId, int nameResId, boolean optimized) {
this.layoutId = layoutId; this.layoutId = layoutId;
this.nameResId = nameResId; this.nameResId = nameResId;
this.optimized = optimized;
} }
public int getLayoutId() { public int getLayoutId() {
@ -130,6 +134,10 @@ public final class CalculatorPreferences {
public int getNameResId() { public int getNameResId() {
return nameResId; return nameResId;
} }
public boolean isOptimized() {
return optimized;
}
} }
} }

View File

@ -8,11 +8,50 @@
<TextView <TextView
a:text="Do you want to use optimized layout for large screens or just big buttons?" a:text="Do you want to use optimized layout for large screens or just big buttons?"
a:layout_height="wrap_content" a:layout_height="wrap_content"
a:layout_width="match_parent" /> a:layout_width="match_parent"/>
<Spinner <LinearLayout a:layout_width="match_parent"
a:id="@+id/wizard_layout_spinner" a:layout_height="match_parent"
a:orientation="vertical">
<RadioGroup a:orientation="horizontal"
a:layout_width="match_parent"
a:layout_height="wrap_content">
<RadioButton a:layout_width="0dp"
a:id="@+id/wizard_optimized_radiobutton"
a:layout_weight="1"
a:text="@string/cpp_wizard_layout_optimized"
a:layout_height="wrap_content"/>
<RadioButton a:layout_width="0dp"
a:id="@+id/wizard_big_buttons_radiobutton"
a:layout_weight="1"
a:text="@string/cpp_wizard_layout_big_buttons"
a:layout_height="wrap_content"/>
</RadioGroup>
<LinearLayout a:layout_width="match_parent"
a:layout_height="wrap_content" a:layout_height="wrap_content"
a:layout_width="match_parent" /> a:orientation="horizontal">
<ImageView a:layout_width="0dp"
a:layout_weight="1"
a:scaleType="centerInside"
a:layout_height="match_parent"
a:padding="10dp"
a:src="@drawable/tablet_optimized"/>
<ImageView a:layout_width="0dp"
a:layout_weight="1"
a:scaleType="centerInside"
a:layout_height="match_parent"
a:padding="10dp"
a:src="@drawable/tablet_big_buttons"/>
</LinearLayout>
</LinearLayout>
</LinearLayout> </LinearLayout>

View File

@ -43,11 +43,13 @@
<item>@string/p_layout_calculator</item> <item>@string/p_layout_calculator</item>
<item>@string/p_layout_calculator_mobile</item> <item>@string/p_layout_calculator_mobile</item>
<item>@string/p_layout_simple</item> <item>@string/p_layout_simple</item>
<item>@string/p_layout_simple_mobile</item>
</string-array> </string-array>
<string-array name="p_layout_values"> <string-array name="p_layout_values">
<item>main_calculator</item> <item>main_calculator</item>
<item>main_calculator_mobile</item> <item>main_calculator_mobile</item>
<item>simple</item> <item>simple</item>
<item>simple_mobile</item>
</string-array> </string-array>
<string-array name="p_calc_haptic_feedback_duration_names"> <string-array name="p_calc_haptic_feedback_duration_names">

View File

@ -26,6 +26,9 @@ import java.lang.reflect.Modifier;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import static org.solovyev.android.calculator.CalculatorPreferences.Gui.Layout.simple;
import static org.solovyev.android.calculator.CalculatorPreferences.Gui.Layout.simple_mobile;
/** /**
* User: serso * User: serso
* Date: 9/28/12 * Date: 9/28/12
@ -149,7 +152,7 @@ public abstract class AbstractCalculatorHelper implements SharedPreferences.OnSh
roundBracketsButton.setOnDragListener(new OnDragListenerVibrator(newOnDragListener(new CalculatorButtons.RoundBracketsDragProcessor(), dragPreferences), vibrator, preferences)); roundBracketsButton.setOnDragListener(new OnDragListenerVibrator(newOnDragListener(new CalculatorButtons.RoundBracketsDragProcessor(), dragPreferences), vibrator, preferences));
} }
if (layout == CalculatorPreferences.Gui.Layout.simple) { if (layout == simple || layout == simple_mobile) {
toggleButtonDirectionText(root, R.id.cpp_button_1, false, DragDirection.up, DragDirection.down); toggleButtonDirectionText(root, R.id.cpp_button_1, false, DragDirection.up, DragDirection.down);
toggleButtonDirectionText(root, R.id.cpp_button_2, false, DragDirection.up, DragDirection.down); toggleButtonDirectionText(root, R.id.cpp_button_2, false, DragDirection.up, DragDirection.down);
toggleButtonDirectionText(root, R.id.cpp_button_3, false, DragDirection.up, DragDirection.down); toggleButtonDirectionText(root, R.id.cpp_button_3, false, DragDirection.up, DragDirection.down);

View File

@ -43,7 +43,22 @@ enum CalculatorLayout {
protected abstract void apply(@Nonnull SharedPreferences preferences); protected abstract void apply(@Nonnull SharedPreferences preferences);
@Nonnull @Nonnull
static CalculatorLayout getDefaultMode(){ static CalculatorLayout getDefaultLayout(){
return big_buttons; return big_buttons;
} }
@Nonnull
static CalculatorLayout fromGuiLayout(@Nonnull CalculatorPreferences.Gui.Layout layout) {
switch (layout) {
case main_calculator:
case main_cellphone:
case simple:
return optimized;
case main_calculator_mobile:
case simple_mobile:
return big_buttons;
default:
return getDefaultLayout();
}
}
} }

View File

@ -9,6 +9,7 @@ import org.solovyev.android.calculator.model.AndroidCalculatorEngine;
import javax.annotation.Nonnull; 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;
import static org.solovyev.android.calculator.CalculatorPreferences.Gui.Layout.main_calculator_mobile;
/** /**
* User: serso * User: serso
@ -20,7 +21,12 @@ enum CalculatorMode {
simple(R.string.cpp_wizard_mode_simple) { simple(R.string.cpp_wizard_mode_simple) {
@Override @Override
protected void apply(@Nonnull SharedPreferences preferences) { protected void apply(@Nonnull SharedPreferences preferences) {
final CalculatorPreferences.Gui.Layout layout = CalculatorPreferences.Gui.layout.getPreference(preferences);
if (layout.isOptimized()) {
CalculatorPreferences.Gui.layout.putPreference(preferences, CalculatorPreferences.Gui.Layout.simple); CalculatorPreferences.Gui.layout.putPreference(preferences, CalculatorPreferences.Gui.Layout.simple);
} else {
CalculatorPreferences.Gui.layout.putPreference(preferences, CalculatorPreferences.Gui.Layout.simple_mobile);
}
CalculatorPreferences.Calculations.preferredAngleUnits.putPreference(preferences, AngleUnit.deg); CalculatorPreferences.Calculations.preferredAngleUnits.putPreference(preferences, AngleUnit.deg);
AndroidCalculatorEngine.Preferences.angleUnit.putPreference(preferences, AngleUnit.deg); AndroidCalculatorEngine.Preferences.angleUnit.putPreference(preferences, AngleUnit.deg);
AndroidCalculatorEngine.Preferences.scienceNotation.putPreference(preferences, false); AndroidCalculatorEngine.Preferences.scienceNotation.putPreference(preferences, false);
@ -31,7 +37,12 @@ enum CalculatorMode {
engineer(R.string.cpp_wizard_mode_engineer) { engineer(R.string.cpp_wizard_mode_engineer) {
@Override @Override
protected void apply(@Nonnull SharedPreferences preferences) { protected void apply(@Nonnull SharedPreferences preferences) {
final CalculatorPreferences.Gui.Layout layout = CalculatorPreferences.Gui.layout.getPreference(preferences);
if (layout.isOptimized()) {
CalculatorPreferences.Gui.layout.putPreference(preferences, main_calculator); CalculatorPreferences.Gui.layout.putPreference(preferences, main_calculator);
} else {
CalculatorPreferences.Gui.layout.putPreference(preferences, main_calculator_mobile);
}
CalculatorPreferences.Calculations.preferredAngleUnits.putPreference(preferences, AngleUnit.rad); CalculatorPreferences.Calculations.preferredAngleUnits.putPreference(preferences, AngleUnit.rad);
AndroidCalculatorEngine.Preferences.angleUnit.putPreference(preferences, AngleUnit.rad); AndroidCalculatorEngine.Preferences.angleUnit.putPreference(preferences, AngleUnit.rad);
AndroidCalculatorEngine.Preferences.scienceNotation.putPreference(preferences, true); AndroidCalculatorEngine.Preferences.scienceNotation.putPreference(preferences, true);
@ -55,4 +66,19 @@ enum CalculatorMode {
static CalculatorMode getDefaultMode(){ static CalculatorMode getDefaultMode(){
return engineer; return engineer;
} }
@Nonnull
static CalculatorMode fromGuiLayout(@Nonnull CalculatorPreferences.Gui.Layout layout) {
switch (layout) {
case main_calculator:
case main_cellphone:
case main_calculator_mobile:
return engineer;
case simple:
case simple_mobile:
return simple;
default:
return getDefaultMode();
}
}
} }

View File

@ -0,0 +1,111 @@
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.RadioButton;
import android.widget.TextView;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import org.solovyev.android.calculator.R;
import com.actionbarsherlock.app.SherlockFragment;
import static org.solovyev.android.calculator.wizard.CalculatorLayout.big_buttons;
import static org.solovyev.android.calculator.wizard.CalculatorLayout.getDefaultLayout;
import static org.solovyev.android.calculator.wizard.CalculatorLayout.optimized;
/**
* User: serso
* Date: 6/19/13
* Time: 12:33 AM
*/
public class ChooseLayoutWizardStep extends SherlockFragment {
/*
**********************************************************************
*
* CONSTANTS
*
**********************************************************************
*/
static final String LAYOUT = "layout";
/*
**********************************************************************
*
* FIELDS
*
**********************************************************************
*/
@Nullable
private RadioButton optimizedRadioButton;
@Nullable
private RadioButton bigButtonsRadioButton;
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);
optimizedRadioButton = (RadioButton) root.findViewById(R.id.wizard_optimized_radiobutton);
bigButtonsRadioButton = (RadioButton) root.findViewById(R.id.wizard_big_buttons_radiobutton);
switch (layout) {
case big_buttons:
bigButtonsRadioButton.setChecked(true);
optimizedRadioButton.setChecked(false);
break;
case optimized:
bigButtonsRadioButton.setChecked(false);
optimizedRadioButton.setChecked(true);
break;
}
}
@Nonnull
CalculatorLayout getSelectedLayout() {
CalculatorLayout layout = getDefaultLayout();
if (bigButtonsRadioButton != null && bigButtonsRadioButton.isChecked()) {
layout = big_buttons;
} else if (optimizedRadioButton != null && optimizedRadioButton.isChecked()) {
layout = optimized;
}
return layout;
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putSerializable(LAYOUT, layout);
}
}

View File

@ -25,7 +25,7 @@ import static org.solovyev.android.calculator.wizard.CalculatorMode.getDefaultMo
* Date: 6/16/13 * Date: 6/16/13
* Time: 9:59 PM * Time: 9:59 PM
*/ */
public final class ChooseModeWizardStep extends SherlockFragment { public class ChooseModeWizardStep extends SherlockFragment {
/* /*
********************************************************************** **********************************************************************

View File

@ -1,156 +0,0 @@
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

@ -32,17 +32,4 @@ public final class Wizard {
throw new IllegalArgumentException("Wizard flow " + name + " is not supported"); throw new IllegalArgumentException("Wizard flow " + name + " is not supported");
} }
} }
/*
**********************************************************************
*
* STATIC/INNER
*
**********************************************************************
*/
static final class Preferences {
static final Preference<CalculatorMode> mode = ofEnum("mode", CalculatorMode.getDefaultMode(), CalculatorMode.class);
static final Preference<CalculatorLayout> layout = ofEnum("layout", CalculatorLayout.getDefaultMode(), CalculatorLayout.class);
}
} }

View File

@ -1,17 +1,22 @@
package org.solovyev.android.calculator.wizard; package org.solovyev.android.calculator.wizard;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.os.Bundle; import android.os.Bundle;
import android.preference.PreferenceManager; import android.preference.PreferenceManager;
import android.support.v4.app.Fragment; import android.support.v4.app.Fragment;
import org.solovyev.android.App;
import org.solovyev.android.Views;
import org.solovyev.android.calculator.CalculatorApplication; import org.solovyev.android.calculator.CalculatorApplication;
import org.solovyev.android.calculator.CalculatorLocator;
import org.solovyev.android.calculator.CalculatorPreferences;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import static org.solovyev.android.calculator.wizard.ChooseModeWizardStep.MODE; 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.ChooseLayoutWizardStep.LAYOUT;
import static org.solovyev.android.calculator.wizard.Wizard.Preferences;
/** /**
* User: serso * User: serso
@ -38,6 +43,40 @@ enum WizardStep {
} }
}, },
choose_layout(ChooseLayoutWizardStep.class) {
@Override
boolean onNext(@Nonnull Fragment f) {
final ChooseLayoutWizardStep fragment = (ChooseLayoutWizardStep) f;
final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(f.getActivity());
final CalculatorLayout layout = fragment.getSelectedLayout();
layout.apply(preferences);
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, CalculatorLayout.fromGuiLayout(CalculatorPreferences.Gui.layout.getPreference(preferences)));
return bundle;
}
@Override
public boolean isVisible() {
return Views.isLayoutSizeAtLeast(Configuration.SCREENLAYOUT_SIZE_LARGE, App.getApplication().getResources().getConfiguration());
}
},
choose_mode(ChooseModeWizardStep.class) { choose_mode(ChooseModeWizardStep.class) {
@Override @Override
boolean onNext(@Nonnull Fragment f) { boolean onNext(@Nonnull Fragment f) {
@ -47,7 +86,6 @@ enum WizardStep {
final CalculatorMode mode = fragment.getSelectedMode(); final CalculatorMode mode = fragment.getSelectedMode();
mode.apply(preferences); mode.apply(preferences);
Preferences.mode.putPreference(preferences, mode);
return true; return true;
} }
@ -63,37 +101,7 @@ enum WizardStep {
final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(CalculatorApplication.getInstance()); final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(CalculatorApplication.getInstance());
final Bundle bundle = new Bundle(); final Bundle bundle = new Bundle();
bundle.putSerializable(MODE, Preferences.mode.getPreference(preferences)); bundle.putSerializable(MODE, CalculatorMode.fromGuiLayout(CalculatorPreferences.Gui.layout.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; return bundle;
} }
}; };

View File

@ -0,0 +1,107 @@
package org.solovyev.android.calculator.wizard;
import android.app.Activity;
import android.preference.PreferenceManager;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.view.Display;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.Robolectric;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;
import org.solovyev.android.calculator.CalculatorPreferences;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.solovyev.android.calculator.CalculatorPreferences.Gui.Layout.main_calculator;
import static org.solovyev.android.calculator.CalculatorPreferences.Gui.Layout.main_calculator_mobile;
import static org.solovyev.android.calculator.wizard.CalculatorLayout.big_buttons;
import static org.solovyev.android.calculator.wizard.CalculatorLayout.optimized;
import static org.solovyev.android.calculator.wizard.CalculatorMode.engineer;
import static org.solovyev.android.calculator.wizard.CalculatorMode.simple;
@RunWith(RobolectricTestRunner.class)
public class WizardStepTest {
private FragmentActivity activity;
@Before
public void setUp() throws Exception {
activity = Robolectric.buildActivity(FragmentActivity.class).create().get();
}
@Test
public void testFragmentsShouldBeInstantiated() throws Exception {
for (WizardStep wizardStep : WizardStep.values()) {
Fragment.instantiate(Robolectric.application, wizardStep.getFragmentClass().getName());
}
}
@Test
public void testShouldBeMainMobileLayout() throws Exception {
chooseLayout(big_buttons);
chooseMode(engineer);
assertUiLayoutEquals(main_calculator_mobile);
}
@Test
public void testShouldBeMainLayout() throws Exception {
chooseLayout(optimized);
chooseMode(engineer);
assertUiLayoutEquals(main_calculator);
}
@Test
public void testShouldBeSimpleLayout() throws Exception {
chooseLayout(optimized);
chooseMode(simple);
assertUiLayoutEquals(CalculatorPreferences.Gui.Layout.simple);
}
@Test
public void testShouldBeSimpleMobileLayout() throws Exception {
chooseLayout(big_buttons);
chooseMode(simple);
assertUiLayoutEquals(CalculatorPreferences.Gui.Layout.simple_mobile);
}
private void assertUiLayoutEquals(CalculatorPreferences.Gui.Layout uiLayout) {
Assert.assertEquals(uiLayout, CalculatorPreferences.Gui.layout.getPreference(PreferenceManager.getDefaultSharedPreferences(Robolectric.application)));
}
private void chooseMode(CalculatorMode mode) {
final ChooseModeWizardStep modeFragment = mock(ChooseModeWizardStep.class);
when(modeFragment.getSelectedMode()).thenReturn(mode);
when(modeFragment.getActivity()).thenReturn(activity);
WizardStep.choose_mode.onNext(modeFragment);
}
private void chooseLayout(CalculatorLayout layout) {
final ChooseLayoutWizardStep layoutFragment = mock(ChooseLayoutWizardStep.class);
when(layoutFragment.getSelectedLayout()).thenReturn(layout);
when(layoutFragment.getActivity()).thenReturn(activity);
WizardStep.choose_layout.onNext(layoutFragment);
}
@Config(qualifiers = "large")
@Test
public void testChooseLayoutShouldBeVisibleForTablet() throws Exception {
assertTrue(WizardStep.choose_layout.isVisible());
}
@Config(qualifiers = "normal")
@Test
public void testChooseLayoutShouldNotBeVisibleForMobile() throws Exception {
assertFalse(WizardStep.choose_layout.isVisible());
}
}