Skip to content

Commit

Permalink
Standardize controllers (#59)
Browse files Browse the repository at this point in the history
* begging of standardizing controllers

* introduce CarControllerBase and move common files from nxt variants into main

* fix the copyright headers

* revert some signatures

* format licenses

* update copyright template a little

* revert the return value type

* refactor a little

* move common res/values-ja/strings to main

* rename NxtController to NxtCarController

* refactor values*/strings

* apply res/strings changes

* rename constants

* use static import

* add methods of output devices and make all the base class's methods throw UnsupportedOperationException

* fix a test a little

* add helper methods that return the list of input/output devices.

* reflect amiq11's comment

* move defaults into the threshold setting fragment for each variant

* rename variables a little

* applied some yusaku's comments

* add 'machine' package for controllers and machine providers

* update packages in a file

* improve comments/javadocs

* fix minor bugs

* move all the resources related to main from nxt variant

* clean up codes

* clean up javadocs

* clean up javadocs a little

* fix a bug that prevents the app from showing help images

* use myusak's suggestion
  • Loading branch information
tiwanari authored Dec 20, 2016
1 parent 9b9dcdc commit 08b446c
Show file tree
Hide file tree
Showing 79 changed files with 968 additions and 702 deletions.
4 changes: 2 additions & 2 deletions .idea/copyright/pileproject.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 1 addition & 6 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

import android.support.test.runner.AndroidJUnit4;

import com.pileproject.drive.machine.CarControllerBase;
import com.pileproject.drive.machine.NxtCarController;
import com.pileproject.drivecommand.machine.device.input.LineSensor;
import com.pileproject.drivecommand.machine.device.input.SoundSensor;
import com.pileproject.drivecommand.machine.device.input.TouchSensor;
Expand All @@ -30,6 +32,8 @@
import org.mockito.Mock;
import org.mockito.internal.util.reflection.Whitebox;

import static com.pileproject.drive.machine.CarControllerBase.MotorKind.LeftMotor;
import static com.pileproject.drive.machine.CarControllerBase.MotorKind.RightMotor;
import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertFalse;
import static junit.framework.Assert.assertTrue;
Expand All @@ -38,10 +42,11 @@
import static org.mockito.Mockito.verify;

@RunWith(AndroidJUnit4.class)
public class NxtControllerTest {
public class NxtCarControllerTest {

@Mock NxtMachine machine;
@InjectMocks NxtController controller;
@InjectMocks
NxtCarController controller;

@Mock TouchSensor touchSensor;
@Mock SoundSensor soundSensor;
Expand All @@ -53,7 +58,7 @@ public class NxtControllerTest {
@Before
public void setUp() {
machine = mock(NxtMachine.class);
controller = new NxtController(machine);
controller = new NxtCarController(machine);

touchSensor = mock(TouchSensor.class);
soundSensor = mock(SoundSensor.class);
Expand All @@ -76,38 +81,38 @@ private void setUpMotors() {
public void whenTouchSensorIsNull_thenReturnFalse() throws Exception {
Whitebox.setInternalState(controller, "mTouchSensor", null);

assertFalse(controller.getTouchSensorValue());
assertFalse(controller.isTouchSensorTouched());
}

@Test
public void whenTouchSensorIsNotNull_andTouchSensorIsTouched_thenReturnTrue() throws Exception {
Whitebox.setInternalState(controller, "mTouchSensor", touchSensor);
doReturn(true).when(touchSensor).isTouched();

assertTrue(controller.getTouchSensorValue());
assertTrue(controller.isTouchSensorTouched());
}

@Test
public void whenTouchSensorIsNotNull_andTouchSensorIsNotTouched_thenReturnFalse() throws Exception {
Whitebox.setInternalState(controller, "mTouchSensor", touchSensor);
doReturn(false).when(touchSensor).isTouched();

assertFalse(controller.getTouchSensorValue());
assertFalse(controller.isTouchSensorTouched());
}

@Test
public void whenSoundSensorIsNull_thenReturnNegative() throws Exception {
Whitebox.setInternalState(controller, "mSoundSensor", null);

assertEquals(controller.getSoundSensorValue(), -1);
assertEquals(controller.getSoundSensorDb(), -1);
}

@Test
public void whenSoundSensorIsNotNull_thenReturnProperValue() throws Exception {
Whitebox.setInternalState(controller, "mSoundSensor", soundSensor);
doReturn(10).when(soundSensor).getDb();

assertEquals(10, controller.getSoundSensorValue());
assertEquals(10, controller.getSoundSensorDb());
}

@Test
Expand Down Expand Up @@ -181,16 +186,16 @@ public void whenMotorSpeedsAreNotInitialized_thenMovesForwardWithDefaultValue()

controller.moveForward();

verify(leftMotor).setSpeed(NxtController.INIT_MOTOR_POWER);
verify(rightMotor).setSpeed(NxtController.INIT_MOTOR_POWER);
verify(leftMotor).setSpeed(CarControllerBase.MotorProperty.INIT_MOTOR_POWER);
verify(rightMotor).setSpeed(CarControllerBase.MotorProperty.INIT_MOTOR_POWER);
}

@Test
public void whenSetMotorPowerCalled_thenMovesForwardWithTheValue() throws Exception {
setUpMotors();

controller.setMotorPower(NxtController.MotorKind.LeftMotor, 10);
controller.setMotorPower(NxtController.MotorKind.RightMotor, 10);
controller.setMotorPower(LeftMotor, 10);
controller.setMotorPower(RightMotor, 10);

controller.moveForward();

Expand All @@ -202,8 +207,8 @@ public void whenSetMotorPowerCalled_thenMovesForwardWithTheValue() throws Except
public void whenSetMotorPowerCalledWithOverUpperBoundValue_thenMovesForwardWithUpperBoundValue() throws Exception {
setUpMotors();

controller.setMotorPower(NxtController.MotorKind.LeftMotor, 200);
controller.setMotorPower(NxtController.MotorKind.RightMotor, 200);
controller.setMotorPower(LeftMotor, 200);
controller.setMotorPower(RightMotor, 200);

controller.moveForward();

Expand All @@ -215,8 +220,8 @@ public void whenSetMotorPowerCalledWithOverUpperBoundValue_thenMovesForwardWithU
public void whenSetMotorPowerCalledWithUnderLowerBoundValue_thenMovesForwardWithLowerBoundValue() throws Exception {
setUpMotors();

controller.setMotorPower(NxtController.MotorKind.LeftMotor, -10);
controller.setMotorPower(NxtController.MotorKind.RightMotor, -10);
controller.setMotorPower(LeftMotor, -10);
controller.setMotorPower(RightMotor, -10);

controller.moveForward();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,7 @@
import com.yahoo.squidb.sql.Table;

/**
* Implementation of SquidDatabase for this app.
*
* @author <a href="mailto:[email protected]">Tatsuya Iwanari</a>
* @version 1.0 3-April-2016
* The implementation of SquidDatabase for this app.
*/
public class DriveDatabase extends SquidDatabase {
private static final int VERSION = 1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,11 @@
import java.util.Collections;

/**
* Program Data manager
*
* @author <a href="mailto:[email protected]">Tatsuya Iwanari</a>
* @version 2.0 2-April-2016
* A manger of {@link ProgramData}.
*/
public class ProgramDataManager {
private static ProgramDataManager mInstance = new ProgramDataManager();
private DriveDatabase mDriveDatabase = null;
private DriveDatabase mDriveDatabase;

// the number of execution programs should be less than or equals to 1
private static final Query EXECUTION_PROGRAM =
Expand Down Expand Up @@ -71,29 +68,29 @@ public static ProgramDataManager getInstance() {
}

/**
* Save program data temporarily to execute it
* Saves a program temporarily to execute it.
*
* @param BlockSpaceLayout The programming space that has blocks
* @param layout The programming space that has blocks
*/
public boolean saveExecutionProgram(BlockSpaceLayout layout) {
return saveProgram(Program.EXECUTION, Program.EXECUTION, layout);
}

/**
* Save a sample program data
* Saves a sample program.
*
* @param String the name of a new program
* @param BlockSpaceLayout the programming space that has blocks
* @param programName the name of a new program
* @param layout the programming space that has blocks
*/
public boolean saveSampleProgram(String programName, BlockSpaceLayout layout) {
return saveProgram(programName, Program.SAMPLE, layout);
}

/**
* Save a user program data
* Saves a user program.
*
* @param String the name of a new program
* @param BlockSpaceLayout the programming space that has blocks
* @param programName the name of a new program
* @param layout the programming space that has blocks
*/
public boolean saveUserProgram(String programName, BlockSpaceLayout layout) {
return saveProgram(programName, Program.USER, layout);
Expand Down Expand Up @@ -141,29 +138,29 @@ private boolean saveProgram(String programName, String programType, BlockSpaceLa
}

/**
* Load an execution program's block data (Sorted)
* Loads an execution program's block data (sorted).
*
* @return ArrayList<BlockBase> loaded data
* @return loaded data as {@link ArrayList<BlockBase>}
*/
public ArrayList<BlockBase> loadExecutionProgram() {
return loadProgram(Program.EXECUTION, Program.EXECUTION);
}

/**
* Load a sample program's block data (Sorted)
* Loads a sample program's block data (sorted).
*
* @param String the name of program
* @return ArrayList<BlockBase> loaded data
* @param programName the name of program
* @return loaded data as {@link ArrayList<BlockBase>}
*/
public ArrayList<BlockBase> loadSampleProgram(String programName) {
return loadProgram(programName, Program.SAMPLE);
}

/**
* Load a user program's block data
* Loads a user program's block data
*
* @param String the name of program
* @return ArrayList<BlockBase> loaded data
* @param programName the name of program
* @return loaded data as {@link ArrayList<BlockBase>}
*/
public ArrayList<BlockBase> loadUserProgram(String programName) {
return loadProgram(programName, Program.USER);
Expand Down Expand Up @@ -217,18 +214,18 @@ private ArrayList<BlockBase> loadBlocks(SquidCursor<ProgramData> c) {
}

/**
* Load all sample program names
* Loads all the sample program names.
*
* @return ArrayList<String> the names of sample programs
* @return the names of sample programs as {@link ArrayList<String>}
*/
public ArrayList<String> loadSampleProgramNames() {
return loadProgramNames(Program.SAMPLE);
}

/**
* Load all user program names
* Loads all user program names
*
* @return ArrayList<String> the names of user programs
* @return the names of user programs as {@link ArrayList<String>}
*/
public ArrayList<String> loadUserProgramNames() {
return loadProgramNames(Program.USER);
Expand Down Expand Up @@ -256,24 +253,24 @@ else if (programType.equals(Program.SAMPLE)) {
}

/**
* Delete an execution program
* Deletes an execution program.
*/
public void deleteExecutionProgram() {
deleteProgram(Program.EXECUTION, Program.EXECUTION);
}

/**
* Delete a sample program with 'programName'
* Deletes a sample program with <code>programName</code>.
*
* @param String the name of a sample program
* @param programName the name of a sample program
*/
public void deleteSampleProgram(String programName) {
deleteProgram(programName, Program.SAMPLE);
}
/**
* Delete a user program with 'programName'
* Deletes a user program with <code>programName</code>.
*
* @param String the name of a user program
* @param programName the name of a user program
*/
public void deleteUserProgram(String programName) {
deleteProgram(programName, Program.USER);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@

/**
* Specification for "program_data" table
*
* @author <a href="mailto:[email protected]">Tatsuya Iwanari</a>
* @version 1.0 22-March-2016
*/
@TableModelSpec(className="ProgramData", tableName="program_data",
tableConstraint = "FOREIGN KEY(programId) references programs(_id) ON DELETE CASCADE")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@

/**
* Specification for "programs" table
*
* @author <a href="mailto:[email protected]">Tatsuya Iwanari</a>
* @version 1.0 22-March-2016
*/
@TableModelSpec(className="Program", tableName="programs")
public class ProgramSpec {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import com.pileproject.drive.comm.CommunicatorProvider;
import com.pileproject.drive.comm.RxMachineConnector;
import com.pileproject.drive.database.ProgramDataManager;
import com.pileproject.drive.machine.MachineProvider;
import com.pileproject.drive.util.bluetooth.BluetoothUtil;
import com.pileproject.drive.util.development.DeployUtils;
import com.pileproject.drive.util.fragment.AlertDialogFragment;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import android.os.Bundle;

import com.pileproject.drive.machine.MachineController;
import com.pileproject.drive.programming.visual.block.BlockBase;

import java.util.List;
Expand Down
Loading

0 comments on commit 08b446c

Please sign in to comment.