app-tests module merged into app module

This commit is contained in:
serso
2016-01-05 17:14:14 +01:00
parent 7da69a2083
commit 60d65d52df
20 changed files with 79 additions and 160 deletions

View File

@@ -0,0 +1,103 @@
/*
* 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;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricGradleTestRunner;
import org.robolectric.annotation.Config;
import org.solovyev.android.calculator.BuildConfig;
import org.solovyev.android.calculator.units.CalculatorNumeralBase;
import org.solovyev.common.units.Unit;
import org.solovyev.common.units.UnitConverter;
import java.util.Date;
import java.util.Random;
import javax.annotation.Nonnull;
import static org.junit.Assert.assertTrue;
/**
* User: serso
* Date: 4/21/12
* Time: 8:24 PM
*/
@Config(constants = BuildConfig.class)
@RunWith(RobolectricGradleTestRunner.class)
public class AndroidNumeralBaseTest {
@Nonnull
private final UnitConverter<String> c = CalculatorNumeralBase.getConverter();
@Test
public void testIsSupported() throws Exception {
assertTrue(c.isSupported(CalculatorNumeralBase.bin, CalculatorNumeralBase.dec));
}
@Test
public void testConvertFromDec() throws Exception {
Assert.assertEquals("101", c.convert(CalculatorNumeralBase.dec.createUnit("5"), CalculatorNumeralBase.bin).getValue());
Assert.assertEquals("1", c.convert(CalculatorNumeralBase.dec.createUnit("1"), CalculatorNumeralBase.bin).getValue());
Assert.assertEquals("0", c.convert(CalculatorNumeralBase.dec.createUnit("0"), CalculatorNumeralBase.bin).getValue());
Assert.assertEquals("1111100111", c.convert(CalculatorNumeralBase.dec.createUnit("999"), CalculatorNumeralBase.bin).getValue());
Assert.assertEquals("A23", c.convert(CalculatorNumeralBase.dec.createUnit("2595"), CalculatorNumeralBase.hex).getValue());
Assert.assertEquals("AEE", c.convert(CalculatorNumeralBase.dec.createUnit("2798"), CalculatorNumeralBase.hex).getValue());
Assert.assertEquals("15", c.convert(CalculatorNumeralBase.dec.createUnit("21"), CalculatorNumeralBase.hex).getValue());
Assert.assertEquals("0", c.convert(CalculatorNumeralBase.dec.createUnit("0"), CalculatorNumeralBase.hex).getValue());
Assert.assertEquals("3E7", c.convert(CalculatorNumeralBase.dec.createUnit("999"), CalculatorNumeralBase.hex).getValue());
Assert.assertEquals("76", c.convert(CalculatorNumeralBase.dec.createUnit("62"), CalculatorNumeralBase.oct).getValue());
Assert.assertEquals("12", c.convert(CalculatorNumeralBase.dec.createUnit("10"), CalculatorNumeralBase.oct).getValue());
Assert.assertEquals("15", c.convert(CalculatorNumeralBase.dec.createUnit("13"), CalculatorNumeralBase.oct).getValue());
Assert.assertEquals("0", c.convert(CalculatorNumeralBase.dec.createUnit("0"), CalculatorNumeralBase.oct).getValue());
Assert.assertEquals("10445", c.convert(CalculatorNumeralBase.dec.createUnit("4389"), CalculatorNumeralBase.oct).getValue());
}
@Test
public void testRandomConvert() throws Exception {
final Random random = new Random(new Date().getTime());
for (int i = 0; i < 100000; i++) {
final String value = String.valueOf(random.nextInt());
Assert.assertEquals(value, convertChain(value, CalculatorNumeralBase.dec, CalculatorNumeralBase.oct, CalculatorNumeralBase.oct, CalculatorNumeralBase.bin, CalculatorNumeralBase.dec));
Assert.assertEquals(value, convertChain(value, CalculatorNumeralBase.dec, CalculatorNumeralBase.bin, CalculatorNumeralBase.hex, CalculatorNumeralBase.dec, CalculatorNumeralBase.dec));
Assert.assertEquals(value, convertChain(value, CalculatorNumeralBase.dec, CalculatorNumeralBase.dec, CalculatorNumeralBase.hex, CalculatorNumeralBase.oct, CalculatorNumeralBase.dec));
Assert.assertEquals(value, convertChain(value, CalculatorNumeralBase.dec, CalculatorNumeralBase.hex, CalculatorNumeralBase.bin, CalculatorNumeralBase.oct, CalculatorNumeralBase.dec));
}
}
@Nonnull
private String convertChain(@Nonnull String value, @Nonnull CalculatorNumeralBase baseAndroid, @Nonnull CalculatorNumeralBase... typeAndroids) {
Unit<String> unit = baseAndroid.createUnit(value);
for (CalculatorNumeralBase typeAndroid : typeAndroids) {
unit = CalculatorNumeralBase.getConverter().convert(unit, typeAndroid);
}
return unit.getValue();
}
}

View File

@@ -0,0 +1,28 @@
package org.solovyev.android;
import org.junit.runners.model.InitializationError;
import org.robolectric.RobolectricGradleTestRunner;
import org.robolectric.annotation.Config;
import org.robolectric.manifest.AndroidManifest;
import org.robolectric.res.Fs;
public class CalculatorTestRunner extends RobolectricGradleTestRunner {
private static final int MAX_SDK_SUPPORTED_BY_ROBOLECTRIC = 18;
public CalculatorTestRunner(Class<?> testClass) throws InitializationError {
super(testClass);
}
@Override
protected AndroidManifest getAppManifest(Config config) {
final String manifestFilePath = "app/src/main/AndroidManifest.xml";
final String resourcesFilePath = "app/src/main/res";
final String assetsFilePath = "app/src/main/assets";
return new AndroidManifest(Fs.fileFromPath(manifestFilePath), Fs.fileFromPath(resourcesFilePath), Fs.fileFromPath(assetsFilePath)) {
@Override
public int getTargetSdkVersion() {
return MAX_SDK_SUPPORTED_BY_ROBOLECTRIC;
}
};
}
}

View File

