tests added

This commit is contained in:
Sergey Solovyev 2013-06-28 16:58:20 +04:00
parent 58b64b1017
commit cd2f43bb43
4 changed files with 129 additions and 7 deletions

View File

@ -15,8 +15,8 @@ public final class CalculatorReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) { public void onReceive(Context context, Intent intent) {
final String action = intent.getAction(); final String action = intent.getAction();
if (CalculatorReceiver.ACTION_BUTTON_PRESSED.equals(action)) { if (ACTION_BUTTON_PRESSED.equals(action)) {
final int buttonId = intent.getIntExtra(CalculatorReceiver.ACTION_BUTTON_ID_EXTRA, 0); final int buttonId = intent.getIntExtra(ACTION_BUTTON_ID_EXTRA, 0);
final CalculatorButton button = CalculatorButton.getById(buttonId); final CalculatorButton button = CalculatorButton.getById(buttonId);
if (button != null) { if (button != null) {
@ -27,9 +27,9 @@ public final class CalculatorReceiver extends BroadcastReceiver {
@Nonnull @Nonnull
public static Intent newButtonClickedIntent(@Nonnull Context context, @Nonnull CalculatorButton button) { public static Intent newButtonClickedIntent(@Nonnull Context context, @Nonnull CalculatorButton button) {
final Intent onButtonClickIntent = new Intent(context, CalculatorReceiver.class); final Intent intent = new Intent(context, CalculatorReceiver.class);
onButtonClickIntent.setAction(ACTION_BUTTON_PRESSED); intent.setAction(ACTION_BUTTON_PRESSED);
onButtonClickIntent.putExtra(ACTION_BUTTON_ID_EXTRA, button.getButtonId()); intent.putExtra(ACTION_BUTTON_ID_EXTRA, button.getButtonId());
return onButtonClickIntent; return intent;
} }
} }

View File

@ -0,0 +1,71 @@
package org.solovyev.android.calculator;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import javax.annotation.Nonnull;
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 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.display_state_changed;
import static org.solovyev.android.calculator.CalculatorEventType.editor_state_changed;
import static org.solovyev.android.calculator.CalculatorEventType.editor_state_changed_light;
@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) {
}
}));
}
}

View File

@ -0,0 +1,47 @@
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.Robolectric;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.robolectric.Robolectric.application;
import static org.solovyev.android.calculator.CalculatorButton.four;
import static org.solovyev.android.calculator.CalculatorReceiver.ACTION_BUTTON_ID_EXTRA;
import static org.solovyev.android.calculator.CalculatorReceiver.ACTION_BUTTON_PRESSED;
import static org.solovyev.android.calculator.CalculatorReceiver.newButtonClickedIntent;
@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());
}
}

View File

@ -65,7 +65,7 @@ public class Locator implements CalculatorLocator {
private CalculatorClipboard calculatorClipboard = new DummyCalculatorClipboard(); private CalculatorClipboard calculatorClipboard = new DummyCalculatorClipboard();
@Nonnull @Nonnull
private static final CalculatorLocator instance = new Locator(); private static final Locator instance = new Locator();
@Nonnull @Nonnull
private CalculatorPreferenceService calculatorPreferenceService; private CalculatorPreferenceService calculatorPreferenceService;
@ -172,4 +172,8 @@ public class Locator implements CalculatorLocator {
public CalculatorPreferenceService getPreferenceService() { public CalculatorPreferenceService getPreferenceService() {
return this.calculatorPreferenceService; return this.calculatorPreferenceService;
} }
public static void setKeyboard(@Nonnull CalculatorKeyboard keyboard) {
instance.calculatorKeyboard = keyboard;
}
} }