diff --git a/app/build.gradle b/app/build.gradle index 899f5c545..3ba240e94 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -31,9 +31,16 @@ android { // optional path to report (default will be lint-results.html in the builddir) htmlOutput file("$project.buildDir/reports/lint/lint.html") } + + testOptions { + unitTests.returnDefaultValues = true + } } dependencies { + testCompile 'junit:junit:4.12' + testCompile "org.mockito:mockito-core:1.9.5" + compile fileTree(dir: 'libs', include: ['*.jar']) compile 'com.android.support:appcompat-v7:21.0.3' compile 'com.android.support:support-v4:21.0.3' @@ -41,9 +48,6 @@ dependencies { compile 'org.slf4j:slf4j-api:1.7.7' compile 'com.github.PhilJay:MPAndroidChart:2.1.0' compile 'com.github.pfichtner:durationformatter:0.1.1' - - testCompile 'junit:junit:4.12' -// testCompile "org.mockito:mockito-core:1.9.5" } check.dependsOn 'findbugs', 'pmd', 'lint' diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/GBApplication.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/GBApplication.java index 51661b44b..f2ef7ae3a 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/GBApplication.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/GBApplication.java @@ -21,8 +21,8 @@ import nodomain.freeyourgadget.gadgetbridge.database.ActivityDatabaseHandler; import nodomain.freeyourgadget.gadgetbridge.database.DBHandler; import nodomain.freeyourgadget.gadgetbridge.impl.GBDeviceService; import nodomain.freeyourgadget.gadgetbridge.model.DeviceService; -import nodomain.freeyourgadget.gadgetbridge.service.DeviceCommunicationService; import nodomain.freeyourgadget.gadgetbridge.util.FileUtils; +import nodomain.freeyourgadget.gadgetbridge.util.GB; public class GBApplication extends Application { // Since this class must not log to slf4j, we use plain android.util.Log @@ -39,7 +39,7 @@ public class GBApplication extends Application { } protected DeviceService createDeviceService() { - return new GBDeviceService(this, DeviceCommunicationService.class); + return new GBDeviceService(this); } @Override @@ -55,6 +55,7 @@ public class GBApplication extends Application { // StatusPrinter.print(lc); // Logger logger = LoggerFactory.getLogger(GBApplication.class); + GB.environment = GBEnvironment.createDeviceEnvironment(); mActivityDatabaseHandler = new ActivityDatabaseHandler(context); // for testing DB stuff // SQLiteDatabase db = mActivityDatabaseHandler.getWritableDatabase(); diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/GBEnvironment.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/GBEnvironment.java new file mode 100644 index 000000000..dceebe055 --- /dev/null +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/GBEnvironment.java @@ -0,0 +1,26 @@ +package nodomain.freeyourgadget.gadgetbridge; + +public class GBEnvironment { + private boolean localTest; + private boolean deviceTest; + + public static GBEnvironment createLocalTestEnvironment() { + GBEnvironment env = new GBEnvironment(); + env.localTest = true; + return env; + } + + public static GBEnvironment createDeviceEnvironment() { + GBEnvironment env = new GBEnvironment(); + return env; + } + + public final boolean isTest() { + return localTest || deviceTest; + } + + public boolean isLocalTest() { + return localTest; + } + +} diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/impl/GBDeviceService.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/impl/GBDeviceService.java index 3bb2415ec..7dab9161e 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/impl/GBDeviceService.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/impl/GBDeviceService.java @@ -12,17 +12,18 @@ import java.util.UUID; import nodomain.freeyourgadget.gadgetbridge.model.Alarm; import nodomain.freeyourgadget.gadgetbridge.model.DeviceService; import nodomain.freeyourgadget.gadgetbridge.model.ServiceCommand; +import nodomain.freeyourgadget.gadgetbridge.service.DeviceCommunicationService; public class GBDeviceService implements DeviceService { protected final Context mContext; protected final Class mServiceClass; - public GBDeviceService(Context context, Class serviceClass) { + public GBDeviceService(Context context) { mContext = context; - mServiceClass = serviceClass; + mServiceClass = DeviceCommunicationService.class; } - private Intent createIntent() { + protected Intent createIntent() { Intent startIntent = new Intent(mContext, mServiceClass); return startIntent; } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/model/DeviceType.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/model/DeviceType.java index 1c4a906a0..04d33fb7e 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/model/DeviceType.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/model/DeviceType.java @@ -3,5 +3,5 @@ package nodomain.freeyourgadget.gadgetbridge.model; public enum DeviceType { UNKNOWN, PEBBLE, - MIBAND + TEST, MIBAND } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/DeviceCommunicationService.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/DeviceCommunicationService.java index c91200a74..61ecd2136 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/DeviceCommunicationService.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/DeviceCommunicationService.java @@ -107,10 +107,12 @@ public class DeviceCommunicationService extends Service { start(); // ensure started String btDeviceAddress = intent.getStringExtra(EXTRA_DEVICE_ADDRESS); SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this); - if (btDeviceAddress == null) { - btDeviceAddress = sharedPrefs.getString("last_device_address", null); - } else { - sharedPrefs.edit().putString("last_device_address", btDeviceAddress).apply(); + if (sharedPrefs != null) { // may be null in test cases + if (btDeviceAddress == null) { + btDeviceAddress = sharedPrefs.getString("last_device_address", null); + } else { + sharedPrefs.edit().putString("last_device_address", btDeviceAddress).apply(); + } } if (btDeviceAddress != null && !isConnecting() && !isConnected()) { diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/DeviceSupportFactory.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/DeviceSupportFactory.java index bd3b1bfb3..47530d2f6 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/DeviceSupportFactory.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/DeviceSupportFactory.java @@ -5,6 +5,7 @@ import android.bluetooth.BluetoothDevice; import android.content.Context; import android.widget.Toast; +import java.lang.reflect.Constructor; import java.util.EnumSet; import nodomain.freeyourgadget.gadgetbridge.GBException; @@ -26,10 +27,17 @@ public class DeviceSupportFactory { public synchronized DeviceSupport createDeviceSupport(String deviceAddress) throws GBException { DeviceSupport deviceSupport; - if (deviceAddress.indexOf(":") == deviceAddress.lastIndexOf(":")) { // only one colon - deviceSupport = createTCPDeviceSupport(deviceAddress); + int indexFirstColon = deviceAddress.indexOf(":"); + if (indexFirstColon > 0) { + if (indexFirstColon == deviceAddress.lastIndexOf(":")) { // only one colon + deviceSupport = createTCPDeviceSupport(deviceAddress); + } else { + // multiple colons -- bt? + deviceSupport = createBTDeviceSupport(deviceAddress); + } } else { - deviceSupport = createBTDeviceSupport(deviceAddress); + // no colon at all, maybe a class name? + deviceSupport = createClassNameDeviceSupport(deviceAddress); } if (deviceSupport != null) { @@ -41,6 +49,21 @@ public class DeviceSupportFactory { return null; } + private DeviceSupport createClassNameDeviceSupport(String className) throws GBException { + try { + Class deviceSupportClass = Class.forName(className); + Constructor constructor = deviceSupportClass.getConstructor(); + DeviceSupport support = (DeviceSupport) constructor.newInstance(); + // has to create the device itself + support.setContext(null, null, mContext); + return support; + } catch (ClassNotFoundException e) { + return null; // not a class, or not known at least + } catch (Exception e) { + throw new GBException("Error creating DeviceSupport instance for " + className, e); + } + } + private void checkBtAvailability() { if (mBtAdapter == null) { GB.toast(mContext.getString(R.string.bluetooth_is_not_supported_), Toast.LENGTH_SHORT, GB.WARN); diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/util/GB.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/util/GB.java index 1b5437b76..ab3f0fddc 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/util/GB.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/util/GB.java @@ -24,6 +24,7 @@ import java.nio.ByteBuffer; import java.nio.ByteOrder; import nodomain.freeyourgadget.gadgetbridge.GBApplication; +import nodomain.freeyourgadget.gadgetbridge.GBEnvironment; import nodomain.freeyourgadget.gadgetbridge.R; import nodomain.freeyourgadget.gadgetbridge.activities.ControlCenter; import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventScreenshot; @@ -42,6 +43,7 @@ public class GB { public static final int INFO = 1; public static final int WARN = 2; public static final int ERROR = 3; + public static GBEnvironment environment; public static Notification createNotification(String text, Context context) { Intent notificationIntent = new Intent(context, ControlCenter.class); @@ -224,6 +226,9 @@ public class GB { * @param ex optional exception to be logged */ public static void toast(final Context context, final String message, final int displayTime, final int severity, final Throwable ex) { + if (env().isLocalTest()) { + return; + } Looper mainLooper = Looper.getMainLooper(); if (Thread.currentThread() == mainLooper.getThread()) { log(message, severity, ex); @@ -291,4 +296,8 @@ public class GB { NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); nm.notify(NOTIFICATION_ID_INSTALL, notification); } + + public static GBEnvironment env() { + return environment; + } } diff --git a/app/src/test/java/nodomain/freeyourgadget/gadgetbridge/service/AbstractServiceTestCase.java b/app/src/test/java/nodomain/freeyourgadget/gadgetbridge/service/AbstractServiceTestCase.java new file mode 100644 index 000000000..caacc520b --- /dev/null +++ b/app/src/test/java/nodomain/freeyourgadget/gadgetbridge/service/AbstractServiceTestCase.java @@ -0,0 +1,95 @@ +package nodomain.freeyourgadget.gadgetbridge.service; + +import android.app.Application; +import android.app.NotificationManager; +import android.app.Service; +import android.content.Context; +import android.content.Intent; +import android.content.pm.PackageManager; + +import junit.framework.Assert; + +import org.junit.After; +import org.junit.Before; + +import nodomain.freeyourgadget.gadgetbridge.test.GBMockApplication; +import nodomain.freeyourgadget.gadgetbridge.test.GBMockContext; +import nodomain.freeyourgadget.gadgetbridge.test.GBMockPackageManager; +import nodomain.freeyourgadget.gadgetbridge.test.MockHelper; + +public abstract class AbstractServiceTestCase { + private static final int ID = -1; // currently not supported + private Class mServiceClass; + private T mServiceInstance; + private Context mContext; + private Application mApplication; + private boolean wasStarted; + private PackageManager mPackageManager; + private NotificationManager mNotificationManager; + private MockHelper mMockHelper; + + protected AbstractServiceTestCase(Class serviceClass) { + mServiceClass = serviceClass; + Assert.assertNotNull(serviceClass); + } + + public Context getContext() { + return mContext; + } + + @Before + public void setUp() throws Exception { + mMockHelper = new MockHelper(); + mPackageManager = createPackageManager(); + mApplication = createApplication(mPackageManager); + mContext = createContext(mApplication); + mNotificationManager = mMockHelper.createNotificationManager(mContext); + mServiceInstance = createService(mServiceClass, mApplication, mNotificationManager); + } + + @After + public void tearDown() throws Exception { + if (mServiceInstance != null) { + stopService(); + } + } + + public void startService(Intent intent) { + if (!wasStarted) { + wasStarted = true; + mServiceInstance.onCreate(); + } + mServiceInstance.onStartCommand(intent, Service.START_FLAG_REDELIVERY, ID); + } + + public void stopService() { + mServiceInstance.onDestroy(); + mServiceInstance = null; + } + + protected Application createApplication(PackageManager packageManager) { + return new GBMockApplication(packageManager); + } + + protected PackageManager createPackageManager() { + return new GBMockPackageManager(); + } + + protected Application getApplication() { + return mApplication; + } + + protected Context createContext(final Application application) { + return new GBMockContext(application); + } + + private T createService(Class serviceClass, Application application, NotificationManager notificationManager) throws Exception { + T service = mMockHelper.createService(serviceClass, application); + mMockHelper.addSystemServiceTo(service, Context.NOTIFICATION_SERVICE, getNotificationService()); + return service; + } + + private NotificationManager getNotificationService() { + return mNotificationManager; + } +} diff --git a/app/src/test/java/nodomain/freeyourgadget/gadgetbridge/service/DeviceCommunicationServiceTestCase.java b/app/src/test/java/nodomain/freeyourgadget/gadgetbridge/service/DeviceCommunicationServiceTestCase.java new file mode 100644 index 000000000..209fa5161 --- /dev/null +++ b/app/src/test/java/nodomain/freeyourgadget/gadgetbridge/service/DeviceCommunicationServiceTestCase.java @@ -0,0 +1,37 @@ +package nodomain.freeyourgadget.gadgetbridge.service; + +import android.test.ServiceTestCase; + +import junit.framework.TestCase; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; + +import nodomain.freeyourgadget.gadgetbridge.test.TestDeviceSupport; + +public class DeviceCommunicationServiceTestCase extends AbstractServiceTestCase { + private static final java.lang.String TEST_DEVICE_ADDRESS = TestDeviceSupport.class.getName(); + + private TestDeviceService mDeviceService; + + public DeviceCommunicationServiceTestCase() { + super(DeviceCommunicationService.class); + } + + @Before + public void setUp() throws Exception { + super.setUp(); + mDeviceService = new TestDeviceService(this); + } + + @Test + public void testStart() { + mDeviceService.start(); + } + + @Test + public void testConnect() { + mDeviceService.connect(TEST_DEVICE_ADDRESS); + } +} diff --git a/app/src/test/java/nodomain/freeyourgadget/gadgetbridge/service/TestDeviceService.java b/app/src/test/java/nodomain/freeyourgadget/gadgetbridge/service/TestDeviceService.java new file mode 100644 index 000000000..ad4a11bef --- /dev/null +++ b/app/src/test/java/nodomain/freeyourgadget/gadgetbridge/service/TestDeviceService.java @@ -0,0 +1,25 @@ +package nodomain.freeyourgadget.gadgetbridge.service; + +import android.content.Intent; + +import nodomain.freeyourgadget.gadgetbridge.impl.GBDeviceService; +import nodomain.freeyourgadget.gadgetbridge.test.GBMockIntent; + +public class TestDeviceService extends GBDeviceService { + private final AbstractServiceTestCase mTestCase; + + public TestDeviceService(AbstractServiceTestCase testCase) throws Exception { + super(testCase.getContext()); + mTestCase = testCase; + } + + @Override + protected Intent createIntent() { + return new GBMockIntent(); + } + + @Override + protected void invokeService(Intent intent) { + mTestCase.startService(intent); + } +} diff --git a/app/src/test/java/nodomain/freeyourgadget/gadgetbridge/test/GBMockApplication.java b/app/src/test/java/nodomain/freeyourgadget/gadgetbridge/test/GBMockApplication.java new file mode 100644 index 000000000..b9f16b123 --- /dev/null +++ b/app/src/test/java/nodomain/freeyourgadget/gadgetbridge/test/GBMockApplication.java @@ -0,0 +1,27 @@ +package nodomain.freeyourgadget.gadgetbridge.test; + +import android.content.Context; +import android.content.pm.PackageManager; +import android.test.mock.MockApplication; + +import nodomain.freeyourgadget.gadgetbridge.GBEnvironment; +import nodomain.freeyourgadget.gadgetbridge.util.GB; + +public class GBMockApplication extends MockApplication { + private final PackageManager mPackageManager; + + public GBMockApplication(PackageManager packageManager) { + GB.environment = GBEnvironment.createDeviceEnvironment().createLocalTestEnvironment(); + mPackageManager = packageManager; + } + + @Override + public Context getApplicationContext() { + return this; + } + @Override + public PackageManager getPackageManager() { + return mPackageManager; + } + +} diff --git a/app/src/test/java/nodomain/freeyourgadget/gadgetbridge/test/GBMockContext.java b/app/src/test/java/nodomain/freeyourgadget/gadgetbridge/test/GBMockContext.java new file mode 100644 index 000000000..ce7d53326 --- /dev/null +++ b/app/src/test/java/nodomain/freeyourgadget/gadgetbridge/test/GBMockContext.java @@ -0,0 +1,24 @@ +package nodomain.freeyourgadget.gadgetbridge.test; + +import android.app.Application; +import android.content.Context; +import android.content.pm.PackageManager; +import android.test.mock.MockContext; + +public class GBMockContext extends MockContext { + private final Application mApplication; + + public GBMockContext(Application application) { + mApplication = application; + } + + @Override + public Context getApplicationContext() { + return mApplication; + } + + @Override + public PackageManager getPackageManager() { + return mApplication.getPackageManager(); + } +} diff --git a/app/src/test/java/nodomain/freeyourgadget/gadgetbridge/test/GBMockIntent.java b/app/src/test/java/nodomain/freeyourgadget/gadgetbridge/test/GBMockIntent.java new file mode 100644 index 000000000..a6bb9857a --- /dev/null +++ b/app/src/test/java/nodomain/freeyourgadget/gadgetbridge/test/GBMockIntent.java @@ -0,0 +1,387 @@ +package nodomain.freeyourgadget.gadgetbridge.test; + +import android.content.Intent; +import android.os.Bundle; +import android.os.Parcelable; +import android.support.annotation.NonNull; + +import java.io.Serializable; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Map; + +public class GBMockIntent extends Intent { + private String mAction; + private Map extras = new HashMap<>(); + + @NonNull + @Override + public Intent setAction(String action) { + mAction = action; + return this; + } + + @Override + public String getAction() { + return mAction; + } + + @NonNull + @Override + public Intent putExtra(String name, boolean value) { + extras.put(name, value); + return this; + } + + @NonNull + @Override + public Intent putExtra(String name, byte value) { + extras.put(name, value); + return this; + } + + @NonNull + @Override + public Intent putExtra(String name, char value) { + extras.put(name, value); + return this; + } + + @NonNull + @Override + public Intent putExtra(String name, short value) { + extras.put(name, value); + return this; + } + + @NonNull + @Override + public Intent putExtra(String name, int value) { + extras.put(name, value); + return this; + } + + @NonNull + @Override + public Intent putExtra(String name, long value) { + extras.put(name, value); + return this; + } + + @NonNull + @Override + public Intent putExtra(String name, float value) { + extras.put(name, value); + return this; + } + + @NonNull + @Override + public Intent putExtra(String name, double value) { + extras.put(name, value); + return this; + } + + @NonNull + @Override + public Intent putExtra(String name, String value) { + extras.put(name, value); + return this; + } + + @NonNull + @Override + public Intent putExtra(String name, CharSequence value) { + extras.put(name, value); + return this; + } + + @NonNull + @Override + public Intent putExtra(String name, Parcelable value) { + extras.put(name, value); + return this; + } + + @NonNull + @Override + public Intent putExtra(String name, Parcelable[] value) { + extras.put(name, value); + return this; + } + + @Override + public Intent putParcelableArrayListExtra(String name, ArrayList value) { + extras.put(name, value); + return this; + } + + @NonNull + @Override + public Intent putIntegerArrayListExtra(String name, ArrayList value) { + extras.put(name, value); + return this; + } + + @NonNull + @Override + public Intent putStringArrayListExtra(String name, ArrayList value) { + extras.put(name, value); + return this; + } + + @NonNull + @Override + public Intent putCharSequenceArrayListExtra(String name, ArrayList value) { + extras.put(name, value); + return this; + } + + @NonNull + @Override + public Intent putExtra(String name, Serializable value) { + extras.put(name, value); + return this; + } + + @NonNull + @Override + public Intent putExtra(String name, boolean[] value) { + extras.put(name, value); + return this; + } + + @NonNull + @Override + public Intent putExtra(String name, byte[] value) { + extras.put(name, value); + return this; + } + + @NonNull + @Override + public Intent putExtra(String name, short[] value) { + extras.put(name, value); + return this; + } + + @NonNull + @Override + public Intent putExtra(String name, char[] value) { + extras.put(name, value); + return this; + } + + @NonNull + @Override + public Intent putExtra(String name, int[] value) { + extras.put(name, value); + return this; + } + + @NonNull + @Override + public Intent putExtra(String name, long[] value) { + extras.put(name, value); + return this; + } + + @NonNull + @Override + public Intent putExtra(String name, float[] value) { + extras.put(name, value); + return this; + } + + @NonNull + @Override + public Intent putExtra(String name, double[] value) { + extras.put(name, value); + return this; + } + + @NonNull + @Override + public Intent putExtra(String name, String[] value) { + extras.put(name, value); + return this; + } + + @NonNull + @Override + public Intent putExtra(String name, CharSequence[] value) { + extras.put(name, value); + return this; + } + + @NonNull + @Override + public Intent putExtra(String name, Bundle value) { + extras.put(name, value); + return this; + } + + @Override + public boolean getBooleanExtra(String name, boolean defaultValue) { + if (extras.containsKey(name)) { + return (boolean) extras.get(name); + } + return defaultValue; + } + + @Override + public byte getByteExtra(String name, byte defaultValue) { + if (extras.containsKey(name)) { + return (byte) extras.get(name); + } + return defaultValue; + } + + @Override + public short getShortExtra(String name, short defaultValue) { + if (extras.containsKey(name)) { + return (short) extras.get(name); + } + return defaultValue; + } + + @Override + public char getCharExtra(String name, char defaultValue) { + if (extras.containsKey(name)) { + return (char) extras.get(name); + } + return defaultValue; + } + + @Override + public int getIntExtra(String name, int defaultValue) { + if (extras.containsKey(name)) { + return (int) extras.get(name); + } + return defaultValue; + } + + @Override + public long getLongExtra(String name, long defaultValue) { + if (extras.containsKey(name)) { + return (long) extras.get(name); + } + return defaultValue; + } + + @Override + public float getFloatExtra(String name, float defaultValue) { + if (extras.containsKey(name)) { + return (float) extras.get(name); + } + return defaultValue; + } + + @Override + public double getDoubleExtra(String name, double defaultValue) { + if (extras.containsKey(name)) { + return (double) extras.get(name); + } + return defaultValue; + } + + @Override + public CharSequence getCharSequenceExtra(String name) { + return (CharSequence) extras.get(name); + } + + @Override + public T getParcelableExtra(String name) { + return (T) extras.get(name); + } + + @Override + public Parcelable[] getParcelableArrayExtra(String name) { + return (Parcelable[]) extras.get(name); + } + + @Override + public ArrayList getParcelableArrayListExtra(String name) { + return (ArrayList) extras.get(name); + } + + @Override + public Serializable getSerializableExtra(String name) { + return (Serializable) extras.get(name); + } + + @Override + public ArrayList getIntegerArrayListExtra(String name) { + return (ArrayList) extras.get(name); + } + + @Override + public ArrayList getStringArrayListExtra(String name) { + return (ArrayList) extras.get(name); + } + + @Override + public ArrayList getCharSequenceArrayListExtra(String name) { + return (ArrayList) extras.get(name); + } + + @Override + public boolean[] getBooleanArrayExtra(String name) { + return (boolean[]) extras.get(name); + } + + @Override + public byte[] getByteArrayExtra(String name) { + return (byte[]) extras.get(name); + } + + @Override + public short[] getShortArrayExtra(String name) { + return (short[]) extras.get(name); + } + + @Override + public char[] getCharArrayExtra(String name) { + return (char[]) extras.get(name); + } + + @Override + public int[] getIntArrayExtra(String name) { + return (int[]) extras.get(name); + } + + @Override + public long[] getLongArrayExtra(String name) { + return (long[]) extras.get(name); + } + + @Override + public float[] getFloatArrayExtra(String name) { + return (float[]) extras.get(name); + } + + @Override + public double[] getDoubleArrayExtra(String name) { + return (double[]) extras.get(name); + } + + @Override + public String[] getStringArrayExtra(String name) { + return (String[]) extras.get(name); + } + + @Override + public CharSequence[] getCharSequenceArrayExtra(String name) { + return (CharSequence[]) extras.get(name); + } + + @Override + public String getStringExtra(String name) { + return (String) extras.get(name); + } + + @Override + public String toString() { + return "GBMockIntent: " + mAction; + } +} diff --git a/app/src/test/java/nodomain/freeyourgadget/gadgetbridge/test/GBMockPackageManager.java b/app/src/test/java/nodomain/freeyourgadget/gadgetbridge/test/GBMockPackageManager.java new file mode 100644 index 000000000..7799532d8 --- /dev/null +++ b/app/src/test/java/nodomain/freeyourgadget/gadgetbridge/test/GBMockPackageManager.java @@ -0,0 +1,11 @@ +package nodomain.freeyourgadget.gadgetbridge.test; + +import android.content.ComponentName; +import android.test.mock.MockPackageManager; + +public class GBMockPackageManager extends MockPackageManager { + @Override + public void setComponentEnabledSetting(ComponentName componentName, int newState, int flags) { + // do nothing + } +} diff --git a/app/src/test/java/nodomain/freeyourgadget/gadgetbridge/test/MockHelper.java b/app/src/test/java/nodomain/freeyourgadget/gadgetbridge/test/MockHelper.java new file mode 100644 index 000000000..9461cab1d --- /dev/null +++ b/app/src/test/java/nodomain/freeyourgadget/gadgetbridge/test/MockHelper.java @@ -0,0 +1,35 @@ +package nodomain.freeyourgadget.gadgetbridge.test; + +import android.app.Application; +import android.app.NotificationManager; +import android.app.Service; +import android.content.Context; + +import junit.framework.Assert; + +import org.mockito.Mockito; + +import java.lang.reflect.Constructor; + +public class MockHelper { + public NotificationManager createNotificationManager(Context mContext) throws Exception { + Constructor[] constructors = NotificationManager.class.getDeclaredConstructors(); + constructors[0].setAccessible(true); + Class[] parameterTypes = constructors[0].getParameterTypes(); + return (NotificationManager) constructors[0].newInstance(); + } + + public T createService(Class serviceClass, Application application) throws Exception { + Constructor constructor = serviceClass.getConstructor(); + Assert.assertNotNull(constructor); + T realService = constructor.newInstance(); + T mockedService = Mockito.spy(realService); + Mockito.when(mockedService.getApplicationContext()).thenReturn(application); + Mockito.when(mockedService.getPackageManager()).thenReturn(application.getPackageManager()); + return mockedService; + } + + public void addSystemServiceTo(Context context, String serviceName, Object service) { + Mockito.when(context.getSystemService(serviceName)).thenReturn(service); + } +} diff --git a/app/src/test/java/nodomain/freeyourgadget/gadgetbridge/test/TestDeviceSupport.java b/app/src/test/java/nodomain/freeyourgadget/gadgetbridge/test/TestDeviceSupport.java new file mode 100644 index 000000000..625904184 --- /dev/null +++ b/app/src/test/java/nodomain/freeyourgadget/gadgetbridge/test/TestDeviceSupport.java @@ -0,0 +1,122 @@ +package nodomain.freeyourgadget.gadgetbridge.test; + +import android.bluetooth.BluetoothAdapter; +import android.content.Context; +import android.net.Uri; +import android.support.annotation.Nullable; + +import java.util.ArrayList; +import java.util.UUID; + +import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice; +import nodomain.freeyourgadget.gadgetbridge.model.Alarm; +import nodomain.freeyourgadget.gadgetbridge.model.DeviceType; +import nodomain.freeyourgadget.gadgetbridge.model.ServiceCommand; +import nodomain.freeyourgadget.gadgetbridge.service.AbstractDeviceSupport; + +public class TestDeviceSupport extends AbstractDeviceSupport { + + public TestDeviceSupport() { + } + + @Override + public void setContext(GBDevice gbDevice, BluetoothAdapter btAdapter, Context context) { + gbDevice = new GBDevice(getClass().getName(), "Test Device", DeviceType.TEST); + super.setContext(gbDevice, btAdapter, context); + } + + @Override + public boolean connect() { + return false; + } + + @Override + public void dispose() { + + } + + @Override + public boolean useAutoConnect() { + return false; + } + + @Override + public void pair() { + + } + + @Override + public void onSMS(String from, String body) { + + } + + @Override + public void onEmail(String from, String subject, String body) { + + } + + @Override + public void onGenericNotification(String title, String details) { + + } + + @Override + public void onSetTime() { + + } + + @Override + public void onSetAlarms(ArrayList alarms) { + + } + + @Override + public void onSetCallState(@Nullable String number, @Nullable String name, ServiceCommand command) { + + } + + @Override + public void onSetMusicInfo(String artist, String album, String track) { + + } + + @Override + public void onInstallApp(Uri uri) { + + } + + @Override + public void onAppInfoReq() { + + } + + @Override + public void onAppStart(UUID uuid) { + + } + + @Override + public void onAppDelete(UUID uuid) { + + } + + @Override + public void onFetchActivityData() { + + } + + @Override + public void onReboot() { + + } + + @Override + public void onFindDevice(boolean start) { + + } + + @Override + public void onScreenshotReq() { + + } +}