Skip to content

Commit

Permalink
Week 6 Exercise 4b: Finish the game, and show Treasures on the map on…
Browse files Browse the repository at this point in the history
…ce it has ended.
  • Loading branch information
Mary-Anne Cosgrove committed May 4, 2014
1 parent 50bcb05 commit a9cffdb
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
import com.thoughtworks.androidbootcamp.test.helpers.When;

import static com.google.android.apps.common.testing.ui.espresso.Espresso.onView;
import static com.google.android.apps.common.testing.ui.espresso.action.ViewActions.click;
import static com.google.android.apps.common.testing.ui.espresso.action.ViewActions.typeText;
import static com.google.android.apps.common.testing.ui.espresso.assertion.ViewAssertions.matches;
import static com.google.android.apps.common.testing.ui.espresso.matcher.ViewMatchers.Visibility.VISIBLE;
import static com.google.android.apps.common.testing.ui.espresso.matcher.ViewMatchers.withEffectiveVisibility;
Expand All @@ -35,13 +33,6 @@ protected void setUp() throws Exception {
getActivity();
}

public void testRetrievePlayerName() {
onView(withId(R.id.player_field)).perform(typeText("Fred"));
onView(withId(R.id.player_ok_button)).perform(click());

onView(withId(R.id.welcome_player)).check(matches(withText(containsString("Fred"))));
}

