Add code to do Fragment instrumentation tests

robolectric-error
Bradlee Speice 2014-07-09 21:14:23 -04:00
parent 9548bb50ef
commit cf7bc7c139
11 changed files with 164 additions and 8 deletions

View File

@ -32,6 +32,7 @@ android {
exclude 'META-INF/DEPENDENCIES'
exclude 'META-INF/LICENSE'
exclude 'META-INF/NOTICE'
exclude 'LICENSE.txt'
}
productFlavors {
testConfig {
@ -50,19 +51,19 @@ android {
dependencies {
compile project(path: ':jsword-minimalbible', configuration: 'buildJSword')
compile 'com.squareup.dagger:dagger:1.2.1'
provided 'com.squareup.dagger:dagger-compiler:1.2.1'
compile 'com.jakewharton:butterknife:5.0.1'
compile 'de.devland.esperandro:esperandro-api:1.1.2'
provided 'de.devland.esperandro:esperandro:1.1.2'
compile 'com.readystatesoftware.systembartint:systembartint:1.0.3'
compile 'com.netflix.rxjava:rxjava-android:0.19.0'
androidTestCompile 'com.jayway.awaitility:awaitility:1.6.0'
androidTestProvided 'com.squareup.dagger:dagger-compiler:1.2.0'
compile 'com.squareup.dagger:dagger:1.2.1'
compile 'com.jakewharton:butterknife:5.0.1'
compile 'de.devland.esperandro:esperandro-api:1.1.2'
compile 'com.readystatesoftware.systembartint:systembartint:1.0.3'
compile 'com.netflix.rxjava:rxjava-android:0.19.0'
compile 'com.android.support:appcompat-v7:20.+'
}

View File

@ -1,9 +1,8 @@
package org.bspeice.minimalbible;
import android.test.ActivityInstrumentationTestCase2;
import android.util.Log;
import org.bspeice.minimalbible.activity.download.DownloadActivity;
import org.bspeice.minimalbible.activity.downloader.DownloadActivity;
import java.lang.reflect.Field;

View File

@ -0,0 +1,10 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android" >
<application>
<activity android:name="org.bspeice.minimalbible.test.activity.FragmentTestActivity"
android:label="@string/title_activity_fragment_test"
>
</activity>
</application>
</manifest>

View File

@ -0,0 +1,40 @@
package org.bspeice.minimalbible;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.test.ActivityInstrumentationTestCase2;
import org.bspeice.minimalbible.test.activity.FragmentTestActivity;
/**
* Created by bspeice on 7/9/14.
*/
public class FragmentTestActivityTest extends ActivityInstrumentationTestCase2<FragmentTestActivity> {
public FragmentTestActivityTest() {
super(FragmentTestActivity.class);
}
public void testCanStartFragmentTestActivity() {
assertNotNull(getActivity());
}
/**
* Test that a Fragment is created properly in our TestCase for testing.
*/
public static class ValidFragmentTest extends Fragment {
public final static String FIELD_SHOULD_BE = "FIELD SHOULD HAVE THIS VALUE";
public String actualField;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.actualField = FIELD_SHOULD_BE;
}
}
public void testValidFragment() {
ValidFragmentTest f = new ValidFragmentTest();
getActivity().startFragment(f);
assertEquals(ValidFragmentTest.FIELD_SHOULD_BE, f.actualField);
}
}

View File

@ -12,7 +12,9 @@ import dagger.Provides;
* Created by Bradlee Speice on 7/5/2014.
*/
@Module(injects = DownloadActivity.class,
overrides = true)
overrides = true,
complete = false,
library = true)
public class TestModules {
public static CharSequence testActivityTitle = "Test";

View File

@ -0,0 +1,72 @@
package org.bspeice.minimalbible.test.activity;
import android.os.Handler;
import android.os.Looper;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build;
import org.bspeice.minimalbible.R;
import java.util.concurrent.CountDownLatch;
public class FragmentTestActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fragment_test);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.fragment_test, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void startFragment(Fragment f) {
final FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction()
.add(R.id.container, f)
.commit();
// Do the ugly work of waiting for the transaction to complete...
final CountDownLatch signal = new CountDownLatch(1);
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run() {
fragmentManager.executePendingTransactions();
signal.countDown();
}
});
try {
signal.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

View File

@ -0,0 +1,7 @@
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="org.bspeice.minimalbible.test.activity.FragmentTestActivity"
tools:ignore="MergeRootFrame" />

View File

@ -0,0 +1,9 @@
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="org.bspeice.minimalbible.test.activity.FragmentTestActivity" >
<item android:id="@+id/action_settings"
android:title="@string/action_settings"
android:orderInCategory="100"
app:showAsAction="never" />
</menu>

View File

@ -0,0 +1,6 @@
<resources>
<!-- Example customization of dimensions originally defined in res/values/dimens.xml
(such as screen margins) for screens with more than 820dp of available width. This
would include 7" and 10" devices in landscape (~960dp and ~1280dp respectively). -->
<dimen name="activity_horizontal_margin">64dp</dimen>
</resources>

View File

@ -0,0 +1,5 @@
<resources>
<!-- Default screen margins, per the Android Design guidelines. -->
<dimen name="activity_horizontal_margin">16dp</dimen>
<dimen name="activity_vertical_margin">16dp</dimen>
</resources>

View File

@ -0,0 +1,5 @@
<resources>
<string name="title_activity_fragment_test">FragmentTestActivity</string>
<string name="hello_world">Hello world!</string>
<string name="action_settings">Settings</string>
</resources>