private void onSendDiagnosticRequest(Spinner busSpinner, EditText idView, EditText modeView, EditText pidView) { boolean validInput = true; if(idView.getText().toString().isEmpty()) { idView.setError("ID is required"); validInput = false; } if(modeView.getText().toString().isEmpty()) { modeView.setError("Mode is required"); validInput = false; } if(validInput) { DiagnosticRequest request = new DiagnosticRequest( Integer.valueOf(busSpinner.getSelectedItem().toString()), Integer.valueOf(idView.getText().toString(), 16), Integer.valueOf(modeView.getText().toString(), 16)); // Make sure to update after sending so the timestamp is set by the // VehicleManager String pidString = pidView.getText().toString(); if(!pidString.isEmpty()) { request.setPid(Integer.valueOf(pidString, 16)); } mVehicleManager.send(request); // Make sure to update after sending so the timestamp is set by the // VehicleManager updateLastRequestView(request); } else { Log.i(TAG, "Form is invalid, not sending diagnostic request"); } }
@Test public void differentDiagnosticRequestActionNotEqual() { message = new Command(request, action); Command anotherMessage = new Command(new DiagnosticRequest(5, 6, 7, 8), "another"); assertThat(message, not(equalTo(anotherMessage))); }
@Test public void differentDiagnosticRequestNotEqual() { message = new Command(request, action); Command anotherMessage = new Command(new DiagnosticRequest(5, 6, 7, 8), action); assertThat(message, not(equalTo(anotherMessage))); }
@Test public void differentFrequencyNotEqual() { DiagnosticRequest anotherRequest = new DiagnosticRequest( bus, id, mode); anotherRequest.setFrequency(2.0); assertThat(request, not(equalTo(anotherRequest))); }
@Test public void differentIdNotEqual() { DiagnosticRequest anotherRequest = new DiagnosticRequest( bus, id + 1, mode); anotherRequest.setPayload(payload); assertThat(request, not(equalTo(anotherRequest))); }
@Test public void differentModeNotEqual() { DiagnosticRequest anotherRequest = new DiagnosticRequest( bus, id, mode + 1); anotherRequest.setPayload(payload); assertThat(request, not(equalTo(anotherRequest))); }
@Test public void differentPayloadNotEqual() { payload[1] = (byte) (payload[1] + 1); DiagnosticRequest anotherRequest = new DiagnosticRequest( bus, id, mode); anotherRequest.setPayload(payload); assertThat(request, not(equalTo(anotherRequest))); }
@Test public void differentMultipleResponsesNotEqual() { DiagnosticRequest anotherRequest = new DiagnosticRequest( bus, id, mode); anotherRequest.setMultipleResponses(true); assertThat(request, not(equalTo(anotherRequest))); }
@Test public void differentBusNotEqual() { DiagnosticRequest anotherRequest = new DiagnosticRequest( bus + 1, id, mode); anotherRequest.setPayload(payload); assertThat(request, not(equalTo(anotherRequest))); }
@Test public void differentNameNotEqual() { DiagnosticRequest anotherRequest = new DiagnosticRequest( bus, id, mode); anotherRequest.setName("foo"); assertThat(request, not(equalTo(anotherRequest))); }
@Test public void defaultsNotIncluded() { DiagnosticRequest request = new DiagnosticRequest(1, 2, 3, 4); String serialized = JsonFormatter.serialize(request); assertThat(serialized, not(containsString("frequency"))); assertThat(serialized, not(containsString("name"))); assertThat(serialized, not(containsString("payload"))); assertThat(serialized, not(containsString("multiple_responses"))); request.setMultipleResponses(false); serialized = JsonFormatter.serialize(request); assertThat(serialized, containsString("multiple_responses")); } }
@Test public void sameDiagnosticRequestEqual() { message = new Command(request, action); Command anotherMessage = new Command(new DiagnosticRequest(1, 2, 3, 4), action); assertEquals(message, anotherMessage); }
@Before public void setup() { request = new DiagnosticRequest(bus, id, mode, pid); request.setPayload(payload); }
@Test public void getPidWithNoPidReturnsNull() { DiagnosticRequest noPidRequest = new DiagnosticRequest(1, 2, 3); assertFalse(noPidRequest.hasPid()); assertThat(noPidRequest.getPid(), nullValue()); }
@Test public void serializeCommandWithDiagnosticRequest() { DiagnosticRequest request = new DiagnosticRequest(1, 2, 3, 4); serializeDeserializeAndCheckEqual(new Command(request, "add")); }
@Test public void serializeCommandWithDiagnosticRequestNoPid() { DiagnosticRequest request = new DiagnosticRequest(1, 2, 3); serializeDeserializeAndCheckEqual(new Command(request, "add")); }
@Test public void getKeyed() { DiagnosticRequest request = new DiagnosticRequest(42, 1, 2, 4); pipeline.receive(request); VehicleMessage message = pipeline.get(request.getKey()); assertThat(message, notNullValue()); assertEquals(message, request); } }
@Test public void writeAndReadFromParcelNoOptionalFields() { request = new DiagnosticRequest(1, 2, 3); Parcel parcel = Parcel.obtain(); request.writeToParcel(parcel, 0); // Reset parcel for reading parcel.setDataPosition(0); VehicleMessage createdFromParcel = VehicleMessage.CREATOR.createFromParcel(parcel); assertThat(createdFromParcel, instanceOf(DiagnosticRequest.class)); assertEquals(request, createdFromParcel); }
@Test public void setPid() { request = new DiagnosticRequest(1, 2, 3); request.setPid(42); assertTrue(request.hasPid()); assertEquals(request.getPid(), Integer.valueOf(42)); }
@Test public void serializeDiagnosticRequestWithOptional() { DiagnosticRequest request = new DiagnosticRequest(1, 2, 3, 4); request.setPayload(new byte[]{1,2,3,4}); request.setMultipleResponses(false); request.setFrequency(2.0); request.setName("foo"); serializeDeserializeAndCheckEqual(new Command(request, "add")); }