[go: nahoru, domu]

Changes in captureToBitmap.

Fixes the captureToBitmap to work with ActionBar and cut outs. This also
replaces the way we were obtaining the screenshots. Instead of taking a
bitmap from a window we use UiAutomator which actually takes a real
screenshot. This enabled us to capture multiple windows and defend
against cases where popup could be on top of our view. Finally it also
removes the need to juggle with Activities (and casting them) and also
removes the O+ API restriction.

Finally I have also removed the method used to capture full screen. The
reason is that no one used it and it is tricky to define how it should
work. E.g. it does not have much value if we don't crop out the action
bar. With that and the fact that developers can use UiAutomator for
fullscreen screenshots it is not beneficial to keep that API around. We
can reintroduce it in the future once we need to.

Bug: 157657482
Test: Added
Change-Id: I5320d61c7a4ebc03472b0cb79c3da6c3870482f3
diff --git a/ui/ui-test/src/androidTest/java/androidx/ui/test/BitmapCapturingTest.kt b/ui/ui-test/src/androidTest/java/androidx/ui/test/BitmapCapturingTest.kt
index 72b497c..75c4e0b5 100644
--- a/ui/ui-test/src/androidTest/java/androidx/ui/test/BitmapCapturingTest.kt
+++ b/ui/ui-test/src/androidTest/java/androidx/ui/test/BitmapCapturingTest.kt
@@ -16,9 +16,9 @@
 
 package androidx.ui.test
 
-import android.os.Build
+import androidx.activity.ComponentActivity
 import androidx.test.filters.MediumTest
-import androidx.test.filters.SdkSuppress
+import androidx.test.rule.ActivityTestRule
 import androidx.ui.core.Modifier
 import androidx.ui.core.testTag
 import androidx.ui.foundation.Box
@@ -27,7 +27,10 @@
 import androidx.ui.layout.Column
 import androidx.ui.layout.Row
 import androidx.ui.layout.fillMaxSize
+import androidx.ui.layout.padding
 import androidx.ui.layout.preferredSize
+import androidx.ui.test.android.AndroidComposeTestRule
+import androidx.ui.test.gesturescope.ActivityWithActionBar
 import androidx.ui.unit.IntPxPosition
 import androidx.ui.unit.IntPxSize
 import androidx.ui.unit.ipx
@@ -35,15 +38,26 @@
 import org.junit.Rule
 import org.junit.Test
 import org.junit.runner.RunWith
-import org.junit.runners.JUnit4
+import org.junit.runners.Parameterized
 
 @MediumTest
-@RunWith(JUnit4::class)
-@SdkSuppress(minSdkVersion = Build.VERSION_CODES.O)
-class BitmapCapturingTest {
+@RunWith(Parameterized::class)
+class BitmapCapturingTest(val config: TestConfig) {
+    data class TestConfig(
+        val activityClass: Class<out ComponentActivity>
+    )
+
+    companion object {
+        @JvmStatic
+        @Parameterized.Parameters(name = "{0}")
+        fun createTestSet(): List<TestConfig> = listOf(
+            TestConfig(ComponentActivity::class.java),
+            TestConfig(ActivityWithActionBar::class.java)
+        )
+    }
 
     @get:Rule
-    val composeTestRule = createComposeRule()
+    val composeTestRule = AndroidComposeTestRule(ActivityTestRule(config.activityClass))
 
     private val rootTag = "Root"
     private val tag11 = "Rect11"
@@ -101,17 +115,6 @@
             }
     }
 
-    @Test
-    fun captureWholeWindow_checkSizeAndColors() {
-        composeCheckerboard()
-
-        composeTestRule
-            .captureScreenOnIdle()
-            .assertPixels() {
-                expectedColorProvider(it)
-            }
-    }
-
     @Test(expected = AssertionError::class)
     fun assertWrongColor_expectException() {
         composeCheckerboard()
@@ -155,30 +158,32 @@
         with(composeTestRule.density) {
             composeTestRule.setContent {
                 Box(Modifier.fillMaxSize(), backgroundColor = colorBg) {
-                    Column(Modifier.testTag(rootTag)) {
-                        Row {
-                            Box(Modifier
-                                .testTag(tag11)
-                                .preferredSize(100.ipx.toDp(), 50.ipx.toDp())
-                                .drawBackground(color11)
-                            )
-                            Box(Modifier
-                                .testTag(tag12)
-                                .preferredSize(100.ipx.toDp(), 50.ipx.toDp())
-                                .drawBackground(color12)
-                            )
-                        }
-                        Row {
-                            Box(Modifier
-                                .testTag(tag21)
-                                .preferredSize(100.ipx.toDp(), 50.ipx.toDp())
-                                .drawBackground(color21)
-                            )
-                            Box(Modifier
-                                .testTag(tag22)
-                                .preferredSize(100.ipx.toDp(), 50.ipx.toDp())
-                                .drawBackground(color22)
-                            )
+                    Box(Modifier.padding(top = 20.ipx.toDp()), backgroundColor = colorBg) {
+                        Column(Modifier.testTag(rootTag)) {
+                            Row {
+                                Box(Modifier
+                                    .testTag(tag11)
+                                    .preferredSize(100.ipx.toDp(), 50.ipx.toDp())
+                                    .drawBackground(color11)
+                                )
+                                Box(Modifier
+                                    .testTag(tag12)
+                                    .preferredSize(100.ipx.toDp(), 50.ipx.toDp())
+                                    .drawBackground(color12)
+                                )
+                            }
+                            Row {
+                                Box(Modifier
+                                    .testTag(tag21)
+                                    .preferredSize(100.ipx.toDp(), 50.ipx.toDp())
+                                    .drawBackground(color21)
+                                )
+                                Box(Modifier
+                                    .testTag(tag22)
+                                    .preferredSize(100.ipx.toDp(), 50.ipx.toDp())
+                                    .drawBackground(color22)
+                                )
+                            }
                         }
                     }
                 }