From fa9288ada108b8f162964f29250fe3e43b0c7a8d Mon Sep 17 00:00:00 2001 From: serso Date: Mon, 24 Jul 2017 17:03:16 +0200 Subject: [PATCH] Fix some Robolectric unit tests --- .../calculator/BaseCalculatorTest.java | 6 +- .../calculator/history/HistoryTest.java | 71 ++++++++++--------- .../calculator/model/AndroidEngineTest.java | 28 +++++--- .../wizard/CalculatorWizardTest.java | 20 ++++-- 4 files changed, 75 insertions(+), 50 deletions(-) diff --git a/app/src/test/java/org/solovyev/android/calculator/BaseCalculatorTest.java b/app/src/test/java/org/solovyev/android/calculator/BaseCalculatorTest.java index ba00c70b..45595726 100644 --- a/app/src/test/java/org/solovyev/android/calculator/BaseCalculatorTest.java +++ b/app/src/test/java/org/solovyev/android/calculator/BaseCalculatorTest.java @@ -17,6 +17,9 @@ import org.mockito.ArgumentMatcher; import org.solovyev.android.calculator.calculations.CalculationFailedEvent; import org.solovyev.android.calculator.calculations.CalculationFinishedEvent; import org.solovyev.android.calculator.jscl.JsclOperation; +import org.solovyev.common.msg.Message; + +import java.util.Collections; public abstract class BaseCalculatorTest { protected Calculator calculator; @@ -91,7 +94,8 @@ public abstract class BaseCalculatorTest { @Override public void describeTo(Description description) { - description.appendText(expected); + description.appendText(new CalculationFinishedEvent(operation, expression, 0, null, expected, + Collections.emptyList()).toString()); } }; } diff --git a/app/src/test/java/org/solovyev/android/calculator/history/HistoryTest.java b/app/src/test/java/org/solovyev/android/calculator/history/HistoryTest.java index 3768d2f4..c1e0e73e 100644 --- a/app/src/test/java/org/solovyev/android/calculator/history/HistoryTest.java +++ b/app/src/test/java/org/solovyev/android/calculator/history/HistoryTest.java @@ -22,36 +22,6 @@ package org.solovyev.android.calculator.history; -import android.content.SharedPreferences; -import android.os.Handler; -import android.os.Looper; - -import com.squareup.otto.Bus; - -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.robolectric.Robolectric; -import org.robolectric.RobolectricGradleTestRunner; -import org.robolectric.RuntimeEnvironment; -import org.robolectric.annotation.Config; -import org.solovyev.android.CalculatorTestRunner; -import org.solovyev.android.calculator.BuildConfig; -import org.solovyev.android.calculator.Display; -import org.solovyev.android.calculator.DisplayState; -import org.solovyev.android.calculator.Editor; -import org.solovyev.android.calculator.EditorState; -import org.solovyev.android.calculator.Engine; -import org.solovyev.android.calculator.ErrorReporter; -import org.solovyev.android.calculator.json.Json; -import org.solovyev.android.io.FileSystem; - -import java.io.File; -import java.util.List; - -import javax.annotation.Nonnull; - import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; @@ -67,8 +37,39 @@ import static org.mockito.Mockito.when; import static org.solovyev.android.calculator.Tests.sameThreadExecutor; import static org.solovyev.android.calculator.jscl.JsclOperation.numeric; -@Config(constants = BuildConfig.class, sdk = CalculatorTestRunner.SUPPORTED_SDK) -@RunWith(RobolectricGradleTestRunner.class) +import android.content.SharedPreferences; +import android.os.Handler; +import android.os.Looper; + +import com.squareup.otto.Bus; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.robolectric.Robolectric; +import org.robolectric.RobolectricTestRunner; +import org.robolectric.RuntimeEnvironment; +import org.robolectric.annotation.Config; +import org.solovyev.android.calculator.BuildConfig; +import org.solovyev.android.calculator.Display; +import org.solovyev.android.calculator.DisplayState; +import org.solovyev.android.calculator.Editor; +import org.solovyev.android.calculator.EditorState; +import org.solovyev.android.calculator.Engine; +import org.solovyev.android.calculator.ErrorReporter; +import org.solovyev.android.calculator.json.Json; +import org.solovyev.android.io.FileSystem; + +import java.io.File; +import java.util.List; + +import javax.annotation.Nonnull; + +import dagger.Lazy; + +@Config(constants = BuildConfig.class, manifest = Config.NONE) +@RunWith(value = RobolectricTestRunner.class) public class HistoryTest { private History history; @@ -77,6 +78,12 @@ public class HistoryTest { public void setUp() throws Exception { history = new History(); history.backgroundThread = sameThreadExecutor(); + history.filesDir = new Lazy() { + @Override + public File get() { + return new File("."); + } + }; history.application = RuntimeEnvironment.application; history.bus = mock(Bus.class); history.errorReporter = mock(ErrorReporter.class); diff --git a/app/src/test/java/org/solovyev/android/calculator/model/AndroidEngineTest.java b/app/src/test/java/org/solovyev/android/calculator/model/AndroidEngineTest.java index 8f6cd6aa..1ec4fd4e 100644 --- a/app/src/test/java/org/solovyev/android/calculator/model/AndroidEngineTest.java +++ b/app/src/test/java/org/solovyev/android/calculator/model/AndroidEngineTest.java @@ -22,6 +22,20 @@ package org.solovyev.android.calculator.model; +import static org.junit.Assert.fail; + +import junit.framework.Assert; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.robolectric.RobolectricTestRunner; +import org.robolectric.annotation.Config; +import org.solovyev.android.calculator.BaseCalculatorTest; +import org.solovyev.android.calculator.BuildConfig; +import org.solovyev.android.calculator.jscl.JsclOperation; +import org.solovyev.android.calculator.variables.CppVariable; + import jscl.AngleUnit; import jscl.JsclMathEngine; import jscl.MathEngine; @@ -29,15 +43,9 @@ import jscl.NumeralBase; import jscl.math.Expression; import jscl.math.function.CustomFunction; import jscl.text.ParseException; -import junit.framework.Assert; -import org.junit.Before; -import org.junit.Test; -import org.solovyev.android.calculator.BaseCalculatorTest; -import org.solovyev.android.calculator.jscl.JsclOperation; -import org.solovyev.android.calculator.variables.CppVariable; - -import static org.junit.Assert.fail; +@Config(constants = BuildConfig.class, manifest = Config.NONE) +@RunWith(value = RobolectricTestRunner.class) public class AndroidEngineTest extends BaseCalculatorTest { @Before @@ -154,7 +162,7 @@ public class AndroidEngineTest extends BaseCalculatorTest { assertEval("e", "e*0x:ABCDEF/0x:ABCDEF"); assertEval("30 606 154.462", "0x:ABCDEF*e*0x:ABCDEF/0x:ABCDEF"); assertEval("30 606 154.462", "c+0x:ABCDEF*e*0x:ABCDEF/0x:ABCDEF-c+0x:C-0x:C"); - assertEval("1 446 257 064 651.832", "28*28 * sin(28) - 0b:1101 + √(28) + exp ( 28) "); + assertEval("1 446 257 064 651.832", "28*28 * sin(28) - 0b:1101 + √(28) + exp (28)"); assertEval("13", "0b:1101"); assertError("0b:π"); @@ -163,7 +171,7 @@ public class AndroidEngineTest extends BaseCalculatorTest { try { me.setNumeralBase(NumeralBase.bin); assertEval("101", "10+11"); - assertEval("0.101", "10/11"); + assertEval("0.1011", "10/11"); me.setNumeralBase(NumeralBase.hex); assertEval("63 7B", "56CE+CAD"); diff --git a/app/src/test/java/org/solovyev/android/calculator/wizard/CalculatorWizardTest.java b/app/src/test/java/org/solovyev/android/calculator/wizard/CalculatorWizardTest.java index 1933d861..d67d7811 100644 --- a/app/src/test/java/org/solovyev/android/calculator/wizard/CalculatorWizardTest.java +++ b/app/src/test/java/org/solovyev/android/calculator/wizard/CalculatorWizardTest.java @@ -1,10 +1,19 @@ package org.solovyev.android.calculator.wizard; -import android.os.Build; +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; + import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; -import org.robolectric.RobolectricGradleTestRunner; +import org.robolectric.RobolectricTestRunner; import org.robolectric.RuntimeEnvironment; import org.robolectric.annotation.Config; import org.solovyev.android.calculator.BuildConfig; @@ -14,11 +23,8 @@ import org.solovyev.android.wizard.Wizards; import javax.annotation.Nonnull; -import static org.junit.Assert.*; -import static org.solovyev.android.calculator.wizard.CalculatorWizardStep.*; - -@Config(constants = BuildConfig.class, sdk = Build.VERSION_CODES.LOLLIPOP) -@RunWith(value = RobolectricGradleTestRunner.class) +@Config(constants = BuildConfig.class, manifest = Config.NONE) +@RunWith(value = RobolectricTestRunner.class) public class CalculatorWizardTest { @Nonnull