Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check for null camera engine before returning from MapboxNavigation #866

Merged
merged 2 commits into from
Apr 17, 2018

Conversation

danesfeder
Copy link
Contributor

@danesfeder danesfeder commented Apr 16, 2018

Closes #842

Always return default SimpleCamera engine if one hasn't already been initialized.

Took a moment to clean up MapboxNavigationTest as well.

@danesfeder danesfeder added bug Defect to be fixed. navigation-core labels Apr 16, 2018
@danesfeder danesfeder added this to the v0.13.0 milestone Apr 16, 2018
@danesfeder danesfeder self-assigned this Apr 16, 2018
Copy link
Contributor

@Guardiola31337 Guardiola31337 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great you took the opportunity to refactor MapboxNavigationTest 🙇

Well done @danesfeder ❤️ it!

A few comments / cleanup not blocking the PR.

public void before() throws Exception {
navigation = new MapboxNavigation(mock(Context.class), ACCESS_TOKEN, mock(NavigationTelemetry.class),
mock(LocationEngine.class));
assertNotNull("should not be null", navigation);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO Adding error messages to tests is kind of confusing because when you 👀 a test using JUnit you think always as expected and actual values being the first parameters and in this case "should not be null" is not the expected value is actually the error message shown when the test fails (which is not giving any extra value because if it fails you're going to get ❌)
I'd rather working on improving the name of the test if needed in these cases.

MapboxNavigation navigationWithOptions = new MapboxNavigation(mock(Context.class),
ACCESS_TOKEN, options, mock(NavigationTelemetry.class), mock(LocationEngine.class));
MapboxNavigation navigationWithOptions = buildMapboxNavigationWithOptions(options);

assertNotNull("should not be null", navigationWithOptions);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here ☝️

assertNotNull("should not be null", navigationWithOptions);
}

@Test
public void voiceMilestone_onInitializationDoesGetAdded() throws Exception {
MapboxNavigation navigation = buildMapboxNavigation();

assertTrue(navigation.getMilestones().get(0).getIdentifier() == VOICE_INSTRUCTION_MILESTONE_ID);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Normally is good to structure the tests following the triple A 👀 https://speakerdeck.com/guardiola31337/elegant-unit-testing-mobilization-2016?slide=40 http://wiki.c2.com/?ArrangeActAssert

Here we're including the Act inside the Assert. It'd be great to have them separated and take advantage of giving great names to variables so it's easy to understand what's going on.

This applies to other tests included in MapboxNavigationTest.

}

@Test
public void addMilestone_milestoneDidGetAdded() throws Exception {
MapboxNavigation navigation = buildMapboxNavigation();

Milestone milestone = new StepMilestone.Builder().build();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO These two lines of code belongs to the Arrange part.

This applies to other tests included in MapboxNavigationTest.

assertEquals(0, navigationWithOptions.getMilestones().size());
}

@Test
public void removeMilestone_correctMilestoneWithIdentifierGetsRemoved() throws Exception {
MapboxNavigationOptions options = MapboxNavigationOptions.builder().defaultMilestonesEnabled(false).build();
MapboxNavigation navigationWithOptions = new MapboxNavigation(mock(Context.class),
ACCESS_TOKEN, options, mock(NavigationTelemetry.class), mock(LocationEngine.class));
MapboxNavigation navigationWithOptions = buildMapboxNavigationWithOptions(options);
Milestone milestone = new StepMilestone.Builder().setIdentifier(5678).build();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about using a well-named variable for 5678 reflecting what's useful for here? Maybe theMilestone?

navigationWithOptions.addMilestone(new StepMilestone.Builder().build());
assertEquals(1, navigationWithOptions.getMilestones().size());

navigationWithOptions.removeMilestone(5678);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here ☝️ but reflecting that the milestone was not found.

assertEquals(1, navigationWithOptions.getMilestones().size());
}

@Test
public void getLocationEngine_returnsCorrectLocationEngine() throws Exception {
MapboxNavigation navigation = buildMapboxNavigation();
LocationEngine locationEngine = mock(LocationEngine.class);
LocationEngine locationEngine2 = mock(LocationEngine.class);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should avoid adding "numbers" to variable names. Could we refactor-rename adding more context so it'll be easier to read and understand what's going on in the test?

@danesfeder
Copy link
Contributor Author

@Guardiola31337 addressed, thanks!

@danesfeder danesfeder merged commit 9e95f4b into master Apr 17, 2018
@danesfeder danesfeder deleted the dan-camera-bug branch April 17, 2018 18:36
Copy link
Contributor

@Guardiola31337 Guardiola31337 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@danesfeder Some minor comments to take into account in the future when working again with MapboxNavigationTest.


int identifier = navigation.getMilestones().get(0).getIdentifier();

assertEquals(identifier, VOICE_INSTRUCTION_MILESTONE_ID);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be the other way around, "expected" value 👉 VOICE_INSTRUCTION_MILESTONE_ID which would be better represented using the literal value instead of using the constant (👀 Expect Literals), "actual" value 👉 identifier.
The same thing happens in bannerMilestone_onInitializationDoesGetAdded test 👇

MapboxNavigation navigationWithOptions = new MapboxNavigation(mock(Context.class),
ACCESS_TOKEN, options, mock(NavigationTelemetry.class), mock(LocationEngine.class));
MapboxNavigation navigationWithOptions = buildMapboxNavigationWithOptions(options);

assertEquals(0, navigationWithOptions.getMilestones().size());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👀 #866 (comment)
navigationWithOptions.getMilestones().size() is the Act here.
This applies to other tests included in MapboxNavigationTest 👇

MapboxNavigation navigationWithOptions = new MapboxNavigation(mock(Context.class),
ACCESS_TOKEN, options, mock(NavigationTelemetry.class), mock(LocationEngine.class));
MapboxNavigation navigationWithOptions = buildMapboxNavigationWithOptions(options);

Milestone milestone = new StepMilestone.Builder().build();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Milestone milestone = new StepMilestone.Builder().build(); should be in the Arrange part.

MapboxNavigation navigationWithOptions = new MapboxNavigation(mock(Context.class),
ACCESS_TOKEN, options, mock(NavigationTelemetry.class), mock(LocationEngine.class));
MapboxNavigation navigationWithOptions = buildMapboxNavigationWithOptions(options);

Milestone milestone = new StepMilestone.Builder().build();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Milestone milestone = new StepMilestone.Builder().build();
navigationWithOptions.addMilestone(milestone);

Should be in the Arrange part.

MapboxNavigation navigationWithOptions = new MapboxNavigation(mock(Context.class),
ACCESS_TOKEN, options, mock(NavigationTelemetry.class), mock(LocationEngine.class));
MapboxNavigation navigationWithOptions = buildMapboxNavigationWithOptions(options);

Milestone milestone = new StepMilestone.Builder().build();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Milestone milestone = new StepMilestone.Builder().build();
navigationWithOptions.addMilestone(new StepMilestone.Builder().build());

Should be in the Arrange part.
We could take advantage of the naming here to understand clearer what's going on. Like naming new StepMilestone.Builder().build() as inexistentMilestone for example.

ACCESS_TOKEN, options, mock(NavigationTelemetry.class), mock(LocationEngine.class));
Milestone milestone = new StepMilestone.Builder().setIdentifier(5678).build();
MapboxNavigation navigationWithOptions = buildMapboxNavigationWithOptions(options);
int removedMilestoneIdentifier = 5678;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO removedMilestoneIdentifier is not a good name here 👀 #866 (comment)

LocationEngine locationEngine = mock(LocationEngine.class);
LocationEngine locationEngine2 = mock(LocationEngine.class);
LocationEngine locationEngineInstanceNotUsed = mock(LocationEngine.class);

navigation.setLocationEngine(locationEngine);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be in the Arrange part 👀 getLocationEngine_returnsCorrectLocationEngine is telling us what's the Act here getLocationEngine.

NavigationEventListener navigationEventListener = mock(NavigationEventListener.class);

navigation.addNavigationEventListener(navigationEventListener);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here ☝️
This applies to other tests included in MapboxNavigationTest 👇

navigation.setSnapEngine(snap);

assertTrue(!(navigation.getSnapEngine() instanceof SnapToRoute));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about using assertFalse instead?

navigation.setOffRouteEngine(offRoute);

assertTrue(!(navigation.getOffRouteEngine() instanceof OffRouteDetector));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here ☝️

@danesfeder danesfeder mentioned this pull request May 3, 2018
13 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Defect to be fixed.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants