I want to unit test my own query calls (CRUD) to the Datastore API on Android.
Robolectric
I tried using Robolectric. This is part of my test code:
TestActivity activity;
@Before
public void setup() {
this.activity = Robolectric.buildActivity(TestActivity.class).create().get();
}
@Test
public void shouldHaveHappySmiles() throws Exception {
assertTrue(activity.getAccount().isLinked());
}
I initialize the instance in my TestActivity which is inside my app code. Here the is important code part:
import com.dropbox.sync.android.DbxAccount;
import com.dropbox.sync.android.DbxAccountManager;
public class TestActivity extends Activity {
private DbxAccountManager mDbxAcctMgr;
private DbxAccount mAccount;
public DbxAccount getAccount() {
mDbxAcctMgr = DbxAccountManager.getInstance(getApplicationContext(), "xxx", "zzz");
mAccount = DropboxUtils.getLinkedAccount(mDbxAcctMgr);
return mAccount;
}
}
This is the line where it falls over:
DbxAccountManager.getInstance(getApplicationContext(), "xxx", "zzz");
The stacktrace and error message are:
com.dropbox.sync.android.DbxRuntimeException$BadState: Required Sync API Activity isn't included in application manifest: com.dropbox.client2.android.AuthActivity, com.dropbox.sync.android.DbxAuthActivity
at com.dropbox.sync.android.CoreAndroidUtil.validateHaveAuthActivities(CoreAndroidUtil.java:244)
at com.dropbox.sync.android.DbxAccountManager.validateAppContext(DbxAccountManager.java:511)
at com.dropbox.sync.android.DbxAccountManager.getInstance(DbxAccountManager.java:193)
at com.dropbox.sync.android.DbxAccountManager.getInstance(DbxAccountManager.java:160)
at com.dropbox.sync.android.DbxAccountManager.getInstance(DbxAccountManager.java:107)
The required activities are in my manifest:
<activity android:name="com.dropbox.sync.android.DbxAuthActivity" />
<activity
android:name="com.dropbox.client2.android.AuthActivity"
android:launchMode="singleTask" >
<intent-filter>
<data android:scheme="db-zzz" />
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.BROWSABLE" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<service
android:name="com.dropbox.sync.android.DbxSyncService"
android:enabled="true"
android:exported="false"
android:label="Dropbox Sync" />
In production mode (so when one just uses the app) there is no such error.
Appium
I also tried to test my queries using Appium. My idea was to start my app via Appium and then run some test code without letting Appium click any button. E.g. insert some data and check if it was inserted correctly - all that in a test method. For doing so I would need an instance of the running Activity (android.app.Activity). As far as I know there is a method called AppiumDriver.getCurrentActivity() but it only returns the name of the Activity as a String and there seems to be no way to retrieve an instance of android.app.Activity.
I'm looking for an answer which explains in detail how to setup the required test tool (Appium, Robolectric or any other) in Android Studio 1.0 Release Candidate 2 (at the time of this writing) and to have at least one test pass querying the Datastore API version 3.1.1 of Dropbox on Android.