glide icon indicating copy to clipboard operation
glide copied to clipboard

Glide Compose Nodes aren't displayed under createComposeRule

Open donaldchai opened this issue 7 months ago • 2 comments
trafficstars

Here's a minimal self-contained test case:

package com.bumptech.glide.integration.compose

import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.testTag
import androidx.compose.ui.test.assertIsDisplayed
import androidx.compose.ui.test.junit4.createComposeRule
import androidx.compose.ui.test.onNodeWithTag
import androidx.compose.ui.test.onRoot
import androidx.compose.ui.test.printToLog
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
import org.robolectric.annotation.Config

@OptIn(ExperimentalGlideComposeApi::class)
@RunWith(RobolectricTestRunner::class)
class GlideImageTest {

  @get:Rule(order = 1) val composeRule = createComposeRule()

  @Test
  fun robo_asdf() {
    val tag = "testtag"
    composeRule.setContent {
      GlideImage(
        model = android.R.drawable.star_big_on,
        contentDescription = "desc",
        modifier = Modifier.testTag(tag),
      )
    }

    composeRule.waitForIdle()

    composeRule.onRoot(useUnmergedTree = true).printToLog("GlideImageTest")
    composeRule.onNodeWithTag(tag).assertIsDisplayed()  // fails
  }
}

I've bisected this down to https://github.com/bumptech/glide/pull/5240.

Curiously, this works on an emulator:

--- a/integration/compose/src/androidTest/java/com/bumptech/glide/integration/compose/GlideImageTest.kt
+++ b/integration/compose/src/androidTest/java/com/bumptech/glide/integration/compose/GlideImageTest.kt
@@ -24,9 +24,11 @@ import androidx.compose.ui.layout.ContentScale
 import androidx.compose.ui.platform.LocalDensity
 import androidx.compose.ui.platform.testTag
 import androidx.compose.ui.test.assert
+import androidx.compose.ui.test.assertIsDisplayed
 import androidx.compose.ui.test.captureToImage
 import androidx.compose.ui.test.hasTestTag
 import androidx.compose.ui.test.onNodeWithContentDescription
+import androidx.compose.ui.test.onNodeWithTag
 import androidx.compose.ui.test.onNodeWithText
 import androidx.compose.ui.test.performClick
 import androidx.compose.ui.test.performScrollToIndex
@@ -468,4 +470,20 @@ class GlideImageTest {
       .captureToImage()
       .compareToGolden(testName.methodName)
   }
+
+  @Test
+  fun asdf() {
+    val testTag = "testTag"
+    glideComposeRule.setContent {
+      GlideImage(
+        model = android.R.drawable.star_big_on,
+        contentDescription = "test",
+        modifier = Modifier.testTag(testTag),
+      )
+    }
+
+    glideComposeRule.waitForIdle()
+
+    glideComposeRule.onNodeWithTag(testTag).assertIsDisplayed()
+  }
 }

donaldchai avatar Apr 10 '25 05:04 donaldchai

Another note, the test I was starting from looked more like this:

  @get:Rule(order = 1)
  val activityRule = ActivityScenarioRule(AppCompatActivity::class.java)
  @get:Rule(order = 2) val composeRule = createEmptyComposeRule()

  private lateinit var composeView: ComposeView

  @Before
  fun setUp() {
    activityRule.scenario.onActivity { activity ->
      composeView = ComposeView(activity)
      activity.setContentView(composeView)
    }
  }

  @Test
  fun bar() {
    // fails with the Box, passes without
    composeRule.setContent {
      Box {
        GlideImage(
          model = android.R.drawable.star_big_on,
          contentDescription = "desc",
          modifier = Modifier.testTag("tag"),
        )
      }
    }

    composeRule.onNodeWithTag("tag").assertIsDisplayed()
  }

where removing the Box made a difference in getting the measurements correct.

donaldchai avatar Apr 10 '25 05:04 donaldchai

It looks like my first test fails on an emulator as well. I guess the main difference is that GlideComposeRule uses GlideIdlingResourceInit?

donaldchai avatar Apr 10 '25 07:04 donaldchai