migration test added
This commit is contained in:
parent
6734bfeaa4
commit
33c0f44a73
@ -26,20 +26,24 @@ import android.app.Application;
|
||||
import android.content.SharedPreferences;
|
||||
import android.os.Handler;
|
||||
import android.support.annotation.NonNull;
|
||||
|
||||
import com.google.common.base.Strings;
|
||||
import com.squareup.otto.Bus;
|
||||
import com.squareup.otto.Subscribe;
|
||||
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONException;
|
||||
import org.solovyev.android.Check;
|
||||
import org.solovyev.android.calculator.*;
|
||||
import org.solovyev.android.calculator.App;
|
||||
import org.solovyev.android.calculator.AppModule;
|
||||
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.Locator;
|
||||
import org.solovyev.android.io.FileLoader;
|
||||
import org.solovyev.android.io.FileSaver;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import javax.annotation.Nullable;
|
||||
import javax.inject.Inject;
|
||||
import javax.inject.Named;
|
||||
import java.io.File;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
@ -47,6 +51,11 @@ import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.Executor;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import javax.annotation.Nullable;
|
||||
import javax.inject.Inject;
|
||||
import javax.inject.Named;
|
||||
|
||||
import static android.text.TextUtils.isEmpty;
|
||||
|
||||
public class History {
|
||||
@ -95,14 +104,12 @@ public class History {
|
||||
}
|
||||
final List<HistoryState> states = new ArrayList<>();
|
||||
for (OldHistoryState state : history.getItems()) {
|
||||
final OldEditorHistoryState oldEditorState = state.getEditorState();
|
||||
final OldDisplayHistoryState oldDisplayState = state.getDisplayState();
|
||||
final String editorText = oldEditorState.getText();
|
||||
final EditorState editor = EditorState.create(Strings.nullToEmpty(editorText), oldEditorState.getCursorPosition());
|
||||
final DisplayState display = oldDisplayState.isValid()
|
||||
? DisplayState.createValid(oldDisplayState.getJsclOperation(), null, Strings.nullToEmpty(oldDisplayState.getEditorState().getText()), EditorState.NO_SEQUENCE)
|
||||
: DisplayState.createError(oldDisplayState.getJsclOperation(), "", EditorState.NO_SEQUENCE);
|
||||
states.add(HistoryState.newBuilder(editor, display).build());
|
||||
final OldEditorHistoryState oldEditor = state.getEditorState();
|
||||
final OldDisplayHistoryState oldDisplay = state.getDisplayState();
|
||||
final String editorText = oldEditor.getText();
|
||||
final EditorState editor = EditorState.create(Strings.nullToEmpty(editorText), oldEditor.getCursorPosition());
|
||||
final DisplayState display = DisplayState.createValid(oldDisplay.getJsclOperation(), null, Strings.nullToEmpty(oldDisplay.getEditorState().getText()), EditorState.NO_SEQUENCE);
|
||||
states.add(HistoryState.newBuilder(editor, display).withTime(state.getTime()).withComment(state.getComment()).build());
|
||||
}
|
||||
return states;
|
||||
}
|
||||
|
@ -1,7 +1,5 @@
|
||||
package org.solovyev.android.calculator.history;
|
||||
|
||||
import android.text.TextUtils;
|
||||
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
import org.solovyev.android.Check;
|
||||
@ -9,6 +7,9 @@ import org.solovyev.android.calculator.DisplayState;
|
||||
import org.solovyev.android.calculator.EditorState;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
import static android.text.TextUtils.isEmpty;
|
||||
|
||||
public class HistoryState {
|
||||
|
||||
@ -52,7 +53,7 @@ public class HistoryState {
|
||||
json.put(JSON_EDITOR, editor.toJson());
|
||||
json.put(JSON_DISPLAY, display.toJson());
|
||||
json.put(JSON_TIME, time);
|
||||
if (!TextUtils.isEmpty(comment)) {
|
||||
if (!isEmpty(comment)) {
|
||||
json.put(JSON_COMMENT, comment);
|
||||
}
|
||||
return json;
|
||||
@ -97,17 +98,21 @@ public class HistoryState {
|
||||
|
||||
private Builder(@Nonnull EditorState editor, @Nonnull DisplayState display) {
|
||||
super(editor, display);
|
||||
setTime(System.currentTimeMillis());
|
||||
withTime(System.currentTimeMillis());
|
||||
}
|
||||
|
||||
public void setTime(long time) {
|
||||
@Nonnull
|
||||
public Builder withTime(long time) {
|
||||
Check.isTrue(!built);
|
||||
this.time = time;
|
||||
return this;
|
||||
}
|
||||
|
||||
public void setComment(@Nonnull String comment) {
|
||||
@Nonnull
|
||||
public Builder withComment(@Nullable String comment) {
|
||||
Check.isTrue(!built);
|
||||
this.comment = comment;
|
||||
this.comment = comment == null ? "" : comment;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
|
@ -1,65 +0,0 @@
|
||||
/*
|
||||
* Copyright 2013 serso aka se.solovyev
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
* Contact details
|
||||
*
|
||||
* Email: se.solovyev@gmail.com
|
||||
* Site: http://se.solovyev.org
|
||||
*/
|
||||
|
||||
package org.solovyev.android.calculator.history;
|
||||
|
||||
import org.simpleframework.xml.Element;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
import java.util.Date;
|
||||
|
||||
class OldBaseHistoryState implements Cloneable {
|
||||
|
||||
@Element
|
||||
private long time = new Date().getTime();
|
||||
|
||||
@Element(required = false)
|
||||
@Nullable
|
||||
private String comment;
|
||||
|
||||
public long getTime() {
|
||||
return time;
|
||||
}
|
||||
|
||||
public void setTime(long time) {
|
||||
this.time = time;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public String getComment() {
|
||||
return comment;
|
||||
}
|
||||
|
||||
public void setComment(@Nullable String comment) {
|
||||
this.comment = comment;
|
||||
}
|
||||
|
||||
@SuppressWarnings("CloneDoesntDeclareCloneNotSupportedException")
|
||||
@Override
|
||||
protected OldBaseHistoryState clone() {
|
||||
try {
|
||||
return (OldBaseHistoryState) super.clone();
|
||||
} catch (CloneNotSupportedException e) {
|
||||
throw new UnsupportedOperationException(e);
|
||||
}
|
||||
}
|
||||
}
|
@ -24,7 +24,6 @@ package org.solovyev.android.calculator.history;
|
||||
|
||||
import org.simpleframework.xml.Element;
|
||||
import org.simpleframework.xml.Root;
|
||||
import org.simpleframework.xml.Transient;
|
||||
import org.solovyev.android.calculator.jscl.JsclOperation;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
@ -32,25 +31,16 @@ import javax.annotation.Nonnull;
|
||||
@Root(name = "DisplayHistoryState")
|
||||
class OldDisplayHistoryState implements Cloneable {
|
||||
|
||||
@Transient
|
||||
private boolean valid = true;
|
||||
|
||||
@Element
|
||||
@Nonnull
|
||||
private OldEditorHistoryState editorState;
|
||||
|
||||
@Element
|
||||
@Nonnull
|
||||
private JsclOperation jsclOperation;
|
||||
|
||||
private OldDisplayHistoryState() {
|
||||
// for xml
|
||||
}
|
||||
|
||||
public boolean isValid() {
|
||||
return valid;
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
public OldEditorHistoryState getEditorState() {
|
||||
return editorState;
|
||||
@ -60,45 +50,4 @@ class OldDisplayHistoryState implements Cloneable {
|
||||
public JsclOperation getJsclOperation() {
|
||||
return jsclOperation;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
|
||||
OldDisplayHistoryState that = (OldDisplayHistoryState) o;
|
||||
|
||||
if (!editorState.equals(that.editorState)) return false;
|
||||
if (jsclOperation != that.jsclOperation) return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
int result = editorState.hashCode();
|
||||
result = 31 * result + jsclOperation.hashCode();
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "CalculatorDisplayHistoryState{" +
|
||||
"editorHistoryState=" + editorState +
|
||||
", jsclOperation=" + jsclOperation +
|
||||
'}';
|
||||
}
|
||||
|
||||
@Override
|
||||
protected OldDisplayHistoryState clone() {
|
||||
try {
|
||||
final OldDisplayHistoryState clone = (OldDisplayHistoryState) super.clone();
|
||||
|
||||
clone.editorState = this.editorState.clone();
|
||||
|
||||
return clone;
|
||||
} catch (CloneNotSupportedException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -61,41 +61,4 @@ class OldEditorHistoryState implements Cloneable {
|
||||
public int getCursorPosition() {
|
||||
return cursorPosition;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (!(o instanceof OldEditorHistoryState)) return false;
|
||||
|
||||
OldEditorHistoryState that = (OldEditorHistoryState) o;
|
||||
|
||||
if (cursorPosition != that.cursorPosition) return false;
|
||||
if (text != null ? !text.equals(that.text) : that.text != null) return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
int result = cursorPosition;
|
||||
result = 31 * result + (text != null ? text.hashCode() : 0);
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "EditorHistoryState{" +
|
||||
"cursorPosition=" + cursorPosition +
|
||||
", text='" + text + '\'' +
|
||||
'}';
|
||||
}
|
||||
|
||||
@Override
|
||||
protected OldEditorHistoryState clone() {
|
||||
try {
|
||||
return (OldEditorHistoryState) super.clone();
|
||||
} catch (CloneNotSupportedException e) {
|
||||
throw new UnsupportedOperationException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -27,12 +27,11 @@ import org.simpleframework.xml.Root;
|
||||
import org.simpleframework.xml.Serializer;
|
||||
import org.simpleframework.xml.core.Persister;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import javax.annotation.Nullable;
|
||||
import java.io.StringWriter;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
@Root(name = "History")
|
||||
class OldHistory {
|
||||
@ -57,36 +56,8 @@ class OldHistory {
|
||||
}
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
public String toXml() {
|
||||
final StringWriter xml = new StringWriter();
|
||||
final Serializer serializer = new Persister();
|
||||
try {
|
||||
serializer.write(this, xml);
|
||||
} catch (Exception e) {
|
||||
return "";
|
||||
}
|
||||
return xml.toString();
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
public List<OldHistoryState> getItems() {
|
||||
return items;
|
||||
}
|
||||
|
||||
public void add(@Nonnull OldHistoryState state) {
|
||||
items.add(state);
|
||||
}
|
||||
|
||||
public void addAll(@Nonnull Collection<OldHistoryState> states) {
|
||||
items.addAll(states);
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
items.clear();
|
||||
}
|
||||
|
||||
public void remove(@Nonnull OldHistoryState state) {
|
||||
items.remove(state);
|
||||
}
|
||||
}
|
||||
|
@ -25,23 +25,40 @@ package org.solovyev.android.calculator.history;
|
||||
import org.simpleframework.xml.Element;
|
||||
import org.simpleframework.xml.Root;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
@Root(name = "HistoryState")
|
||||
class OldHistoryState extends OldBaseHistoryState {
|
||||
class OldHistoryState {
|
||||
|
||||
@Element
|
||||
@Nonnull
|
||||
private OldEditorHistoryState editorState;
|
||||
|
||||
@Element
|
||||
@Nonnull
|
||||
private OldDisplayHistoryState displayState;
|
||||
|
||||
@Element
|
||||
private long time = new Date().getTime();
|
||||
|
||||
@Element(required = false)
|
||||
@Nullable
|
||||
private String comment;
|
||||
|
||||
private OldHistoryState() {
|
||||
// for xml
|
||||
}
|
||||
|
||||
public long getTime() {
|
||||
return time;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public String getComment() {
|
||||
return comment;
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
public OldEditorHistoryState getEditorState() {
|
||||
return editorState;
|
||||
@ -51,22 +68,4 @@ class OldHistoryState extends OldBaseHistoryState {
|
||||
public OldDisplayHistoryState getDisplayState() {
|
||||
return displayState;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "HistoryState{" +
|
||||
"editorState=" + editorState +
|
||||
", displayState=" + displayState +
|
||||
'}';
|
||||
}
|
||||
|
||||
@Override
|
||||
protected OldHistoryState clone() {
|
||||
final OldHistoryState that = (OldHistoryState) super.clone();
|
||||
|
||||
that.editorState = this.editorState.clone();
|
||||
that.displayState = this.displayState.clone();
|
||||
|
||||
return that;
|
||||
}
|
||||
}
|
||||
|
@ -24,7 +24,9 @@ package org.solovyev.android.calculator.history;
|
||||
|
||||
import android.content.SharedPreferences;
|
||||
import android.os.Handler;
|
||||
|
||||
import com.squareup.otto.Bus;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
@ -38,12 +40,14 @@ import org.solovyev.android.calculator.DisplayState;
|
||||
import org.solovyev.android.calculator.Editor;
|
||||
import org.solovyev.android.calculator.EditorState;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.Executor;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertNull;
|
||||
|
||||
@Config(constants = BuildConfig.class, sdk = CalculatorTestRunner.SUPPORTED_SDK)
|
||||
@RunWith(RobolectricGradleTestRunner.class)
|
||||
@ -124,9 +128,10 @@ public class HistoryTest {
|
||||
" <historyItems class=\"java.util.ArrayList\">\n" +
|
||||
" <calculatorHistoryState>\n" +
|
||||
" <time>100000000</time>\n" +
|
||||
" <comment>boom</comment>\n" +
|
||||
" <editorState>\n" +
|
||||
" <cursorPosition>3</cursorPosition>\n" +
|
||||
" <text>1+1</text>\n" +
|
||||
" <text>1+11</text>\n" +
|
||||
" </editorState>\n" +
|
||||
" <displayState>\n" +
|
||||
" <editorState>\n" +
|
||||
@ -165,7 +170,7 @@ public class HistoryTest {
|
||||
" </displayState>\n" +
|
||||
" </calculatorHistoryState>\n" +
|
||||
" <calculatorHistoryState>\n" +
|
||||
" <time>100000000</time>\n" +
|
||||
" <time>1</time>\n" +
|
||||
" <editorState>\n" +
|
||||
" <cursorPosition>0</cursorPosition>\n" +
|
||||
" <text>4+5/35sin(41)+dfdsfsdfs</text>\n" +
|
||||
@ -189,5 +194,33 @@ public class HistoryTest {
|
||||
|
||||
HistoryState state = states.get(0);
|
||||
assertEquals(100000000, state.time);
|
||||
assertEquals("", state.comment);
|
||||
assertEquals("1+1", state.editor.getTextString());
|
||||
assertEquals(3, state.editor.selection);
|
||||
assertEquals("Error", state.display.text);
|
||||
assertEquals(true, state.display.valid);
|
||||
assertNull(state.display.getResult());
|
||||
|
||||
states = History.convertOldHistory(oldXml2);
|
||||
assertNotNull(states);
|
||||
assertEquals(4, states.size());
|
||||
|
||||
state = states.get(0);
|
||||
assertEquals(100000000, state.time);
|
||||
assertEquals("boom", state.comment);
|
||||
assertEquals("1+11", state.editor.getTextString());
|
||||
assertEquals(3, state.editor.selection);
|
||||
assertEquals("Error", state.display.text);
|
||||
assertEquals(true, state.display.valid);
|
||||
assertNull(state.display.getResult());
|
||||
|
||||
state = states.get(3);
|
||||
assertEquals(1, state.time);
|
||||
assertEquals("", state.comment);
|
||||
assertEquals("4+5/35sin(41)+dfdsfsdfs", state.editor.getTextString());
|
||||
assertEquals(0, state.editor.selection);
|
||||
assertEquals("4+5/35sin(41)+dfdsfsdfs", state.display.text);
|
||||
assertEquals(true, state.display.valid);
|
||||
assertNull(state.display.getResult());
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user