Tests added

This commit is contained in:
serso
2016-01-14 12:36:52 +01:00
parent 5efa9cd3a0
commit e015a12718
9 changed files with 195 additions and 30 deletions

View File

@@ -33,21 +33,27 @@ import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;
import org.solovyev.android.Views;
import org.solovyev.android.calculator.history.History;
import org.solovyev.android.calculator.history.HistoryDragProcessor;
import org.solovyev.android.calculator.view.AngleUnitsButton;
import org.solovyev.android.calculator.view.LongClickEraser;
import org.solovyev.android.calculator.view.NumeralBasesButton;
import org.solovyev.android.calculator.view.ViewsCache;
import org.solovyev.android.views.dragbutton.*;
import org.solovyev.android.views.dragbutton.DirectionDragButton;
import org.solovyev.android.views.dragbutton.DragButton;
import org.solovyev.android.views.dragbutton.DragDirection;
import org.solovyev.android.views.dragbutton.DragListener;
import org.solovyev.android.views.dragbutton.SimpleDragListener;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import javax.inject.Inject;
import java.util.ArrayList;
import java.util.List;
import static org.solovyev.android.calculator.Preferences.Gui.Layout.simple;
import static org.solovyev.android.calculator.Preferences.Gui.Layout.simple_mobile;
import static org.solovyev.android.calculator.model.AndroidCalculatorEngine.Preferences.angleUnit;
@@ -118,6 +124,9 @@ public abstract class BaseUi implements SharedPreferences.OnSharedPreferenceChan
@Inject
Editor editor;
@Inject
History history;
protected void onCreate(@Nonnull Activity activity) {
((CalculatorApplication) activity.getApplication()).getComponent().inject(this);
@@ -166,7 +175,7 @@ public abstract class BaseUi implements SharedPreferences.OnSharedPreferenceChan
final ViewsCache views = ViewsCache.forView(root);
setOnDragListeners(views, activity);
HistoryDragProcessor historyDragProcessor = new HistoryDragProcessor();
HistoryDragProcessor historyDragProcessor = new HistoryDragProcessor(history);
final DragListener historyDragListener = newDragListener(historyDragProcessor, activity);
final DragButton historyButton = getButton(views, R.id.cpp_button_history);
if (historyButton != null) {

View File

@@ -1,5 +1,6 @@
package org.solovyev.android.calculator;
import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
@@ -13,8 +14,10 @@ import java.util.Map;
import javax.annotation.Nonnull;
import javax.inject.Inject;
import javax.inject.Singleton;
public final class CalculatorBroadcaster implements SharedPreferences.OnSharedPreferenceChangeListener {
@Singleton
public class Broadcaster implements SharedPreferences.OnSharedPreferenceChangeListener {
public static final String ACTION_INIT = "org.solovyev.android.calculator.INIT";
public static final String ACTION_EDITOR_STATE_CHANGED = "org.solovyev.android.calculator.EDITOR_STATE_CHANGED";
@@ -26,8 +29,8 @@ public final class CalculatorBroadcaster implements SharedPreferences.OnSharedPr
private final Intents intents = new Intents();
@Inject
public CalculatorBroadcaster(@Nonnull Context context, @Nonnull SharedPreferences preferences, @Nonnull Bus bus, @Nonnull Handler handler) {
this.context = context;
public Broadcaster(@Nonnull Application application, @Nonnull SharedPreferences preferences, @Nonnull Bus bus, @Nonnull Handler handler) {
this.context = application;
preferences.registerOnSharedPreferenceChangeListener(this);
bus.register(this);
handler.postDelayed(new Runnable() {

View File

@@ -34,6 +34,7 @@ import org.acra.ACRA;
import org.acra.ACRAConfiguration;
import org.acra.sender.HttpSender;
import org.solovyev.android.Android;
import org.solovyev.android.calculator.history.History;
import org.solovyev.android.calculator.language.Language;
import org.solovyev.android.calculator.language.Languages;
import org.solovyev.android.calculator.model.AndroidCalculatorEngine;
@@ -84,6 +85,12 @@ public class CalculatorApplication extends android.app.Application implements Sh
@Named(AppModule.THREAD_UI)
Executor uiThread;
@Inject
History history;
@Inject
Broadcaster broadcaster;
@Override
public void onCreate() {
final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);

View File

@@ -57,14 +57,17 @@ import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Singleton;
import static android.text.TextUtils.isEmpty;
@Singleton
public class History {
public static final String TAG = App.subTag("History");
private static final ChangedEvent CHANGED_EVENT_RECENT = new ChangedEvent(true);
private static final ChangedEvent CHANGED_EVENT_SAVED = new ChangedEvent(false);
private static final int MAX_INTERMEDIATE_STREAK = 5;
@NonNull
private final Runnable writeRecent = new WriteTask(true);
@NonNull
@@ -74,6 +77,8 @@ public class History {
@Nonnull
private final List<HistoryState> saved = new ArrayList<>();
@Nonnull
private final Application application;
@Nonnull
private final Bus bus;
@Inject
Handler handler;
@@ -81,12 +86,10 @@ public class History {
SharedPreferences preferences;
@Inject
Editor editor;
@Inject
Application application;
private boolean initialized;
@Inject
public History(@NonNull Bus bus, @Nonnull @Named(AppModule.THREAD_INIT) Executor initThread) {
public History(@NonNull Application application, @NonNull Bus bus, @Nonnull @Named(AppModule.THREAD_INIT) Executor initThread) {
this.application = application;
this.bus = bus;
this.bus.register(this);
initThread.execute(new Runnable() {
@@ -117,7 +120,7 @@ public class History {
}
@Nonnull
private static List<HistoryState> loadStates(@Nonnull File file) {
static List<HistoryState> loadStates(@Nonnull File file) {
if (!file.exists()) {
return Collections.emptyList();
}
@@ -219,7 +222,6 @@ public class History {
Check.isTrue(saved.isEmpty());
recent.addAll(recentStates);
saved.addAll(savedStates);
initialized = true;
}
});
}
@@ -258,13 +260,17 @@ public class History {
final List<HistoryState> states = recent.asList();
final int statesCount = states.size();
int streak = 0;
for (int i = 1; i < statesCount; i++) {
final HistoryState olderState = states.get(i - 1);
final HistoryState newerState = states.get(i);
final String olderText = olderState.editor.getTextString();
final String newerText = newerState.editor.getTextString();
if (!isIntermediate(olderText, newerText, groupingSeparator)) {
if (streak >= MAX_INTERMEDIATE_STREAK || !isIntermediate(olderText, newerText, groupingSeparator)) {
result.add(0, olderState);
streak = 0;
} else {
streak++;
}
}
if (statesCount > 0) {
@@ -330,9 +336,6 @@ public class History {
@Subscribe
public void onDisplayChanged(@Nonnull Display.ChangedEvent e) {
if (!initialized) {
return;
}
final EditorState editorState = editor.getState();
final DisplayState displayState = e.newState;
if (editorState.sequence != displayState.sequence) {

View File

@@ -30,12 +30,15 @@ import org.solovyev.android.views.dragbutton.DragDirection;
import org.solovyev.android.views.dragbutton.SimpleDragListener;
import javax.annotation.Nonnull;
import javax.inject.Inject;
public class HistoryDragProcessor implements SimpleDragListener.DragProcessor {
@Inject
History history;
@Nonnull
private final History history;
public HistoryDragProcessor(@Nonnull History history) {
this.history = history;
}
@Override
public boolean processDragEvent(@Nonnull DragDirection direction, @Nonnull DragButton button, @Nonnull PointF startPoint, @Nonnull MotionEvent motionEvent) {

View File

@@ -16,7 +16,7 @@ import java.util.List;
public class RecentHistory {
private static final int MAX_HISTORY = 20;
private static final int MAX_HISTORY = 40;
@NonNull
private final List<HistoryState> list = new LinkedList<>();

View File

@@ -59,10 +59,10 @@ import javax.annotation.Nullable;
import static android.appwidget.AppWidgetManager.OPTION_APPWIDGET_MIN_HEIGHT;
import static android.content.Intent.ACTION_CONFIGURATION_CHANGED;
import static android.os.Build.VERSION_CODES.JELLY_BEAN;
import static org.solovyev.android.calculator.CalculatorBroadcaster.ACTION_DISPLAY_STATE_CHANGED;
import static org.solovyev.android.calculator.CalculatorBroadcaster.ACTION_EDITOR_STATE_CHANGED;
import static org.solovyev.android.calculator.CalculatorBroadcaster.ACTION_INIT;
import static org.solovyev.android.calculator.CalculatorBroadcaster.ACTION_THEME_CHANGED;
import static org.solovyev.android.calculator.Broadcaster.ACTION_DISPLAY_STATE_CHANGED;
import static org.solovyev.android.calculator.Broadcaster.ACTION_EDITOR_STATE_CHANGED;
import static org.solovyev.android.calculator.Broadcaster.ACTION_INIT;
import static org.solovyev.android.calculator.Broadcaster.ACTION_THEME_CHANGED;
import static org.solovyev.android.calculator.CalculatorReceiver.newButtonClickedIntent;
public class CalculatorWidget extends AppWidgetProvider {