<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Android Testing - Medium]]></title>
        <description><![CDATA[Stories and advice to help you write better software and tests on Android - Medium]]></description>
        <link>https://medium.com/android-testing-daily?source=rss----ffe4584c7f8a---4</link>
        <image>
            <url>https://cdn-images-1.medium.com/proxy/1*TGH72Nnw24QL3iV9IOm4VA.png</url>
            <title>Android Testing - Medium</title>
            <link>https://medium.com/android-testing-daily?source=rss----ffe4584c7f8a---4</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 21 Nov 2026 12:06:38 GMT</lastBuildDate>
        <atom:link href="https://medium.com/feed/android-testing-daily" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[The Best of Android Testing and More]]></title>
            <link>https://medium.com/android-testing-daily/the-best-of-android-testing-and-more-b54171b20a82?source=rss----ffe4584c7f8a---4</link>
            <guid isPermaLink="false">https://medium.com/p/b54171b20a82</guid>
            <category><![CDATA[productivity-hacks]]></category>
            <category><![CDATA[productivity]]></category>
            <category><![CDATA[android]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[android-app-development]]></category>
            <dc:creator><![CDATA[Chuck Greb]]></dc:creator>
            <pubDate>Wed, 22 Jan 2025 03:24:34 GMT</pubDate>
            <atom:updated>2023-02-21T19:11:24.945Z</atom:updated>
            <content:encoded><![CDATA[<p>It’s been a while since writing any new articles on here, but recently I’ve had a number of folks reach out about blog posts I’ve written, conference talks I’ve given, and open source projects I’ve worked on over the years.</p><p>So I decided to put together “best of” article of sorts — highlighting some of my more popular talks, posts, and projects all in one place.</p><p>The astute reader will notice a common theme here, as many of these topics come from my <a href="https://medium.com/android-testing-daily">Android Testing</a> blog series, but there’s a few (hopefully) interesting bits on other topics as well.</p><p>Enjoy.</p><h3>Conference Talks</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Af5NDi1jYsAGlm-vK3z25A.jpeg" /><figcaption>DevFest Ukraine 2018</figcaption></figure><p><a href="https://www.droidcon.com/2019/08/07/productivity-hacks-for-busy-engineers/"><strong>Productivity Hacks for Busy Engineers</strong></a><strong> | Droidcon NYC 2019</strong><br>What do tomatoes have to do with productivity? Does multi-tasking really work? Can organizing your TODO list into boxes really help you get more things done? I don’t know. But come to this interactive session and we’ll find out together.</p><p><a href="https://www.youtube.com/watch?v=PEtd-UvC2ac&amp;list=PLpvIK6394rcSg0b2TChgzcbMTgYeWVzr-&amp;index=2"><strong>Working Effectively with Android Legacy Code</strong></a><strong> | DevFest Ukraine 2018</strong><br>Android isn’t new anymore. Most applications are not greenfield projects. Many of us find ourselves in the position of working with code we did not the author and which we don’t fully understand.</p><p><a href="https://www.youtube.com/watch?v=5kdZNsiSQt4&amp;list=PLpvIK6394rcSg0b2TChgzcbMTgYeWVzr-&amp;index=3"><strong>Clean app design with Architecture Components</strong></a><strong> | Droidcon SF 2017</strong><br>Handling lifecycle events, maintaining view state, and persisting data are all common challenges on Android that have contributed to the widespread adoption of clean architecture patterns like MVC, MVP, and MVVM. Android Architecture Components is a new collection of libraries to help developers manage these same nagging issues. This talk explores how components like Lifecycle, ViewModel, LiveData, and Room can be leveraged in an app already using clean architecture principles to help make your code even more robust, maintainable, and testable.</p><p><a href="https://www.youtube.com/watch?v=lo3tooMU3gc&amp;list=PLpvIK6394rcSg0b2TChgzcbMTgYeWVzr-&amp;index=7"><strong>Testable Android Architecture</strong></a><strong> | Droidcon NYC 2016</strong><br>Today we have many testing tools for Android including JUnit, Mockito, PowerMock, Robolectric, Espresso, and more. But how can you design your application to leverage each one most effectively? This talk introduces a modified version of the Model View Presenter (MVP) architecture that organizes your code to be more flexible, maintainable, and testable.</p><p><a href="https://www.youtube.com/watch?v=Yz_0owGx_eQ&amp;list=PLpvIK6394rcSg0b2TChgzcbMTgYeWVzr-&amp;index=8"><strong>Forks, Fragmentation, and the Future of Android</strong></a><strong> | Droidcon SF 2015</strong><br>Android powers more than 80% of smartphones worldwide and shipped on over 1 billion devices in 2014. However the hidden story in these numbers is that Android forks now account for over 20% of the global Android ecosystem and 41% of new devices. Early on “fragmentation” was an issue of screen size, display density, and platform version. The new “fragmentation” is forked versions of the OS and alternate app stores.</p><h3>Blog Posts</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*JhVolIICVTyDWjV-DwksNw.jpeg" /><figcaption>DevFest Conakry 2019 group photo on the steps of the Malick Conde Cultural Center in Guinea, West Africa</figcaption></figure><p><a href="https://ecgreb.medium.com/build-collaborate-scale-devfest-conakry-2019-b279720b2e42"><strong>Build, Collaborate, Scale: DevFest Conakry 2019</strong></a><br>The second annual DevFest Conakry was held on December 7, 2019 in Guinea, West Africa. This is the story of that event told by an attendee, speaker, and organizer.</p><p><a href="https://medium.com/android-testing-daily/working-effectively-with-android-legacy-code-b71414f195d6"><strong>Working Effectively with (Android) Legacy Code</strong></a><br>Android isn’t new anymore. Most apps are not greenfield projects. Many of us find ourselves in the position of working with code we did not author and which we don’t fully understand.</p><p><a href="https://medium.com/android-testing-daily/breaking-dependencies-709677ef760c"><strong>Breaking Dependencies</strong></a><br>Dependencies are one of the biggest challenges to getting legacy code into a test harness. Especially dependencies on the Android framework.</p><p><a href="https://medium.com/android-testing-daily/test-first-then-refactor-f8495fe0dee2"><strong>Test First Then Refactor</strong></a><br>Tests are the safety net that allow you to refactor with confidence. But what if your legacy code doesn’t have any tests? Then add some!</p><p><a href="https://medium.com/android-testing-daily/passive-view-e750bb10b20d"><strong>Passive View</strong></a><br>Passive View is the concept that logic in UI code should be minimized. This idea is also sometimes referred to as Humble View.</p><p><a href="https://medium.com/android-testing-daily/refactoring-28b8a4a07d42"><strong>Refactoring</strong></a><br>Refactoring is making a series of small structural changes to improve readability and maintainability of software without modifying existing behavior.</p><p><a href="https://medium.com/android-testing-daily/testing-presenters-8eaa8298065f"><strong>Testing Presenters</strong></a><br>Since all our Android framework dependencies are encapsulated in the view layer, the presenter can be tested using pure JUnit.</p><p><a href="https://medium.com/android-testing-daily/integration-testing-with-robolectric-2482b9f2c91f"><strong>Integration Testing with Robolectric</strong></a><br>This amazing framework made it possible to unit test Android code in the local JVM using bytecode manipulation to replace Android classes with mock (shadow) classes.</p><p><a href="https://medium.com/android-testing-daily/the-3-tiers-of-the-android-test-pyramid-c1211b359acd"><strong>The 3 tiers of the Android test pyramid</strong></a><br>The <a href="http://martinfowler.com/bliki/TestPyramid.html">test pyramid</a> is a concept that any automated testing strategy should have more low-level unit tests than high-level UI tests. Let’s take a look at how the test pyramid can be applied to modern Android development.</p><h3>Open Source</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/641/1*VG7QYEdIyo4spiq52e_HkA.jpeg" /><figcaption>Loginator: A clean architecture case study based on the Android Studio login template</figcaption></figure><p><a href="https://github.com/lostzen/lost"><strong>Location Open Source Tracker (LOST)</strong></a><br>Drop-in replacement for Google’s proprietary location services library for Android focused on data transparency and privacy.</p><p><a href="https://github.com/mapzen/android"><strong>Mapzen Android SDK</strong></a><br>Mapping SDK and replacement for Google Places API for location based apps using <a href="https://www.openstreetmap.org/">OpenStreetMap</a> data.</p><p><a href="https://github.com/mapzen/ios"><strong>Mapzen iOS SDK</strong></a><br>Open source and open data mapping SDK for location based apps on iOS using <a href="https://www.openstreetmap.org/">OpenStreetMap</a> data.</p><p><a href="https://github.com/mapzen/eraser-map"><strong>Eraser Map</strong></a><br>Privacy-focused mapping application for Android where we <a href="https://github.com/mapzen/eraser-map/commit/c183dd51cbc833c54104050e99c1604829ba346d">adopted Kotlin</a> (pre-1.0 release) in early 2015.</p><p><a href="https://github.com/robolectric/robolectric"><strong>Robolectric</strong></a><strong> </strong><br>I was an early <a href="https://github.com/robolectric/robolectric/commits?author=ecgreb">contributor</a> to one of the first unit testing frameworks for Android that is still around today.</p><p><a href="https://github.com/ecgreb/loginator"><strong>Loginator</strong></a><br>Clean architecture case study based on the Android Studio login template.</p><p><a href="https://github.com/ecgreb/mvpc"><strong>Model View Presenter Controller</strong></a><br>Demo app with my own take on clean architecture based on MVP and MVI design patterns. (see <em>Testable Android Architecture</em> video above)</p><p><strong>If you found this article helpful, please give it some applause 👏 to help others find it. You can also leave a comment below. Thanks!</strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b54171b20a82" width="1" height="1" alt=""><hr><p><a href="https://medium.com/android-testing-daily/the-best-of-android-testing-and-more-b54171b20a82">The Best of Android Testing and More</a> was originally published in <a href="https://medium.com/android-testing-daily">Android Testing</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Breaking Dependencies]]></title>
            <link>https://medium.com/android-testing-daily/breaking-dependencies-709677ef760c?source=rss----ffe4584c7f8a---4</link>
            <guid isPermaLink="false">https://medium.com/p/709677ef760c</guid>
            <category><![CDATA[unit-testing]]></category>
            <category><![CDATA[android-development]]></category>
            <category><![CDATA[refactoring]]></category>
            <category><![CDATA[clean-code]]></category>
            <category><![CDATA[android-app-development]]></category>
            <dc:creator><![CDATA[Chuck Greb]]></dc:creator>
            <pubDate>Fri, 14 Sep 2018 11:01:15 GMT</pubDate>
            <atom:updated>2018-09-14T11:01:15.241Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*gKcy37voNkFf2NIOtbb-JQ.jpeg" /><figcaption><a href="https://www.flickr.com/photos/arnehendriks/3962691201/in/photolist-73aQ36-9n7iYN-qCoG1-6sjCuP-qYzmGh-87LBD6-24jDRR-87PQQd-87PQW3-6tm2ny-ckeZ7G-87LC44-8xbg4U-dS6VbH-kMX1sp-8xbfNA-3wCBUo-bqoRgV-3wCypu-q3AjQD-hpQuHW-kMWYQ6-bqoNYk-kMWseZ-Anpbd-7H3ycb-nU9Ado-obkXLB-7YnhUn-6VibL9-ckeZeh-hpQvxw-hpQwou-84aNjJ-24nWbz-dS6VB6-8b9NhW-LUHvw-7GYCa8-mCUBgz-3wCwrw-3YuJhi-bqoQ7K-8478Zr-dJ5Zp-7YpBBw-3YvtkV-3wybma-64uzbb-7H3y4J">The Breakfast Machine</a> by Arne Hendriks via Flickr Creative Commons</figcaption></figure><p>Dependencies are one of the biggest challenges to getting legacy code into a test harness. Especially dependencies on the Android framework.</p><p>In the Android world, having a dependency on an activity, fragment, or view is basically a non-starter for unit testing. Other types of dependencies that pose challenges to unit testing include networking calls, file I/O, database access, and globals, just to name a few.</p><blockquote>Dependencies are ofter the most obvious impediment to testing. The two ways this problem manifests itself are difficulty instantiating objects in test harnesses and difficulty running methods in test harnesses. Often in legacy code, you have to break dependencies to get test in place.</blockquote><p><strong>Working Effectively with Legacy Code</strong>, Michael Feathers (2005)</p><h3>Android Dependencies</h3><p>In the <a href="https://medium.com/android-testing-daily/test-first-then-refactor-f8495fe0dee2">previous post</a>, we extracted a simple presenter class to allow us to unit test the presentation logic that was originally inline in a button click handler. The key here was breaking the dependency on the Android framework to allow us to unit test the logic in isolation.</p><p>Last time we were able to first add a few instrumentation tests as a bit of a safety net before refactoring. Unfortunately, is not always possible. Let’s look at another example, this time in Java so we can enjoy a more authentic <em>legacy code </em>experience.</p><p><strong>UserPresenter.java</strong></p><pre><strong>public class </strong>UserPresenter {<br>    <strong>private final </strong>UserController controller;<br><br>    <strong>private </strong>UserProfile user;<br><br>    <strong>public </strong>UserPresenter(UserController controller) {<br>        <strong>this</strong>.controller = controller;<br>    }<br><br>    <strong>public void </strong>onProfileButtonClick(Context context) {<br>        <strong>if </strong>(user == <strong>null</strong>) {<br>            user = DatabaseManager.<em>getInstance</em>().fetchUser(context);<br>        }<br><br>        controller.showProfile(user);<br>    }<br>}</pre><p>In this example, we have an app with a feature to display a user profile screen.</p><p><strong>The good news is</strong> we already have a UserPresenter class decoupled from the Activity.</p><p><strong>The bad news is</strong> the presenter depends on a DatabaseManager and a Context.</p><p>Even worse, the DatabaseManager is a singleton. Having a dependency on a global singleton can be particularly challenging when trying to get a class into a unit test harness.</p><p>Let’s also assume we can’t introduce instrumentation tests for this feature without pre-populating the database with user profile data and navigating a complex series of UI interactions. While this might sound interesting to you, I can think of better ways to spend my time. Remember we would have to do this every time we run the tests on an emulator or device.</p><p>And after all that, you still won’t have the ability to write unit tests that can verify the behavior of your presenter in isolation. As an alternative, we can use a <em>dependency breaking technique</em>.</p><h3>Dependency Breaking Techniques</h3><p>Dependency breaking techniques are minimally invasive refactors that can help make a system more testable.</p><p>Isn’t there risk when you start refactoring without having tests in place? Of course. However these techniques are designed so the chance of making a mistake is very small.</p><p>Breaking dependencies in <a href="https://medium.com/android-testing-daily/working-effectively-with-android-legacy-code-b71414f195d6">legacy code</a> will allow the introduction of tests into the system before making more invasive design improvements.</p><p>In the above example, we actually have a few different options for how to break the dependency of the UserPresenter on the DatabaseManager singleton and the Android Context.</p><ol><li><em>Parameterize Constructor</em></li><li><em>Parameterize Method</em></li><li><em>Introduce Static Setter</em></li></ol><h4>Parameterize Constructor</h4><p>Rather than relying on the getInstance() method of the DatabaseManager singleton, we could pass an instance of the class into the constructor.</p><p>This would allow us to inject a fake or mock DatabaseManager in the test environment into the UserPresenter.</p><p><strong>UserPresenter.java</strong></p><pre><strong>public class </strong>UserPresenter {<br>    <strong>private final </strong>UserController controller;<br>    <strong>private final </strong>DatabaseManager databaseManager;<br><br>    <strong>private </strong>UserProfile user;<br><br>    <strong>public </strong>UserPresenter(UserController controller,<br>            DatabaseManager databaseManager) {<br>        <strong>this</strong>.controller = controller;<br>        <strong>this</strong>.databaseManager = databaseManager;<br>    }<br><br>    <strong>public void </strong>onProfileButtonClick(Context context) {<br>        <strong>if </strong>(user == <strong>null</strong>) {<br>            user = databaseManager.fetchUser(context);<br>        }<br><br>        controller.showProfile(user);<br>    }<br>}</pre><h4><strong>Parameterize Method</strong></h4><p>We can perform a similar technique by introducing a new parameter to the onProfileButtonClick method.</p><p><strong>UserProfile.java</strong></p><pre><strong>public class </strong>UserPresenter {<br>    <strong>private final </strong>UserController controller;<br><br>    <strong>private </strong>UserProfile user;<br><br>    <strong>public </strong>UserPresenter(UserController controller) {<br>        <strong>this</strong>.controller = controller;<br>    }<br><br>    <strong>public void </strong>onProfileButtonClick(Context context,<br>            DatabaseManager databaseManager) {<br>        <strong>if </strong>(user == <strong>null</strong>) {<br>            user = databaseManager.fetchUser(context);<br>        }<br><br>        controller.showProfile(user);<br>    }<br>}</pre><h4>Introduce Static Setter</h4><p>A third way we can decouple the dependency on the real DatabaseManager in tests is by introducing a static setter method that allows us to replace the real instance by a fake instance in the test environment.</p><p>Let’s say our DatabaseManager looks something like the following. For the sake of brevity we’ll omit the details of the database access.</p><p><strong>DatabaseManager.java</strong></p><pre><strong>public class </strong>DatabaseManager {<br>    <strong>private static final </strong>DatabaseManager <strong><em>instance </em></strong>=<br>            <strong>new </strong>DatabaseManager();<br><br>    <strong>public static </strong>DatabaseManager getInstance() {<br>        <strong>return <em>instance</em></strong>;<br>    }<br><br>    <strong>private </strong>DatabaseManager() {<br>    }<br><br>    <strong>public </strong>UserProfile fetchUser(Context context) {<br>        UserProfile user = <strong>new </strong>UserProfile();<br><br>        <em>// Fetch user profile data from SQLite database...<br><br>        </em><strong>return </strong>user;<br>    }<br>}</pre><p>Introducing a static setter method that can be used in the test harness requires a few small steps.</p><ol><li>Relax constructor scope from private to protected so we can create a test subclass that avoids accessing the real database.</li><li>Make the static instance mutable by removing the keyword final.</li><li>Add a test-only method to set the instance in the test environment.</li></ol><p><strong>DatabaseManager.java</strong></p><pre><strong>public class </strong>DatabaseManager {<br>    <strong>private static </strong>DatabaseManager <em>instance </em>= <strong>new </strong>DatabaseManager();<br><br>    <strong>public static </strong>DatabaseManager getInstance() {<br>        <strong>return </strong><em>instance</em>;<br>    }<br><br>    <em>@VisibleForTesting<br>    </em><strong>public static void </strong>setTestInstance(DatabaseManager instance) {<br>        DatabaseManager.<em>instance </em>= instance;<br>    }<br><br>    <strong>protected </strong>DatabaseManager() {<br>    }<br><br>    <strong>public </strong>UserProfile fetchUser(Context context) {<br>        UserProfile user = <strong>new </strong>UserProfile();<br><br>        <em>// Fetch user profile data from SQLite database...<br><br>        </em><strong>return </strong>user;<br>    }<br>}</pre><p>Does this looks a little gross? Yes. When introducing tests in legacy code, sometimes the design of the program will get worse before it gets better.</p><p>Once we have tests in place, we can start to make more comprehensive design improvements. For example, perhaps we could eventually migrate the DatabaseManager from a singleton to a normal instance class.</p><p>Which one of these dependency breaking technique is best to use? This answer is individual to your program.</p><p><em>Parameterize Constructor</em> and <em>Parameterize Method </em>are less invasive, but can lead to widespread changes in the code base if many classes have the same dependency.</p><p><em>Introduce Static Setter</em> is a slightly more invasive technique, as it introduces “test only” code into the production class. Yet this can be a reasonable trade-off when there are many classes that depend on the same singleton instance.</p><p>In the book <a href="https://www.amazon.com/FEATHERS-WORK-EFFECT-LEG-CODE/dp/0131177052">Working Effectively with Legacy Code</a>, there are many more dependency breaking techniques. In the coming posts, we will look at even more of these small tactical refactors to facilitate testing, and how they can be applied to Android projects. See you next time!</p><p><em>This post is part of a series on working with legacy code on Android. It explores ways we can navigate, maintain, improve, and evolve legacy code using clean architecture, refactoring, dependency breaking techniques, and testing.</em></p><p><strong>If you found this article helpful, please give it some applause 👏 to help others find it. You can also leave a comment below. Thanks!</strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=709677ef760c" width="1" height="1" alt=""><hr><p><a href="https://medium.com/android-testing-daily/breaking-dependencies-709677ef760c">Breaking Dependencies</a> was originally published in <a href="https://medium.com/android-testing-daily">Android Testing</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Test First Then Refactor]]></title>
            <link>https://medium.com/android-testing-daily/test-first-then-refactor-f8495fe0dee2?source=rss----ffe4584c7f8a---4</link>
            <guid isPermaLink="false">https://medium.com/p/f8495fe0dee2</guid>
            <category><![CDATA[android]]></category>
            <category><![CDATA[unit-testing]]></category>
            <category><![CDATA[androiddev]]></category>
            <category><![CDATA[clean-code]]></category>
            <category><![CDATA[android-app-development]]></category>
            <dc:creator><![CDATA[Chuck Greb]]></dc:creator>
            <pubDate>Thu, 06 Sep 2018 17:56:08 GMT</pubDate>
            <atom:updated>2018-09-12T11:21:04.675Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/640/1*znYqPL9iIvsXMBAVRdXRkA.jpeg" /></figure><p>Tests are the safety net that allow you to <a href="https://medium.com/android-testing-daily/refactoring-28b8a4a07d42">refactor</a> with confidence. But what if your legacy code doesn’t have any tests? Then add some!</p><p>Ordinarily, I would advocate for a <a href="https://medium.com/android-testing-daily/the-3-tiers-of-the-android-test-pyramid-c1211b359acd">unit test first</a> strategy on Android. Instrumentation tests and UI tests can come after your logic is well tested.</p><p>However, when dealing with untested legacy code, sometimes we need to talk a different approach. Let’s look at a simple example.</p><h4><strong>MainActivity.kt</strong></h4><pre><strong>class </strong>MainActivity : Activity() {<br>    <strong>override fun </strong>onCreate(savedInstanceState: Bundle?) {<br>        <strong>super</strong>.onCreate(savedInstanceState)<br>        setContentView(R.layout.<em>activity_main</em>)<br><br>        toggle_button.setOnClickListener <strong>{<br>            if </strong>(image_view.<em>visibility </em>== View.<em>VISIBLE</em>) {<br>                image_view.<em>visibility </em>= View.<em>GONE<br>            </em>} <strong>else </strong>{<br>                image_view.<em>visibility </em>= View.<em>VISIBLE<br>            </em>}<br>        <strong>}<br>    </strong>}<br>}</pre><p>In this app we have a Button and an ImageView. When the user clicks the button, it toggles the visibility of the image.</p><p>The issue here is that our presentation logic is mixed into with our Activity. This makes it challenging to maintain, hard to modify, and hard to test. By definition it is <a href="https://medium.com/android-testing-daily/working-effectively-with-android-legacy-code-b71414f195d6">legacy code</a>.</p><p>Let’s say we want to make a change to the presentation logic. For example, once the view is shown do not hide it again. Once we implement the change, our only option is to launch the app on a device and manually test it.</p><p>We know there are a lot of new features planned for this part of the app. In order to make the code more flexible and maintainable, we decide to migrate the presentation logic to a presenter class.</p><p>But wait! How can we be sure that when we introduce the presenter class, we don’t also accidentally change the behavior of the code? First, we need to write some tests.</p><p>Unfortunately, since the presentation logic is currently stuffed into the Activity there is no way we an unit test it. We need to add some instrumentation tests using Espresso.</p><h4>MainActivityTest.kt</h4><pre>@RunWith(AndroidJUnit4::<strong>class</strong>)<br><strong>class </strong>MainActivityTest {<br>    @get:Rule<br>    <strong>val </strong>rule: ActivityTestRule&lt;MainActivity&gt;<br>            = ActivityTestRule(MainActivity::<strong>class</strong>.<em>java</em>)<br><br>    @Test<br>    <strong>fun </strong>onClick_once_shouldHideImageView() {<br>        onView(withId(R.id.<em>toggle_button</em>)).perform(click())<br>        onView(withId(R.id.<em>image_view</em>))<br>                .check(matches(not(isDisplayed())))<br>    }<br><br>    @Test<br>    <strong>fun </strong>onClick_twice_shouldShowImageView() {<br>        onView(withId(R.id.<em>toggle_button</em>)).perform(click())<br>        onView(withId(R.id.<em>toggle_button</em>)).perform(click())<br>        onView(withId(R.id.<em>image_view</em>))<br>                .check(matches(isDisplayed()))<br>    }<br>}</pre><p>We add one test for the scenario when the image is currently visible, and the other for when the image is currently hidden. Now, we can begin to refactor.</p><p>First, we add the presenter class.</p><h4>MainPresenter.kt</h4><pre><strong>class </strong>MainPresenter(<strong>private val </strong>controller: MainController) {<br>    <strong>fun </strong>onToggleButtonClick(visibility: Int) {<br>        <strong>if </strong>(visibility == View.<em>VISIBLE</em>) {<br>            controller.setImageVisibility(View.<em>GONE</em>)<br>        } <strong>else </strong>{<br>            controller.setImageVisibility(View.<em>VISIBLE</em>)<br>        }<br>    }</pre><p>Next, we add the <a href="https://medium.com/android-testing-daily/activities-are-not-views-9200b3c1a990">controller interface</a> the presenter will use to communicate with the view layer.</p><h4>MainController.kt</h4><pre><strong>interface </strong>MainController {<br>    <strong>fun </strong>setImageVisibility(visibility: Int)<br>}</pre><p>Now we can refactor our MainActivity to (1) implement the controller interface and (2) delegate to the new presenter class to handle user input and presentation logic.</p><h4>MainActivity.kt</h4><pre><strong>class </strong>MainActivity : Activity(), MainController {<br>    <strong>private val </strong>presenter = MainPresenter(<strong>this</strong>)<br><br>    <strong>override fun </strong>onCreate(savedInstanceState: Bundle?) {<br>        <strong>super</strong>.onCreate(savedInstanceState)<br>        setContentView(R.layout.<em>activity_main</em>)<br><br>        toggle_button.setOnClickListener <strong>{<br>            </strong>presenter.onToggleButtonClick(image_view.<em>visibility</em>)<br>        <strong>}<br>    </strong>}<br><br>    <strong>override fun </strong>setImageVisibility(visibility: Int) {<br>        image_view.<em>visibility </em>= visibility<br>    }<br>}</pre><p>Last, but definitely not least, we can now write some pure unit tests for our presentation logic.</p><h4>MainPresenterTest.kt</h4><pre><strong>class </strong>MainPresenterTest {<br>    <strong>private val </strong>controller = TestController()<br>    <strong>private val </strong>presenter = MainPresenter(controller)<br><br>    @Test<br>    <strong>fun </strong>onToggleButtonClick_visible_shouldSetImageGone() {<br>        controller.visibility = View.<em>GONE<br>        </em>presenter.onToggleButtonClick(View.<em>VISIBLE</em>)<br>        assertEquals(View.<em>GONE</em>, controller.visibility)<br>    }<br><br>    @Test<br>    <strong>fun </strong>onToggleButtonClick_gone_shouldSetImageVisible() {<br>        controller.visibility = View.<em>VISIBLE<br>        </em>presenter.onToggleButtonClick(View.<em>GONE</em>)<br>        assertEquals(View.<em>VISIBLE</em>, controller.visibility)<br>    }<br><br>    <strong>class </strong>TestController : MainController {<br>        <strong>var </strong>visibility = View.<em>VISIBLE<br><br>        </em><strong>override fun </strong>setImageVisibility(visibility: Int) {<br>            <strong>this</strong>.visibility = visibility<br>        }<br>    }<br>}</pre><p>Now the code is in a much better position to make future changes. We have unit tests, instrumentation tests, and clear separation of concerns. But most importantly, we have confidence that our refactor improved the design without changing the behavior of the production code.</p><p><em>This post is part of a series on working with legacy code on Android. It explores ways we can navigate, maintain, improve, and evolve legacy code using clean architecture, refactoring, dependency breaking techniques, and testing.</em></p><p><strong>If you found this article helpful, please give it some applause 👏 to help others find it. You can also leave a comment below. Thanks!</strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f8495fe0dee2" width="1" height="1" alt=""><hr><p><a href="https://medium.com/android-testing-daily/test-first-then-refactor-f8495fe0dee2">Test First Then Refactor</a> was originally published in <a href="https://medium.com/android-testing-daily">Android Testing</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Refactoring]]></title>
            <link>https://medium.com/android-testing-daily/refactoring-28b8a4a07d42?source=rss----ffe4584c7f8a---4</link>
            <guid isPermaLink="false">https://medium.com/p/28b8a4a07d42</guid>
            <category><![CDATA[android-development]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[android-app-development]]></category>
            <category><![CDATA[unit-testing]]></category>
            <category><![CDATA[clean-code]]></category>
            <dc:creator><![CDATA[Chuck Greb]]></dc:creator>
            <pubDate>Wed, 05 Sep 2018 12:11:01 GMT</pubDate>
            <atom:updated>2018-09-05T12:11:00.587Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/640/1*9sp19OlB23TUpNd-cgU-KQ.jpeg" /><figcaption><a href="https://www.flickr.com/photos/amatuerphotographer/33969070584/in/photolist-TKJndL-dsigmd-i3kofZ-fAkFqW-Uci367-op9u7T-rejs9K-565CyZ-rpCYwv-6qJSR8-569Pbu-TbcRjd-6WmRhZ-cTeQB9-SVb6ZY-dqfhjw-5ChN1v-6kMy4o-qNEXTh-86U5Dg-pyUgte-9YtFCb-gDUkyD-reE9ue-odAQxZ-nbTxLK-pyXKtV-obwupk-pPihqw-rgxM3e-TXkXF4-pRneH3-7H7Gma-pyTnf6-p3a5PM-WAYiw6-9ZzSsZ-s3FKso-Trzzq9-gDUeUH-8WN4zC-7CLjYo-oV7ETu-6HNr14-7QCzYf-Su3dyW-a9U8Nx-3KhLNh-8ms3vz-gzC6BX">The old and new Tappan Zee Bridge side by side</a> by Andrew Dallos via Flickr Creative Commons</figcaption></figure><p><em>“Any fool can write code that a computer can understand. Good programmers write code that humans can understand.”</em> — Martin Fowler (1999)</p><h3>What is (or isn’t) a refactor</h3><p>In <a href="https://www.safaribooksonline.com/library/view/working-effectively-with/0131177052/">Working Effectively with Legacy Code</a>, Michael Feathers defines refactoring as “the act of improving design without changing its behavior.”</p><p>To me, what is most significant about this definition is what refactoring is not.</p><p><em>Refactoring is not adding a new feature.</em></p><p><em>Refactoring is not fixing a bug.</em></p><p><em>Refactoring is not optimization.</em></p><p>Refactoring is making a series of small structural changes to improve readability and maintainability of software without modifying existing behavior.</p><p>There have been too many times when I’ve been asked to review a pull request that has a description that starts out with something like the following:</p><blockquote>Refactor presenter to handle dialog button click events…</blockquote><p><strong>Wrong!</strong></p><p>If you are changing the behavior, it’s not a refactor.</p><p>But how can we be sure that when making small incremental improvements to the design of our system that we are not unintentionally changing the behavior?</p><p><strong>Tests!</strong></p><p>Refactoring becomes much easier (and safer) if you have a robust suite of unit tests and instrumentation tests to verify the behavior of your library or app.</p><h3>Refactoring in Android Studio</h3><p>Fortunately, Android Studio has a powerful set of built-in refactoring tools powered by the IntelliJ platform.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/722/1*kn1_FVBPDic1YNQpoQr5JA.png" /><figcaption>Android Studio Refactor Menu</figcaption></figure><p>The <strong>Refactor</strong> menu in Android Studio will automate many common refactoring tasks including:</p><ul><li>Rename class/method/variable</li><li>Change a method signature</li><li>Move a class to a new package</li><li>Extract a method or variable</li><li>Extract a method parameter</li><li>Create a new constant</li><li>Inline a method</li><li>Safe delete</li><li>And more…</li></ul><p>For more information about how to execute each of these automated refactors, checkout <a href="https://jeroenmols.com/blog/2018/04/26/androidstudioshortcuts2/">this excellent article</a> by Jeroen Mols.</p><p>Refactoring can be a useful tool for making incremental improvements to Android legacy code. In the next post, we’ll explore how we can begin to add tests <em>before</em> refactoring where none currently exist.</p><p><em>This post is part of a series on working with legacy code on Android. It explores ways we can navigate, maintain, improve, and evolve legacy code using clean architecture, refactoring, dependency breaking techniques, and testing.</em></p><p><strong>If you found this article helpful, please give it some applause 👏 to help others find it. You can also leave a comment below. Thanks!</strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=28b8a4a07d42" width="1" height="1" alt=""><hr><p><a href="https://medium.com/android-testing-daily/refactoring-28b8a4a07d42">Refactoring</a> was originally published in <a href="https://medium.com/android-testing-daily">Android Testing</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Working Effectively with (Android) Legacy Code]]></title>
            <link>https://medium.com/android-testing-daily/working-effectively-with-android-legacy-code-b71414f195d6?source=rss----ffe4584c7f8a---4</link>
            <guid isPermaLink="false">https://medium.com/p/b71414f195d6</guid>
            <category><![CDATA[android-development]]></category>
            <category><![CDATA[unit-testing]]></category>
            <category><![CDATA[software-testing]]></category>
            <category><![CDATA[android]]></category>
            <category><![CDATA[clean-code]]></category>
            <dc:creator><![CDATA[Chuck Greb]]></dc:creator>
            <pubDate>Tue, 04 Sep 2018 12:35:34 GMT</pubDate>
            <atom:updated>2018-09-04T12:35:34.087Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/377/1*tjj7VHyCEieivc5Q-rKifw.jpeg" /></figure><p>How many times have you heard yourself (or a teammate) say one of the following?</p><ul><li>How are we going to add this new feature when the code is a mess?</li><li>We can’t change this file — it’s too risky!</li><li>How do I test this class when it depends on X, Y, and Z?</li><li>There is not enough time to make the changes you want!</li><li>What does this code even do!?</li><li><em>I feel overwhelmed and it’s never going to get any better.</em></li></ul><p>Android isn’t new anymore. Most apps are not greenfield projects. Many of us find ourselves in the position of working with code we did not author and which we don’t fully understand.</p><h3>Legacy Code</h3><p>But what is legacy code? First, let’s consider a strict definition.</p><blockquote>Legacy code is code that we’ve gotten from someone else.</blockquote><p>In the classic 2005 book, <a href="https://www.amazon.com/FEATHERS-WORK-EFFECT-LEG-CODE/dp/0131177052">Working Effectively with Legacy Code</a>, Michael C. Feathers offers a different definition.</p><blockquote>Legacy code is code without tests.</blockquote><p>Why? Without tests, there is no way to know if your code is getting better or worse.</p><h3>Modern Android</h3><p>Android development has come a long way since I started in 2009. Today we have support libraries, MVP, MVVM, MVI, dependency injection, unit tests, multidex, Espresso, RxJava, OkHttp, Architecture Components, Data Binding, Kotlin, Jetpack, etc, etc, etc.</p><p>But what if your app doesn’t currently use any of these things? What if it was written in 2009? What if you can’t take advantage of the latest and greatest frameworks due to restrictions on external dependencies? What if the original author of the code is long gone, and now you are tasked with fixing all the bugs, delivering new features on time, and keeping the project alive?</p><h3>Change Management</h3><p>For many of us, our first instinct when inheriting a project is to clean it up immediately, so the code is “the way it should be.” This is a bad idea.</p><p>All change has risk. If code is functioning properly, there is no reason to change it. Especially if that code is not well tested. However, when it comes time to fix a bug, or add a new feature, change becomes necessary.</p><p>But how can we improve and evolve a code base with poor architecture and no unit tests? <em>By finding the seams</em>. In Android, this often means where to draw the line between the operating system and your library or app.</p><p>Like a surgeon, you must decide where to make the incision. Then you can isolate the component that needs to change, extract it into its own method or class, and tests its behavior in isolation.</p><p>This can be messy work. Oftentimes things will get worse before they get better. You might have to duplicate code, relax scope, violate the <a href="https://en.wikipedia.org/wiki/Single_responsibility_principle">single responsibility principle</a>, or make other poor design trade-offs.</p><p>Over time, you can start to grow areas of high-quality code in legacy code bases, but the steps you must take to get there can often make the code around it uglier in the process (at least for a while).</p><p>In the coming posts, we will explore specific techniques for improving Android legacy code including:</p><ul><li>Introducing a new architecture pattern</li><li>Migrating to a new framework</li><li>Breaking dependencies</li><li>Backwards compatibility</li><li>Enforcing code styles and standards</li><li>Leaving code better than you found it</li><li>And more…</li></ul><p>Have an idea for something else you would like to see covered? Drop your suggestion in the comments section below.</p><p><em>This post is part of a series on working with legacy code on Android. It explores ways we can navigate, maintain, improve, and evolve legacy code using clean architecture, refactoring, dependency breaking techniques, and testing.</em></p><p><strong>If you found this article helpful, please give it some applause 👏 to help others find it. You can also leave a comment below. Thanks!</strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b71414f195d6" width="1" height="1" alt=""><hr><p><a href="https://medium.com/android-testing-daily/working-effectively-with-android-legacy-code-b71414f195d6">Working Effectively with (Android) Legacy Code</a> was originally published in <a href="https://medium.com/android-testing-daily">Android Testing</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[KotlinConf 2017 Recap]]></title>
            <link>https://medium.com/android-testing-daily/kotlinconf-2017-recap-f667c3f8c8a?source=rss----ffe4584c7f8a---4</link>
            <guid isPermaLink="false">https://medium.com/p/f667c3f8c8a</guid>
            <category><![CDATA[conference]]></category>
            <category><![CDATA[functional-programming]]></category>
            <category><![CDATA[kotlin]]></category>
            <category><![CDATA[android-app-development]]></category>
            <category><![CDATA[android]]></category>
            <dc:creator><![CDATA[Chuck Greb]]></dc:creator>
            <pubDate>Wed, 15 Nov 2017 17:42:13 GMT</pubDate>
            <atom:updated>2017-11-15T18:52:21.087Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/733/1*VT7lz_JJsQ9rUW8iaXxMfQ.png" /></figure><p>Last week I attended the first ever <a href="https://www.kotlinconf.com/">KotlinConf</a>. The event was hosted by JetBrains and held in SF behind the massive buzz around Kotlin especially in the Android community. Attendees I spoke to were probably split 50/50 between Android and other platforms (web, server-side, native).</p><p>For those who are not familiar, Kotlin is strongly typed modern language with elements of functional programming, 100% interoperability with Java, and can be compiled into JVM bytecode, JavaScript, or native binary (iOS, desktop, etc).</p><p>This is the most excited I have been personally to be at a conference <em>as an attendee</em> in a long time. From the energy around the venue it was obvious I was not the only one.</p><h3>Huyen Tue Dao on Twitter</h3><p>More of the Android crew here and happy at #kotlinconf17</p><h3>Keynote Summary</h3><ul><li>This was the first ever KotlinConf and it sold out to over 1200 attendees.</li><li>Kotlin was first announced in 2011 and was first used on Android in 2012.</li><li>Google announced official support for Kotlin on Android at I/O 2017.</li><li>Kotlin projects in general require 15–20% less code than Java (Basecamp).</li><li>Teams that have migrated projects from Java to Kotlin have reported up to 30% fewer defects post-migration (Pinterest).</li><li>JetBrains recently announced multi-platform project support (code sharing) and support for web assembly, iOS, and desktop via Kotlin native.</li></ul><h3>Sessions</h3><p>Here’s a quick summary of some of my favorite sessions. You can find list of<a href="https://www.kotlinconf.com/sessions/"> all the sessions here</a>. And yes, I’m told there will be videos.</p><p><strong>Edit: </strong><a href="https://www.youtube.com/playlist?list=PLQ176FUIyIUY6UK1cgVsbdPYA3X5WLam5"><strong>The videos are here!</strong></a></p><h4>Introduction to Coroutines</h4><p><strong>Roman Elizarov<br></strong><em>Concurrency expert at JetBrains</em></p><p>Coroutines in many ways are a pure upgrade to traditional threads in terms of complexity and performance. This session covered the what, why, when, where (and how) of Kotlin coroutines.</p><h4>My Transition from Swift to Kotlin</h4><p><strong>Hector Matos<br></strong><em>Kotlin/Swift Dev at Twitter</em></p><p>Entertaining talk that covered the speaker’s personal experience learning Kotlin as an iOS/Swift developer. Compared the pros and cons of each language side-by-side and kept score the whole time. (Hint: it’s a tie).</p><h4>Architectures Using Functional Programming Concepts</h4><p><strong>Jorge Castillo<br></strong><em>GoMore Android Engineer</em></p><p>This talk outlined the benefits of functional programming concepts applied to Android architecture including immutability, statelessness, and deferred execution. Honestly, much of this talk was over my head from a functional programming standpoint but it was cool nonetheless.</p><h4>My Life as a Tech Transfer Monad</h4><p><strong>Erik Meijer<br></strong><em>Think Like A Fundamentalist, Code Like A Hacker</em></p><p>Erik is a living legend and a pioneer of reactive programming. This talk covered <em>a lot</em> but focused mostly on the ideas of abstraction, pragmatic solutions, probability, and machine learning and how they can be applied to increase developer productivity.</p><h3>Chuck Greb on Twitter</h3><p>New Kotlin slogan courtesy of Erik Meijer #kotlinconf17</p><h4>RxJava with Kotlin in Baby Steps</h4><p><strong>Annyce Davis<br></strong><em>Lead Android Developer at Off Grid Electric, Online Video Instructor</em></p><p>Great intro level talk to Rx concepts that made them simple enough it <em>almost</em> convinced me to try RxJava in my next Android project.</p><h4>Generating Kotlin Code</h4><p><strong>Alec Strong<br></strong><em>Android Developer at Square</em></p><p><strong>Jake Wharton<br></strong><em>Android Framework at Google</em></p><p>Cool talk about efficiently and idiomatically generating Kotlin code given its unique syntax and language features.</p><h3>Conclusion</h3><p>Overall a fantastic event (including the venue and food) and great to see the cross-pollination of the Android, iOS, and server development communities rallying around a common thread… <em>or coroutine.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f667c3f8c8a" width="1" height="1" alt=""><hr><p><a href="https://medium.com/android-testing-daily/kotlinconf-2017-recap-f667c3f8c8a">KotlinConf 2017 Recap</a> was originally published in <a href="https://medium.com/android-testing-daily">Android Testing</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Droidcon SF 2017]]></title>
            <link>https://medium.com/android-testing-daily/droidcon-sf-2017-a9bccab5dabf?source=rss----ffe4584c7f8a---4</link>
            <guid isPermaLink="false">https://medium.com/p/a9bccab5dabf</guid>
            <category><![CDATA[events]]></category>
            <category><![CDATA[clean-architecture]]></category>
            <category><![CDATA[testing]]></category>
            <category><![CDATA[san-francisco]]></category>
            <category><![CDATA[android]]></category>
            <dc:creator><![CDATA[Chuck Greb]]></dc:creator>
            <pubDate>Fri, 03 Nov 2017 15:49:56 GMT</pubDate>
            <atom:updated>2017-11-03T15:49:55.592Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/1*8BJRmgt5eaRKfwi_F-CU7g.png" /></figure><p>I’m pretty excited to be giving another talk about Android clean architecture and testing this weekend at Droidcon SF.</p><p>You can check out the <a href="https://sf.droidcon.com/">full schedule here</a>. Tickets are still available <em>and very reasonably priced</em> so its not to late to jump on board.</p><p>If you’re going to be there stop by and say hi.</p><p>Happy testing!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a9bccab5dabf" width="1" height="1" alt=""><hr><p><a href="https://medium.com/android-testing-daily/droidcon-sf-2017-a9bccab5dabf">Droidcon SF 2017</a> was originally published in <a href="https://medium.com/android-testing-daily">Android Testing</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Droidcon NYC 2017]]></title>
            <link>https://medium.com/android-testing-daily/droidcon-nyc-2017-c2bf6ad8e61c?source=rss----ffe4584c7f8a---4</link>
            <guid isPermaLink="false">https://medium.com/p/c2bf6ad8e61c</guid>
            <category><![CDATA[droidcon]]></category>
            <category><![CDATA[software-testing]]></category>
            <category><![CDATA[android-app-development]]></category>
            <category><![CDATA[clean-architecture]]></category>
            <category><![CDATA[android]]></category>
            <dc:creator><![CDATA[Chuck Greb]]></dc:creator>
            <pubDate>Fri, 22 Sep 2017 19:06:52 GMT</pubDate>
            <atom:updated>2017-09-22T19:06:52.122Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/828/1*SY2nYQLx456t3Qutgym3LQ.png" /></figure><p>Hey all, I’m going to be giving a talk about clean architecture and testing next week at Droidcon NYC.</p><p><a href="https://droidcon-server.herokuapp.com/showSession/103045">Droidcon NYC 2017 - Clean app design with Architecture Components</a></p><p>This is the fourth year going strong for the original Droidcon in the U.S., and its going to be the best one yet. You can check out the <a href="http://droidcon.nyc/">full schedule here</a>.</p><p>If you make decisions last minute (like I do) the good news is tickets are still available!</p><p><a href="https://www.eventbrite.com/e/droidcon-nyc-2017-tickets-34417872784">droidcon NYC 2017</a></p><p>If you are going to be around, come say hi! Hope to see you there.</p><p>Happy testing!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c2bf6ad8e61c" width="1" height="1" alt=""><hr><p><a href="https://medium.com/android-testing-daily/droidcon-nyc-2017-c2bf6ad8e61c">Droidcon NYC 2017</a> was originally published in <a href="https://medium.com/android-testing-daily">Android Testing</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Passive View]]></title>
            <link>https://medium.com/android-testing-daily/passive-view-e750bb10b20d?source=rss----ffe4584c7f8a---4</link>
            <guid isPermaLink="false">https://medium.com/p/e750bb10b20d</guid>
            <category><![CDATA[android-testing]]></category>
            <category><![CDATA[android-app-development]]></category>
            <category><![CDATA[android]]></category>
            <category><![CDATA[software-testing]]></category>
            <category><![CDATA[clean-architecture]]></category>
            <dc:creator><![CDATA[Chuck Greb]]></dc:creator>
            <pubDate>Tue, 19 Sep 2017 13:42:52 GMT</pubDate>
            <atom:updated>2017-09-19T13:42:52.191Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*YoGZ8mkm_8eT_sHqe8Zmkg.jpeg" /><figcaption>“<a href="https://www.flickr.com/photos/jasewong/7582743522/in/photolist-cy4xth-5gcKxN-8JwzFw-Ajth5G-YbdsKm-rLJGLe-GGhLE9-cgPmJE-ksn3fX-bsm8Lb-dwU326-mV4LEm-kc3qx9-pu4cQv-XzRSxk-pF5VuK-mJRJjn-5Fjwzv-6U1PR4-iTdeqp-Yy7QLh-b7bYbk-ooc44W-5h2PBe-nkgPo3-dh7Te7-regunt-YBai7k-qWGVEu-d3LqNo-fAymsQ-dSQ8Ni-4D8Gw1-g1Sn1o-9SdCLV-9rx5Z1-cqM39j-jpQ3rj-97KDBt-7aoozd-pKpR8o-eFF6NZ-bsfU1L-e3D39k-kJQ1Sp-doxVVb-j31ReE-cE44vf-vMUmSu-9d5GgN">Bamboo</a>” by <a href="https://www.flickr.com/photos/jasewong/">Jason Wong</a> licensed under <a href="https://creativecommons.org/licenses/by-nc-nd/2.0/">CC BY-NC-ND 2.0</a></figcaption></figure><p><a href="https://martinfowler.com/eaaDev/PassiveScreen.html">Passive View</a> is the concept that logic in UI code should be minimized. This idea is also sometimes referred to as <a href="https://martinfowler.com/eaaDev/uiArchs.html#HumbleView">Humble View</a>.</p><p>View code is typically the most difficult part of an application to test. <a href="https://medium.com/android-testing-daily/the-3-tiers-of-the-android-test-pyramid-c1211b359acd">UI tests are slow, brittle, and expensive</a>.</p><p>In Android this is especially true. Tests written using Espresso or UI Automator require c<a href="https://developer.android.com/training/testing/unit-testing/instrumented-unit-tests.html#run">ompiling, dexing, and installing not one but two applications</a> on an emulator or physical device.</p><p>This unfortunate truth is often mitigated by extracting presentation logic out of the view layer and into a <strong>controller</strong>, <strong>presenter</strong>, or <strong>view model</strong> where it can be more easily maintained and tested.</p><p>Controllers, presenters, and view models all play a similar role in clean architecture but there are subtle differences in the flow of information.</p><h4>Model View Controller (MVC)</h4><p>MVC architecture implies a one-way flow of information. View events are handled by framework classes (activity, fragment, or custom view) and then forwarded to the controller.</p><p>The controller performs input validation and then passes the relevant data to the domain model. Finally, an update is triggered in view layer using the observer pattern and/or data binding.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/451/1*WZtYqmq4rG51cq3UhHcctQ.png" /><figcaption>MVC uses a one-way flow of information</figcaption></figure><h4>Model View Presenter (MVP)</h4><p>MVP architecture uses a bi-directional flow of information. UI events are forwarded from the framework classes to the presenter for input validation. The presenter relies on the domain model to perform business logic and/or mutation of state.</p><p>The primary difference between a presenter and a controller is that the presenter then performs any presentation logic associated with the new state and then calls back to the view layer through an interface.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/454/1*ufxIJmJ2GOi-rTqhfmrl7w.png" /><figcaption>MVP pushes updates back to the view via an interface</figcaption></figure><h4>Model View ViewModel (MVVM)</h4><p>MVVM also uses bi-direction flow of information, but unlike MVC there is no dependency between the view and the domain model.</p><p>Rather the view maintains two connections to the view model class. One is to forward user input and UI events. The other is an observer that listens for changes in the view model state to trigger an update to the display.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/457/1*IQYj51xmxei9Jn4Q4waEbw.png" /><figcaption>In MVVM, the view observes changes in the view model</figcaption></figure><h4>Sit Down, Be Humble</h4><p>Of these three architectures, the only one that allows for a truly passive (humble) view is MVP.</p><p>Why?</p><p>In both MVC and MVVP, some logic must exist in the view to <a href="https://medium.com/android-testing-daily/state-synchronization-5e415709dcc7">synchronize screen state and session state</a>. Whether it is data binding expressions in the layout, or callbacks registered by activity as an observer, this logic is impossible to eliminate and difficult to test.</p><p>With MVP, synchronization is handled by the presenter resulting in a legit dumb view. The tradeoff here is that testing becomes slightly more verbose since the view must be replaced by a stub that implements the same interface in the test harness.</p><h4>Should I use Passive View?</h4><p>The answer to this question depends on the nature of your individual application.</p><p>For simpler apps, MVC or MVVM can be sufficient as sync logic will be minimal and unit tests will requires less boilerplate code to implement.</p><p>For more complex applications, I always recommend using a Passive View. As sync logic grows in complexity testing it becomes increasingly vital and is well worth the tradeoff of maintaining a test double of the view driver.</p><p>That’s all for today. As always, happy testing!</p><p><em>This post is part of a series on clean architecture that explores how classic software design principles can be applied to modern Android development.</em></p><p><strong>If you found this article helpful, please give it some applause 👏 to help others find it. Feel free to leave a comment below.</strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e750bb10b20d" width="1" height="1" alt=""><hr><p><a href="https://medium.com/android-testing-daily/passive-view-e750bb10b20d">Passive View</a> was originally published in <a href="https://medium.com/android-testing-daily">Android Testing</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Case Study: Login]]></title>
            <link>https://medium.com/android-testing-daily/case-study-login-9579928167fe?source=rss----ffe4584c7f8a---4</link>
            <guid isPermaLink="false">https://medium.com/p/9579928167fe</guid>
            <category><![CDATA[login]]></category>
            <category><![CDATA[software-testing]]></category>
            <category><![CDATA[clean-architecture]]></category>
            <category><![CDATA[android]]></category>
            <category><![CDATA[android-app-development]]></category>
            <dc:creator><![CDATA[Chuck Greb]]></dc:creator>
            <pubDate>Sun, 17 Sep 2017 16:40:16 GMT</pubDate>
            <atom:updated>2017-09-17T16:40:16.314Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/641/1*VG7QYEdIyo4spiq52e_HkA.jpeg" /></figure><p>Clean architecture is not a one-size-fits-all solution.</p><p>It’s not even a single solution, but rather a collection of principles and patterns that can be applied to organize your code and enable testing.</p><h3>Clean architecture principles (an incomplete list)</h3><ul><li>Forms and controls</li><li>Separated presentation</li><li>Passive view</li><li>Model-View-Controller (MVC)</li><li>Model-View-Presenter (MVP)</li><li>Model-View-ViewModel (MVVM)</li><li>Model-View-Intent (MVI)</li><li>The Clean Architecture (Uncle Bob)</li><li>Observer pattern</li><li>Repository pattern</li><li>Domain model</li><li>Data binding</li><li>Dependency Inversion Principle</li><li>Inversion of Control</li><li>State synchronization</li><li>Dependency injection</li><li><a href="https://medium.com/android-testing-daily/the-3-tiers-of-the-android-test-pyramid-c1211b359acd">Testing</a></li></ul><p>These concepts are not unique to Android. <a href="https://medium.com/android-testing-daily/history-of-clean-architecture-9a5e73060ee1">Most have been around longer than the platform itself</a>. The challenge for us as Android developers is when and how to best apply them.</p><p>Many of these concepts can be used alone or in conjunction with each other. For example, using <strong>dependency injection</strong> to provide a <strong>repository</strong> to your <strong>presentation layer</strong>.</p><p>However it is important to not try jamming all of these tools into every application for this will lead to a cluttered codebase that is anything but <em>clean</em>.</p><p>Which principles and patterns you should use really depends on the individual application. That’s why I think its useful to look at <em>case studies</em>.</p><p>By evaluating various case studies you can see how the same principle or pattern can (or can’t) be applied to different types of applications.</p><h3>Login Case Study</h3><p>Login presents a common combination of problems including accepting user input, comparing it against some remote data store, and updating the UI based on the result.</p><p>Using Android Studio’s built-in templates we can generate the code for a basic login activity. What we get is a monolithic ~300 line activity that handles all facets of login including input validation, presentation logic, as well as a simulated network call wrapped in an AsyncTask.</p><p>And that is in Kotlin. I’m pretty sure the Java version would be over 1,000 lines of code but you can check for yourself.</p><p>I’ll spare you the nitty-gritty details for now but you can check out the full source code here.</p><p><a href="https://github.com/ecgreb/loginator">ecgreb/loginator</a></p><p>In the next series of posts we will explore how we can apply some of the classic principles and patterns of clean architecture to the login use case.</p><p>Stay tuned!</p><p><em>This post is part of a series on clean architecture that explores how classic software design principles can be applied to modern Android development.</em></p><p><strong>If you found this article helpful, please give it some applause 👏 to help others find it. Feel free to leave a comment below.</strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9579928167fe" width="1" height="1" alt=""><hr><p><a href="https://medium.com/android-testing-daily/case-study-login-9579928167fe">Case Study: Login</a> was originally published in <a href="https://medium.com/android-testing-daily">Android Testing</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>