Test modules
This commit is contained in:
112
android-app-tests/android-app-tests.iml
Normal file
112
android-app-tests/android-app-tests.iml
Normal file
@@ -0,0 +1,112 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module external.linked.project.id=":android-app-tests" external.linked.project.path="$MODULE_DIR$" external.root.project.path="$MODULE_DIR$/.." external.system.id="GRADLE" external.system.module.group="calculatorpp" external.system.module.version="unspecified" type="JAVA_MODULE" version="4">
|
||||
<component name="FacetManager">
|
||||
<facet type="android-gradle" name="Android-Gradle">
|
||||
<configuration>
|
||||
<option name="GRADLE_PROJECT_PATH" value=":android-app-tests" />
|
||||
</configuration>
|
||||
</facet>
|
||||
<facet type="java-gradle" name="Java-Gradle">
|
||||
<configuration>
|
||||
<option name="BUILD_FOLDER_PATH" />
|
||||
</configuration>
|
||||
</facet>
|
||||
</component>
|
||||
<component name="NewModuleRootManager" inherit-compiler-output="false">
|
||||
<output url="file://$MODULE_DIR$/build/classes/main" />
|
||||
<output-test url="file://$MODULE_DIR$/build/classes/test" />
|
||||
<exclude-output />
|
||||
<content url="file://$MODULE_DIR$">
|
||||
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/src/test/resources" type="java-test-resource" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/.gradle" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/build" />
|
||||
</content>
|
||||
<orderEntry type="inheritedJdk" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
<orderEntry type="module" module-name="android-app" exported="" />
|
||||
<orderEntry type="library" exported="" scope="TEST" name="junit-4.11" level="project" />
|
||||
<orderEntry type="library" exported="" scope="TEST" name="robolectric-2.3" level="project" />
|
||||
<orderEntry type="library" exported="" scope="TEST" name="mockito-all-1.9.5" level="project" />
|
||||
<orderEntry type="library" exported="" scope="TEST" name="jsonassert-1.2.3" level="project" />
|
||||
<orderEntry type="library" exported="" scope="TEST" name="hamcrest-core-1.3" level="project" />
|
||||
<orderEntry type="library" exported="" scope="TEST" name="objenesis-1.3" level="project" />
|
||||
<orderEntry type="library" exported="" scope="TEST" name="sqlite4java-0.282" level="project" />
|
||||
<orderEntry type="library" exported="" scope="TEST" name="asm-5.0.1" level="project" />
|
||||
<orderEntry type="library" exported="" scope="TEST" name="asm-commons-5.0.1" level="project" />
|
||||
<orderEntry type="library" exported="" scope="TEST" name="asm-util-5.0.1" level="project" />
|
||||
<orderEntry type="library" exported="" scope="TEST" name="asm-analysis-5.0.1" level="project" />
|
||||
<orderEntry type="library" exported="" scope="TEST" name="commons-logging-1.1.1" level="project" />
|
||||
<orderEntry type="library" exported="" scope="TEST" name="commons-codec-1.3" level="project" />
|
||||
<orderEntry type="library" exported="" scope="TEST" name="httpclient-4.0.3" level="project" />
|
||||
<orderEntry type="library" exported="" scope="TEST" name="vtd-xml-2.11" level="project" />
|
||||
<orderEntry type="library" exported="" scope="TEST" name="support-v4-19.0.1" level="project" />
|
||||
<orderEntry type="library" exported="" scope="TEST" name="fest-util-1.2.5" level="project" />
|
||||
<orderEntry type="library" exported="" scope="TEST" name="fest-reflect-1.4.1" level="project" />
|
||||
<orderEntry type="library" exported="" scope="TEST" name="maven-ant-tasks-2.1.3" level="project" />
|
||||
<orderEntry type="library" exported="" scope="TEST" name="json-20090211" level="project" />
|
||||
<orderEntry type="library" exported="" scope="TEST" name="asm-tree-5.0.1" level="project" />
|
||||
<orderEntry type="library" exported="" scope="TEST" name="httpcore-4.0.1" level="project" />
|
||||
<orderEntry type="library" exported="" scope="TEST" name="ant-1.8.0" level="project" />
|
||||
<orderEntry type="library" exported="" scope="TEST" name="classworlds-1.1-alpha-2" level="project" />
|
||||
<orderEntry type="library" exported="" scope="TEST" name="plexus-container-default-1.0-alpha-9-stable-1" level="project" />
|
||||
<orderEntry type="library" exported="" scope="TEST" name="plexus-utils-1.5.15" level="project" />
|
||||
<orderEntry type="library" exported="" scope="TEST" name="plexus-interpolation-1.11" level="project" />
|
||||
<orderEntry type="library" exported="" scope="TEST" name="maven-artifact-2.2.1" level="project" />
|
||||
<orderEntry type="library" exported="" scope="TEST" name="maven-artifact-manager-2.2.1" level="project" />
|
||||
<orderEntry type="library" exported="" scope="TEST" name="maven-model-2.2.1" level="project" />
|
||||
<orderEntry type="library" exported="" scope="TEST" name="maven-project-2.2.1" level="project" />
|
||||
<orderEntry type="library" exported="" scope="TEST" name="maven-error-diagnostics-2.2.1" level="project" />
|
||||
<orderEntry type="library" exported="" scope="TEST" name="maven-settings-2.2.1" level="project" />
|
||||
<orderEntry type="library" exported="" scope="TEST" name="wagon-file-1.0-beta-6" level="project" />
|
||||
<orderEntry type="library" exported="" scope="TEST" name="wagon-http-lightweight-1.0-beta-6" level="project" />
|
||||
<orderEntry type="library" exported="" scope="TEST" name="wagon-provider-api-1.0-beta-6" level="project" />
|
||||
<orderEntry type="library" exported="" scope="TEST" name="ant-launcher-1.8.0" level="project" />
|
||||
<orderEntry type="library" exported="" scope="TEST" name="maven-repository-metadata-2.2.1" level="project" />
|
||||
<orderEntry type="library" exported="" scope="TEST" name="backport-util-concurrent-3.1" level="project" />
|
||||
<orderEntry type="library" exported="" scope="TEST" name="maven-profile-2.2.1" level="project" />
|
||||
<orderEntry type="library" exported="" scope="TEST" name="maven-plugin-registry-2.2.1" level="project" />
|
||||
<orderEntry type="library" exported="" scope="TEST" name="wagon-http-shared-1.0-beta-6" level="project" />
|
||||
<orderEntry type="library" exported="" scope="TEST" name="xercesMinimal-1.9.6.2" level="project" />
|
||||
<orderEntry type="library" exported="" scope="TEST" name="nekohtml-1.9.6.2" level="project" />
|
||||
<orderEntry type="library" exported="" scope="TEST" name="classes" level="project" />
|
||||
<orderEntry type="library" exported="" scope="TEST" name="simple-xml-2.6.1" level="project" />
|
||||
<orderEntry type="library" exported="" scope="TEST" name="core" level="project" />
|
||||
<orderEntry type="library" exported="" scope="TEST" name="commons-cli-1.2" level="project" />
|
||||
<orderEntry type="library" exported="" scope="TEST" name="annotations-2.0.1" level="project" />
|
||||
<orderEntry type="library" exported="" scope="TEST" name="classes" level="project" />
|
||||
<orderEntry type="library" exported="" scope="TEST" name="classes" level="project" />
|
||||
<orderEntry type="library" exported="" scope="TEST" name="stax-1.2.0" level="project" />
|
||||
<orderEntry type="library" exported="" scope="TEST" name="classes" level="project" />
|
||||
<orderEntry type="library" exported="" scope="TEST" name="classes" level="project" />
|
||||
<orderEntry type="library" exported="" scope="TEST" name="classes" level="project" />
|
||||
<orderEntry type="library" exported="" scope="TEST" name="classes" level="project" />
|
||||
<orderEntry type="library" exported="" scope="TEST" name="classes" level="project" />
|
||||
<orderEntry type="library" exported="" scope="TEST" name="classes" level="project" />
|
||||
<orderEntry type="library" exported="" scope="TEST" name="classes" level="project" />
|
||||
<orderEntry type="library" exported="" scope="TEST" name="support-annotations-21.0.3" level="project" />
|
||||
<orderEntry type="library" exported="" scope="TEST" name="common-msg-1.0.5" level="project" />
|
||||
<orderEntry type="library" exported="" scope="TEST" name="classes" level="project" />
|
||||
<orderEntry type="library" exported="" scope="TEST" name="common-listeners-1.0.7" level="project" />
|
||||
<orderEntry type="library" exported="" scope="TEST" name="guava-11.0.2" level="project" />
|
||||
<orderEntry type="library" exported="" scope="TEST" name="admob-6.4.1-r11.0.0" level="project" />
|
||||
<orderEntry type="library" exported="" scope="TEST" name="common-text-1.0.7" level="project" />
|
||||
<orderEntry type="library" exported="" scope="TEST" name="classes" level="project" />
|
||||
<orderEntry type="library" exported="" scope="TEST" name="acra-4.5.0" level="project" />
|
||||
<orderEntry type="library" exported="" scope="TEST" name="classes" level="project" />
|
||||
<orderEntry type="library" exported="" scope="TEST" name="internal_impl-21.0.3" level="project" />
|
||||
<orderEntry type="library" exported="" scope="TEST" name="classes" level="project" />
|
||||
<orderEntry type="library" exported="" scope="TEST" name="classes" level="project" />
|
||||
<orderEntry type="library" exported="" scope="TEST" name="common-core-1.0.7" level="project" />
|
||||
<orderEntry type="library" exported="" scope="TEST" name="jscl-1.0.8" level="project" />
|
||||
<orderEntry type="library" exported="" scope="TEST" name="classes" level="project" />
|
||||
<orderEntry type="library" exported="" scope="TEST" name="common-security-1.0.7" level="project" />
|
||||
<orderEntry type="library" exported="" scope="TEST" name="classes" level="project" />
|
||||
<orderEntry type="library" exported="" scope="TEST" name="classes" level="project" />
|
||||
<orderEntry type="library" exported="" scope="TEST" name="_home_serso_projects_java_serso_android_calculatorpp_android_app_build_intermediates_classes_debug" level="project" />
|
||||
<orderEntry type="library" exported="" scope="TEST" name="_home_serso_projects_java_serso_android_calculatorpp_android_app_build_intermediates_dependency_cache_debug" level="project" />
|
||||
<orderEntry type="library" exported="" scope="TEST" name="android" level="project" />
|
||||
</component>
|
||||
</module>
|
52
android-app-tests/build.gradle
Normal file
52
android-app-tests/build.gradle
Normal file
@@ -0,0 +1,52 @@
|
||||
/*
|
||||
* Copyright 2014 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
|
||||
*/
|
||||
|
||||
apply plugin: 'java'
|
||||
|
||||
dependencies {
|
||||
def androidModule = project(':android-app')
|
||||
compile androidModule
|
||||
|
||||
testCompile androidModule.android.applicationVariants.toList().first().javaCompile.classpath
|
||||
testCompile androidModule.android.applicationVariants.toList().first().javaCompile.outputs.files
|
||||
testCompile files(androidModule.plugins.findPlugin("com.android.application").getBootClasspath())
|
||||
|
||||
testCompile 'junit:junit:4.11'
|
||||
testCompile 'org.robolectric:robolectric:2.3'
|
||||
testCompile "org.mockito:mockito-all:1.9.5"
|
||||
testCompile "org.skyscreamer:jsonassert:1.2.3"
|
||||
}
|
||||
|
||||
tasks.withType(Test) {
|
||||
scanForTestClasses = false
|
||||
include "**/*Test.class"
|
||||
exclude "**/*AndroidCalculatorEditorViewTest*"
|
||||
exclude "**/*CalculatorWizardActivityTest*"
|
||||
exclude "**/*OnScreenCalculatorWizardStepTest*"
|
||||
exclude "**/*CalculatorWizardTest*"
|
||||
exclude "**/*WizardStepTest*"
|
||||
}
|
||||
|
||||
test {
|
||||
maxParallelForks = 15
|
||||
forkEvery = 1
|
||||
}
|
@@ -0,0 +1,97 @@
|
||||
/*
|
||||
* 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.solovyev.android.calculator.units.CalculatorNumeralBase;
|
||||
import org.solovyev.common.units.Unit;
|
||||
import org.solovyev.common.units.UnitConverter;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.Random;
|
||||
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
/**
|
||||
* User: serso
|
||||
* Date: 4/21/12
|
||||
* Time: 8:24 PM
|
||||
*/
|
||||
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();
|
||||
}
|
||||
}
|
@@ -0,0 +1,27 @@
|
||||
package org.solovyev.android;
|
||||
|
||||
import org.junit.runners.model.InitializationError;
|
||||
import org.robolectric.AndroidManifest;
|
||||
import org.robolectric.RobolectricTestRunner;
|
||||
import org.robolectric.annotation.Config;
|
||||
import org.robolectric.res.Fs;
|
||||
|
||||
public class CalculatorTestRunner extends RobolectricTestRunner {
|
||||
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 = "../android-app/src/main/AndroidManifest.xml";
|
||||
final String resourcesFilePath = "../android-app-core/src/main/res";
|
||||
return new AndroidManifest(Fs.fileFromPath(manifestFilePath), Fs.fileFromPath(resourcesFilePath)) {
|
||||
@Override
|
||||
public int getTargetSdkVersion() {
|
||||
return MAX_SDK_SUPPORTED_BY_ROBOLECTRIC;
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
@@ -0,0 +1,111 @@
|
||||
/*
|
||||
* 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!");
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,79 @@
|
||||
/*
|
||||
* 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.content.Context;
|
||||
import jscl.JsclMathEngine;
|
||||
|
||||
import org.mockito.Mockito;
|
||||
import org.solovyev.android.calculator.history.CalculatorHistory;
|
||||
import org.solovyev.android.calculator.plot.CalculatorPlotter;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
/**
|
||||
* User: serso
|
||||
* Date: 10/7/12
|
||||
* Time: 8:56 PM
|
||||
*/
|
||||
public class CalculatorTestUtils {
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
public static void staticSetUp() throws Exception {
|
||||
staticSetUp(null);
|
||||
}
|
||||
|
||||
|
||||
@Nonnull
|
||||
static CalculatorEngineImpl newCalculatorEngine() {
|
||||
final MathEntityDao mathEntityDao = Mockito.mock(MathEntityDao.class);
|
||||
|
||||
final JsclMathEngine jsclEngine = JsclMathEngine.getInstance();
|
||||
|
||||
final CalculatorVarsRegistry varsRegistry = new CalculatorVarsRegistry(jsclEngine.getConstantsRegistry(), mathEntityDao);
|
||||
final CalculatorFunctionsMathRegistry functionsRegistry = new CalculatorFunctionsMathRegistry(jsclEngine.getFunctionsRegistry(), mathEntityDao);
|
||||
final CalculatorOperatorsMathRegistry operatorsRegistry = new CalculatorOperatorsMathRegistry(jsclEngine.getOperatorsRegistry(), mathEntityDao);
|
||||
final CalculatorPostfixFunctionsRegistry postfixFunctionsRegistry = new CalculatorPostfixFunctionsRegistry(jsclEngine.getPostfixFunctionsRegistry(), mathEntityDao);
|
||||
|
||||
return new CalculatorEngineImpl(jsclEngine, varsRegistry, functionsRegistry, operatorsRegistry, postfixFunctionsRegistry, null);
|
||||
}
|
||||
}
|
@@ -0,0 +1,179 @@
|
||||
/*
|
||||
* 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 jscl.MathEngine;
|
||||
import jscl.NumeralBase;
|
||||
|
||||
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 static org.junit.Assert.assertEquals;
|
||||
|
||||
/**
|
||||
* 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(0, 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));
|
||||
}
|
||||
}
|
@@ -0,0 +1,209 @@
|
||||
/*
|
||||
* 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 {
|
||||
|
||||
@Test
|
||||
public void testFromXml() throws Exception {
|
||||
|
||||
}
|
||||
|
||||
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 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)));
|
||||
}
|
||||
}
|
@@ -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 jscl.math.function.IConstant;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.simpleframework.xml.Serializer;
|
||||
import org.simpleframework.xml.core.Persister;
|
||||
|
||||
import java.io.StringWriter;
|
||||
|
||||
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());
|
||||
}
|
||||
}
|
@@ -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));
|
||||
}
|
||||
}
|
@@ -0,0 +1,214 @@
|
||||
/*
|
||||
* 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.shadows.ShadowActivity;
|
||||
import org.robolectric.util.ActivityController;
|
||||
import org.solovyev.android.CalculatorTestRunner;
|
||||
import org.solovyev.android.wizard.BaseWizardActivity;
|
||||
import org.solovyev.android.wizard.Wizard;
|
||||
import org.solovyev.android.wizard.WizardUi;
|
||||
import org.solovyev.android.wizard.Wizards;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import java.lang.reflect.Field;
|
||||
|
||||
import static android.view.View.GONE;
|
||||
import static android.view.View.VISIBLE;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.solovyev.android.calculator.wizard.CalculatorWizardStep.choose_mode;
|
||||
|
||||
@RunWith(value = CalculatorTestRunner.class)
|
||||
public class CalculatorWizardActivityTest {
|
||||
|
||||
private ActivityController<CalculatorWizardActivity> controller;
|
||||
private CalculatorWizardActivity activity;
|
||||
private Wizards wizards;
|
||||
private Field uiField;
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
controller = Robolectric.buildActivity(CalculatorWizardActivity.class);
|
||||
activity = controller.get();
|
||||
wizards = new CalculatorWizards(Robolectric.application);
|
||||
activity.setWizards(wizards);
|
||||
controller.attach();
|
||||
controller.create();
|
||||
|
||||
uiField = BaseWizardActivity.class.getDeclaredField("ui");
|
||||
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(CalculatorWizardActivity.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, CalculatorWizardActivity.class);
|
||||
intent.putExtra("flow", CalculatorWizards.DEFAULT_WIZARD_FLOW);
|
||||
controller = Robolectric.buildActivity(CalculatorWizardActivity.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(CalculatorWizardActivity.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 = Robolectric.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());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNextButtonShouldBeShownAtTheEnd() throws Exception {
|
||||
setLastStep();
|
||||
assertEquals(VISIBLE, getWizardUi().getPrevButton().getVisibility());
|
||||
assertEquals(VISIBLE, getWizardUi().getNextButton().getVisibility());
|
||||
}
|
||||
|
||||
private void setLastStep() throws IllegalAccessException {
|
||||
getWizardUi().setStep(CalculatorWizardStep.values()[CalculatorWizardStep.values().length - 1]);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPrevButtonShouldNotBeShownAtTheStart() throws Exception {
|
||||
setFirstStep();
|
||||
assertEquals(VISIBLE, getWizardUi().getNextButton().getVisibility());
|
||||
assertEquals(GONE, getWizardUi().getPrevButton().getVisibility());
|
||||
}
|
||||
|
||||
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());
|
||||
}
|
||||
}
|
@@ -0,0 +1,99 @@
|
||||
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.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.*;
|
||||
import static org.solovyev.android.calculator.wizard.CalculatorWizardStep.*;
|
||||
|
||||
@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(Robolectric.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());
|
||||
}
|
||||
}
|
@@ -0,0 +1,108 @@
|
||||
/*
|
||||
* 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.SharedPreferences;
|
||||
import android.preference.PreferenceManager;
|
||||
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.calculator.CalculatorPreferences;
|
||||
import org.solovyev.android.wizard.BaseWizardActivity;
|
||||
import org.solovyev.android.wizard.WizardUi;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import java.lang.reflect.Field;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
@RunWith(CalculatorTestRunner.class)
|
||||
public class OnScreenCalculatorWizardStepTest {
|
||||
|
||||
@Nonnull
|
||||
private OnScreenCalculatorWizardStep fragment;
|
||||
|
||||
@Nonnull
|
||||
private CalculatorWizardActivity activity;
|
||||
|
||||
@Nonnull
|
||||
private ActivityController<CalculatorWizardActivity> controller;
|
||||
private Field uiField;
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
uiField = BaseWizardActivity.class.getDeclaredField("ui");
|
||||
uiField.setAccessible(true);
|
||||
|
||||
createActivity();
|
||||
setFragment();
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
private WizardUi getWizardUi() throws IllegalAccessException {
|
||||
return (WizardUi) uiField.get(activity);
|
||||
}
|
||||
|
||||
private void createActivity() {
|
||||
controller = Robolectric.buildActivity(CalculatorWizardActivity.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.getOnscreenCalculatorCheckbox().setChecked(true);
|
||||
controller.restart();
|
||||
assertTrue(fragment.getOnscreenCalculatorCheckbox().isChecked());
|
||||
|
||||
fragment.getOnscreenCalculatorCheckbox().setChecked(false);
|
||||
controller.restart();
|
||||
assertFalse(fragment.getOnscreenCalculatorCheckbox().isChecked());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testShouldBeEnabledIfIconIsShown() throws Exception {
|
||||
testShouldBeEqualsToIconState(true);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testShouldBeDisabledIfIconIsNotShown() throws Exception {
|
||||
testShouldBeEqualsToIconState(false);
|
||||
}
|
||||
|
||||
private void testShouldBeEqualsToIconState(boolean iconEnabled) throws IllegalAccessException {
|
||||
final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(Robolectric.application);
|
||||
CalculatorPreferences.OnscreenCalculator.showAppIcon.putPreference(preferences, iconEnabled);
|
||||
createActivity();
|
||||
setFragment();
|
||||
assertEquals(iconEnabled, fragment.isOnscreenCalculatorEnabled());
|
||||
}
|
||||
}
|
@@ -0,0 +1,181 @@
|
||||
/*
|
||||
* 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.preference.PreferenceManager;
|
||||
import android.support.v4.app.Fragment;
|
||||
import android.support.v4.app.FragmentActivity;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.robolectric.Robolectric;
|
||||
import org.robolectric.annotation.Config;
|
||||
import org.solovyev.android.CalculatorTestRunner;
|
||||
import org.solovyev.android.calculator.CalculatorPreferences;
|
||||
|
||||
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.CalculatorWizardStep.*;
|
||||
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;
|
||||
|
||||
@RunWith(CalculatorTestRunner.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 (CalculatorWizardStep wizardStep : CalculatorWizardStep.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);
|
||||
CalculatorWizardStep.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);
|
||||
choose_layout.onNext(layoutFragment);
|
||||
}
|
||||
|
||||
/* @Config(qualifiers = "large")
|
||||
@Test
|
||||
public void testChooseLayoutShouldBeVisibleForTablet() throws Exception {
|
||||
assertTrue(CalculatorWizardStep.choose_layout.isVisible());
|
||||
}*/
|
||||
|
||||
@Config(qualifiers = "normal")
|
||||
@Test
|
||||
public void testChooseLayoutShouldNotBeVisibleForMobile() throws Exception {
|
||||
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);
|
||||
CalculatorWizardStep.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));
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user