@@ -0,0 +1,112 @@
/*
* 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;
import org.junit.Assert;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.solovyev.android.CalculatorTestRunner;
import org.solovyev.common.text.Strings;
import java.util.Date;
import java.util.Random;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
/**
* User: serso
* Date: 10/13/12
* Time: 1:11 PM
*/
@RunWith(value = CalculatorTestRunner.class)
public class AndroidCalculatorEditorViewTest {
@BeforeClass
public static void staticSetUp() throws Exception {
/*CalculatorTestUtils.staticSetUp(null);*/
}
@Before
public void setUp() throws Exception {
/* CalculatorActivity context = new CalculatorActivity();
CalculatorTestUtils.initViews(context);*/
}
@Test
public void testCreation() throws Exception {
new AndroidCalculatorEditorView(CalculatorApplication.getInstance());
}
@Test
public void testAsyncWork() throws Exception {
final int threadNum = 10;
final int count = 10;
final int maxTextLength = 100;
final Random random = new Random(new Date().getTime());
final CountDownLatch startLatchLatch = new CountDownLatch(threadNum);
final CountDownLatch finishLatch = new CountDownLatch(threadNum * count);
final AtomicBoolean error = new AtomicBoolean(false);
for (int i = 0; i < threadNum; i++) {
new Thread(new Runnable() {
@Override
public void run() {
try {
startLatchLatch.await();
} catch (InterruptedException e) {
System.out.println(e);
error.set(true);
for (int j = 0; j < count; j++) {
finishLatch.countDown();
}
return;
}
for (int j = 0; j < count; j++) {
try {
int textLength = random.nextInt(maxTextLength);
Locator.getInstance().getEditor().insert(Strings.generateRandomString(textLength), textLength);
} catch (Throwable e) {
System.out.println(e);
error.set(true);
} finally {
finishLatch.countDown();
}
}
}
}).start();
startLatchLatch.countDown();
}
if (finishLatch.await(60, TimeUnit.SECONDS)) {
Assert.assertFalse(error.get());
} else {
Assert.fail("Too long execution!");
}
}
}

View File

@@ -0,0 +1,71 @@
package org.solovyev.android.calculator;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import org.hamcrest.BaseMatcher;
import org.hamcrest.Description;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.robolectric.RobolectricGradleTestRunner;
import org.robolectric.annotation.Config;
import javax.annotation.Nonnull;
import static org.mockito.Matchers.argThat;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.robolectric.RuntimeEnvironment.application;
import static org.solovyev.android.calculator.CalculatorBroadcaster.ACTION_DISPLAY_STATE_CHANGED;
import static org.solovyev.android.calculator.CalculatorBroadcaster.ACTION_EDITOR_STATE_CHANGED;
import static org.solovyev.android.calculator.CalculatorEventType.display_state_changed;
import static org.solovyev.android.calculator.CalculatorEventType.editor_state_changed;
import static org.solovyev.android.calculator.CalculatorEventType.editor_state_changed_light;
@Config(manifest = Config.NONE)
@RunWith(RobolectricGradleTestRunner.class)
public class CalculatorBroadcasterTest {
@Nonnull
private CalculatorBroadcaster broadcaster;
@Before
public void setUp() throws Exception {
broadcaster = new CalculatorBroadcaster(application, null);
}
@Test
public void testShouldSendEditorStateChangedIntent() throws Exception {
assertIntentSent(editor_state_changed, ACTION_EDITOR_STATE_CHANGED);
}
@Test
public void testShouldSendEditorStateChangedLiteIntent() throws Exception {
assertIntentSent(editor_state_changed_light, ACTION_EDITOR_STATE_CHANGED);
}
@Test
public void testShouldSendDisplayStateChangedIntent() throws Exception {
assertIntentSent(display_state_changed, ACTION_DISPLAY_STATE_CHANGED);
}
private void assertIntentSent(@Nonnull CalculatorEventType eventType, final String expectedAction) {
final BroadcastReceiver receiver = Mockito.mock(BroadcastReceiver.class);
application.registerReceiver(receiver, new IntentFilter(expectedAction));
broadcaster.onCalculatorEvent(CalculatorEventDataImpl.newInstance(1L, 1L), eventType, null);
verify(receiver, times(1)).onReceive(Mockito.<Context>any(), argThat(new BaseMatcher<Intent>() {
@Override
public boolean matches(Object o) {
return ((Intent) o).getAction().equals(expectedAction);
}
@Override
public void describeTo(Description description) {
}
}));
}
}

View File

@@ -0,0 +1,46 @@
package org.solovyev.android.calculator;
import android.content.Intent;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.robolectric.RobolectricGradleTestRunner;
import org.robolectric.annotation.Config;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.robolectric.RuntimeEnvironment.application;
import static org.solovyev.android.calculator.CalculatorButton.four;
import static org.solovyev.android.calculator.CalculatorReceiver.ACTION_BUTTON_ID_EXTRA;
import static org.solovyev.android.calculator.CalculatorReceiver.ACTION_BUTTON_PRESSED;
import static org.solovyev.android.calculator.CalculatorReceiver.newButtonClickedIntent;
@Config(manifest = Config.NONE)
@RunWith(RobolectricGradleTestRunner.class)
public class CalculatorReceiverTest {
@Test
public void testShouldPressButtonOnIntent() throws Exception {
Locator.setKeyboard(mock(CalculatorKeyboard.class));
final Intent intent = newButtonClickedIntent(application, four);
new CalculatorReceiver().onReceive(application, intent);
verify(Locator.getInstance().getKeyboard(), times(1)).buttonPressed(Mockito.anyString());
verify(Locator.getInstance().getKeyboard(), times(1)).buttonPressed("4");
}
@Test
public void testShouldDoNothingIfButtonInvalid() throws Exception {
Locator.setKeyboard(mock(CalculatorKeyboard.class));
final Intent intent = new Intent(application, CalculatorReceiver.class);
intent.setAction(ACTION_BUTTON_PRESSED);
intent.putExtra(ACTION_BUTTON_ID_EXTRA, "test!@");
new CalculatorReceiver().onReceive(application, intent);
verify(Locator.getInstance().getKeyboard(), times(0)).buttonPressed(Mockito.anyString());
}
}

