Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
5 changes: 4 additions & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,10 @@ dependencies {
androidTestCompile 'com.android.support:support-annotations:23.4.0'

// mockito
androidTestCompile 'org.mockito:mockito-core:1.9.5'
androidTestCompile 'org.mockito:mockito-core:1.10.19'
androidTestCompile "com.crittercism.dexmaker:dexmaker:1.4"
androidTestCompile "com.crittercism.dexmaker:dexmaker-dx:1.4"
androidTestCompile "com.crittercism.dexmaker:dexmaker-mockito:1.4"

// our library
compile 'com.pileproject:drivecommand:2.1.0'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
* Copyright (C) 2011-2015 PILE Project, Inc. <dev@pileproject.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.pileproject.drive.programming.visual.block;

import android.support.test.runner.AndroidJUnit4;
import android.widget.TextView;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;

import java.math.BigDecimal;

import static org.junit.Assert.*;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.verify;

@RunWith(AndroidJUnit4.class)
public class NumberTextViewDelegateTest {

private static final int PRECISION = 3;

public TextView mockTextView;

public NumberTextHolder mockNumberTextHolder;

public NumberTextViewDelegate numberTextViewDelegate;

@Before
public void before() {
mockTextView = Mockito.mock(TextView.class);

mockNumberTextHolder = Mockito.mock(NumberTextHolder.class);

doReturn(PRECISION).when(mockNumberTextHolder).getPrecision();

numberTextViewDelegate = new NumberTextViewDelegate(mockTextView, mockNumberTextHolder);
}

@Test
public void whenTextViewReturnsNumber_thenGetValueSucceeds() throws Exception {

doReturn("2.500").when(mockTextView).getText();
assertEquals(new BigDecimal("2.5"), numberTextViewDelegate.getValue());
}

@Test
public void whenTextViewReturnsNumber_thenGetActionValueSucceeds() throws Exception {

doReturn("2.500").when(mockTextView).getText();
assertEquals(2500, numberTextViewDelegate.getActionValue());
}

@Test
public void whenTextViewDisabled_thenVerifyTheMethodsCalled() throws Exception {

numberTextViewDelegate.enableTextView(false);

verify(mockTextView).setFocusable(false);
verify(mockTextView).setFocusableInTouchMode(false);
verify(mockTextView).setEnabled(false);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/*
* Copyright (C) 2011-2015 PILE Project, Inc. <dev@pileproject.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.pileproject.drive.util.development;

import android.content.res.Configuration;
import android.content.res.Resources;

import com.pileproject.drive.app.DriveApplication;

import org.junit.Before;
import org.junit.Test;
import org.junit.experimental.runners.Enclosed;
import org.junit.runner.RunWith;

import java.util.Locale;

import static junit.framework.Assert.assertEquals;

@RunWith(Enclosed.class)
public class UnitTest {

public static void setLocale(Locale locale) {

Resources res = DriveApplication.getContext().getResources();
Configuration config = res.getConfiguration();
config.locale = locale;
res.updateConfiguration(config, res.getDisplayMetrics());
}

public static class InUS {

@Before
public void before() {
setLocale(Locale.US);
}

@Test
public void whenUnitIsSecond_thenDecoratesAsSecondString() throws Exception {
assertEquals("When value is 1.0 and unit is second",
"1.000s", Unit.Second.decorateValue(Locale.ENGLISH, 1.0, 3));
}

@Test
public void whenUnitIsPercent_thenDecoratesAsPercentString() throws Exception {
assertEquals("When value is 100 and unit is percentage",
"100%", Unit.Percentage.decorateValue(Locale.ENGLISH, 100, 0));
}

@Test
public void whenUnitIsNumberOfTimes_thenDecoratesAsNumberOfTimesString() throws Exception {
assertEquals("When the value 100 and unit is NumberOfTimes",
"Repeats: 100", Unit.NumberOfTimes.decorateValue(Locale.ENGLISH, 100, 0));
}
}

public static class InJP {

@Before
public void before() {
setLocale(Locale.JAPAN);
}

@Test
public void whenUnitIsSecond_thenDecoratesAsSecondString() throws Exception {
assertEquals("When value is 1.0 and unit is second",
"1.000びょう", Unit.Second.decorateValue(Locale.JAPAN, 1.0, 3));
}

@Test
public void whenUnitIsPercent_thenDecoratesAsPercentString() throws Exception {
assertEquals("When value is 100 and unit is percentage",
"100%", Unit.Percentage.decorateValue(Locale.JAPAN, 100, 0));
}

@Test
public void whenUnitIsNumberOfTimes_thenDecoratesAsNumberOfTimesString() throws Exception {
assertEquals("When the value 100 and unit is NumberOfTimes",
"100 かい", Unit.NumberOfTimes.decorateValue(Locale.JAPAN, 100, 0));
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import com.pileproject.drive.programming.visual.activity.BlockPositionComparator;
import com.pileproject.drive.programming.visual.block.BlockBase;
import com.pileproject.drive.programming.visual.block.BlockFactory;
import com.pileproject.drive.programming.visual.block.NumTextHolder;
import com.pileproject.drive.programming.visual.block.NumberTextHolder;
import com.pileproject.drive.programming.visual.layout.BlockSpaceLayout;
import com.yahoo.squidb.data.SquidCursor;
import com.yahoo.squidb.sql.Query;
Expand Down Expand Up @@ -129,8 +129,8 @@ private boolean saveProgram(String programName, String programType, BlockSpaceLa
.setLeft(b.getLeft())
.setTop(b.getTop());
// get the number of TextView if the block has one
if (b instanceof NumTextHolder) {
data.setNumber(((NumTextHolder) b).getNum());
if (b instanceof NumberTextHolder) {
data.setNumber(((NumberTextHolder) b).getValueAsString());
}

// save the data
Expand Down Expand Up @@ -202,8 +202,8 @@ private ArrayList<BlockBase> loadBlocks(SquidCursor<ProgramData> c) {
// set data's properties
b.setLeft(data.getLeft());
b.setTop(data.getTop());
if (b instanceof NumTextHolder) {
((NumTextHolder) b).setNum(data.getNumber());
if (b instanceof NumberTextHolder) {
((NumberTextHolder) b).setValueAsString(data.getNumber());
}
// add the block to a list
blocks.add(b);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,5 @@ public class ProgramDataSpec {

// the number which a number holder block has
@ColumnSpec(defaultValue = "0")
int number;
String number;
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,7 @@

package com.pileproject.drive.execution;


import com.pileproject.drive.programming.visual.block.BlockBase;
import com.pileproject.drive.programming.visual.block.repetition.RepetitionEndBlock;
import com.pileproject.drive.programming.visual.block.repetition.WhileForeverBlock;

import java.util.ArrayList;
import java.util.Collections;
Expand All @@ -31,6 +28,8 @@
* A container class that has the condition of program execution.
*/
public class ExecutionCondition {
private static final int FOREVER_WHILE_OFFSET = -1000;

private Stack<Integer> mWhileStack;
private Stack<SelectionResult> mIfStack;
private int mBeginningOfCurrentLoop;
Expand Down Expand Up @@ -147,10 +146,29 @@ public int sizeOfSelectionResult() {
* Push the index of the beginning block of the current loop to a stack.
* @param index the index of the beginning block
*/
public void pushBeginningOfLoop(int index) {
private void pushBeginningOfLoop(int index) {
mWhileStack.push(index);
mBeginningOfCurrentLoop = index >= 0 ?
index : index - WhileForeverBlock.FOREVER_WHILE_OFFSET;
index : index - FOREVER_WHILE_OFFSET;
}

/**
* Enters infinite loop.
*/
public void enterInfiniteLoop() {
int index = getProgramCount();
pushBeginningOfLoop(index + FOREVER_WHILE_OFFSET); // push with offset
}

/**
* Enters N-times loop.
* @param count number of times this loop repeats.
*/
public void enterNTimesLoop(int count) {
int index = getProgramCount();
for (int i = 1; i < count; i++) {
pushBeginningOfLoop(index);
}
}

/**
Expand All @@ -160,7 +178,7 @@ public void reachEndOfLoop() {
if (mWhileStack.isEmpty()) return ;

int index = mWhileStack.peek() >= 0 ?
mWhileStack.peek() : mWhileStack.peek() - WhileForeverBlock.FOREVER_WHILE_OFFSET;
mWhileStack.peek() : mWhileStack.peek() - FOREVER_WHILE_OFFSET;

// the loop has already finished
if (mBeginningOfCurrentLoop != index) {
Expand Down Expand Up @@ -189,7 +207,7 @@ public void breakLoop() {
// update index
if (!mWhileStack.isEmpty()) {
mBeginningOfCurrentLoop = mWhileStack.peek() >= 0 ?
mWhileStack.peek() : mWhileStack.peek() - WhileForeverBlock.FOREVER_WHILE_OFFSET;
mWhileStack.peek() : mWhileStack.peek() - FOREVER_WHILE_OFFSET;
} else {
mBeginningOfCurrentLoop = -1;
}
Expand All @@ -199,7 +217,7 @@ public void breakLoop() {

// move to the end of the current loop
for (; mBlocks.size() >= mProgramCount; ++mProgramCount) {
if (mBlocks.get(mProgramCount).getKind() == RepetitionEndBlock.class) break;
if (mBlocks.get(mProgramCount).getKind() == BlockBase.BlockKind.REPETITION_END) break;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.pileproject.drive.app.DriveApplication;
import com.pileproject.drive.database.ProgramDataManager;
import com.pileproject.drive.programming.visual.block.BlockBase;
import com.pileproject.drive.programming.visual.block.selection.SelectionEndBlock;

/**
* A Thread class to execute program.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// Get results
int howToMake = data.getIntExtra(getString(R.string.key_block_how_to_make), BlockFactory.SEQUENCE);
String blockName = data.getStringExtra(getString(R.string.key_block_block_name));
ArrayList<BlockBase> blocks = BlockFactory.createBlocks(howToMake, blockName);
List<BlockBase> blocks = BlockFactory.createBlocks(howToMake, blockName);
mSpaceManager.addBlocks(blocks);
}
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
package com.pileproject.drive.programming.visual.block;

import android.content.Context;
import android.support.annotation.LayoutRes;
import android.view.LayoutInflater;
import android.widget.RelativeLayout;

import com.pileproject.drive.execution.ExecutionCondition;
Expand All @@ -29,10 +31,20 @@
* @version 1.0 18-June-2013
*/
public abstract class BlockBase extends RelativeLayout {
public BlockBase(Context context) {

public BlockBase(Context context, @LayoutRes int layoutRes) {
super(context);

LayoutInflater.from(context).inflate(layoutRes, this);
}

/**
* Returns kind of this block.
*
* @return {@link BlockKind}
*/
public abstract BlockKind getKind();

/**
* Action that this block does while the execution of program.
* Return delay that occurs after this action
Expand All @@ -45,10 +57,7 @@ public BlockBase(Context context) {
public abstract int action(
MachineController controller, ExecutionCondition condition);

/**
* Return class to check a program is correct or not
*
* @return Class<? extends BaseBlock>
*/
public abstract Class<? extends BlockBase> getKind();
public enum BlockKind {
SEQUENCE, SELECTION_BEGIN, SELECTION_END, REPETITION_BEGIN, REPETITION_END, REPETITION_BREAK
}
}
Loading