SlideShare a Scribd company logo
Calculon
     A Java DSL for Android functional testing




© 2010 Matthias Käppler
    www.qype.com
My fortune cookie says...



 Every half-way complex app should be
  backed by an automated test suite!
Tests should be...




           Easy to write.
Tests should be...




      Easy to read!
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));
 }
What a mess!
Calculon to the rescue!
Calculon to the rescue!




 assertThat(R.id.button).click().starts(BarActivity.class);
Calculon is...




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




        … deployed as a JAR.
Calculon is...




                 … a DSL*.

         *as far as that's possible in Java.
Calculon is...




           … open source!
Testing with Calculon



   Tests with Calculon are written as
         functional Activity tests.
   ActivityInstrumentationTestCase2
Testing with Calculon

  public class FooTest extends FunctionalTest<FooActivity> {


      public FooTest() {
          super("com.example", FooActivity.class);
      }


      public void testStuff() {
          ...
      }
  }
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(...)
     ...
 }
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();
          }
      });
  }
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;
          }
      });
  }
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;
         }
     });
 }
Thanks!



          $ git clone
git://github.com/kaeppler/calculon.git

More Related Content

PDF
Agile Android
PDF
Android Design Patterns
PDF
Agile Swift
PDF
Stop Making Excuses and Start Testing Your JavaScript
DOC
Ejemplos Interfaces Usuario 3
DOC
Reverse123
PPTX
Code generation for alternative languages
PPTX
JavaZone 2014 - goto java;
Agile Android
Android Design Patterns
Agile Swift
Stop Making Excuses and Start Testing Your JavaScript
Ejemplos Interfaces Usuario 3
Reverse123
Code generation for alternative languages
JavaZone 2014 - goto java;

What's hot (18)

KEY
Unit testing en iOS @ MobileCon Galicia
PDF
Unit testing in iOS featuring OCUnit, GHUnit & OCMock
PDF
Rx java testing patterns
PPTX
TDD? Sure, but What About My Legacy Code?
PDF
An introduction to Google test framework
PDF
Unit Testing: Special Cases
PPTX
Migrating to JUnit 5
PDF
JUnit Kung Fu: Getting More Out of Your Unit Tests
PDF
Agile mobile
PDF
Bad test, good test
PDF
Unit-Testing Bad-Practices by Example
PDF
Unittesting Bad-Practices by Example
PDF
UI 모듈화로 워라밸 지키기
PDF
Why Kotlin - Apalon Kotlin Sprint Part 1
PDF
How do we use hooks
PPT
Introduzione al TDD
DOCX
Junit With Eclipse
PPTX
100% Code Coverage - TDD mit Java EE
Unit testing en iOS @ MobileCon Galicia
Unit testing in iOS featuring OCUnit, GHUnit & OCMock
Rx java testing patterns
TDD? Sure, but What About My Legacy Code?
An introduction to Google test framework
Unit Testing: Special Cases
Migrating to JUnit 5
JUnit Kung Fu: Getting More Out of Your Unit Tests
Agile mobile
Bad test, good test
Unit-Testing Bad-Practices by Example
Unittesting Bad-Practices by Example
UI 모듈화로 워라밸 지키기
Why Kotlin - Apalon Kotlin Sprint Part 1
How do we use hooks
Introduzione al TDD
Junit With Eclipse
100% Code Coverage - TDD mit Java EE
Ad

Viewers also liked (8)

PDF
Secure Webservices
PDF
Testing With Open Source
PDF
Hands on the Gradle
PDF
Crear o Morir, Resumen del libro
PDF
The impact of innovation on travel and tourism industries (World Travel Marke...
PPSX
Reuters: Pictures of the Year 2016 (Part 2)
PDF
The Six Highest Performing B2B Blog Post Formats
PDF
The Outcome Economy
Secure Webservices
Testing With Open Source
Hands on the Gradle
Crear o Morir, Resumen del libro
The impact of innovation on travel and tourism industries (World Travel Marke...
Reuters: Pictures of the Year 2016 (Part 2)
The Six Highest Performing B2B Blog Post Formats
The Outcome Economy
Ad

Similar to Calculon (20)

PDF
Android testing
PDF
Understanding JavaScript Testing
PPTX
Android Event and IntentAndroid Event and Intent
PDF
Testing, Performance Analysis, and jQuery 1.4
PDF
I am getting a syntax error. I cant seem to find whats causing t.pdf
PPTX
A GWT Application with MVP Pattern Deploying to CloudFoundry using Spring Roo
PDF
33rd Degree 2013, Bad Tests, Good Tests
PDF
Workshop 5: JavaScript testing
PPTX
The uniform interface is 42
PPT
2012 JDays Bad Tests Good Tests
PDF
Improving android experience for both users and developers
PDF
Droidcon2013 android experience lahoda
PDF
The evolution of redux action creators
PDF
Google guava
PDF
How to build rock solid apps & keep 100m+ users happy
DOCX
srcArtifact.javasrcArtifact.javaclassArtifactextendsCave{pub.docx
RTF
Easy Button
PDF
Unit & Automation Testing in Android - Stanislav Gatsev, Melon
ODP
Unit Testing
PDF
JAVA...With N.E.T_B.E.A.N.S___________________________________.pdf
Android testing
Understanding JavaScript Testing
Android Event and IntentAndroid Event and Intent
Testing, Performance Analysis, and jQuery 1.4
I am getting a syntax error. I cant seem to find whats causing t.pdf
A GWT Application with MVP Pattern Deploying to CloudFoundry using Spring Roo
33rd Degree 2013, Bad Tests, Good Tests
Workshop 5: JavaScript testing
The uniform interface is 42
2012 JDays Bad Tests Good Tests
Improving android experience for both users and developers
Droidcon2013 android experience lahoda
The evolution of redux action creators
Google guava
How to build rock solid apps & keep 100m+ users happy
srcArtifact.javasrcArtifact.javaclassArtifactextendsCave{pub.docx
Easy Button
Unit & Automation Testing in Android - Stanislav Gatsev, Melon
Unit Testing
JAVA...With N.E.T_B.E.A.N.S___________________________________.pdf

Calculon

  • 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)); }
  • 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 clone git://github.com/kaeppler/calculon.git