How to test against different display sizes in android (espresso)? - android

In robolectric we can use annotations inside unit tests to test against specific display sizes/densities, like
#Config(qualifiers = "w2000dp")
My aim is that my test should only run when a specific condition is given (here: w2000dp).
I'm searching for a similar way to do this when testing with Espresso.
Currently I have two alternative (fall back) solution I could use to solve the issue, but maybe there is something more generic like in robolectric.
a) I can place a parameter inside ressource value folder (values-w1000dp, values-w2000dp).
I could read the value of that parameter (displayWidth) inside the test and decide whether to run the test or not.
values-w1000dp/strings.xml
<string name="displayWidth">1000dp</string>
values-w2000dp/strings.xml
<string name="displayWidth">2000dp</string>
b) I could read out the display size / density programmatically inside the test and decide, whether to run the test or not.
But both solutions feel wrong. Is there any other common way to solve this?

Related

How to give fake value for androidx.wear.compose.material.TimeText for testing

I am using androidx.wear.compose.material.TimeText (link) in my wear app to show the time on top of the screen. I am trying to write a screenshot test where I set a fake time for the screen.
TimeText has timeSource (link) parameter that I can manually set in order to give time. For this, I created real and fake implementations of TimeSource to use in production and tests, respectively using Hilt.
However, I have one condition. Inside my test class, I need to do something like:
private val fakeTimeSource = FakeTimeSource()
fakeTimeSource.setCurrentTime("10:10")
And I should not pass fakeTimeSource to any class that uses real TimeSource but it should update the time. Is it possible? If yes, how can I achieve this?
Note: Or any other approach is fine to fake it. It doesn't have to use TimeSource.

For Android apps, how do I check what resource ID is being used in a specific image view?

I'm writing automated tests for my company's app. The tests use a combination of Espresso and Robotium. Several of those tests are supposed to check whether certain ReactImageView elements are displaying a specific image.
I first tried using hasBackground() as follows:
onView(...).check(matches(hasBackground(R.drawable...)));
However, this returns a NoMatchingViewException.
I then tried Daniele Bottillo's matcher as described here: https://medium.com/#dbottillo/android-ui-test-espresso-matcher-for-imageview-1a28c832626f
onView(allOf(..., new DrawableMatcher(R.drawable....), isDisplayed())).check(matches(isDisplayed()));
But this didn't work either:
java.lang.ClassCastException: com.facebook.drawee.generic.RootDrawable cannot be cast to android.graphics.drawable.BitmapDrawable
Lastly, I tried Frankie Sardo's solution: Android Espresso match BitmapDrawables with different tint
onView(allOf(..., withImageDrawable(R.drawable...), isDisplayed())).check(matches(isDisplayed()));
However, this again throws a NoMatchingViewException. I'm running out of ideas. Is it even possible to check what resource is attached to an image view?
The easiest way would be to use Barista library and its assertHasDrawable(R.id.<imageview_id>, R.drawable.<drawable_id>) assertion function.

Android Unit Test + DecimalFormat

I have a method that formats a number, and I wanted to test it.
I built a test calling this method and the result showed that: decimalFormat.setRoundingMode(\* Any rounding mode \*);
doesn't work. It always takes the default RoundingMode.HALF_EVEN.
My method works fine - decimalFormat.setRoundingMode(RoundingMode.HALF_UP); do work when running the app (I copied the test to the app and logged it..)
Why can't I call decimalFormat.setRoundingMode() in a unit test?
Please post your code. Replace the RoundingMode.HALF_UP value with its hard coded primitive value (the value behind it).The static call to RoundingMode might be the cause of this.
It also seems to me that your unit test is wrong, you're checking a behavior of a class based on an existing implementation of your dependency (decimalFormat). You should never do this in an unit test instead you have to mock decimalFormat away and test the behavior of your method independently from the implementation of 'decimalFormat' or any other dependency.

Correct way to check text of button?

Correct way to check text of button
I need to check that English text on button is "Start this".
In string.xml
<string name="start">Start this</string>
So here Espresso test:
Approach#1
private val buttonStart = viewWithId(startButton)
#Test
fun buttonStartText() {
onView(withId(startButton))
.check(matches(withText(R.string.start)))
}
The test "buttonStartText" success pass. Nice.
Approach#2
Check hard code text.
#Test
fun buttonStartText() {
onView(withId(startButton))
.check(matches(withText("Start this")))
}
The test "buttonStartText" success pass. Nice.
The question is:
What approach is correct?
What I mean.
Suppose the developer made error and in string.xml change value of key "start" to "Start this 2222".
E.g.
<string name="start">Start this 2222</string>
As result the test in approach#1 is still pass. This is NOT CORRECT behavior.
But test in approach#2 is broken. This is a correct behavior.
As result I think test in approach#2 is correct way to check title/text on button.
Is I'm right?
P.S.
But if I has multi language app (e.g. 3 languages). In approach#1 nothing cha nge. But in in approach#2 I need to add two additional tests that check text in specific languages.
I think approach #1 is correct as you cover all the local's in that else in approach #2 you only cover english or specific language.
To be honest both approach are correct.
Because approach #1 checking if there is a button with text from R.strings..
but approach #2 checking if there is exactly that text.
So You are checking two different things. Depends what You want to test.

In Android Project :- In my application there are lots of images, i have to verify them, How can i use Assertion

In Robotium :- I am writing automation scripts using Solo class. Now i have to verify the images in my application.
For texts we can use Assert.assertTrue(solo.searchText("rtf"));
For images, is there any command in Robotium Solo Class?
Not really. Most suggested way to test (its meaning may vary) ImageViews is given by:
In code under test, set something in the ImageView tag that you want to check later. E.g. the drawable resource Id.
In test code, get whatever you set in the tag, and verify it has the expected value.
Also, you may want to have a look at this discussion on some other method to visually check the image.
You can try this:
assertTrue(solo.getCurrentActivity().getResources().getDrawable(R.drawable.logo).isVisible());
For more: https://stackoverflow.com/a/26114862

Categories

Resources