I am developing an Android application using Kotlin. I am writing instrumented tests for my application. Now, I am having a problem testing ImageButton because it is not working as expected. I want to test that an ImageButton view is set with the right drawable when the activity is opened.
In my activity XML layout file, I have an ImageButton with the following code
<ImageButton
android:id="@+id/camera_image_btn_capture"
android:adjustViewBounds="true"
android:scaleType="centerInside"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
android:background="@null"
android:src="@drawable/camera_capture"
android:layout_width="85dp"
android:layout_height="85dp" />
I have written a test to assert that the image button view is set with the right drawable resource. Following is the implementation of my test.
@RunWith(AndroidJUnit4::class)
@LargeTest
class CameraTest: TestBuilder()
{
@get:Rule
var cameraActivityRule: ActivityTestRule<CameraActivity> = ActivityTestRule<CameraActivity>(CameraActivity::class.java, true, false)
@get:Rule
var permissionRule: GrantPermissionRule = GrantPermissionRule.grant(android.Manifest.permission.CAMERA, android.Manifest.permission.WRITE_EXTERNAL_STORAGE, android.Manifest.permission.READ_EXTERNAL_STORAGE)
@Test
fun cameraCaptureImageButtonIsInitiallySetWithCaptureImage() {
this.cameraActivityRule.launchActivity(null)
val imageButtonCapture: ImageButton = this.cameraActivityRule.activity.findViewById<ImageButton>(R.id.camera_image_btn_capture)
Assert.assertEquals(imageButtonCapture.drawable, this.cameraActivityRule.activity.getDrawable(R.drawable.camera_capture))
}
}
As you can see in the test method, I am asserting that if two drawable resources are equal. When I run the test, the test always fails. But it is supposed to pass. What is missing in my code and how can I fix it?