2015-08-22 01:08:46 +02:00
|
|
|
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<T extends Service> {
|
|
|
|
private static final int ID = -1; // currently not supported
|
2015-11-23 23:04:46 +01:00
|
|
|
private final Class<T> mServiceClass;
|
2015-08-22 01:08:46 +02:00
|
|
|
private T mServiceInstance;
|
|
|
|
private Context mContext;
|
2016-04-29 21:49:17 +02:00
|
|
|
private GBMockApplication mApplication;
|
2015-08-22 01:08:46 +02:00
|
|
|
private boolean wasStarted;
|
|
|
|
private PackageManager mPackageManager;
|
|
|
|
private NotificationManager mNotificationManager;
|
|
|
|
private MockHelper mMockHelper;
|
|
|
|
|
2016-03-20 15:00:05 +01:00
|
|
|
protected AbstractServiceTestCase(Class<T> serviceClass) {
|
2015-08-22 01:08:46 +02:00
|
|
|
mServiceClass = serviceClass;
|
|
|
|
Assert.assertNotNull(serviceClass);
|
|
|
|
}
|
|
|
|
|
|
|
|
public Context getContext() {
|
|
|
|
return mContext;
|
|
|
|
}
|
|
|
|
|
2015-08-23 00:54:28 +02:00
|
|
|
public T getServiceInstance() {
|
|
|
|
return mServiceInstance;
|
|
|
|
}
|
|
|
|
|
2016-04-29 21:49:17 +02:00
|
|
|
protected MockHelper getmMockHelper() {
|
|
|
|
return mMockHelper;
|
|
|
|
}
|
|
|
|
|
2015-08-22 01:08:46 +02:00
|
|
|
@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);
|
2015-08-23 00:54:28 +02:00
|
|
|
mServiceInstance.onCreate();
|
2015-08-22 01:08:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@After
|
|
|
|
public void tearDown() throws Exception {
|
|
|
|
if (mServiceInstance != null) {
|
|
|
|
stopService();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void startService(Intent intent) {
|
2015-08-23 00:54:28 +02:00
|
|
|
wasStarted = true;
|
2015-08-22 01:08:46 +02:00
|
|
|
mServiceInstance.onStartCommand(intent, Service.START_FLAG_REDELIVERY, ID);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void stopService() {
|
|
|
|
mServiceInstance.onDestroy();
|
|
|
|
mServiceInstance = null;
|
|
|
|
}
|
|
|
|
|
2016-04-29 21:49:17 +02:00
|
|
|
protected GBMockApplication createApplication(PackageManager packageManager) {
|
2015-08-22 01:08:46 +02:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2016-04-29 21:49:17 +02:00
|
|
|
protected T createService(Class<T> serviceClass, GBMockApplication application, NotificationManager notificationManager) throws Exception {
|
2015-08-22 01:08:46 +02:00
|
|
|
T service = mMockHelper.createService(serviceClass, application);
|
2016-04-29 21:49:17 +02:00
|
|
|
mMockHelper.addSystemServiceTo(service, Context.NOTIFICATION_SERVICE, notificationManager);
|
2015-08-22 01:08:46 +02:00
|
|
|
return service;
|
|
|
|
}
|
|
|
|
|
|
|
|
private NotificationManager getNotificationService() {
|
|
|
|
return mNotificationManager;
|
|
|
|
}
|
|
|
|
}
|