Skip to content
This repository was archived by the owner on Jan 11, 2024. It is now read-only.
This repository was archived by the owner on Jan 11, 2024. It is now read-only.

Android Basics: Dogglers app #37

@asaywitz

Description

@asaywitz

URL of codelab
https://developer.android.com/codelabs/basic-android-kotlin-training-project-dogglers-app?continue=https%3A%2F%2Fdeveloper.android.com%2Fcourses%2Fpathways%2Fandroid-basics-kotlin-unit-2-pathway-3%23codelab-https%3A%2F%2Fdeveloper.android.com%2Fcodelabs%2Fbasic-android-kotlin-training-project-dogglers-app#3

In which task and step of the codelab can this issue be found?
4. Test your app

Describe the problem
4 of the 19 tests faill

For 3 of the tests, they all fail for the same reason.
The test is not handling the string resources with variables.
This happens for age and hobbies

<string name="dog_age">Age: %1$s</string>
<string name="dog_hobbies">Hobbies: %1$s</string>

But in BaseTest.hasListItemContent() you have this:

    onView(withText(age))
        .check(matches(isDisplayed()))
    onView(withText(hobbies))
        .check(matches(isDisplayed()))

This won't work because the actual TextView will be a like "Age: 7" and you can calling withText("7")

So to fix you can just do this:

  1. Add a private method so you don't duplicate your code (dry)

    private fun getResourceStringWithVariable(id: Int, variable: String): String? {
    val targetContext: Context = ApplicationProvider.getApplicationContext()
    return targetContext.getResources().getString(id, variable)
    }

  2. Fix your tests to be this:

     onView(withText(getResourceStringWithVariable(R.string.dog_age, age)))
         .check(matches(isDisplayed()))
     onView(withText(getResourceStringWithVariable(R.string.dog_hobbies, hobbies)))
         .check(matches(isDisplayed()))
    

The fourth test HorizontalListTests.horizontal_scrolling() is failing, because at least on on my emulator (pixel 5 api 31) if you do a swipeLeft you actually go inbetween the third and fourth card so the nama showing is from the fourth card "Nox" not the third card "Frankie" So instead of

fun `horizontal_scrolling`() {
    onView(withId(R.id.horizontal_recycler_view))
        .perform(swipeLeft())
    onView(withText("Nox")).check(matches(isDisplayed()))
}

it should be

fun `horizontal_scrolling`() {
    onView(withId(R.id.horizontal_recycler_view))
        .perform(swipeLeft())
    onView(withText("Nox")).check(matches(isDisplayed()))
}

Steps to reproduce?

  1. Run the tests and you can see 4 failing ones.

Versions
Android Studio Chipmunk | 2021.2.1 Patch 1
Build #AI-212.5712.43.2112.8609683, built on May 18, 2022
Runtime version: 11.0.12+0-b1504.28-7817840 x86_64
VM: OpenJDK 64-Bit Server VM by JetBrains s.r.o.
macOS 10.15.7
GC: G1 Young Generation, G1 Old Generation
Memory: 2048M
Cores: 4
Registry: external.system.auto.import.disabled=true
Non-Bundled Plugins: org.intellij.plugins.markdown (212.5457.16), Dart (212.5744), io.flutter (69.0.2)

Additional information
Include screenshots if they would be useful in clarifying the problem.
Screen Shot 2022-07-09 at 4 51 17 PM

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions