numeral base/angle units button fix
This commit is contained in:
@@ -274,12 +274,12 @@ public class AndroidCalculatorEngine implements CalculatorEngine, SharedPreferen
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
public NumeralBase getNumeralBaseFromPrefs(@Nonnull SharedPreferences preferences) {
|
||||
public static NumeralBase getNumeralBaseFromPrefs(@Nonnull SharedPreferences preferences) {
|
||||
return Preferences.numeralBase.getPreference(preferences);
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
public AngleUnit getAngleUnitsFromPrefs(@Nonnull SharedPreferences preferences) {
|
||||
public static AngleUnit getAngleUnitsFromPrefs(@Nonnull SharedPreferences preferences) {
|
||||
return Preferences.angleUnit.getPreference(preferences);
|
||||
}
|
||||
|
||||
|
@@ -27,6 +27,7 @@ import android.content.res.Resources;
|
||||
import android.graphics.Paint;
|
||||
import android.text.TextPaint;
|
||||
import android.util.AttributeSet;
|
||||
import jscl.AngleUnit;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
|
||||
@@ -41,8 +42,12 @@ import org.solovyev.android.view.drag.DirectionDragButton;
|
||||
*/
|
||||
public class AngleUnitsButton extends DirectionDragButton {
|
||||
|
||||
@Nonnull
|
||||
private AngleUnit angleUnit;
|
||||
|
||||
public AngleUnitsButton(Context context, @Nonnull AttributeSet attrs) {
|
||||
super(context, attrs);
|
||||
this.angleUnit = Locator.getInstance().getEngine().getAngleUnits();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -52,11 +57,32 @@ public class AngleUnitsButton extends DirectionDragButton {
|
||||
super.initDirectionTextPaint(basePaint, directionTextData, resources);
|
||||
|
||||
final TextPaint directionTextPaint = directionTextData.getPaint();
|
||||
if (Locator.getInstance().getEngine().getAngleUnits().name().equals(directionTextData.getText())) {
|
||||
directionTextPaint.setColor(resources.getColor(R.color.cpp_selected_angle_unit_text_color));
|
||||
} else {
|
||||
directionTextPaint.setColor(resources.getColor(R.color.cpp_default_text_color));
|
||||
final int color = getDirectionTextColor(directionTextData.getText());
|
||||
directionTextPaint.setColor(color);
|
||||
if (!isCurrentAngleUnits(directionTextData.getText())) {
|
||||
directionTextPaint.setAlpha(getDirectionTextAlpha());
|
||||
}
|
||||
}
|
||||
|
||||
int getDirectionTextColor(@Nonnull String directionText) {
|
||||
final int color;
|
||||
final Resources resources = getResources();
|
||||
if (isCurrentAngleUnits(directionText)) {
|
||||
color = resources.getColor(R.color.cpp_selected_angle_unit_text_color);
|
||||
} else {
|
||||
color = resources.getColor(R.color.cpp_default_text_color);
|
||||
}
|
||||
return color;
|
||||
}
|
||||
|
||||
boolean isCurrentAngleUnits(@Nonnull String directionText) {
|
||||
return this.angleUnit.name().equals(directionText);
|
||||
}
|
||||
|
||||
public void setAngleUnit(@Nonnull AngleUnit angleUnit) {
|
||||
if (this.angleUnit != angleUnit) {
|
||||
this.angleUnit = angleUnit;
|
||||
invalidate();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -27,6 +27,7 @@ import android.content.res.Resources;
|
||||
import android.graphics.Paint;
|
||||
import android.text.TextPaint;
|
||||
import android.util.AttributeSet;
|
||||
import jscl.NumeralBase;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
|
||||
@@ -41,8 +42,12 @@ import org.solovyev.android.view.drag.DirectionDragButton;
|
||||
*/
|
||||
public class NumeralBasesButton extends DirectionDragButton {
|
||||
|
||||
@Nonnull
|
||||
private NumeralBase numeralBase;
|
||||
|
||||
public NumeralBasesButton(Context context, @Nonnull AttributeSet attrs) {
|
||||
super(context, attrs);
|
||||
this.numeralBase = Locator.getInstance().getEngine().getNumeralBase();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -52,11 +57,33 @@ public class NumeralBasesButton extends DirectionDragButton {
|
||||
super.initDirectionTextPaint(basePaint, directionTextData, resources);
|
||||
|
||||
final TextPaint directionTextPaint = directionTextData.getPaint();
|
||||
if (Locator.getInstance().getEngine().getNumeralBase().name().equals(directionTextData.getText())) {
|
||||
directionTextPaint.setColor(resources.getColor(R.color.cpp_selected_angle_unit_text_color));
|
||||
} else {
|
||||
directionTextPaint.setColor(resources.getColor(R.color.cpp_default_text_color));
|
||||
|
||||
final int color = getDirectionTextColor(directionTextData.getText());
|
||||
directionTextPaint.setColor(color);
|
||||
|
||||
if (!isCurrentNumberBase(directionTextData.getText())) {
|
||||
directionTextPaint.setAlpha(getDirectionTextAlpha());
|
||||
}
|
||||
}
|
||||
|
||||
int getDirectionTextColor(@Nonnull String directionText) {
|
||||
final int color;
|
||||
if (isCurrentNumberBase(directionText)) {
|
||||
color = getResources().getColor(R.color.cpp_selected_angle_unit_text_color);
|
||||
} else {
|
||||
color = getResources().getColor(R.color.cpp_default_text_color);
|
||||
}
|
||||
return color;
|
||||
}
|
||||
|
||||
boolean isCurrentNumberBase(@Nonnull String directionText) {
|
||||
return this.numeralBase.name().equals(directionText);
|
||||
}
|
||||
|
||||
public void setNumeralBase(@Nonnull NumeralBase numeralBase) {
|
||||
if (this.numeralBase != numeralBase) {
|
||||
this.numeralBase = numeralBase;
|
||||
invalidate();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -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 javax.annotation.Nonnull;
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
import org.mockito.Mockito;
|
||||
import org.solovyev.android.calculator.history.CalculatorHistory;
|
||||
import org.solovyev.android.calculator.plot.CalculatorPlotter;
|
||||
|
||||
/**
|
||||
* 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,75 @@
|
||||
package org.solovyev.android.calculator.view;
|
||||
|
||||
import android.app.Activity;
|
||||
import jscl.AngleUnit;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mockito;
|
||||
import org.robolectric.Robolectric;
|
||||
import org.robolectric.RobolectricTestRunner;
|
||||
import org.robolectric.res.Attribute;
|
||||
import org.robolectric.shadows.ShadowActivity;
|
||||
|
||||
import static jscl.AngleUnit.deg;
|
||||
import static jscl.AngleUnit.grad;
|
||||
import static jscl.AngleUnit.rad;
|
||||
import static jscl.AngleUnit.turns;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNotSame;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.solovyev.android.calculator.CalculatorTestUtils.staticSetUp;
|
||||
|
||||
@RunWith(RobolectricTestRunner.class)
|
||||
public class AngleUnitsButtonTest {
|
||||
|
||||
private AngleUnitsButton button;
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
staticSetUp();
|
||||
|
||||
final Activity context = Robolectric.buildActivity(Activity.class).create().get();
|
||||
final ShadowActivity activity = Robolectric.shadowOf(context);
|
||||
button = new AngleUnitsButton(context, activity.createAttributeSet(new ArrayList<Attribute>(), AngleUnitsButton.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testShouldReturnDifferentColorsForDifferentAngleUnits() throws Exception {
|
||||
button.setAngleUnit(deg);
|
||||
|
||||
assertEquals(button.getDirectionTextColor(deg.name()), button.getDirectionTextColor(deg.name()));
|
||||
assertEquals(button.getDirectionTextColor(grad.name()), button.getDirectionTextColor(rad.name()));
|
||||
assertNotSame(button.getDirectionTextColor(deg.name()), button.getDirectionTextColor(rad.name()));
|
||||
assertNotSame(button.getDirectionTextColor(deg.name()), button.getDirectionTextColor(grad.name()));
|
||||
assertNotSame(button.getDirectionTextColor(deg.name()), button.getDirectionTextColor(turns.name()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIsCurrentAngleUnits() throws Exception {
|
||||
button.setAngleUnit(rad);
|
||||
assertTrue(button.isCurrentAngleUnits(rad.name()));
|
||||
assertFalse(button.isCurrentAngleUnits(deg.name()));
|
||||
assertFalse(button.isCurrentAngleUnits(grad.name()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInvalidateShouldBeCalledOnlyWhenChangeIsDone() throws Exception {
|
||||
button.setAngleUnit(rad);
|
||||
|
||||
button = Mockito.spy(button);
|
||||
|
||||
button.setAngleUnit(deg);
|
||||
verify(button, times(1)).invalidate();
|
||||
|
||||
button.setAngleUnit(deg);
|
||||
verify(button, times(1)).invalidate();
|
||||
}
|
||||
}
|
@@ -0,0 +1,76 @@
|
||||
package org.solovyev.android.calculator.view;
|
||||
|
||||
import android.app.Activity;
|
||||
import jscl.NumeralBase;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mockito;
|
||||
import org.robolectric.Robolectric;
|
||||
import org.robolectric.RobolectricTestRunner;
|
||||
import org.robolectric.res.Attribute;
|
||||
import org.robolectric.shadows.ShadowActivity;
|
||||
|
||||
import static jscl.AngleUnit.deg;
|
||||
import static jscl.AngleUnit.rad;
|
||||
import static jscl.NumeralBase.bin;
|
||||
import static jscl.NumeralBase.dec;
|
||||
import static jscl.NumeralBase.hex;
|
||||
import static jscl.NumeralBase.oct;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNotSame;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.solovyev.android.calculator.CalculatorTestUtils.staticSetUp;
|
||||
|
||||
@RunWith(RobolectricTestRunner.class)
|
||||
public class NumeralBasesButtonTest {
|
||||
|
||||
private NumeralBasesButton button;
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
staticSetUp();
|
||||
|
||||
final Activity context = Robolectric.buildActivity(Activity.class).create().get();
|
||||
final ShadowActivity activity = Robolectric.shadowOf(context);
|
||||
button = new NumeralBasesButton(context, activity.createAttributeSet(new ArrayList<Attribute>(), NumeralBasesButton.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testShouldReturnDifferentColorsForDifferentNumeralBase() throws Exception {
|
||||
button.setNumeralBase(dec);
|
||||
|
||||
assertEquals(button.getDirectionTextColor(dec.name()), button.getDirectionTextColor(dec.name()));
|
||||
assertEquals(button.getDirectionTextColor(hex.name()), button.getDirectionTextColor(bin.name()));
|
||||
assertNotSame(button.getDirectionTextColor(dec.name()), button.getDirectionTextColor(bin.name()));
|
||||
assertNotSame(button.getDirectionTextColor(dec.name()), button.getDirectionTextColor(hex.name()));
|
||||
assertNotSame(button.getDirectionTextColor(dec.name()), button.getDirectionTextColor(oct.name()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIsCurrentNumeralBase() throws Exception {
|
||||
button.setNumeralBase(dec);
|
||||
assertTrue(button.isCurrentNumberBase(dec.name()));
|
||||
assertFalse(button.isCurrentNumberBase(hex.name()));
|
||||
assertFalse(button.isCurrentNumberBase(bin.name()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInvalidateShouldBeCalledOnlyWhenChangeIsDone() throws Exception {
|
||||
button.setNumeralBase(dec);
|
||||
|
||||
button = Mockito.spy(button);
|
||||
|
||||
button.setNumeralBase(hex);
|
||||
verify(button, times(1)).invalidate();
|
||||
|
||||
button.setNumeralBase(hex);
|
||||
verify(button, times(1)).invalidate();
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user