Skip to content

Commit

Permalink
[*] code reformatted
Browse files Browse the repository at this point in the history
  • Loading branch information
nisrulz committed Jun 24, 2016
1 parent e5551ce commit 6ff8c50
Show file tree
Hide file tree
Showing 14 changed files with 682 additions and 724 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
* <a href="http://d.android.com/tools/testing/testing_android.html">Testing Fundamentals</a>
*/
public class ApplicationTest extends ApplicationTestCase<Application> {
public ApplicationTest() {
super(Application.class);
}
public ApplicationTest() {
super(Application.class);
}
}
7 changes: 4 additions & 3 deletions library/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.github.nisrulz.sensey">

<application android:allowBackup="true"
android:supportsRtl="true">
<application
android:allowBackup="true"
android:supportsRtl="true">

</application>
</application>

</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,11 @@ public class OrientationDetector {
private final int ORIENTATION_PORTRAIT_REVERSE = ExifInterface.ORIENTATION_ROTATE_270; // 8
private final int smoothness = 1;
private final OrientationListener orientationListener;
private final float[] pitches;
private final float[] rolls;
private float averagePitch = 0;
private float averageRoll = 0;
private int orientation = ORIENTATION_PORTRAIT;

private final float[] pitches;
private final float[] rolls;
final SensorEventListener sensorEventListener = new SensorEventListener() {
float[] mGravity;
float[] mGeomagnetic;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,6 @@ public class ProximityDetector {

private final float threshold;
private final ProximityListener proximityListener;

public ProximityDetector(ProximityListener proximityListener) {
this.proximityListener = proximityListener;
this.threshold = (float) 3;
}

final SensorEventListener sensorEventListener = new SensorEventListener() {
@Override public void onSensorChanged(SensorEvent sensorEvent) {
if (sensorEvent.sensor.getType() == Sensor.TYPE_PROXIMITY) {
Expand All @@ -47,6 +41,11 @@ public ProximityDetector(ProximityListener proximityListener) {
}
};

public ProximityDetector(ProximityListener proximityListener) {
this.proximityListener = proximityListener;
this.threshold = (float) 3;
}

public interface ProximityListener {
void onNear();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,11 @@

public class ShakeDetector {

private final ShakeListener shakeListener;
private final int threshold;
private float mAccel;
private float mAccelCurrent;
private float mAccelLast;

private final ShakeListener shakeListener;
private final int threshold;
final SensorEventListener sensorEventListener = new SensorEventListener() {
@Override public void onSensorChanged(SensorEvent sensorEvent) {
float[] mGravity = sensorEvent.values.clone();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,14 @@

public class TouchTypeDetector {

//gesture detector
private GestureDetectorCompat gDetect;

private TouchTypListener touchTypListener;
final GestureListener listener = new GestureListener();

public static final int SCROLL_DIR_UP = 1;
public static final int SCROLL_DIR_RIGHT = 2;
public static final int SCROLL_DIR_DOWN = 3;
public static final int SCROLL_DIR_LEFT = 4;
final GestureListener listener = new GestureListener();
//gesture detector
private GestureDetectorCompat gDetect;
private TouchTypListener touchTypListener;

public TouchTypeDetector(Context context, TouchTypListener touchTypListener) {
gDetect = new GestureDetectorCompat(context, listener);
Expand All @@ -44,6 +42,20 @@ void onTouchEvent(MotionEvent event) {
gDetect.onTouchEvent(event);
}

public interface TouchTypListener {
void onDoubleTap();

void onScroll(int scroll_dir);

void onSingleTap();

void onSwipeLeft();

void onSwipeRight();

void onLongPress();
}

class GestureListener extends GestureDetector.SimpleOnGestureListener {
private static final int SWIPE_MIN_DISTANCE = 120;
private static final int SWIPE_MAX_OFF_PATH = 250;
Expand Down Expand Up @@ -120,18 +132,4 @@ public boolean onScroll(MotionEvent startevent, MotionEvent finishevent, float d
return super.onSingleTapUp(e);
}
}

public interface TouchTypListener {
void onDoubleTap();

void onScroll(int scroll_dir);

void onSingleTap();

void onSwipeLeft();

void onSwipeRight();

void onLongPress();
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.github.nisrulz.sensey;

import com.github.nisrulz.sensey.FlipDetector.FlipListener;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
Expand All @@ -13,62 +12,61 @@
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;

@RunWith(MockitoJUnitRunner.class)
public class FlipDetectorTest {
@RunWith(MockitoJUnitRunner.class) public class FlipDetectorTest {

@Mock private FlipListener mockListener;
private FlipDetector testFlipDetector;
@Mock private FlipListener mockListener;
private FlipDetector testFlipDetector;

@Before
public void setUp() {
testFlipDetector = new FlipDetector(mockListener);
}
@Before public void setUp() {
testFlipDetector = new FlipDetector(mockListener);
}

@Test
public void detectFlipWithMiddleFaceUpValue() {
testFlipDetector.sensorEventListener.onSensorChanged(testAccelerometerEvent(new float[]{0, 0, 9.5f}));
verify(mockListener, only()).onFaceUp();
}
@Test public void detectFlipWithMiddleFaceUpValue() {
testFlipDetector.sensorEventListener.onSensorChanged(
testAccelerometerEvent(new float[] { 0, 0, 9.5f }));
verify(mockListener, only()).onFaceUp();
}

@Test
public void notDetectFlipWithMinFaceUpValue() {
testFlipDetector.sensorEventListener.onSensorChanged(testAccelerometerEvent(new float[]{0, 0, 9}));
verifyNoMoreInteractions(mockListener);
}
@Test public void notDetectFlipWithMinFaceUpValue() {
testFlipDetector.sensorEventListener.onSensorChanged(
testAccelerometerEvent(new float[] { 0, 0, 9 }));
verifyNoMoreInteractions(mockListener);
}

@Test
public void notDetectFlipWithMaxFaceUpValue() {
testFlipDetector.sensorEventListener.onSensorChanged(testAccelerometerEvent(new float[]{0, 0, 10}));
verifyNoMoreInteractions(mockListener);
}
@Test public void notDetectFlipWithMaxFaceUpValue() {
testFlipDetector.sensorEventListener.onSensorChanged(
testAccelerometerEvent(new float[] { 0, 0, 10 }));
verifyNoMoreInteractions(mockListener);
}

@Test
public void detectFlipWithMiddleFaceDownValue() {
testFlipDetector.sensorEventListener.onSensorChanged(testAccelerometerEvent(new float[]{0, 0, -9.5f}));
verify(mockListener, only()).onFaceDown();
}
@Test public void detectFlipWithMiddleFaceDownValue() {
testFlipDetector.sensorEventListener.onSensorChanged(
testAccelerometerEvent(new float[] { 0, 0, -9.5f }));
verify(mockListener, only()).onFaceDown();
}

@Test
public void notDetectFlipWithMinFaceDownValue() {
testFlipDetector.sensorEventListener.onSensorChanged(testAccelerometerEvent(new float[]{0, 0, -10}));
verifyNoMoreInteractions(mockListener);
}
@Test public void notDetectFlipWithMinFaceDownValue() {
testFlipDetector.sensorEventListener.onSensorChanged(
testAccelerometerEvent(new float[] { 0, 0, -10 }));
verifyNoMoreInteractions(mockListener);
}

@Test
public void notDetectFlipWithMaxFaceDownValue() {
testFlipDetector.sensorEventListener.onSensorChanged(testAccelerometerEvent(new float[]{0, 0, -9}));
verifyNoMoreInteractions(mockListener);
}
@Test public void notDetectFlipWithMaxFaceDownValue() {
testFlipDetector.sensorEventListener.onSensorChanged(
testAccelerometerEvent(new float[] { 0, 0, -9 }));
verifyNoMoreInteractions(mockListener);
}

@Test
public void notDetectFlipWithOtherValue() {
testFlipDetector.sensorEventListener.onSensorChanged(testAccelerometerEvent(new float[]{0, 0, 0}));
verifyNoMoreInteractions(mockListener);
}
@Test public void notDetectFlipWithOtherValue() {
testFlipDetector.sensorEventListener.onSensorChanged(
testAccelerometerEvent(new float[] { 0, 0, 0 }));
verifyNoMoreInteractions(mockListener);
}

@Test(expected = ArrayIndexOutOfBoundsException.class)
public void exceptionWithArrayLessThenThreeElements() {
testFlipDetector.sensorEventListener.onSensorChanged(testAccelerometerEvent(new float[]{0, 0}));
verifyNoMoreInteractions(mockListener);
}
@Test(expected = ArrayIndexOutOfBoundsException.class)
public void exceptionWithArrayLessThenThreeElements() {
testFlipDetector.sensorEventListener.onSensorChanged(
testAccelerometerEvent(new float[] { 0, 0 }));
verifyNoMoreInteractions(mockListener);
}
}
120 changes: 55 additions & 65 deletions library/src/test/java/com/github/nisrulz/sensey/LightDetectorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

import android.hardware.Sensor;
import android.hardware.SensorEvent;

import com.github.nisrulz.sensey.LightDetector.LightListener;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
Expand All @@ -14,67 +12,59 @@
import static org.mockito.Mockito.only;
import static org.mockito.Mockito.verify;

@RunWith(MockitoJUnitRunner.class)
public class LightDetectorTest {

@Mock private LightListener mockListener;

@Test
public void detectOnDarkWithLuxLessThanDefaultThreshold() {
testDetector().sensorEventListener.onSensorChanged(testLightEvent(new float[]{1}));
verify(mockListener, only()).onDark();
}

@Test
public void detectOnLightWithLuxMoreThanDefaultThreshold() {
testDetector().sensorEventListener.onSensorChanged(testLightEvent(new float[]{10}));
verify(mockListener, only()).onLight();
}

@Test
public void detectOnLightWithLuxEqualsToDefaultThreshold() {
testDetector().sensorEventListener.onSensorChanged(testLightEvent(new float[]{3}));
verify(mockListener, only()).onLight();
}

@Test
public void detectOnDarkWithLuxLessThanCustomThreshold() {
testDetector(9).sensorEventListener.onSensorChanged(testLightEvent(new float[]{3}));
verify(mockListener, only()).onDark();
}

@Test
public void detectOnLightWithLuxMoreThanCustomThreshold() {
testDetector(9).sensorEventListener.onSensorChanged(testLightEvent(new float[]{12}));
verify(mockListener, only()).onLight();
}

@Test
public void detectOnLightWithLuxEqualsToCustomThreshold() {
testDetector(9).sensorEventListener.onSensorChanged(testLightEvent(new float[]{9}));
verify(mockListener, only()).onLight();
}

@Test
public void detectOnLightWithExtraValues() {
testDetector().sensorEventListener.onSensorChanged(testLightEvent(new float[]{10, 0, 43, 3, -423}));
verify(mockListener, only()).onLight();
}

@Test(expected = ArrayIndexOutOfBoundsException.class)
public void exceptionWithEmptyValues() {
testDetector().sensorEventListener.onSensorChanged(testLightEvent(new float[]{}));
}

private LightDetector testDetector() {
return new LightDetector(mockListener);
}

private LightDetector testDetector(float threshold) {
return new LightDetector(threshold, mockListener);
}

private SensorEvent testLightEvent(float[] values) {
return testSensorEvent(values, Sensor.TYPE_LIGHT);
}
@RunWith(MockitoJUnitRunner.class) public class LightDetectorTest {

@Mock private LightListener mockListener;

@Test public void detectOnDarkWithLuxLessThanDefaultThreshold() {
testDetector().sensorEventListener.onSensorChanged(testLightEvent(new float[] { 1 }));
verify(mockListener, only()).onDark();
}

@Test public void detectOnLightWithLuxMoreThanDefaultThreshold() {
testDetector().sensorEventListener.onSensorChanged(testLightEvent(new float[] { 10 }));
verify(mockListener, only()).onLight();
}

@Test public void detectOnLightWithLuxEqualsToDefaultThreshold() {
testDetector().sensorEventListener.onSensorChanged(testLightEvent(new float[] { 3 }));
verify(mockListener, only()).onLight();
}

@Test public void detectOnDarkWithLuxLessThanCustomThreshold() {
testDetector(9).sensorEventListener.onSensorChanged(testLightEvent(new float[] { 3 }));
verify(mockListener, only()).onDark();
}

@Test public void detectOnLightWithLuxMoreThanCustomThreshold() {
testDetector(9).sensorEventListener.onSensorChanged(testLightEvent(new float[] { 12 }));
verify(mockListener, only()).onLight();
}

@Test public void detectOnLightWithLuxEqualsToCustomThreshold() {
testDetector(9).sensorEventListener.onSensorChanged(testLightEvent(new float[] { 9 }));
verify(mockListener, only()).onLight();
}

@Test public void detectOnLightWithExtraValues() {
testDetector().sensorEventListener.onSensorChanged(
testLightEvent(new float[] { 10, 0, 43, 3, -423 }));
verify(mockListener, only()).onLight();
}

@Test(expected = ArrayIndexOutOfBoundsException.class) public void exceptionWithEmptyValues() {
testDetector().sensorEventListener.onSensorChanged(testLightEvent(new float[] {}));
}

private LightDetector testDetector() {
return new LightDetector(mockListener);
}

private LightDetector testDetector(float threshold) {
return new LightDetector(threshold, mockListener);
}

private SensorEvent testLightEvent(float[] values) {
return testSensorEvent(values, Sensor.TYPE_LIGHT);
}
}
Loading

0 comments on commit 6ff8c50

Please sign in to comment.