Project restructure
We don't need to separate widget/onscreen from the main module, let's merge them together.
This commit is contained in:
@@ -0,0 +1,68 @@
|
||||
package org.solovyev.android.calculator;
|
||||
|
||||
import android.content.BroadcastReceiver;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.IntentFilter;
|
||||
import org.hamcrest.BaseMatcher;
|
||||
import org.hamcrest.Description;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mockito;
|
||||
import org.robolectric.RobolectricTestRunner;
|
||||
import org.robolectric.annotation.Config;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
|
||||
import static org.mockito.Matchers.argThat;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.robolectric.Robolectric.application;
|
||||
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.CalculatorEventType.*;
|
||||
|
||||
@Config(manifest = Config.NONE)
|
||||
@RunWith(RobolectricTestRunner.class)
|
||||
public class CalculatorBroadcasterTest {
|
||||
|
||||
@Nonnull
|
||||
private CalculatorBroadcaster broadcaster;
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
broadcaster = new CalculatorBroadcaster(application);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testShouldSendEditorStateChangedIntent() throws Exception {
|
||||
assertIntentSent(editor_state_changed, ACTION_EDITOR_STATE_CHANGED);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testShouldSendEditorStateChangedLiteIntent() throws Exception {
|
||||
assertIntentSent(editor_state_changed_light, ACTION_EDITOR_STATE_CHANGED);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testShouldSendDisplayStateChangedIntent() throws Exception {
|
||||
assertIntentSent(display_state_changed, ACTION_DISPLAY_STATE_CHANGED);
|
||||
}
|
||||
|
||||
private void assertIntentSent(@Nonnull CalculatorEventType eventType, final String expectedAction) {
|
||||
final BroadcastReceiver receiver = Mockito.mock(BroadcastReceiver.class);
|
||||
application.registerReceiver(receiver, new IntentFilter(expectedAction));
|
||||
broadcaster.onCalculatorEvent(CalculatorEventDataImpl.newInstance(1L, 1L), eventType, null);
|
||||
verify(receiver, times(1)).onReceive(Mockito.<Context>any(), argThat(new BaseMatcher<Intent>() {
|
||||
@Override
|
||||
public boolean matches(Object o) {
|
||||
return ((Intent) o).getAction().equals(expectedAction);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void describeTo(Description description) {
|
||||
}
|
||||
}));
|
||||
}
|
||||
}
|
@@ -0,0 +1,41 @@
|
||||
package org.solovyev.android.calculator;
|
||||
|
||||
import android.content.Intent;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mockito;
|
||||
import org.robolectric.RobolectricTestRunner;
|
||||
import org.robolectric.annotation.Config;
|
||||
|
||||
import static org.mockito.Mockito.*;
|
||||
import static org.robolectric.Robolectric.application;
|
||||
import static org.solovyev.android.calculator.CalculatorButton.four;
|
||||
import static org.solovyev.android.calculator.CalculatorReceiver.*;
|
||||
|
||||
@Config(manifest = Config.NONE)
|
||||
@RunWith(RobolectricTestRunner.class)
|
||||
public class CalculatorReceiverTest {
|
||||
|
||||
@Test
|
||||
public void testShouldPressButtonOnIntent() throws Exception {
|
||||
Locator.setKeyboard(mock(CalculatorKeyboard.class));
|
||||
|
||||
final Intent intent = newButtonClickedIntent(application, four);
|
||||
new CalculatorReceiver().onReceive(application, intent);
|
||||
|
||||
verify(Locator.getInstance().getKeyboard(), times(1)).buttonPressed(Mockito.anyString());
|
||||
verify(Locator.getInstance().getKeyboard(), times(1)).buttonPressed("4");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testShouldDoNothingIfButtonInvalid() throws Exception {
|
||||
Locator.setKeyboard(mock(CalculatorKeyboard.class));
|
||||
|
||||
final Intent intent = new Intent(application, CalculatorReceiver.class);
|
||||
intent.setAction(ACTION_BUTTON_PRESSED);
|
||||
intent.putExtra(ACTION_BUTTON_ID_EXTRA, "test!@");
|
||||
new CalculatorReceiver().onReceive(application, intent);
|
||||
|
||||
verify(Locator.getInstance().getKeyboard(), times(0)).buttonPressed(Mockito.anyString());
|
||||
}
|
||||
}
|
@@ -0,0 +1,66 @@
|
||||
package org.solovyev.android.calculator.view;
|
||||
|
||||
import android.app.Activity;
|
||||
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 java.util.ArrayList;
|
||||
|
||||
import static jscl.AngleUnit.*;
|
||||
import static org.junit.Assert.*;
|
||||
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,66 @@
|
||||
package org.solovyev.android.calculator.view;
|
||||
|
||||
import android.app.Activity;
|
||||
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 java.util.ArrayList;
|
||||
|
||||
import static jscl.NumeralBase.*;
|
||||
import static org.junit.Assert.*;
|
||||
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