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

Android Configure Player Types and Markers #12

Open
wants to merge 25 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
6bd1ebf
Rename the LoopBuilder methods
damonkelley Jul 25, 2016
d81c364
Add the GameOptions form
damonkelley Jul 25, 2016
996016a
Replace the settings with a game configuration form
damonkelley Jul 25, 2016
5524dbf
An IllegalMoveException is thrown when an illegal move is attempted
damonkelley Jul 26, 2016
13cd34b
Rework the Loop class so that Turns determine the transition
damonkelley Jul 26, 2016
20c1976
Remove ComputerTurn and replace it with AsyncComputerTurn
damonkelley Jul 26, 2016
99e7c2b
Add a method to initialize the Turn
damonkelley Jul 26, 2016
e31ce05
Allow any human vs computer or human vs human game combination
damonkelley Jul 26, 2016
4562411
Organize classes into packages
damonkelley Jul 26, 2016
343cbf6
Update the turns to hold a reference to the UI
damonkelley Jul 27, 2016
29a05be
Update MultiPlayerHumanTurn to handle illegal moves
damonkelley Jul 27, 2016
c455163
Add toggle buttons to the game options form for marker selection
damonkelley Jul 27, 2016
187d896
Add a method to Marker to get the opposite of a member
damonkelley Jul 27, 2016
c02020e
Update GameOptions to include getPreset and getFirstMarker
damonkelley Jul 27, 2016
06f2a8e
Introduce the PresetFactory
damonkelley Jul 27, 2016
d4c23ee
Use the PresetFactory to bootstrap a new LoopBuilder
damonkelley Jul 27, 2016
de970af
Make the getPlayerType methods private
damonkelley Jul 27, 2016
3d9c4f0
Add a new ActivityTestRule to stub out data sent through the Intent
damonkelley Jul 27, 2016
b6be4bd
Update getFirstMarker to resturn a string instead of a Marker
damonkelley Jul 28, 2016
877a28b
Move the preset names into constants
damonkelley Jul 28, 2016
75dd33a
Move string constants into resources files
damonkelley Jul 28, 2016
4e810a1
Clean up some of the unit tests
damonkelley Jul 28, 2016
f144da6
Adjust the form layout
damonkelley Jul 28, 2016
89e2c95
Prevent a user from selecting Computer vs Computer
damonkelley Jul 28, 2016
a3e6434
Change the alert dialog title
damonkelley Jul 28, 2016
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

59 changes: 48 additions & 11 deletions app/src/androidTest/java/me/damonkelley/tictactoe_app/UITest.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package me.damonkelley.tictactoe_app;

import android.preference.Preference;
import android.preference.PreferenceManager;
import android.support.test.filters.LargeTest;
import android.support.test.rule.ActivityTestRule;
import android.support.test.runner.AndroidJUnit4;
import me.damonkelley.tictactoe_app.activity.MainActivity;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
Expand All @@ -14,10 +14,9 @@
import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.action.ViewActions.click;
import static android.support.test.espresso.assertion.ViewAssertions.matches;
import static android.support.test.espresso.matcher.PreferenceMatchers.withKey;
import static android.support.test.espresso.matcher.PreferenceMatchers.withSummaryText;
import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed;
import static android.support.test.espresso.matcher.ViewMatchers.withId;
import static android.support.test.espresso.matcher.ViewMatchers.withSpinnerText;
import static android.support.test.espresso.matcher.ViewMatchers.withText;
import static org.hamcrest.CoreMatchers.anything;
import static org.hamcrest.Matchers.allOf;
Expand Down Expand Up @@ -101,7 +100,6 @@ private void clickSpace(int position) {
.inAdapterView(withId(R.id.game))
.atPosition(position)
.perform(click());

}

