tests added

This commit is contained in:
Sergey Solovyev 2013-07-11 16:31:46 +04:00
parent 1abaed2bab
commit 1aee47049d
7 changed files with 188 additions and 142 deletions

View File

@ -242,6 +242,11 @@ public class CalculatorApplication extends android.app.Application implements Sh
return instance;
}
@Nonnull
public static SharedPreferences getPreferences() {
return PreferenceManager.getDefaultSharedPreferences(getInstance());
}
public static boolean isMonkeyRunner(@Nonnull Context context) {
// NOTE: this code is only for monkeyrunner
return context.checkCallingOrSelfPermission(android.Manifest.permission.DISABLE_KEYGUARD) == PackageManager.PERMISSION_GRANTED;

View File

@ -1,102 +0,0 @@
/*
* Copyright 2013 serso aka se.solovyev
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* Contact details
*
* Email: se.solovyev@gmail.com
* Site: http://se.solovyev.org
*/
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.last;
import static org.solovyev.android.calculator.wizard.Wizards.DEFAULT_WIZARD_FLOW;
import static org.solovyev.android.calculator.wizard.Wizards.FIRST_TIME_WIZARD;
import static org.solovyev.android.calculator.wizard.WizardStep.welcome;
/**
* User: serso
* Date: 6/16/13
* Time: 9:25 PM
*/
final class AppWizardFlow implements WizardFlow {
@Nonnull
private final ListWizardFlow listWizardFlow;
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 && wizardStep != last && wizardStep.isVisible()) {
wizardSteps.add(wizardStep);
}
}
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() {
return listWizardFlow.getName();
}
@Nullable
@Override
public WizardStep getStep(@Nonnull String name) {
return listWizardFlow.getStep(name);
}
@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

@ -1,20 +1,13 @@
package org.solovyev.android.calculator.wizard;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.preference.Preference;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import net.robotmedia.billing.BillingController;
import org.solovyev.android.ads.AdsController;
import org.solovyev.android.calculator.R;
import org.solovyev.android.calculator.preferences.CalculatorPurchaseDialogActivity;

View File

