{"id":1608,"date":"2014-11-03T03:39:42","date_gmt":"2014-11-03T10:39:42","guid":{"rendered":"http:\/\/wiebe-elsinga.com\/blog\/?p=1608"},"modified":"2015-02-16T03:35:37","modified_gmt":"2015-02-16T10:35:37","slug":"android-testing-support-library-announced","status":"publish","type":"post","link":"http:\/\/wiebe-elsinga.com\/blog\/android-testing-support-library-announced\/","title":{"rendered":"Android Testing Support Library announced"},"content":{"rendered":"<div class=\"wpsso-pinterest-pin-it-image\" style=\"display:none !important;\">\n<\/div><!-- .wpsso-pinterest-pin-it-image -->\n\n<p>During the <a href=\"http:\/\/uk.droidcon.com\/2014\/\" title=\"Droidcon UK 2014\" target=\"_blank\">Droidcon UK 2014<\/a>, <a href=\"https:\/\/plus.google.com\/+StephanLinzner\" title=\"StephanLinzner G+ Profile\" target=\"_blank\">Stephan Linzner<\/a> of Google announced the upcoming Android Testing Support Library. This means Google is (finally) taking testing Android apps seriously. <\/p>\n<p>The library is a unbundled static testing library for Android that containers all Android testing frameworks by Google. It will become a available through the Android Support repository and as open source in AOSP.<\/p>\n<p>So what does it contain?<\/p>\n<ul>\n<li><em><strong>AndroidJUnitRunner<\/strong><\/em><br \/>\nThis Runner, based on <em>android.test.InstrumentationTestRunner<\/em>, runs <em>JUnit3<\/em> and <em>JUnit4<\/em> tests against an Android package (application). Let me give an example:<\/p>\n<div class=\"codecolorer-container java default\" style=\"overflow:auto;white-space:nowrap;width:100%;height:100%;\"><div class=\"java codecolorer\">@RunWith <span class=\"br0\">&#40;<\/span>JUnit4.<span class=\"kw1\">class<\/span><span class=\"br0\">&#41;<\/span><br \/>\n<span class=\"kw1\">public<\/span> <span class=\"kw1\">class<\/span> MyJUnit4Test <span class=\"br0\">&#123;<\/span><br \/>\n<span class=\"br0\">&#123;<\/span><br \/>\n&nbsp; Droidcon mDroidconUK<span class=\"sy0\">;<\/span><br \/>\n&nbsp;<br \/>\n&nbsp; @Before <br \/>\n&nbsp; <span class=\"kw1\">public<\/span> <span class=\"kw4\">void<\/span> setUp<span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span> <span class=\"br0\">&#123;<\/span><br \/>\n&nbsp; &nbsp; mDroidconUK <span class=\"sy0\">=<\/span> <span class=\"kw1\">new<\/span> Droidcons.<span class=\"me1\">get<\/span><span class=\"br0\">&#40;<\/span>Droidcon.<span class=\"me1\">UK<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n&nbsp; &nbsp; mDroidconUK.<span class=\"me1\">open<\/span><span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n&nbsp; <span class=\"br0\">&#125;<\/span><br \/>\n<br \/>\n&nbsp; @Test <br \/>\n&nbsp; <span class=\"kw1\">public<\/span> <span class=\"kw4\">void<\/span> checkPreconditions<span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span> <span class=\"br0\">&#123;<\/span><br \/>\n&nbsp; &nbsp; assertNotNull<span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;mDroidconUK cannot be null&quot;<\/span>, mDroidconUK<span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n&nbsp; <span class=\"br0\">&#125;<\/span><br \/>\n<br \/>\n&nbsp; @After <br \/>\n&nbsp; <span class=\"kw1\">public<\/span> <span class=\"kw4\">void<\/span> tearDown<span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span> <span class=\"br0\">&#123;<\/span><br \/>\n&nbsp; &nbsp; mDroidconUK.<span class=\"me1\">close<\/span><span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n&nbsp; <span class=\"br0\">&#125;<\/span><br \/>\n<span class=\"br0\">&#125;<\/span><\/div><\/div>\n<p>\n<\/li>\n<li><em><strong>Espresso<\/strong><\/em><br \/>\nIntroduced at the GTAC in 2013, Espresso makes it possible to write concise, beautiful, and reliable Android UI tests quickly. Lets look at some examples:<\/p>\n<div class=\"codecolorer-container java default\" style=\"overflow:auto;white-space:nowrap;width:100%;\"><div class=\"java codecolorer\"><span class=\"co1\">\/\/ Find view using a Matcher and type text using a ViewAction<\/span><br \/>\nonView<span class=\"br0\">&#40;<\/span>withId<span class=\"br0\">&#40;<\/span>R.<span class=\"me1\">id<\/span>.<span class=\"me1\">message_edit_text<\/span><span class=\"br0\">&#41;<\/span><span class=\"br0\">&#41;<\/span>.<span class=\"me1\">perform<\/span><span class=\"br0\">&#40;<\/span>typeText<span class=\"br0\">&#40;<\/span>TEXT_MESSAGE<span class=\"br0\">&#41;<\/span>, closeSoftKeyboard<span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n<br \/>\n<span class=\"co1\">\/\/ Perform a click ViewAction<\/span><br \/>\nonView<span class=\"br0\">&#40;<\/span>withId<span class=\"br0\">&#40;<\/span>R.<span class=\"me1\">id<\/span>.<span class=\"me1\">send_button<\/span><span class=\"br0\">&#41;<\/span><span class=\"br0\">&#41;<\/span>.<span class=\"me1\">perform<\/span><span class=\"br0\">&#40;<\/span>click<span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n<br \/>\n<span class=\"co1\">\/\/ Verify using a ViewAssertion<\/span><br \/>\nonView<span class=\"br0\">&#40;<\/span>withId<span class=\"br0\">&#40;<\/span>R.<span class=\"me1\">id<\/span>.<span class=\"me1\">received_message_text_view<\/span><span class=\"br0\">&#41;<\/span><span class=\"br0\">&#41;<\/span>.<span class=\"me1\">check<\/span><span class=\"br0\">&#40;<\/span>matches<span class=\"br0\">&#40;<\/span>withText<span class=\"br0\">&#40;<\/span><span class=\"br0\">&#40;<\/span>TEXT_MESSAGE<span class=\"br0\">&#41;<\/span><span class=\"br0\">&#41;<\/span><span class=\"br0\">&#41;<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><\/div><\/div>\n<p>\n<\/li>\n<li><em><strong>Intento<\/strong><\/em><br \/>\nIntento is like Mockito but for Intents. Basically a mock framework that allows you to create and configure mock objects. Let me give you an example:<\/p>\n<div class=\"codecolorer-container java default\" style=\"overflow:auto;white-space:nowrap;width:100%;\"><div class=\"java codecolorer\"><span class=\"kw1\">public<\/span> <span class=\"kw4\">void<\/span> testDailerInput_typeNumberAndCall<span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span> <span class=\"br0\">&#123;<\/span><br \/>\n&nbsp; <br \/>\n&nbsp; <span class=\"co1\">\/\/Type phonenumber in dialer<\/span><br \/>\n&nbsp; onView<span class=\"br0\">&#40;<\/span>withId<span class=\"br0\">&#40;<\/span>R.<span class=\"me1\">id<\/span>.<span class=\"me1\">send_data_to_call_edit_text<\/span><span class=\"br0\">&#41;<\/span><span class=\"br0\">&#41;<\/span>.<span class=\"me1\">perform<\/span><span class=\"br0\">&#40;<\/span>scrollTo<span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span>, typeText<span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;123-345-6789&quot;<\/span><span class=\"br0\">&#41;<\/span>, closeSoftKeyboard<span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n&nbsp; <br \/>\n&nbsp; <span class=\"co1\">\/\/ Click the call button<\/span><br \/>\n&nbsp; onView<span class=\"br0\">&#40;<\/span>withId<span class=\"br0\">&#40;<\/span>R.<span class=\"me1\">id<\/span>.<span class=\"me1\">send_to_call_button<\/span><span class=\"br0\">&#41;<\/span><span class=\"br0\">&#41;<\/span>.<span class=\"me1\">perform<\/span><span class=\"br0\">&#40;<\/span>scrollTo<span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span>, click<span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n&nbsp; <br \/>\n&nbsp; <span class=\"co1\">\/\/Validate Intent has been send<\/span><br \/>\n&nbsp; intended<span class=\"br0\">&#40;<\/span>allOf<span class=\"br0\">&#40;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; hasAction<span class=\"br0\">&#40;<\/span>Intent.<span class=\"me1\">ACTION_CALL<\/span><span class=\"br0\">&#41;<\/span>,<br \/>\n&nbsp; &nbsp; &nbsp; toPackage<span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;com.android.phone&quot;<\/span><span class=\"br0\">&#41;<\/span>,<br \/>\n&nbsp; &nbsp; &nbsp; hasData<span class=\"br0\">&#40;<\/span>allOf<span class=\"br0\">&#40;<\/span>hasSchemaSpecificPart<span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;tel&quot;<\/span>, <span class=\"st0\">&quot;123-345-6789&quot;<\/span><span class=\"br0\">&#41;<\/span><span class=\"br0\">&#41;<\/span><span class=\"br0\">&#41;<\/span><span class=\"br0\">&#41;<\/span>,<br \/>\n&nbsp; &nbsp; &nbsp; times<span class=\"br0\">&#40;<\/span><span class=\"nu0\">1<\/span><span class=\"br0\">&#41;<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n<span class=\"br0\">&#125;<\/span><\/div><\/div>\n<\/li>\n<\/ul>\n<p>\n<strong>In conclusion<\/strong>: Testing with Android is finally becoming a more prominent part of Android Development cycle with the introduction of the Android Testing Support Library<\/p>\n","protected":false},"excerpt":{"rendered":"<p>During the Droidcon UK 2014, Stephan Linzner of Google announced the upcoming Android Testing Support Library. This means Google is (finally) taking testing Android apps seriously. The library is a unbundled static testing library for Android that containers all Android testing frameworks by Google. It will become a available through the Android Support repository and as open source in AOSP. So what does it contain? AndroidJUnitRunner This Runner, based on android.test.InstrumentationTestRunner, runs JUnit3 and JUnit4 tests against an Android package (application). Let me give an example: @RunWith &#40;JUnit4.class&#41; public class MyJUnit4Test &#123; &#123; &nbsp; Droidcon mDroidconUK; &nbsp; &nbsp; @Before &nbsp; public void setUp&#40;&#41; &#123; &nbsp; &nbsp; mDroidconUK = new Droidcons.get&#40;Droidcon.UK&#41;; &nbsp; &nbsp; mDroidconUK.open&#40;&#41;; &nbsp; &#125; &nbsp; @Test &nbsp; public void checkPreconditions&#40;&#41; &#123; &nbsp; &nbsp; assertNotNull&#40;&quot;mDroidconUK cannot be null&quot;, mDroidconUK&#41;; &nbsp; &#125; &nbsp; @After &nbsp; public void tearDown&#40;&#41; &#123; &nbsp; &nbsp; mDroidconUK.close&#40;&#41;; &nbsp; &#125; &#125; Espresso Introduced at the GTAC in 2013, Espresso makes it possible to write concise, beautiful, and reliable Android UI tests quickly. Lets look at some examples: \/\/ Find view using a Matcher and type text using a ViewAction onView&#40;withId&#40;R.id.message_edit_text&#41;&#41;.perform&#40;typeText&#40;TEXT_MESSAGE&#41;, closeSoftKeyboard&#40;&#41;&#41;; \/\/ Perform a click ViewAction onView&#40;withId&#40;R.id.send_button&#41;&#41;.perform&#40;click&#40;&#41;&#41;; \/\/ Verify using a ViewAssertion onView&#40;withId&#40;R.id.received_message_text_view&#41;&#41;.check&#40;matches&#40;withText&#40;&#40;TEXT_MESSAGE&#41;&#41;&#41;&#41;; Intento Intento is like Mockito but for Intents. Basically a mock framework that allows you to create and configure mock objects. Let me give you an example: public void testDailerInput_typeNumberAndCall&#40;&#41; &#123; &nbsp; &nbsp; \/\/Type phonenumber in dialer &nbsp; onView&#40;withId&#40;R.id.send_data_to_call_edit_text&#41;&#41;.perform&#40;scrollTo&#40;&#41;, typeText&#40;&quot;123-345-6789&quot;&#41;, closeSoftKeyboard&#40;&#41;&#41;; &nbsp; &nbsp; \/\/ Click the call button &nbsp; onView&#40;withId&#40;R.id.send_to_call_button&#41;&#41;.perform&#40;scrollTo&#40;&#41;, click&#40;&#41;&#41;; &nbsp; &nbsp; \/\/Validate Intent has been send &nbsp; intended&#40;allOf&#40; &nbsp; &nbsp; &nbsp; hasAction&#40;Intent.ACTION_CALL&#41;, &nbsp; &nbsp; &nbsp; toPackage&#40;&quot;com.android.phone&quot;&#41;, &nbsp; &nbsp; &nbsp; hasData&#40;allOf&#40;hasSchemaSpecificPart&#40;&quot;tel&quot;, &quot;123-345-6789&quot;&#41;&#41;&#41;&#41;, &nbsp; &nbsp; &nbsp; times&#40;1&#41;&#41;; &#125; In conclusion: Testing with Android is finally becoming a more prominent part of Android Development cycle with the introduction of the Android Testing Support Library<\/p>\n","protected":false},"author":1,"featured_media":1622,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[7,4],"tags":[154,135,145],"_links":{"self":[{"href":"http:\/\/wiebe-elsinga.com\/blog\/wp-json\/wp\/v2\/posts\/1608"}],"collection":[{"href":"http:\/\/wiebe-elsinga.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/wiebe-elsinga.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/wiebe-elsinga.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/wiebe-elsinga.com\/blog\/wp-json\/wp\/v2\/comments?post=1608"}],"version-history":[{"count":0,"href":"http:\/\/wiebe-elsinga.com\/blog\/wp-json\/wp\/v2\/posts\/1608\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/wiebe-elsinga.com\/blog\/wp-json\/wp\/v2\/media\/1622"}],"wp:attachment":[{"href":"http:\/\/wiebe-elsinga.com\/blog\/wp-json\/wp\/v2\/media?parent=1608"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/wiebe-elsinga.com\/blog\/wp-json\/wp\/v2\/categories?post=1608"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/wiebe-elsinga.com\/blog\/wp-json\/wp\/v2\/tags?post=1608"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}