Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
tbressler committed Jan 22, 2021
2 parents 8e07917 + 0bad0de commit 048f457
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
*/
public class TestDataMemoryMessage {


// Constructors:

@Test(expected = IllegalArgumentException.class)
public void new_withLocationLowerThan0_throwsIAE() {
new DataMemoryMessage(-1, 1);
Expand All @@ -33,6 +36,28 @@ public void new_withValue1GreaterThan255_throwsIAE() {
new DataMemoryMessage(1, 256);
}

@Test(expected = IllegalArgumentException.class)
public void new_withValue2LowerThan0_throwsIAE() {
new DataMemoryMessage(1, -1, 0);
}

@Test(expected = IllegalArgumentException.class)
public void new_withValue2GreaterThan255_throwsIAE() {
new DataMemoryMessage(1, 256, 0);
}

@Test(expected = IllegalArgumentException.class)
public void new_withValue3LowerThan0_throwsIAE() {
new DataMemoryMessage(1, -1, 0, 0);
}

@Test(expected = IllegalArgumentException.class)
public void new_withValue3GreaterThan255_throwsIAE() {
new DataMemoryMessage(1, 256, 0, 0);
}


// getLocations:

@Test
public void getLocation_whenConstructedWith100_returns100() {
Expand All @@ -53,6 +78,8 @@ public void getLocation_whenConstructedWith4095_returns4095() {
}


// getValue1:

@Test
public void getValue1_whenConstructedWith100_returns100() {
DataMemoryMessage msg = new DataMemoryMessage(1, 100);
Expand All @@ -71,9 +98,68 @@ public void getValue1_whenConstructedWith255_returns255() {
assertEquals(255, msg.getValue1());
}

@Test
public void getValue1AsACH_whenConstructedWith255_returns91() {
DataMemoryMessage msg = new DataMemoryMessage(1, 145);
assertEquals("91", msg.getValue1AsACH());
}


// getValue2:

@Test
public void getValue2_whenConstructedWith100_returns100() {
DataMemoryMessage msg = new DataMemoryMessage(1, 200, 100);
assertEquals(200, msg.getValue2());
}

@Test
public void getValue2_whenConstructedWith0_returns0() {
DataMemoryMessage msg = new DataMemoryMessage(1, 50, 0);
assertEquals(50, msg.getValue2());
}

@Test
public void getValue2_whenConstructedWith255_returns255() {
DataMemoryMessage msg = new DataMemoryMessage(1, 255, 1);
assertEquals(255, msg.getValue2());
}

@Test
public void getValue2AsACH_whenConstructedWith255_returns91() {
DataMemoryMessage msg = new DataMemoryMessage(1, 145, 0);
assertEquals("91", msg.getValue2AsACH());
}


// getValue3:

@Test
public void getValue3_whenConstructedWith50_returns50() {
DataMemoryMessage msg = new DataMemoryMessage(1, 50, 0, 0);
assertEquals(50, msg.getValue3());
}

@Test
public void getValue3_whenConstructedWith100_returns100() {
DataMemoryMessage msg = new DataMemoryMessage(1, 100, 0, 0);
assertEquals(100, msg.getValue3());
}

@Test
public void getValue3_whenConstructedWith255_returns255() {
DataMemoryMessage msg = new DataMemoryMessage(1, 255, 0, 0);
assertEquals(255, msg.getValue3());
}

@Test
public void getValue3AsACH_whenConstructedWith255_returns91() {
DataMemoryMessage msg = new DataMemoryMessage(1, 145, 0, 0);
assertEquals("91", msg.getValue3AsACH());
}

// TODO Test other constructors!

// toString:

@Test
public void toString_returnsObjectInfo() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
import de.tbressler.waterrower.io.msg.AbstractMessage;
import de.tbressler.waterrower.io.msg.in.DataMemoryMessage;
import de.tbressler.waterrower.io.msg.out.ReadMemoryMessage;
import de.tbressler.waterrower.io.msg.out.StartCommunicationMessage;
import de.tbressler.waterrower.model.WorkoutFlags;
import org.junit.Test;
import org.mockito.ArgumentMatcher;

import static de.tbressler.waterrower.io.msg.Memory.SINGLE_MEMORY;
import static de.tbressler.waterrower.model.MemoryLocation.FEXTENDED;
Expand Down Expand Up @@ -45,12 +47,11 @@ public void poll_returnsMessageWithSingleMemoryAndFEXTENDED() {
public void handle_withFEXTENDEDAnd0x01_notifiesOnWorkoutModeUpdated() {
subscription = newWorkoutModeSubscription();

DataMemoryMessage msg = new DataMemoryMessage(FEXTENDED.getLocation(), 0x01);
DataMemoryMessage msg = new DataMemoryMessage(FEXTENDED.getLocation(), 0xB6);

subscription.handle((AbstractMessage) msg);

// TODO Matcher erstellen!
verify(internalSubscription, times(1)).onWorkoutFlagsUpdated(any(WorkoutFlags.class));
verify(internalSubscription, times(1)).onWorkoutFlagsUpdated(argThat(matchesFlags(false, true, true, false, true, true, false, true)));
}

@Test
Expand All @@ -63,20 +64,28 @@ public void handle_twoTimesWithSameMessages_onlyNotifiesOneTime() {
subscription.handle((AbstractMessage) msg1);
subscription.handle((AbstractMessage) msg2);

verify(internalSubscription, times(1)).onWorkoutFlagsUpdated(any(WorkoutFlags.class));
verify(internalSubscription, times(1)).onWorkoutFlagsUpdated(argThat(matchesFlags(true, false, false, false, false, false, false, false)));
}

@Test
public void handle_twoTimesWithNotSameMessages_notifiesTwoTime() {
subscription = newWorkoutModeSubscription();

DataMemoryMessage msg1 = new DataMemoryMessage(FEXTENDED.getLocation(), 0x01);
DataMemoryMessage msg2 = new DataMemoryMessage(FEXTENDED.getLocation(), 0x10);
DataMemoryMessage msg2 = new DataMemoryMessage(FEXTENDED.getLocation(), 0x80);

subscription.handle((AbstractMessage) msg1);
subscription.handle((AbstractMessage) msg2);

verify(internalSubscription, times(2)).onWorkoutFlagsUpdated(any(WorkoutFlags.class));
verify(internalSubscription, times(1)).onWorkoutFlagsUpdated(argThat(matchesFlags(true, false, false, false, false, false, false, false)));
verify(internalSubscription, times(1)).onWorkoutFlagsUpdated(argThat(matchesFlags(false, false, false, false, false, false, false, true)));
}

@Test
public void handle_withOtherMessage_doesntNotifyOnClockCountDownUpdated() {
subscription = newWorkoutModeSubscription();
subscription.handle(new StartCommunicationMessage());
verify(internalSubscription, never()).onWorkoutFlagsUpdated(any(WorkoutFlags.class));
}


Expand All @@ -92,4 +101,24 @@ protected void onWorkoutFlagsUpdated(WorkoutFlags flags) {
};
}


private ArgumentMatcher<WorkoutFlags> matchesFlags(boolean heartRateZone, boolean intensityZone, boolean strokeRateZone, boolean prognosticsActive, boolean workoutDistanceMode, boolean workoutDurationMode, boolean workoutDistanceIntervalMode, boolean workoutDurationIntervalMode) {
return new ArgumentMatcher<>() {
@Override
public boolean matches(Object argument) {
if (!(argument instanceof WorkoutFlags))
return false;
WorkoutFlags flags = (WorkoutFlags) argument;
return (flags.isWorkingInHeartRateZone() == heartRateZone)
&& (flags.isWorkingInIntensityZone() == intensityZone)
&& (flags.isWorkingInStrokeRateZone() == strokeRateZone)
&& (flags.isPrognosticsActive() == prognosticsActive)
&& (flags.isWorkoutDistanceMode() == workoutDistanceMode)
&& (flags.isWorkoutDurationMode() == workoutDurationMode)
&& (flags.isWorkoutDistanceIntervalMode() == workoutDistanceIntervalMode)
&& (flags.isWorkoutDurationIntervalMode() == workoutDurationIntervalMode);
}
};
}

}

0 comments on commit 048f457

Please sign in to comment.