@ -22,21 +22,18 @@
package org.solovyev.android.calculator.wizard;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.os.Bundle;
import android.preference.PreferenceManager;
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.CalculatorPreferences;
import org.solovyev.android.calculator.R;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import static org.solovyev.android.calculator.CalculatorApplication.getPreferences;
import static org.solovyev.android.calculator.wizard.ChooseLayoutWizardStep.LAYOUT;
import static org.solovyev.android.calculator.wizard.ChooseModeWizardStep.MODE;
import static org.solovyev.android.calculator.wizard.OnScreenCalculatorWizardStep.ONSCREEN_CALCULATOR_ENABLED;
@ -55,10 +52,8 @@ enum WizardStep {
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);
layout.apply(getPreferences());
return true;
}
@ -66,10 +61,8 @@ enum WizardStep {
@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)));
bundle.putSerializable(LAYOUT, CalculatorLayout.fromGuiLayout(CalculatorPreferences.Gui.layout.getPreference(getPreferences())));
return bundle;
}
@ -84,10 +77,8 @@ enum WizardStep {
boolean onNext(@Nonnull Fragment f) {
final ChooseModeWizardStep fragment = (ChooseModeWizardStep) f;
final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(f.getActivity());
final CalculatorMode mode = fragment.getSelectedMode();
mode.apply(preferences);
mode.apply(getPreferences());
return true;
}
@ -95,10 +86,8 @@ enum WizardStep {
@Nullable
@Override
Bundle getFragmentArgs() {
final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(CalculatorApplication.getInstance());
final Bundle bundle = new Bundle();
bundle.putSerializable(MODE, CalculatorMode.fromGuiLayout(CalculatorPreferences.Gui.layout.getPreference(preferences)));
bundle.putSerializable(MODE, CalculatorMode.fromGuiLayout(CalculatorPreferences.Gui.layout.getPreference(getPreferences())));
return bundle;
}
},
@ -108,9 +97,7 @@ enum WizardStep {
boolean onNext(@Nonnull Fragment f) {
final OnScreenCalculatorWizardStep fragment = (OnScreenCalculatorWizardStep) f;
final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(f.getActivity());
CalculatorPreferences.OnscreenCalculator.showAppIcon.putPreference(preferences, fragment.isOnscreenCalculatorEnabled());
CalculatorPreferences.OnscreenCalculator.showAppIcon.putPreference(getPreferences(), fragment.isOnscreenCalculatorEnabled());
return true;
}
@ -118,10 +105,8 @@ enum WizardStep {
@Nullable
@Override
Bundle getFragmentArgs() {
final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(CalculatorApplication.getInstance());
final Bundle bundle = new Bundle();
bundle.putSerializable(ONSCREEN_CALCULATOR_ENABLED, CalculatorPreferences.OnscreenCalculator.showAppIcon.getPreference(preferences));
bundle.putSerializable(ONSCREEN_CALCULATOR_ENABLED, CalculatorPreferences.OnscreenCalculator.showAppIcon.getPreference(getPreferences()));
return bundle;
}
},
@ -136,8 +121,6 @@ enum WizardStep {
private final int titleResId;
private final int nextButtonTitleResId;
WizardStep(@Nonnull Class<? extends Fragment> fragmentClass, int titleResId) {
this(fragmentClass, titleResId, R.string.cpp_wizard_next);
}

View File

@ -30,8 +30,11 @@ import org.solovyev.android.calculator.CalculatorApplication;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import static org.solovyev.android.calculator.wizard.AppWizardFlow.newDefaultWizardFlow;
import static org.solovyev.android.calculator.wizard.AppWizardFlow.newFirstTimeWizardFlow;
import java.util.ArrayList;
import java.util.List;
import static org.solovyev.android.calculator.wizard.WizardStep.last;
import static org.solovyev.android.calculator.wizard.WizardStep.welcome;
/**
* User: serso
@ -114,4 +117,26 @@ public final class Wizards {
private static String makeLastStepPreferenceKey(@Nonnull String flowName) {
return FLOW + ":" + flowName;
}
@Nonnull
static WizardFlow newDefaultWizardFlow() {
final List<WizardStep> wizardSteps = new ArrayList<WizardStep>();
for (WizardStep wizardStep : WizardStep.values()) {
if (wizardStep != welcome && wizardStep != last && wizardStep.isVisible()) {
wizardSteps.add(wizardStep);
}
}
return new ListWizardFlow(DEFAULT_WIZARD_FLOW, wizardSteps);
}
@Nonnull
static WizardFlow newFirstTimeWizardFlow() {
final List<WizardStep> wizardSteps = new ArrayList<WizardStep>();
for (WizardStep wizardStep : WizardStep.values()) {
if (wizardStep.isVisible()) {
wizardSteps.add(wizardStep);
}
}
return new ListWizardFlow(FIRST_TIME_WIZARD, wizardSteps);
}
}

View File

@ -22,12 +22,9 @@
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;
@ -37,16 +34,23 @@ 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.junit.Assert.*;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.solovyev.android.calculator.CalculatorApplication.getPreferences;
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.CalculatorPreferences.OnscreenCalculator.showAppIcon;
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;
import static org.solovyev.android.calculator.wizard.ChooseLayoutWizardStep.LAYOUT;
import static org.solovyev.android.calculator.wizard.ChooseModeWizardStep.MODE;
import static org.solovyev.android.calculator.wizard.OnScreenCalculatorWizardStep.ONSCREEN_CALCULATOR_ENABLED;
import static org.solovyev.android.calculator.wizard.WizardStep.choose_layout;
import static org.solovyev.android.calculator.wizard.WizardStep.choose_mode;
import static org.solovyev.android.calculator.wizard.WizardStep.on_screen_calculator;
@RunWith(RobolectricTestRunner.class)
public class WizardStepTest {
@ -112,7 +116,7 @@ public class WizardStepTest {
final ChooseLayoutWizardStep layoutFragment = mock(ChooseLayoutWizardStep.class);
when(layoutFragment.getSelectedLayout()).thenReturn(layout);
when(layoutFragment.getActivity()).thenReturn(activity);
WizardStep.choose_layout.onNext(layoutFragment);
choose_layout.onNext(layoutFragment);
}
/* @Config(qualifiers = "large")
@ -124,6 +128,56 @@ public class WizardStepTest {
@Config(qualifiers = "normal")
@Test
public void testChooseLayoutShouldNotBeVisibleForMobile() throws Exception {
assertFalse(WizardStep.choose_layout.isVisible());
assertFalse(choose_layout.isVisible());
}
@Test
public void testOnscreenCalculatorShouldNotBeShown() throws Exception {
doOnscreenStep(false);
assertFalse(showAppIcon.getPreference(getPreferences()));
}
@Test
public void testOnscreenCalculatorShouldBeShown() throws Exception {
doOnscreenStep(true);
assertTrue(showAppIcon.getPreference(getPreferences()));
}
private void doOnscreenStep(boolean onscreenCalculatorEnabled) {
final OnScreenCalculatorWizardStep f = mock(OnScreenCalculatorWizardStep.class);
when(f.isOnscreenCalculatorEnabled()).thenReturn(onscreenCalculatorEnabled);
when(f.getActivity()).thenReturn(activity);
WizardStep.on_screen_calculator.onNext(f);
}
@SuppressWarnings("ConstantConditions")
@Test
public void testChooseLayoutFragmentArgs() throws Exception {
CalculatorPreferences.Gui.layout.putPreference(getPreferences(), CalculatorPreferences.Gui.Layout.simple);
assertEquals(CalculatorLayout.optimized, choose_layout.getFragmentArgs().getSerializable(LAYOUT));
CalculatorPreferences.Gui.layout.putPreference(getPreferences(), CalculatorPreferences.Gui.Layout.simple_mobile);
assertEquals(CalculatorLayout.big_buttons, choose_layout.getFragmentArgs().getSerializable(LAYOUT));
}
@SuppressWarnings("ConstantConditions")
@Test
public void testChooseModeFragmentArgs() throws Exception {
CalculatorPreferences.Gui.layout.putPreference(getPreferences(), CalculatorPreferences.Gui.Layout.main_calculator);
assertEquals(CalculatorMode.engineer, choose_mode.getFragmentArgs().getSerializable(MODE));
CalculatorPreferences.Gui.layout.putPreference(getPreferences(), CalculatorPreferences.Gui.Layout.simple_mobile);
assertEquals(CalculatorMode.simple, choose_mode.getFragmentArgs().getSerializable(MODE));
}
@SuppressWarnings("ConstantConditions")
@Test
public void testOnscreenFragmentArgs() throws Exception {
CalculatorPreferences.OnscreenCalculator.showAppIcon.putPreference(getPreferences(), true);
assertTrue(on_screen_calculator.getFragmentArgs().getBoolean(ONSCREEN_CALCULATOR_ENABLED));
CalculatorPreferences.OnscreenCalculator.showAppIcon.putPreference(getPreferences(), false);
assertFalse(on_screen_calculator.getFragmentArgs().getBoolean(ONSCREEN_CALCULATOR_ENABLED));
}
}

View File

@ -0,0 +1,88 @@
package org.solovyev.android.calculator.wizard;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import static org.junit.Assert.*;
import static org.solovyev.android.calculator.wizard.WizardStep.choose_mode;
import static org.solovyev.android.calculator.wizard.WizardStep.last;
import static org.solovyev.android.calculator.wizard.WizardStep.welcome;
import static org.solovyev.android.calculator.wizard.Wizards.*;
/**
* User: serso
* Date: 7/11/13
* Time: 10:29 AM
*/
@RunWith(value = RobolectricTestRunner.class)
public class WizardsTest {
@Test
public void testDefaultFlowShouldNotContainWelcomeAndLastSteps() throws Exception {
final WizardFlow flow = Wizards.newDefaultWizardFlow();
assertNull(flow.getStep(welcome.getName()));
assertNull(flow.getStep(last.getName()));
}
@Test
public void testFirstTimeFlowShouldContainWelcomeAndLastSteps() throws Exception {
final WizardFlow flow = Wizards.newFirstTimeWizardFlow();
assertNotNull(flow.getStep(welcome.getName()));
assertNotNull(flow.getStep(last.getName()));
}
@Test
public void testShouldThrowExceptionIfUnknownFlow() throws Exception {
try {
getWizardFlow("testtesttesttesttest");
fail();
} catch (IllegalArgumentException e) {
// ok
}
}
@Test
public void testShouldReturnFlow() throws Exception {
assertNotNull(getWizardFlow(FIRST_TIME_WIZARD));
assertNotNull(getWizardFlow(DEFAULT_WIZARD_FLOW));
}
@Test
public void testShouldSaveWizardIsFinishedWhenNotLastStepAndForce() throws Exception {
assertFalse(isWizardFinished(FIRST_TIME_WIZARD));
final WizardFlow flow = getWizardFlow(FIRST_TIME_WIZARD);
saveWizardFinished(flow, WizardStep.drag_button, true);
assertTrue(isWizardFinished(FIRST_TIME_WIZARD));
}
@Test
public void testShouldNotSaveWizardIsFinishedWhenNotLastStepAndNotForce() throws Exception {
assertFalse(isWizardFinished(FIRST_TIME_WIZARD));
final WizardFlow flow = getWizardFlow(FIRST_TIME_WIZARD);
saveWizardFinished(flow, WizardStep.drag_button, false);
assertFalse(isWizardFinished(FIRST_TIME_WIZARD));
}
@Test
public void testShouldSaveWizardIsFinishedWhenLastStep() throws Exception {
assertFalse(isWizardFinished(FIRST_TIME_WIZARD));
final WizardFlow flow = getWizardFlow(FIRST_TIME_WIZARD);
saveWizardFinished(flow, WizardStep.last, false);
assertTrue(isWizardFinished(FIRST_TIME_WIZARD));
}
@Test
public void testShouldSaveLastWizardStep() throws Exception {
assertFalse(isWizardStarted(FIRST_TIME_WIZARD));
assertNull(getLastSavedWizardStepName(FIRST_TIME_WIZARD));
final WizardFlow flow = getWizardFlow(FIRST_TIME_WIZARD);
saveLastWizardStep(flow, choose_mode);
assertTrue(isWizardStarted(FIRST_TIME_WIZARD));
assertEquals(choose_mode.name(), getLastSavedWizardStepName(FIRST_TIME_WIZARD));
}
}