View File

@@ -22,6 +22,8 @@
package org.solovyev.android.calculator;
import android.content.Context;
import org.junit.Assert;
import org.mockito.Mockito;
import org.solovyev.android.calculator.history.CalculatorHistory;
@@ -63,6 +65,25 @@ public class CalculatorTestUtils {
Locator.getInstance().getEngine().setDecimalGroupSymbols(decimalGroupSymbols);
}
public static void staticSetUp(@Nullable Context context) throws Exception {
Locator.getInstance().init(new CalculatorImpl(), newCalculatorEngine(), Mockito.mock(CalculatorClipboard.class), Mockito.mock(CalculatorNotifier.class), Mockito.mock(CalculatorHistory.class), new SystemOutCalculatorLogger(), Mockito.mock(CalculatorPreferenceService.class), Mockito.mock(CalculatorKeyboard.class), Mockito.mock(CalculatorPlotter.class), null);
Locator.getInstance().getEngine().init();
if (context != null) {
initViews(context);
}
}
public static void initViews(@Nonnull Context context) {
final AndroidCalculatorEditorView editor = new AndroidCalculatorEditorView(context);
editor.init();
Locator.getInstance().getEditor().setView(editor);
final AndroidCalculatorDisplayView display = new AndroidCalculatorDisplayView(context);
display.init(context);
Locator.getInstance().getDisplay().setView(display);
}
@Nonnull
static CalculatorEngineImpl newCalculatorEngine() {
final MathEntityDao mathEntityDao = Mockito.mock(MathEntityDao.class);

View File

@@ -0,0 +1,197 @@
/*
* 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;
import android.graphics.Color;
import org.junit.Before;
import org.junit.Test;
import org.solovyev.android.calculator.text.TextProcessor;
import org.solovyev.android.calculator.view.TextHighlighter;
import java.util.Date;
import java.util.Random;
import jscl.MathEngine;
import jscl.NumeralBase;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
/**
* User: serso
* Date: 10/12/11
* Time: 10:07 PM
*/
public class TextHighlighterTest {
@Before
public void setUp() throws Exception {
CalculatorTestUtils.staticSetUp();
}
@Test
public void testProcess() throws Exception {
TextProcessor<?, String> textHighlighter = new TextHighlighter(Color.TRANSPARENT, false);
final Random random = new Random(new Date().getTime());
for (int i = 0; i < 1000; i++) {
final StringBuilder sb = new StringBuilder();
for (int j = 0; j < 1000; j++) {
sb.append(random.nextBoolean() ? "(" : ")");
}
try {
textHighlighter.process(sb.toString());
} catch (Exception e) {
System.out.println(sb.toString());
throw e;
}
}
assertEquals("<font color=\"#000000\"></font>)(((())())", textHighlighter.process(")(((())())").toString());
assertEquals(")", textHighlighter.process(")").toString());
assertEquals(")()(", textHighlighter.process(")()(").toString());
textHighlighter = new TextHighlighter(0, true);
assertEquals("1 000 000", textHighlighter.process("1000000").toString());
assertEquals("1 000 000", textHighlighter.process("1000000").toString());
assertEquals("0.1E3", textHighlighter.process("0.1E3").toString());
assertEquals("1E3", textHighlighter.process("1E3").toString());
assertEquals("2<b>0x:</b>", textHighlighter.process("20x:").toString());
assertEquals("20g", textHighlighter.process("20g").toString());
assertEquals("22g", textHighlighter.process("22g").toString());
assertEquals("20ю", textHighlighter.process("20ю").toString());
assertEquals("20ъ", textHighlighter.process("20ъ").toString());
assertEquals("3!!", textHighlighter.process("3!!").toString());
assertEquals("2", textHighlighter.process("2").toString());
assertEquals("21", textHighlighter.process("21").toString());
assertEquals("214", textHighlighter.process("214").toString());
assertEquals("2 145", textHighlighter.process("2 145").toString());
assertEquals("1 000 000E3", textHighlighter.process("1000000E3").toString());
assertEquals("-1 000 000E3", textHighlighter.process("-1000000E3").toString());
assertEquals("-1 000 000E-3", textHighlighter.process("-1000000E-3").toString());
assertEquals("-1 000 000E-30000", textHighlighter.process("-1000000E-30000").toString());
textHighlighter = new TextHighlighter(0, false);
textHighlighter.process("cannot calculate 3^10^10 !!!\n" +
" unable to enter 0. FIXED\n" +
" empty display in Xperia Rayo\n" +
" check привиденная FIXED\n" +
" set display result only if text in editor was not changed FIXED\n" +
" shift M text to the left\n" +
" do not show SYNTAX ERROR always (may be show send clock?q) FIXED\n" +
" ln(8)*log(8) => ln(8)*og(8) FIXED\n" +
" copy/paste ln(8)*log(8)\n" +
" 6!^2 ERROR");
assertEquals("<font color=\"#000000\"><i>sin</i>(</font><font color=\"#ffff9a\">2</font><font color=\"#000000\">)</font>", textHighlighter.process("sin(2)").toString());
assertEquals("<font color=\"#000000\"><i>atanh</i>(</font><font color=\"#ffff9a\">2</font><font color=\"#000000\">)</font>", textHighlighter.process("atanh(2)").toString());
assertEquals("<b>0x:</b>E", textHighlighter.process("0x:E").toString());
assertEquals("<b>0x:</b>6F", textHighlighter.process("0x:6F").toString());
assertEquals("<b>0x:</b>6F.", textHighlighter.process("0x:6F.").toString());
assertEquals("<b>0x:</b>6F.2", textHighlighter.process("0x:6F.2").toString());
assertEquals("<b>0x:</b>6F.B", textHighlighter.process("0x:6F.B").toString());
assertEquals("<b>0x:</b>006F.B", textHighlighter.process("0x:006F.B").toString());
assertEquals("<b>0x:</b>0", textHighlighter.process("0x:0").toString());
assertEquals("<b>0x:</b>FF33233FFE", textHighlighter.process("0x:FF33233FFE").toString());
assertEquals("<b>0x:</b>FF33 233 FFE", textHighlighter.process("0x:FF33 233 FFE").toString());
final MathEngine me = Locator.getInstance().getEngine().getMathEngine0();
try {
me.setNumeralBase(NumeralBase.hex);
assertEquals("E", textHighlighter.process("E").toString());
assertEquals(".E", textHighlighter.process(".E").toString());
assertEquals("E+", textHighlighter.process("E+").toString());
assertEquals("E.", textHighlighter.process("E.").toString());
assertEquals(".E.", textHighlighter.process(".E.").toString());
assertEquals("6F", textHighlighter.process("6F").toString());
assertEquals("6F", textHighlighter.process("6F").toString());
assertEquals("6F.", textHighlighter.process("6F.").toString());
assertEquals("6F.2", textHighlighter.process("6F.2").toString());
assertEquals("6F.B", textHighlighter.process("6F.B").toString());
assertEquals("006F.B", textHighlighter.process("006F.B").toString());
} finally {
me.setNumeralBase(NumeralBase.dec);
}
assertEquals("<b>0b:</b>110101", textHighlighter.process("0b:110101").toString());
assertEquals("<b>0b:</b>110101.", textHighlighter.process("0b:110101.").toString());
assertEquals("<b>0b:</b>110101.101", textHighlighter.process("0b:110101.101").toString());
assertEquals("<b>0b:</b>11010100.1", textHighlighter.process("0b:11010100.1").toString());
assertEquals("<b>0b:</b>110101.0", textHighlighter.process("0b:110101.0").toString());
assertEquals("<b>0b:</b>0", textHighlighter.process("0b:0").toString());
assertEquals("<b>0b:</b>1010100101111010101001", textHighlighter.process("0b:1010100101111010101001").toString());
assertEquals("<b>0b:</b>101 010 01 0 111 1 0 10101001", textHighlighter.process("0b:101 010 01 0 111 1 0 10101001").toString());
try {
me.setNumeralBase(NumeralBase.bin);
assertEquals("110101", textHighlighter.process("110101").toString());
assertEquals("110101.", textHighlighter.process("110101.").toString());
assertEquals("110101.101", textHighlighter.process("110101.101").toString());
assertEquals("11010100.1", textHighlighter.process("11010100.1").toString());
assertEquals("110101.0", textHighlighter.process("110101.0").toString());
assertEquals("0", textHighlighter.process("0").toString());
assertEquals("1010100101111010101001", textHighlighter.process("1010100101111010101001").toString());
assertEquals("101 010 01 0 111 1 0 10101001", textHighlighter.process("101 010 01 0 111 1 0 10101001").toString());
} finally {
me.setNumeralBase(NumeralBase.dec);
}
}
@Test
public void testTime() throws Exception {
final TextProcessor<?, String> textHighlighter = new TextHighlighter(Color.WHITE, false);
final int count = 1000;
final String subExpression = "cos(acos(t8ln(t5t85tln(8ln(5t55tln(5))))))+tln(88cos(tln(t)))+t√(ln(t))";
final StringBuilder expression = new StringBuilder(subExpression.length() * count);
for (int i = 0; i < count; i++) {
expression.append(subExpression);
expression.append("+");
}
expression.append(subExpression);
long startTime = System.currentTimeMillis();
textHighlighter.process(expression.toString());
long endTime = System.currentTimeMillis();
System.out.println("Total time, ms: " + (endTime - startTime));
}
@Test
public void testDarkColor() throws Exception {
final TextProcessor<?, String> textHighlighter = new TextHighlighter(Color.BLACK, false);
assertEquals("", textHighlighter.process("sin(2cos(3))"));
}
@Test
public void testIsDark() throws Exception {
assertFalse(TextHighlighter.isDark(Color.WHITE));
assertFalse(TextHighlighter.isDark(Color.LTGRAY));
assertTrue(TextHighlighter.isDark(Color.DKGRAY));
assertTrue(TextHighlighter.isDark(Color.BLACK));
}
}

View File

@@ -0,0 +1,207 @@
/*
* 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.history;
import org.junit.Assert;
import org.junit.Test;
import org.solovyev.android.calculator.CalculatorDisplayViewState;
import org.solovyev.android.calculator.CalculatorDisplayViewStateImpl;
import org.solovyev.android.calculator.CalculatorEditorViewState;
import org.solovyev.android.calculator.CalculatorEditorViewStateImpl;
import org.solovyev.android.calculator.jscl.JsclOperation;
import org.solovyev.common.Objects;
import org.solovyev.common.equals.CollectionEqualizer;
import org.solovyev.common.history.HistoryHelper;
import org.solovyev.common.history.SimpleHistoryHelper;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import static org.junit.Assert.assertEquals;
/**
* User: serso
* Date: 12/17/11
* Time: 10:01 PM
*/
public class HistoryUtilsTest {
private static final String emptyHistory = "<history>\n" +
" <historyItems class=\"java.util.ArrayList\"/>\n" +
"</history>";
private static final String toXml1 = "<history>\n" +
" <historyItems class=\"java.util.ArrayList\">\n" +
" <calculatorHistoryState>\n" +
" <time>100000000</time>\n" +
" <editorState>\n" +
" <cursorPosition>3</cursorPosition>\n" +
" <text>1+1</text>\n" +
" </editorState>\n" +
" <displayState>\n" +
" <editorState>\n" +
" <cursorPosition>0</cursorPosition>\n" +
" <text>Error</text>\n" +
" </editorState>\n" +
" <jsclOperation>simplify</jsclOperation>\n" +
" </displayState>\n" +
" </calculatorHistoryState>\n" +
" </historyItems>\n" +
"</history>";
private static final String toXml2 = "<history>\n" +
" <historyItems class=\"java.util.ArrayList\">\n" +
" <calculatorHistoryState>\n" +
" <time>100000000</time>\n" +
" <editorState>\n" +
" <cursorPosition>3</cursorPosition>\n" +
" <text>1+1</text>\n" +
" </editorState>\n" +
" <displayState>\n" +
" <editorState>\n" +
" <cursorPosition>0</cursorPosition>\n" +
" <text>Error</text>\n" +
" </editorState>\n" +
" <jsclOperation>simplify</jsclOperation>\n" +
" </displayState>\n" +
" </calculatorHistoryState>\n" +
" <calculatorHistoryState>\n" +
" <time>100000000</time>\n" +
" <editorState>\n" +
" <cursorPosition>2</cursorPosition>\n" +
" <text>5/6</text>\n" +
" </editorState>\n" +
" <displayState>\n" +
" <editorState>\n" +
" <cursorPosition>3</cursorPosition>\n" +
" <text>5/6</text>\n" +
" </editorState>\n" +
" <jsclOperation>numeric</jsclOperation>\n" +
" </displayState>\n" +
" </calculatorHistoryState>\n" +
" <calculatorHistoryState>\n" +
" <time>100000000</time>\n" +
" <editorState>\n" +
" <cursorPosition>1</cursorPosition>\n" +
" <text></text>\n" +
" </editorState>\n" +
" <displayState>\n" +
" <editorState>\n" +
" <cursorPosition>0</cursorPosition>\n" +
" <text>Error</text>\n" +
" </editorState>\n" +
" <jsclOperation>elementary</jsclOperation>\n" +
" </displayState>\n" +
" </calculatorHistoryState>\n" +
" <calculatorHistoryState>\n" +
" <time>100000000</time>\n" +
" <editorState>\n" +
" <cursorPosition>0</cursorPosition>\n" +
" <text>4+5/35sin(41)+dfdsfsdfs</text>\n" +
" </editorState>\n" +
" <displayState>\n" +
" <editorState>\n" +
" <cursorPosition>1</cursorPosition>\n" +
" <text>4+5/35sin(41)+dfdsfsdfs</text>\n" +
" </editorState>\n" +
" <jsclOperation>numeric</jsclOperation>\n" +
" </displayState>\n" +
" </calculatorHistoryState>\n" +
" </historyItems>\n" +
"</history>";
@Test
public void testFromXml() throws Exception {
}
@Test
public void testToXml() throws Exception {
final Date date = new Date(100000000);
HistoryHelper<CalculatorHistoryState> history = SimpleHistoryHelper.newInstance();
CalculatorDisplayViewState calculatorDisplay = CalculatorDisplayViewStateImpl.newErrorState(JsclOperation.simplify, "Error");
CalculatorEditorViewState calculatorEditor = CalculatorEditorViewStateImpl.newInstance("1+1", 3);
CalculatorHistoryState state = CalculatorHistoryState.newInstance(calculatorEditor, calculatorDisplay);
state.setTime(date.getTime());
history.addState(state);
assertEquals(emptyHistory, HistoryUtils.toXml(history.getStates()));
state.setSaved(true);
assertEquals(toXml1, HistoryUtils.toXml(history.getStates()));
calculatorDisplay = CalculatorDisplayViewStateImpl.newValidState(JsclOperation.numeric, null, "5/6", 3);
calculatorEditor = CalculatorEditorViewStateImpl.newInstance("5/6", 2);
state = CalculatorHistoryState.newInstance(calculatorEditor, calculatorDisplay);
state.setSaved(true);
state.setTime(date.getTime());
history.addState(state);
calculatorDisplay = CalculatorDisplayViewStateImpl.newErrorState(JsclOperation.elementary, "Error");
calculatorEditor = CalculatorEditorViewStateImpl.newInstance("", 1);
state = CalculatorHistoryState.newInstance(calculatorEditor, calculatorDisplay);
state.setSaved(true);
state.setTime(date.getTime());
history.addState(state);
calculatorDisplay = CalculatorDisplayViewStateImpl.newValidState(JsclOperation.numeric, null, "4+5/35sin(41)+dfdsfsdfs", 1);
calculatorEditor = CalculatorEditorViewStateImpl.newInstance("4+5/35sin(41)+dfdsfsdfs", 0);
state = CalculatorHistoryState.newInstance(calculatorEditor, calculatorDisplay);
state.setSaved(true);
state.setTime(date.getTime());
history.addState(state);
String xml = HistoryUtils.toXml(history.getStates());
assertEquals(toXml2, xml);
final List<CalculatorHistoryState> fromXml = new ArrayList<CalculatorHistoryState>();
final HistoryHelper<CalculatorHistoryState> historyFromXml = SimpleHistoryHelper.newInstance();
HistoryUtils.fromXml(xml, fromXml);
for (CalculatorHistoryState historyState : fromXml) {
historyFromXml.addState(historyState);
}
assertEquals(history.getStates().size(), historyFromXml.getStates().size());
for (CalculatorHistoryState historyState : history.getStates()) {
historyState.setId(0);
historyState.setSaved(true);
}
for (CalculatorHistoryState historyState : historyFromXml.getStates()) {
historyState.setId(0);
historyState.setSaved(true);
}
Assert.assertTrue(Objects.areEqual(history.getStates(), historyFromXml.getStates(), new CollectionEqualizer<CalculatorHistoryState>(null)));
}
}

View File

@@ -0,0 +1,86 @@
/*
* 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.model;
import org.junit.Test;
import org.simpleframework.xml.Serializer;
import org.simpleframework.xml.core.Persister;
import java.io.StringWriter;
import jscl.math.function.IConstant;
import static org.junit.Assert.assertEquals;
/**
* User: serso
* Date: 11/7/11
* Time: 7:52 PM
*/
public class VarTest {
private static final String xml = "<vars>\n" +
" <vars class=\"java.util.ArrayList\">\n" +
" <var>\n" +
" <name>e</name>\n" +
" <value>2.718281828459045</value>\n" +
" <system>true</system>\n" +
" <description>description</description>\n" +
" </var>\n" +
" <var>\n" +
" <name>;</name>\n" +
" <value>3.0</value>\n" +
" <system>true</system>\n" +
" </var>\n" +
" </vars>\n" +
"</vars>";
@Test
public void testXml() throws Exception {
final Vars vars = new Vars();
Var first = new Var.Builder("e", Math.E).setDescription("description").setSystem(true).create();
vars.getEntities().add(first);
Var second = new Var.Builder(";", 3d).setSystem(true).create();
vars.getEntities().add(second);
final StringWriter sw = new StringWriter();
final Serializer serializer = new Persister();
serializer.write(vars, sw);
assertEquals(xml, sw.toString());
final Vars result = serializer.read(Vars.class, xml);
final IConstant actualFirst = result.getEntities().get(0);
final IConstant actualSecond = result.getEntities().get(1);
areEqual(first, actualFirst);
areEqual(second, actualSecond);
}
private void areEqual(IConstant expected, IConstant actual) {
assertEquals(expected.getName(), actual.getName());
assertEquals(expected.getDescription(), actual.getDescription());
assertEquals(expected.getValue(), actual.getValue());
}
}

View File

@@ -0,0 +1,63 @@
/*
* 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.plot;
import org.junit.Test;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import static org.junit.Assert.assertEquals;
/**
* User: serso
* Date: 1/15/13
* Time: 9:58 PM
*/
public class CalculatorGraph2dViewTest {
@Test
public void testFormatTick() throws Exception {
assertEquals("23324", CalculatorGraph2dView.formatTick(23324.0f, 0));
final DecimalFormat format = (DecimalFormat) DecimalFormat.getInstance();
final DecimalFormatSymbols decimalFormatSymbols = format.getDecimalFormatSymbols();
if (',' == decimalFormatSymbols.getDecimalSeparator()) {
assertEquals("23324,1", CalculatorGraph2dView.formatTick(23324.1f, 1));
} else if ('.' == decimalFormatSymbols.getDecimalSeparator()) {
assertEquals("23324.1", CalculatorGraph2dView.formatTick(23324.1f, 1));
}
}
@Test
public void testCountTickDigits() throws Exception {
assertEquals(0, CalculatorGraph2dView.countTickDigits(1));
assertEquals(0, CalculatorGraph2dView.countTickDigits(10));
assertEquals(0, CalculatorGraph2dView.countTickDigits(100));
assertEquals(1, CalculatorGraph2dView.countTickDigits(0.9f));
assertEquals(1, CalculatorGraph2dView.countTickDigits(0.2f));
assertEquals(1, CalculatorGraph2dView.countTickDigits(0.1f));
assertEquals(2, CalculatorGraph2dView.countTickDigits(0.099f));
assertEquals(3, CalculatorGraph2dView.countTickDigits(0.009f));
}
}

View File

@@ -0,0 +1,77 @@
package org.solovyev.android.calculator.view;
import android.app.Activity;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.robolectric.Robolectric;
import org.robolectric.RobolectricGradleTestRunner;
import org.robolectric.Shadows;
import org.robolectric.annotation.Config;
import org.robolectric.res.Attribute;
import org.robolectric.shadows.ShadowActivity;
import org.solovyev.android.calculator.BuildConfig;
import java.util.ArrayList;
import static jscl.AngleUnit.deg;
import static jscl.AngleUnit.grad;
import static jscl.AngleUnit.rad;
import static jscl.AngleUnit.turns;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.solovyev.android.calculator.CalculatorTestUtils.staticSetUp;
@Config(constants = BuildConfig.class)
@RunWith(RobolectricGradleTestRunner.class)
public class AngleUnitsButtonTest {
private AngleUnitsButton button;
@Before
public void setUp() throws Exception {
staticSetUp();
final Activity context = Robolectric.buildActivity(Activity.class).create().get();
final ShadowActivity activity = Shadows.shadowOf(context);
button = new AngleUnitsButton(context, activity.createAttributeSet(new ArrayList<Attribute>(), AngleUnitsButton.class));
}
@Test
public void testShouldReturnDifferentColorsForDifferentAngleUnits() throws Exception {
button.setAngleUnit(deg);
assertEquals(button.getDirectionTextColor(deg.name()), button.getDirectionTextColor(deg.name()));
assertEquals(button.getDirectionTextColor(grad.name()), button.getDirectionTextColor(rad.name()));
assertNotSame(button.getDirectionTextColor(deg.name()), button.getDirectionTextColor(rad.name()));
assertNotSame(button.getDirectionTextColor(deg.name()), button.getDirectionTextColor(grad.name()));
assertNotSame(button.getDirectionTextColor(deg.name()), button.getDirectionTextColor(turns.name()));
}
@Test
public void testIsCurrentAngleUnits() throws Exception {
button.setAngleUnit(rad);
assertTrue(button.isCurrentAngleUnits(rad.name()));
assertFalse(button.isCurrentAngleUnits(deg.name()));
assertFalse(button.isCurrentAngleUnits(grad.name()));
}
@Test
public void testInvalidateShouldBeCalledOnlyWhenChangeIsDone() throws Exception {
button.setAngleUnit(rad);
button = Mockito.spy(button);
button.setAngleUnit(deg);
verify(button, times(1)).invalidate();
button.setAngleUnit(deg);
verify(button, times(1)).invalidate();
}
}

View File

@@ -0,0 +1,74 @@
package org.solovyev.android.calculator.view;
import android.app.Activity;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.robolectric.Robolectric;
import org.robolectric.RobolectricGradleTestRunner;
import org.robolectric.Shadows;
import org.robolectric.res.Attribute;
import org.robolectric.shadows.ShadowActivity;
import java.util.ArrayList;
import static jscl.NumeralBase.bin;
import static jscl.NumeralBase.dec;
import static jscl.NumeralBase.hex;
import static jscl.NumeralBase.oct;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.solovyev.android.calculator.CalculatorTestUtils.staticSetUp;
@RunWith(RobolectricGradleTestRunner.class)
public class NumeralBasesButtonTest {
private NumeralBasesButton button;
@Before
public void setUp() throws Exception {
staticSetUp();
final Activity context = Robolectric.buildActivity(Activity.class).create().get();
final ShadowActivity activity = Shadows.shadowOf(context);
button = new NumeralBasesButton(context, activity.createAttributeSet(new ArrayList<Attribute>(), NumeralBasesButton.class));
}
@Test
public void testShouldReturnDifferentColorsForDifferentNumeralBase() throws Exception {
button.setNumeralBase(dec);
assertEquals(button.getDirectionTextColor(dec.name()), button.getDirectionTextColor(dec.name()));
assertEquals(button.getDirectionTextColor(hex.name()), button.getDirectionTextColor(bin.name()));
assertNotSame(button.getDirectionTextColor(dec.name()), button.getDirectionTextColor(bin.name()));
assertNotSame(button.getDirectionTextColor(dec.name()), button.getDirectionTextColor(hex.name()));
assertNotSame(button.getDirectionTextColor(dec.name()), button.getDirectionTextColor(oct.name()));
}
@Test
public void testIsCurrentNumeralBase() throws Exception {
button.setNumeralBase(dec);
assertTrue(button.isCurrentNumberBase(dec.name()));
assertFalse(button.isCurrentNumberBase(hex.name()));
assertFalse(button.isCurrentNumberBase(bin.name()));
}
@Test
public void testInvalidateShouldBeCalledOnlyWhenChangeIsDone() throws Exception {
button.setNumeralBase(dec);
button = Mockito.spy(button);
button.setNumeralBase(hex);
verify(button, times(1)).invalidate();
button.setNumeralBase(hex);
verify(button, times(1)).invalidate();
}
}

View File

@@ -0,0 +1,205 @@
/*
* 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 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;
import org.robolectric.Robolectric;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.Shadows;
import org.robolectric.shadows.ShadowActivity;
import org.robolectric.util.ActivityController;
import org.solovyev.android.CalculatorTestRunner;
import org.solovyev.android.wizard.Wizard;
import org.solovyev.android.wizard.WizardUi;
import org.solovyev.android.wizard.Wizards;
import java.lang.reflect.Field;
import javax.annotation.Nonnull;
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.CalculatorWizardStep.choose_mode;
@RunWith(value = CalculatorTestRunner.class)
public class CalculatorWizardActivityTest {
private ActivityController<WizardActivity> controller;
private WizardActivity activity;
private Wizards wizards;
private Field uiField;
@Before
public void setUp() throws Exception {
controller = Robolectric.buildActivity(WizardActivity.class);
activity = controller.get();
wizards = new CalculatorWizards(RuntimeEnvironment.application);
activity.setWizards(wizards);
controller.attach();
controller.create();
uiField = WizardActivity.class.getDeclaredField("wizardUi");
uiField.setAccessible(true);
}
@Test
public void testShouldBeFirstTimeWizardByDefault() throws Exception {
assertEquals(CalculatorWizards.FIRST_TIME_WIZARD, getWizardUi().getWizard().getName());
}
@Nonnull
private WizardUi getWizardUi() throws IllegalAccessException {
return (WizardUi) uiField.get(activity);
}
@Test
public void testShouldBeFirstStep() throws Exception {
assertNotNull(getWizardUi().getStep());
assertEquals(getWizardUi().getFlow().getFirstStep(), getWizardUi().getStep());
}
@Test
public void testShouldSaveState() throws Exception {
getWizardUi().setStep(choose_mode);
final Bundle outState = new Bundle();
controller.saveInstanceState(outState);
controller = Robolectric.buildActivity(WizardActivity.class);
controller.create(outState);
activity = controller.get();
assertNotNull(getWizardUi().getFlow());
assertEquals(CalculatorWizards.FIRST_TIME_WIZARD, getWizardUi().getWizard().getName());
assertNotNull(getWizardUi().getStep());
assertEquals(choose_mode, getWizardUi().getStep());
}
@Test
public void testCreate() throws Exception {
final Intent intent = new Intent();
intent.setClass(activity, WizardActivity.class);
intent.putExtra("flow", CalculatorWizards.DEFAULT_WIZARD_FLOW);
controller = Robolectric.buildActivity(WizardActivity.class).withIntent(intent);
controller.create();
activity = controller.get();
assertEquals(CalculatorWizards.DEFAULT_WIZARD_FLOW, getWizardUi().getWizard().getName());
assertEquals(getWizardUi().getFlow().getFirstStep(), getWizardUi().getStep());
final Bundle outState1 = new Bundle();
controller.saveInstanceState(outState1);
controller = Robolectric.buildActivity(WizardActivity.class);
activity = controller.get();
controller.create(outState1);
assertEquals(CalculatorWizards.DEFAULT_WIZARD_FLOW, getWizardUi().getWizard().getName());
assertEquals(getWizardUi().getFlow().getFirstStep(), getWizardUi().getStep());
}
@Test
public void testShouldAddFirstFragment() throws Exception {
controller.start().resume();
final FragmentManager fm = activity.getSupportFragmentManager();
final Fragment f = fm.findFragmentByTag(CalculatorWizardStep.welcome.getFragmentTag());
assertNotNull(f);
assertTrue(f.isAdded());
}
@Test
public void testShouldAddStepFragment() throws Exception {
controller.start().resume();
final FragmentManager fm = activity.getSupportFragmentManager();
getWizardUi().setStep(choose_mode);
final Fragment f = fm.findFragmentByTag(choose_mode.getFragmentTag());
assertNotNull(f);
assertTrue(f.isAdded());
}
@Test
public void testSetStep() throws Exception {
getWizardUi().setStep(choose_mode);
assertEquals(choose_mode, getWizardUi().getStep());
}
@Test
public void testShouldStartWizardActivityAfterStart() throws Exception {
final ShadowActivity shadowActivity = Shadows.shadowOf(controller.get());
WizardUi.startWizard(activity.getWizards(), CalculatorWizards.DEFAULT_WIZARD_FLOW, shadowActivity.getApplicationContext());
assertNotNull(shadowActivity.getNextStartedActivity());
}
@Test
public void testTitleShouldBeSet() throws Exception {
getWizardUi().setStep(choose_mode);
assertEquals(activity.getString(choose_mode.getTitleResId()), activity.getTitle().toString());
}
private void setLastStep() throws IllegalAccessException {
getWizardUi().setStep(CalculatorWizardStep.values()[CalculatorWizardStep.values().length - 1]);
}
private void setFirstStep() throws IllegalAccessException {
getWizardUi().setStep(CalculatorWizardStep.values()[0]);
}
@Test
public void testShouldSaveLastWizardStateOnPause() throws Exception {
final Wizard wizard = wizards.getWizard(getWizardUi().getWizard().getName());
assertNull(wizard.getLastSavedStepName());
getWizardUi().setStep(CalculatorWizardStep.drag_button);
activity.onPause();
assertEquals(CalculatorWizardStep.drag_button.getName(), wizard.getLastSavedStepName());
}
@Test
public void testShouldSaveFinishedIfLastStep() throws Exception {
final Wizard wizard = wizards.getWizard(getWizardUi().getWizard().getName());
assertFalse(wizard.isFinished());
setLastStep();
getWizardUi().finishWizard();
assertTrue(wizard.isFinished());
}
@Test
public void testShouldNotSaveFinishedIfNotLastStep() throws Exception {
final Wizard wizard = wizards.getWizard(getWizardUi().getWizard().getName());
assertFalse(wizard.isFinished());
setFirstStep();
getWizardUi().finishWizard();
assertFalse(wizard.isFinished());
}
}

View File

@@ -0,0 +1,106 @@
package org.solovyev.android.calculator.wizard;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RuntimeEnvironment;
import org.solovyev.android.CalculatorTestRunner;
import org.solovyev.android.wizard.Wizard;
import org.solovyev.android.wizard.WizardFlow;
import org.solovyev.android.wizard.Wizards;
import javax.annotation.Nonnull;
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.junit.Assert.fail;
import static org.solovyev.android.calculator.wizard.CalculatorWizardStep.choose_mode;
import static org.solovyev.android.calculator.wizard.CalculatorWizardStep.last;
import static org.solovyev.android.calculator.wizard.CalculatorWizardStep.welcome;
@RunWith(value = CalculatorTestRunner.class)
public class CalculatorWizardTest {
@Nonnull
private Wizards wizards;
@Nonnull
private Wizard wizard;
@Nonnull
private Wizard defaultWizard;
@Before
public void setUp() throws Exception {
wizards = new CalculatorWizards(RuntimeEnvironment.application);
wizard = wizards.getWizard(null);
defaultWizard = wizards.getWizard(CalculatorWizards.DEFAULT_WIZARD_FLOW);
}
@Test
public void testDefaultFlowShouldNotContainWelcomeAndLastSteps() throws Exception {
final WizardFlow flow = defaultWizard.getFlow();
assertNull(flow.getStepByName(welcome.getName()));
assertNull(flow.getStepByName(last.getName()));
}
@Test
public void testFirstTimeFlowShouldContainWelcomeAndLastSteps() throws Exception {
final WizardFlow flow = wizard.getFlow();
assertNotNull(flow.getStepByName(welcome.getName()));
assertNotNull(flow.getStepByName(last.getName()));
}
@Test
public void testShouldThrowExceptionIfUnknownWizard() throws Exception {
try {
wizards.getWizard("testtesttesttesttest");
fail();
} catch (IllegalArgumentException e) {
// ok
}
}
@Test
public void testShouldReturnWizard() throws Exception {
assertNotNull(wizards.getWizard(CalculatorWizards.FIRST_TIME_WIZARD));
assertNotNull(wizards.getWizard(CalculatorWizards.DEFAULT_WIZARD_FLOW));
}
@Test
public void testShouldSaveWizardIsFinishedWhenNotLastStepAndForce() throws Exception {
assertFalse(wizard.isFinished());
wizard.saveFinished(CalculatorWizardStep.drag_button, true);
assertTrue(wizard.isFinished());
}
@Test
public void testShouldNotSaveWizardIsFinishedWhenNotLastStepAndNotForce() throws Exception {
assertFalse(wizard.isFinished());
wizard.saveFinished(CalculatorWizardStep.drag_button, false);
assertFalse(wizard.isFinished());
}
@Test
public void testShouldSaveWizardIsFinishedWhenLastStep() throws Exception {
assertFalse(wizard.isFinished());
wizard.saveFinished(CalculatorWizardStep.last, false);
assertTrue(wizard.isFinished());
}
@Test
public void testShouldSaveLastWizardStep() throws Exception {
assertFalse(wizard.isStarted());
assertNull(wizard.getLastSavedStepName());
wizard.saveLastStep(choose_mode);
assertTrue(wizard.isStarted());
assertEquals(choose_mode.name(), wizard.getLastSavedStepName());
}
}

View File

@@ -0,0 +1,88 @@
/*
* 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 org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.Robolectric;
import org.robolectric.util.ActivityController;
import org.solovyev.android.CalculatorTestRunner;
import org.solovyev.android.wizard.WizardUi;
import java.lang.reflect.Field;
import javax.annotation.Nonnull;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
@RunWith(CalculatorTestRunner.class)
public class OnScreenCalculatorWizardStepTest {
@Nonnull
private OnScreenCalculatorWizardStep fragment;
@Nonnull
private WizardActivity activity;
@Nonnull
private ActivityController<WizardActivity> controller;
private Field uiField;
@Before
public void setUp() throws Exception {
uiField = WizardActivity.class.getDeclaredField("wizardUi");
uiField.setAccessible(true);
createActivity();
setFragment();
}
@Nonnull
private WizardUi getWizardUi() throws IllegalAccessException {
return (WizardUi) uiField.get(activity);
}
private void createActivity() {
controller = Robolectric.buildActivity(WizardActivity.class).create().start().resume();
activity = controller.get();
}
private void setFragment() throws IllegalAccessException {
getWizardUi().setStep(CalculatorWizardStep.on_screen_calculator);
activity.getSupportFragmentManager().executePendingTransactions();
fragment = (OnScreenCalculatorWizardStep) activity.getSupportFragmentManager().findFragmentByTag(CalculatorWizardStep.on_screen_calculator.getFragmentTag());
}
@Test
public void testShouldRestoreStateOnRestart() throws Exception {
fragment.getCheckbox().setChecked(true);
controller.restart();
assertTrue(fragment.getCheckbox().isChecked());
fragment.getCheckbox().setChecked(false);
controller.restart();
assertFalse(fragment.getCheckbox().isChecked());
}
}