private void clickAndCheck(int position, String marker) {
Expand All @@ -114,21 +112,60 @@ private void clickAndCheck(int position, String marker) {

@Test
public void playAHumanVsComputerGame() {
onData(allOf(is(instanceOf(Preference.class)), withKey("game_type")))
.check(matches(isDisplayed()))
.perform(click());
choosePlayerType(R.id.player_one_type, "Human");
choosePlayerType(R.id.player_two_type, "Computer");

onView(withId(R.id.start_button)).perform(click());

clickSpace(0);
clickSpace(3);
clickSpace(1);

onView(withText("Human vs. Computer")).perform(click());
onView(withId(R.id.game_message)).check(matches(withText("O wins!")));
}

onData(allOf(is(instanceOf(Preference.class)), withKey("game_type"), withSummaryText("Human vs. Computer")))
.check(matches(isDisplayed()));
@Test
public void playAComputerVsHumanGame() {
choosePlayerType(R.id.player_one_type, "Computer");
choosePlayerType(R.id.player_two_type, "Human");

onView(withId(R.id.start_button)).perform(click());

clickSpace(0);
clickSpace(1);

onView(withId(R.id.game_message)).check(matches(withText("X wins!")));
}

@Test
public void playAComputerVsHumanGameWithOAsFirstMarker() {
choosePlayerType(R.id.player_one_type, "Computer");
choosePlayerType(R.id.player_two_type, "Human");

onView(withId(R.id.player_one_marker)).perform(click());

onView(withId(R.id.start_button)).perform(click());

clickSpace(0);
clickSpace(3);
clickSpace(1);

onView(withId(R.id.game_message)).check(matches(withText("O wins!")));
}

@Test
public void attemptingAComputerVsComputerGameIsNotAllowed() {
choosePlayerType(R.id.player_one_type, "Computer");
choosePlayerType(R.id.player_two_type, "Computer");

onView(withText("OK")).check(matches(isDisplayed())).perform(click());

onView(withId(R.id.player_one_type)).check(matches(withSpinnerText(R.string.human)));
onView(withId(R.id.player_two_type)).check(matches(withSpinnerText(R.string.human)));
}


private void choosePlayerType(int player_type, String option) {
onView(withId(player_type)).perform(click());
onData(allOf(is(instanceOf(String.class)), is(option))).perform(click());
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package me.damonkelley.tictactoe_app;
package me.damonkelley.tictactoe_app.adapter;

import android.support.test.filters.SmallTest;
import android.support.test.rule.ActivityTestRule;
Expand All @@ -7,27 +7,31 @@
import me.damonkelley.tictactoe.Board;
import me.damonkelley.tictactoe.Marker;
import me.damonkelley.tictactoe.Space;
import me.damonkelley.tictactoe_app.activity.GameActivity;
import me.damonkelley.tictactoe_app.rule.GameActivityTestRule;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;

import static junit.framework.Assert.assertEquals;
import static org.junit.Assert.assertEquals;

@SmallTest
@RunWith(AndroidJUnit4.class)
public class BoardAdapterTest {

@Rule
public ActivityTestRule<GameActivity> mActivityRule = new GameActivityTestRule();

private Board board;
private BoardAdapter adapter;

@Rule
public ActivityTestRule<GameActivity> mActivityRule = new ActivityTestRule(GameActivity.class);
private GameActivity mActivity;

@Before
public void setUp() throws Exception {
board = new Board();
adapter = new BoardAdapter(mActivityRule.getActivity(), board);
mActivity = mActivityRule.getActivity();
adapter = new BoardAdapter(mActivity, board);
}

@Test
Expand All @@ -41,7 +45,7 @@ public void itGetsAViewAtThePosition() {
@Test
public void itGetsAViewAtThePositionForA4By4Board() {
Board board = new Board(4);
BoardAdapter adapter = new BoardAdapter(mActivityRule.getActivity(), board);
BoardAdapter adapter = new BoardAdapter(mActivity, board);

board.put(new Space(3, 3), Marker.X);

Expand All @@ -51,8 +55,8 @@ public void itGetsAViewAtThePositionForA4By4Board() {

@Test
public void theCount() {
assertEquals(9, new BoardAdapter(mActivityRule.getActivity(), new Board()).getCount());
assertEquals(16, new BoardAdapter(mActivityRule.getActivity(), new Board(4)).getCount());
assertEquals(9, new BoardAdapter(mActivity, new Board()).getCount());
assertEquals(16, new BoardAdapter(mActivity, new Board(4)).getCount());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package me.damonkelley.tictactoe_app.fragment;

import android.support.test.espresso.ViewInteraction;
import android.support.test.rule.ActivityTestRule;
import me.damonkelley.tictactoe.Marker;
import me.damonkelley.tictactoe_app.R;
import me.damonkelley.tictactoe_app.activity.MainActivity;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;

import static android.support.test.espresso.Espresso.onData;
import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.action.ViewActions.click;
import static android.support.test.espresso.assertion.ViewAssertions.matches;
import static android.support.test.espresso.matcher.ViewMatchers.withId;
import static android.support.test.espresso.matcher.ViewMatchers.withText;
import static junit.framework.Assert.assertEquals;
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.core.AllOf.allOf;

public class GameOptionsTest {

@Rule
public ActivityTestRule<MainActivity> mActivityRule = new ActivityTestRule(MainActivity.class);

private GameOptions fragment;
private ViewInteraction playerOneMarkerToggle;
private ViewInteraction playerTwoMarkerToggle;

@Before
public void setUp() {
fragment = (GameOptions) mActivityRule.getActivity()
.getFragmentManager()
.findFragmentById(R.id.options);

playerOneMarkerToggle = onView(withId(R.id.player_one_marker));
playerTwoMarkerToggle = onView(withId(R.id.player_two_marker));
}

@Test
public void choosingHumanAndHuman() {
choosePlayerType(R.id.player_one_type, "Human");
choosePlayerType(R.id.player_two_type, "Human");

assertEquals("human-vs-human", fragment.getPreset());
}

@Test
public void choosingComputerAndHuman() {
choosePlayerType(R.id.player_one_type, "Computer");
choosePlayerType(R.id.player_two_type, "Human");

assertEquals("computer-vs-human", fragment.getPreset());
}

@Test
public void choosingHumanAndComputer() {
choosePlayerType(R.id.player_one_type, "Human");
choosePlayerType(R.id.player_two_type, "Computer");

assertEquals("human-vs-computer", fragment.getPreset());
}

@Test
public void togglingThePlayerOneMarkerTogglesThePlayerTwoMarker() {
playerTwoMarkerToggle.check(matches(withText("O")));

playerOneMarkerToggle.check(matches(withText("X")))
.perform(click())
.check(matches(withText("O")));

playerTwoMarkerToggle.check(matches(withText("X")));
}

@Test
public void togglingThePlayerTwoMarkerTogglesThePlayerOneMarker() {
playerOneMarkerToggle.check(matches(withText("X")));

playerTwoMarkerToggle.check(matches(withText("O")))
.perform(click())
.check(matches(withText("X")));

playerOneMarkerToggle.check(matches(withText("O")));
}

@Test
public void getFirstMarker() {
assertEquals(Marker.X.toString(), fragment.getFirstMarker());
playerOneMarkerToggle.perform(click());
assertEquals(Marker.O.toString(), fragment.getFirstMarker());
}

private void choosePlayerType(int player_type, String option) {
onView(withId(player_type)).perform(click());
onData(allOf(is(instanceOf(String.class)), is(option))).perform(click());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package me.damonkelley.tictactoe_app.rule;

import android.content.Intent;
import android.support.test.rule.ActivityTestRule;
import me.damonkelley.tictactoe_app.activity.GameActivity;

public class GameActivityTestRule extends ActivityTestRule<GameActivity> {
public GameActivityTestRule() {
super(GameActivity.class);
}

@Override
protected Intent getActivityIntent() {
return new Intent()
.putExtra("preset", "human-vs-human")
.putExtra("first-marker", "X");
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package me.damonkelley.tictactoe_app;
package me.damonkelley.tictactoe_app.wrapper;

import android.support.test.rule.ActivityTestRule;
import android.widget.TextView;
import me.damonkelley.tictactoe.Game;
import me.damonkelley.tictactoe.Marker;
import me.damonkelley.tictactoe.Space;
import me.damonkelley.tictactoe_app.activity.GameActivity;
import me.damonkelley.tictactoe_app.rule.GameActivityTestRule;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
Expand All @@ -13,12 +15,12 @@

public class MessageViewWrapperTest {

@Rule
public ActivityTestRule<GameActivity> mActivityRule = new GameActivityTestRule();

private Game game;
private TextView view;

@Rule
public ActivityTestRule<GameActivity> mActivityRule = new ActivityTestRule(GameActivity.class);

@Before
public void setUp() throws Exception {
game = new Game(Marker.X);
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<activity android:name=".activity.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".GameActivity"></activity>
<activity android:name=".activity.GameActivity"></activity>
</application>

</manifest>
29 changes: 0 additions & 29 deletions app/src/main/java/me/damonkelley/tictactoe_app/ComputerTask.java

This file was deleted.

Loading