public void testTreasureList() throws InterruptedException {
Given.thePlayerHasEnteredTheirName();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import android.widget.Toast;

import com.thoughtworks.androidbootcamp.R;
import com.thoughtworks.androidbootcamp.controller.fragment.HighScoresFragment;
Expand All @@ -20,6 +21,8 @@

import java.util.List;

import static java.lang.String.format;

public class HelloAndroid extends Activity implements ActionBar.OnNavigationListener {

/**
Expand Down Expand Up @@ -82,8 +85,7 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) {
}

private void welcomePlayer() {
TextView welcome = (TextView) findViewById(R.id.welcome_player);
welcome.setText("Welcome " + getGame().getPlayer() + "!");
Toast.makeText(this, "Welcome "+getGame().getPlayer(), Toast.LENGTH_LONG);
}

@Override
Expand Down Expand Up @@ -180,4 +182,23 @@ public List<Treasure> getTreasures() {
public List<Attempt> getAttempts() {
return getGame().getAttempts();
}

public boolean hasEnded() {
return getGame().hasEnded();
}

public void endGame(View view) {
getGame().end();
Toast.makeText(this, getEndGameMessage(), Toast.LENGTH_LONG).show();
}

protected String getEndGameMessage() {
return format("Good game! Your final score was %d. \n" +
"Check out the Treasure Map to see where all the treasures are.", getScore());
}


public int getScore() {
return getGame().getScore().getScore();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ public void takePhoto() {
// tell camera app when to put the photo
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));

if (intent.resolveActivity(getActivity().getPackageManager()) != null) {
if (intent.resolveActivity(mActivity.getPackageManager()) != null) {
// ask to use the camera
startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
}
Expand All @@ -167,10 +167,14 @@ public void onActivityResult(int requestCode, int resultCode, Intent data) {

protected void onTreasureAttempted() {
// Retrieve geocode info from the taken photo and treasure
Game game = getGame();
if (game.hasEnded()) {
Toast.makeText(mActivity, "Too late for that now!", Toast.LENGTH_LONG).show();
return;
}
Attempt attempt = createAttemptForPhoto(getCurrentPhotoPath());
Treasure thisTreasure = getSelectedTreasure();
attempt.setDistance(calculateDistance(thisTreasure, attempt));
Game game = getGame();
boolean previouslyAttemptedTreasure = game.hasPreviouslyAttemptedTreasure(thisTreasure);

int distanceDifference = game.recordAttempt(thisTreasure, attempt);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import com.thoughtworks.androidbootcamp.R;
Expand All @@ -27,6 +28,9 @@

import java.util.List;

import static com.google.android.gms.maps.model.BitmapDescriptorFactory.HUE_GREEN;
import static com.google.android.gms.maps.model.BitmapDescriptorFactory.HUE_YELLOW;


public class TreasureMapFragment extends Fragment implements GooglePlayServicesClient.ConnectionCallbacks,
GooglePlayServicesClient.OnConnectionFailedListener {
Expand Down Expand Up @@ -72,7 +76,10 @@ public void onConnected(Bundle bundle) {
Toast.makeText(mActivity, "Connected to Play Services", Toast.LENGTH_SHORT).show();
mMap = mMapFragment.getMap();
moveToCurrentLocation();
setMarkers(mActivity.getAttempts());
setMarkers(mActivity.getAttempts(), HUE_GREEN);
if (mActivity.hasEnded()) {
setMarkers(mActivity.getTreasures(), HUE_YELLOW);
}
}

@Override
Expand All @@ -95,10 +102,10 @@ public void onConnectionFailed(ConnectionResult connectionResult) {
}
}

public void setMarkers(List<? extends Locatable> locatables) {
public void setMarkers(List<? extends Locatable> locatables, float hue) {
if (canUseMap()) {
for (Locatable locatable : locatables) {
mMap.addMarker(createMarkerForLocatable(locatable));
mMap.addMarker(createMarkerForLocatable(locatable, hue));
}
}
}
Expand All @@ -119,14 +126,15 @@ private void moveToCurrentLocation() {
}
}

private MarkerOptions createMarkerForLocatable(Locatable locatable) {
private MarkerOptions createMarkerForLocatable(Locatable locatable, float hue) {
LatLng position = new LatLng(
locatable.getLatitude(),
locatable.getLongitude());
MarkerOptions markerOption = new MarkerOptions();
markerOption.position(
new LatLng(
locatable.getLatitude(),
locatable.getLongitude())
);
markerOption.title(locatable.getName());
markerOption
.position(position)
.title(locatable.getName())
.icon(BitmapDescriptorFactory.defaultMarker(hue));
return markerOption;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public class Game implements Serializable {
//See http:https://www.developerphil.com/parcelable-vs-serializable/, for example
private Map<Treasure, Attempt> attempts;
private Score score;
private boolean ended = false;

public Game() {
attempts = new HashMap<Treasure, Attempt>();
Expand All @@ -20,6 +21,10 @@ public Game() {
score.setScore(0);
}

public boolean hasEnded() {
return ended;
}

public void setPlayer(String player) {
score.setName(player);
}
Expand Down Expand Up @@ -90,4 +95,8 @@ public Score getScore() {
score.setScore(calculateScore());
return score;
}

public void end() {
ended = true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;

import java.util.ArrayList;
import java.util.List;

import static com.google.common.collect.Lists.newArrayList;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
Expand All @@ -23,6 +24,9 @@
@RunWith(RobolectricTestRunner.class)
public class HelloAndroidUnitTest {

public static final int TREASURE_MENU_ITEM = 0;
public static final int HIGH_SCORES_MENU_ITEM = 1;
public static final int MAP_MENU_ITEM = 2;
HelloAndroid activity;

@Before
Expand All @@ -32,19 +36,19 @@ public void setUp() {

@Test
public void shouldShowTreasureListWhenFirstSpinnerItemSelected() throws Exception {
activity.onNavigationItemSelected(0, 0);
activity.onNavigationItemSelected(TREASURE_MENU_ITEM, 0);
verify(activity).showTreasureList();
}

@Test
public void shouldShowHighScoresWhenSecondSpinnerItemSelected() throws Exception {
activity.onNavigationItemSelected(1, 0);
activity.onNavigationItemSelected(HIGH_SCORES_MENU_ITEM, 0);
verify(activity).showHighScores();
}

@Test
public void shouldShowMapWhenThirdSpinnerItemSelected() throws Exception {
activity.onNavigationItemSelected(2, 0);
activity.onNavigationItemSelected(MAP_MENU_ITEM, 0);
verify(activity).showMap();
}

Expand All @@ -68,6 +72,13 @@ public void shouldGetTreasuresFromGame() throws Exception {
assertThat(actual.get(0), is(treasure));
}

@Test
public void shouldCreateEndGameMessage() throws Exception {
doReturn(3363).when(activity).getScore();

assertThat(activity.getEndGameMessage(), containsString("Good game! Your final score was 3363."));
}



}

0 comments on commit a9cffdb

Please sign in to comment.