{"id":16584031,"url":"https://github.com/ajay-prabhakar/espresso","last_synced_at":"2025-03-06T04:17:05.604Z","repository":{"id":129733335,"uuid":"201724743","full_name":"ajay-prabhakar/Espresso","owner":"ajay-prabhakar","description":"Tea ordering app that demonstrates various uses of the Espresso Testing framework (i.e. Views, AdapterViews, Intents, IdlingResources)","archived":false,"fork":false,"pushed_at":"2019-08-23T16:03:37.000Z","size":3753,"stargazers_count":4,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-01-16T14:52:11.373Z","etag":null,"topics":["android","android-testing-espresso","coffee","java","order"],"latest_commit_sha":null,"homepage":"https://classroom.udacity.com/courses/ud855/lessons/f0084cc7-2cbc-4b8e-8644-375e8c927167/concepts/f0c53eb6-722d-4558-b317-d4205dc7822d","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ajay-prabhakar.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-08-11T06:03:16.000Z","updated_at":"2020-05-22T10:21:38.000Z","dependencies_parsed_at":null,"dependency_job_id":"94ebe30b-432f-41c9-b13e-b7d8aba8af87","html_url":"https://github.com/ajay-prabhakar/Espresso","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ajay-prabhakar%2FEspresso","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ajay-prabhakar%2FEspresso/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ajay-prabhakar%2FEspresso/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ajay-prabhakar%2FEspresso/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ajay-prabhakar","download_url":"https://codeload.github.com/ajay-prabhakar/Espresso/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":242144582,"owners_count":20078970,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["android","android-testing-espresso","coffee","java","order"],"created_at":"2024-10-11T22:43:46.267Z","updated_at":"2025-03-06T04:17:05.597Z","avatar_url":"https://github.com/ajay-prabhakar.png","language":"Java","funding_links":[],"categories":[],"sub_categories":[],"readme":"# TeaTime Code\n\nThis is a exercise repository for the TeaTime example app which is part of Udacity's Advanced Android course. TeaTime is a mock tea ordering app that demonstrates various uses of the Espresso Testing framework (i.e. Views, AdapterViews, Intents, IdlingResources). You can learn more about how to use this repository [here](https://classroom.udacity.com/courses/ud857/lessons/8b2a9d63-0ff5-48ff-90d3-a9855b701dae/concepts/41b82e3c-2797-46e5-8a66-684098ca8cbb)\n\n**Android has mainly two types of tests**\n\n##### Unit Tests\n\nTesting every method/function (or unit) of your code for e.g: given a function, calling it with param x should return y. These tests are run on JVM locally without the need of an emulator or Device.\n\n##### Instrumentation Testing\n\nInstrumentation tests are used for testing Android Frameworks such as UI,SharedPreferences and So on.Since they are for Android Framework they are run on a device or an emulator.\nInstrumentation tests use a separate apk for the purpose of testing. Thus, every time a test case is run, Android Studio will first install the target apk on which the tests are conducted. After that, Android Studio will install the test apk which contains only test related code.\n\n**What is Espresso?**\n\nEspresso is an instrumentation Testing framework made available by Google for the ease of UI Testing.\n\n##### Setting Up\nGo to your app/build.gradle\n\n1.Add dependencies\n```\nandroidTestCompile ‘com.android.support.test.espresso:espresso-core:3.0.1’\nandroidTestCompile ‘com.android.support.test:runner:1.0.1’\n```\n2.Add to the same build.gradle file the following line in\n\n```\nandroid.defaultConfig{\ntestInstrumentationRunner “android.support.test.runner.AndroidJUnitRunner”\n}\n```\n\nThis sets up the Android Instrumentation Runner in our app.\n\n**AndroidJUnitRunner** is the instrumentation runner. This is essentially the entry point into running your entire suite of tests. It controls the test environment, the test apk, and launches all of the tests defined in your test package\n\nAnd annotate it with `@RunWith(AndroidJUnit4::class)`\n\nThe instrumentation runner will process each test class and inspect its annotations. It will determine which class runner is set with @RunWith, initialize it, and use it to run the tests in that class. In Android’s case, the AndroidJUnitRunner explicitly checks if the AndroidJUnit4 class runner is set to allow passing configuration parameters to it.\n\n**Important things to note is that**\nThe activity will be launched using the `@Rule` before test code begins\n\nBy default the rule will be initialised and the activity will be launched(onCreate, onStart, onResume) before running every `@Before` method\n\nActivity will be Destroyed(onPause, onStop, onDestroy) after running the `@After` method which in turn is called after every `@Test` Method\n\nThe activity’s launch can be postponed by setting the launchActivity to false in the constructor of ActivityTestRule ,in that case you will have to manually launch the activity before the tests\n\n### Example Code\nIn the example below we do the testing of a login screen where we search the login and password edit text and enter the values and after that we test the two scenarios Login success and Login Failure\n\n```\nimport android.support.test.rule.ActivityTestRule\nimport android.support.test.runner.AndroidJUnit4\nimport org.junit.runner.RunWith\nimport android.support.test.espresso.Espresso;\nimport android.support.test.espresso.action.ViewActions\nimport android.support.test.espresso.assertion.ViewAssertions.matches\nimport android.support.test.espresso.matcher.ViewMatchers.*\n\n@RunWith(AndroidJUnit4::class)\nclass MainActivityInstrumentationTest {\n\n    @Rule\n    @JvmField\n    public val rule  = ActivityTestRule(MainActivity::class.java)\n\n    private val username_tobe_typed=\"Chromicle\"\n    private val correct_password =\"password\"\n    private val wrong_password = \"passme123\"\n\n    @Test\n    fun login_success(){\n        Log.e(\"@Test\",\"Performing login success test\")\n        Espresso.onView((withId(R.id.user_name)))\n                .perform(ViewActions.typeText(username_tobe_typed))\n\n        Espresso.onView(withId(R.id.password))\n                .perform(ViewActions.typeText(correct_password))\n\n        Espresso.onView(withId(R.id.login_button))\n                .perform(ViewActions.click())\n\n        Espresso.onView(withId(R.id.login_result))\n                .check(matches(withText(R.string.login_success)))\n    }\n\n    @Test\n    fun login_failure(){\n        Log.e(\"@Test\",\"Performing login failure test\")\n        Espresso.onView((withId(R.id.user_name)))\n                .perform(ViewActions.typeText(username_tobe_typed))\n\n        Espresso.onView(withId(R.id.password))\n                .perform(ViewActions.typeText(wrong_password))\n\n        Espresso.onView(withId(R.id.login_button))\n                .perform(ViewActions.click())\n\n        Espresso.onView(withId(R.id.login_result))\n                .check(matches(withText(R.string.login_failed)))\n    }\n}\n```\n\n## Idling Resource\n\n[this](https://github.com/udacity/AdvancedAndroid_TeaTime/blob/TESP.03-Solution-AddOrderSummaryActivityTest/app/src/androidTest/java/com/example/android/teatime/OrderSummaryActivityTest.java) is a very simple implementation of the IdlingResource interface\n\nRemember that if idle is false there are pending operations in the background and any testing operations should be paused. If idle is true all is clear and testing operations can continue.\n\nImplementing the IdlingResource interface is straight forward: it requires completing the 3 required methods. We also created an instance of AtomicBoolean in order to control idleness across multiple threads.\n\nWe also declare a private variable called mIdlingResource of type SimpleIdlingResource. Notice that it has an annotation @Nullable which indicates that this variable will be null in production. This is because this setup with IdlingResource is only used for testing, so when the project is run in production, IdlingResource can be null.\n\n#### Summary\n\nWhen the changeTextBt is clicked, onClick() in MainActivity triggers MessageDelayer.processMessage().\n\nprocessMessage() sets the IdlingResource to false, then creates a Handler which contains a Runnable that will be run after a pre-determined time delay, DELAY_MILLIS. The Runnable that will be executed after the delay:\n\n1) Returns the String entered by the user via a callback to the calling activity (e.g. MainActivity)\n\n2) Sets the IdlingResource to true\n\n[class](https://classroom.udacity.com/courses/ud855/lessons/f0084cc7-2cbc-4b8e-8644-375e8c927167/concepts/1449718b-df48-4789-a152-4e52f0093006)\n\n\u003cimg width=\"437\" alt=\"screen-shot-2017-03-09-at-2 27 40-pm\" src=\"https://user-images.githubusercontent.com/48018942/63033351-1c159a00-bed5-11e9-9a4d-e9bfb457b314.png\"\u003e\n\nImplement the IdlingResource interface (SimpleIdlingResource.java)\n\nCreate a callback interface (MessageDelayer.java) where the actual asynchronous task will occur\n\nSet the state of IdlingResource to false when the task is running, and then back to true when the task is done\n\nHave the delayer notify the activity that the process is complete via a callback (MainActivity.onDone)\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fajay-prabhakar%2Fespresso","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fajay-prabhakar%2Fespresso","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fajay-prabhakar%2Fespresso/lists"}