Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Calculon - A Java DSL for Android functional testing

Calculon - A Java DSL for Android functional testing

5cfa38a7e54e6c6a850dc6169a699246?s=128

Matthias Käppler

October 21, 2012
Tweet

Transcript

  1. Calculon A Java DSL for Android functional testing © 2010

    Matthias Käppler www.qype.com
  2. My fortune cookie says... Every half-way complex app should be

    backed by an automated test suite!
  3. Tests should be... Easy to write.

  4. Tests should be... Easy to read!

  5. Sorry, I don't speak binary public void whatInHeavensNameDoesThisEvenTest() { final

    Button b = (Button) getActivity().findViewById(R.id.b); ActivityMonitor monitor = getInstrumentation().addMonitor( BarActivity.class.getCanonicalName(), null, false); getInstrumentation().runOnMainSync(new Runnable() { public void run() { b.performClick(); } }); getInstrumentation().waitForIdleSync(); assertTrue(getInstrumentation().checkMonitorHit(monitor, 1)); }
  6. What a mess!

  7. Calculon to the rescue!

  8. Calculon to the rescue! assertThat(R.id.button).click().starts(BarActivity.class);

  9. Calculon is... … a testing library for Android.

  10. Calculon is... … deployed as a JAR.

  11. Calculon is... … a DSL*. *as far as that's possible

    in Java.
  12. Calculon is... … open source!

  13. Testing with Calculon Tests with Calculon are written as functional

    Activity tests. ActivityInstrumentationTestCase2
  14. Testing with Calculon public class FooTest extends FunctionalTest<FooActivity> { public

    FooTest() { super("com.example", FooActivity.class); } public void testStuff() { ... } }
  15. Assertions public void testStuff() { // testing against activities assertThat()...

    assertThat(getActivity())... assertThat(someOtherActivity)... // testing against views assertThat(R.id.some_button)... assertThat(someButton)... // of course all Junit assertions work as well assertEquals(...) assertNotNull(...) ... }
  16. Activity Assertions public void testStuff() { // testing for an

    orientation assertThat().inPortraitMode(); // testing for views assertThat().viewExists(R.id.some_button); // testing for input actions assertThat().keyDown(KeyEvent.KEYCODE_BACK)... // testing for custom predicates assertThat().satisfies(new Predicate<Activity>() { public boolean check(Activity target) { return target.isTaskRoot(); } }); }
  17. View Assertions public void testStuff() { // testing for view

    state assertThat(R.id.button).isVisible(); assertThat(R.id.button).isGone(); // testing for input actions assertThat(R.id.button).keyDown(KeyEvent.KEYCODE_BACK)... assertThat(R.id.button).click()... assertThat(R.id.button).longClick()... // testing for custom predicates assertThat(R.id.button).satisfies(new Predicate<View>() { public boolean check(View target) { return target.getVisibility() == View.VISIBLE; } }); }
  18. Action Assertions public void testStuff() { // testing for actions

    that launch a new activity assertThat(R.id.b1).click().starts(BarActivity.class); // testing for actions that finish an activity assertThat(R.id.b2).keyDown(KeyEvent.KEYCODE_Q).finishesActivity(); // testing for actions that change something assertThat(R.id.b3).click().implies(R.id.b2).isGone(); assertThat(R.id.b4).click().implies(getActivity()).inLandscapeMode(); assertThat(R.id.b5).click().implies(new Predicate<Model>() { public boolean check(Model target) { return target.someAttribute() == 5; } }); }
  19. Thanks! git://github.com/kaeppler/calculon.git $ git clone