From cf7bc7c13950401031b895f7b50b4d34bb071959 Mon Sep 17 00:00:00 2001 From: Bradlee Speice Date: Wed, 9 Jul 2014 21:14:23 -0400 Subject: [PATCH] Add code to do Fragment instrumentation tests --- app/build.gradle | 11 +-- .../minimalbible/DownloadActivityTest.java | 3 +- app/src/testConfig/AndroidManifest.xml | 10 +++ .../FragmentTestActivityTest.java | 40 +++++++++++ .../org/bspeice/minimalbible/TestModules.java | 4 +- .../test/activity/FragmentTestActivity.java | 72 +++++++++++++++++++ .../res/layout/activity_fragment_test.xml | 7 ++ app/src/testConfig/res/menu/fragment_test.xml | 9 +++ .../testConfig/res/values-w820dp/dimens.xml | 6 ++ app/src/testConfig/res/values/dimens.xml | 5 ++ app/src/testConfig/res/values/strings.xml | 5 ++ 11 files changed, 164 insertions(+), 8 deletions(-) create mode 100644 app/src/testConfig/AndroidManifest.xml create mode 100644 app/src/testConfig/java/org/bspeice/minimalbible/FragmentTestActivityTest.java create mode 100644 app/src/testConfig/java/org/bspeice/minimalbible/test/activity/FragmentTestActivity.java create mode 100644 app/src/testConfig/res/layout/activity_fragment_test.xml create mode 100644 app/src/testConfig/res/menu/fragment_test.xml create mode 100644 app/src/testConfig/res/values-w820dp/dimens.xml create mode 100644 app/src/testConfig/res/values/dimens.xml create mode 100644 app/src/testConfig/res/values/strings.xml diff --git a/app/build.gradle b/app/build.gradle index 8ba7bec..139c313 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -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.+' } diff --git a/app/src/androidTest/java/org/bspeice/minimalbible/DownloadActivityTest.java b/app/src/androidTest/java/org/bspeice/minimalbible/DownloadActivityTest.java index 3729f3e..9e57380 100644 --- a/app/src/androidTest/java/org/bspeice/minimalbible/DownloadActivityTest.java +++ b/app/src/androidTest/java/org/bspeice/minimalbible/DownloadActivityTest.java @@ -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; diff --git a/app/src/testConfig/AndroidManifest.xml b/app/src/testConfig/AndroidManifest.xml new file mode 100644 index 0000000..559954b --- /dev/null +++ b/app/src/testConfig/AndroidManifest.xml @@ -0,0 +1,10 @@ + + + + + + + + diff --git a/app/src/testConfig/java/org/bspeice/minimalbible/FragmentTestActivityTest.java b/app/src/testConfig/java/org/bspeice/minimalbible/FragmentTestActivityTest.java new file mode 100644 index 0000000..1c58db9 --- /dev/null +++ b/app/src/testConfig/java/org/bspeice/minimalbible/FragmentTestActivityTest.java @@ -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 { + + 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); + } +} diff --git a/app/src/testConfig/java/org/bspeice/minimalbible/TestModules.java b/app/src/testConfig/java/org/bspeice/minimalbible/TestModules.java index fc4943e..ec1ac02 100644 --- a/app/src/testConfig/java/org/bspeice/minimalbible/TestModules.java +++ b/app/src/testConfig/java/org/bspeice/minimalbible/TestModules.java @@ -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"; diff --git a/app/src/testConfig/java/org/bspeice/minimalbible/test/activity/FragmentTestActivity.java b/app/src/testConfig/java/org/bspeice/minimalbible/test/activity/FragmentTestActivity.java new file mode 100644 index 0000000..b13b33b --- /dev/null +++ b/app/src/testConfig/java/org/bspeice/minimalbible/test/activity/FragmentTestActivity.java @@ -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(); + } + } +} diff --git a/app/src/testConfig/res/layout/activity_fragment_test.xml b/app/src/testConfig/res/layout/activity_fragment_test.xml new file mode 100644 index 0000000..6e6357c --- /dev/null +++ b/app/src/testConfig/res/layout/activity_fragment_test.xml @@ -0,0 +1,7 @@ + diff --git a/app/src/testConfig/res/menu/fragment_test.xml b/app/src/testConfig/res/menu/fragment_test.xml new file mode 100644 index 0000000..9bb73ad --- /dev/null +++ b/app/src/testConfig/res/menu/fragment_test.xml @@ -0,0 +1,9 @@ + + + diff --git a/app/src/testConfig/res/values-w820dp/dimens.xml b/app/src/testConfig/res/values-w820dp/dimens.xml new file mode 100644 index 0000000..63fc816 --- /dev/null +++ b/app/src/testConfig/res/values-w820dp/dimens.xml @@ -0,0 +1,6 @@ + + + 64dp + diff --git a/app/src/testConfig/res/values/dimens.xml b/app/src/testConfig/res/values/dimens.xml new file mode 100644 index 0000000..47c8224 --- /dev/null +++ b/app/src/testConfig/res/values/dimens.xml @@ -0,0 +1,5 @@ + + + 16dp + 16dp + diff --git a/app/src/testConfig/res/values/strings.xml b/app/src/testConfig/res/values/strings.xml new file mode 100644 index 0000000..981e6a3 --- /dev/null +++ b/app/src/testConfig/res/values/strings.xml @@ -0,0 +1,5 @@ + + FragmentTestActivity + Hello world! + Settings +