Skip to content

Commit

Permalink
Merge branch 'main' into cherrypickscript
Browse files Browse the repository at this point in the history
  • Loading branch information
brettchabot committed Jun 26, 2024
2 parents faa482c + 136bf7f commit 071a189
Show file tree
Hide file tree
Showing 17 changed files with 185 additions and 152 deletions.
4 changes: 3 additions & 1 deletion WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ load(
"GUAVA_VERSION",
"JUNIT_VERSION",
"UIAUTOMATOR_VERSION",
"ATF_VERSION"
)

# gRPC
Expand Down Expand Up @@ -104,6 +105,7 @@ maven_install(
"androidx.viewpager:viewpager:" + ANDROIDX_VIEWPAGER_VERSION,
"androidx.window:window:" + ANDROIDX_WINDOW_VERSION,
"androidx.window:window-java:" + ANDROIDX_WINDOW_VERSION,
"androidx.window:window-core:" + ANDROIDX_WINDOW_VERSION,
"aopalliance:aopalliance:1.0",
"com.android.tools.lint:lint-api:30.1.0",
"com.android.tools.lint:lint-checks:30.1.0",
Expand All @@ -128,7 +130,7 @@ maven_install(
),
],
group = "com.google.android.apps.common.testing.accessibility.framework",
version = "3.1",
version = ATF_VERSION,
),
"com.google.android.material:material:" + GOOGLE_MATERIAL_VERSION,
"com.google.auto.value:auto-value:1.5.1",
Expand Down
20 changes: 10 additions & 10 deletions build_extensions/axt_released_versions.bzl
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
"""Defines current released AXT versions."""

RUNNER_VERSION = "1.6.0-alpha07"
RULES_VERSION = "1.6.0-alpha04"
MONITOR_VERSION = "1.7.0-alpha05"
ESPRESSO_VERSION = "3.6.0-alpha04"
CORE_VERSION = "1.6.0-alpha06"
ESPRESSO_DEVICE_VERSION = "1.0.0-alpha09"
ANDROIDX_JUNIT_VERSION = "1.2.0-alpha04"
ANDROIDX_TRUTH_VERSION = "1.6.0-alpha04"
ORCHESTRATOR_VERSION = "1.5.0-alpha04"
SERVICES_VERSION = "1.5.0-alpha04"
RUNNER_VERSION = "1.6.0"
RULES_VERSION = "1.6.0"
MONITOR_VERSION = "1.7.0"
ESPRESSO_VERSION = "3.6.0"
CORE_VERSION = "1.6.0"
ESPRESSO_DEVICE_VERSION = "1.0.0"
ANDROIDX_JUNIT_VERSION = "1.2.0"
ANDROIDX_TRUTH_VERSION = "1.6.0"
ORCHESTRATOR_VERSION = "1.5.0"
SERVICES_VERSION = "1.5.0"
1 change: 1 addition & 0 deletions espresso/device/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
**Breaking Changes**

**API Changes**
* Update WidthSizeClass and HeightSizeClass to use androidx.window size classes

**Breaking API Changes**

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ import kotlin.math.roundToInt
/** Action to set the test device to the provided display size. */
internal class DisplaySizeAction(
val widthDisplaySize: WidthSizeClass,
val heightDisplaySize: HeightSizeClass
val heightDisplaySize: HeightSizeClass,
) : DeviceAction {
override fun perform(deviceController: DeviceController) {
if (getDeviceApiLevel() < 24) {
Expand All @@ -48,12 +48,11 @@ internal class DisplaySizeAction(

val currentActivity = getResumedActivityOrNull()
if (currentActivity != null) {
val displaySize = calculateCurrentDisplayWidthAndHeightDp(currentActivity)
val startingWidth = displaySize.first
val startingHeight = displaySize.second
val (startingWidthDp, startingHeightDp) =
calculateCurrentDisplayWidthAndHeightDp(currentActivity)
if (
widthDisplaySize == WidthSizeClass.compute(startingWidth) &&
heightDisplaySize == HeightSizeClass.compute(startingHeight)
WidthSizeClass.compute(startingWidthDp) == widthDisplaySize &&
HeightSizeClass.compute(startingHeightDp) == heightDisplaySize
) {
Log.d(TAG, "Device display is already the requested size, no changes needed.")
return
Expand All @@ -65,10 +64,10 @@ internal class DisplaySizeAction(
object : View(currentActivity) {
override fun onConfigurationChanged(newConfig: Configuration?) {
super.onConfigurationChanged(newConfig)
val currentDisplaySize = calculateCurrentDisplayWidthAndHeightDp(currentActivity)
val (newWidthDp, newHeightDp) = calculateCurrentDisplayWidthAndHeightDp(currentActivity)
if (
WidthSizeClass.compute(currentDisplaySize.first) == widthDisplaySize &&
HeightSizeClass.compute(currentDisplaySize.second) == heightDisplaySize
WidthSizeClass.compute(newWidthDp) == widthDisplaySize &&
HeightSizeClass.compute(newHeightDp) == heightDisplaySize
) {
latch.countDown()
}
Expand All @@ -78,21 +77,21 @@ internal class DisplaySizeAction(
currentActivity.getWindow().findViewById(android.R.id.content) as ViewGroup
currentActivity.runOnUiThread { container.addView(currentActivityView) }

val widthDp = WidthSizeClass.getWidthDpInSizeClass(widthDisplaySize)
val heightDp = HeightSizeClass.getHeightDpInSizeClass(heightDisplaySize)
val widthDpToSet = WidthSizeClass.getWidthDpInSizeClass(widthDisplaySize)
val heightDpToSet = HeightSizeClass.getHeightDpInSizeClass(heightDisplaySize)

executeShellCommand("wm size ${widthDp}dpx${heightDp}dp")
executeShellCommand("wm size ${widthDpToSet}dpx${heightDpToSet}dp")

latch.await(5, TimeUnit.SECONDS)
currentActivity.runOnUiThread { container.removeView(currentActivityView) }

val finalSize = calculateCurrentDisplayWidthAndHeightDp(currentActivity)
val (finalWidthDp, finalHeightDp) = calculateCurrentDisplayWidthAndHeightDp(currentActivity)
if (
WidthSizeClass.compute(finalSize.first) != widthDisplaySize ||
HeightSizeClass.compute(finalSize.second) != heightDisplaySize
WidthSizeClass.compute(finalWidthDp) != widthDisplaySize ||
HeightSizeClass.compute(finalHeightDp) != heightDisplaySize
) {
// Display could not be set to the requested size, reset to starting size
executeShellCommand("wm size ${startingWidth}dpx${startingHeight}dp")
executeShellCommand("wm size ${startingWidthDp}dpx${startingHeightDp}dp")
throw UnsupportedDeviceOperationException(
"Device could not be set to the requested display size."
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ kt_android_library(
"//espresso/device/java/androidx/test/espresso/device/sizeclass",
"//runner/android_junit_runner",
"//runner/monitor",
"@maven//:androidx_window_window_core",
"@maven//:junit_junit",
],
)
Expand All @@ -37,6 +38,7 @@ kt_android_library(
"//espresso/device/java/androidx/test/espresso/device/sizeclass",
"//runner/android_junit_runner",
"//runner/monitor",
"@maven//:androidx_window_window_core",
"@maven//:junit_junit",
],
)
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,13 @@
package androidx.test.espresso.device.filter

import android.app.Instrumentation
import androidx.test.espresso.device.sizeclass.HeightSizeClass
import androidx.test.espresso.device.sizeclass.WidthSizeClass
import androidx.test.espresso.device.sizeclass.HeightSizeClass.Companion.HeightSizeClassEnum
import androidx.test.espresso.device.sizeclass.WidthSizeClass.Companion.WidthSizeClassEnum
import androidx.test.filters.AbstractFilter
import androidx.test.platform.app.InstrumentationRegistry
import androidx.window.core.layout.WindowHeightSizeClass
import androidx.window.core.layout.WindowSizeClass
import androidx.window.core.layout.WindowWidthSizeClass
import org.junit.runner.Description

/**
Expand All @@ -37,10 +40,9 @@ internal class RequiresDisplayFilter(

if (!annotations.isEmpty() && annotations[0] is RequiresDisplay) {
val requiresDisplay: RequiresDisplay = annotations[0] as RequiresDisplay
return requiresDisplay.widthSizeClass ==
WidthSizeClass.getEnum(WidthSizeClass.compute(getWidthDp())) &&
requiresDisplay.heightSizeClass ==
HeightSizeClass.getEnum(HeightSizeClass.compute(getHeightDp()))
val windowSize = WindowSizeClass.compute(getWidthDp(), getHeightDp())
return requiresDisplay.widthSizeClass == getWidthEnum(windowSize.windowWidthSizeClass) &&
requiresDisplay.heightSizeClass == getHeightEnum(windowSize.windowHeightSizeClass)
} else {
return true // no RequiresDisplay annotation, run the test
}
Expand All @@ -51,14 +53,30 @@ internal class RequiresDisplayFilter(
}

/** Returns the current screen width in dp */
private fun getWidthDp(): Int {
private fun getWidthDp(): Float {
val displayMetrics = instrumentation.getTargetContext().getResources().displayMetrics
return (displayMetrics.widthPixels / displayMetrics.density).toInt()
return (displayMetrics.widthPixels / displayMetrics.density)
}

/** Returns the current screen height in dp */
private fun getHeightDp(): Int {
private fun getHeightDp(): Float {
val displayMetrics = instrumentation.getTargetContext().getResources().displayMetrics
return (displayMetrics.heightPixels / displayMetrics.density).toInt()
return (displayMetrics.heightPixels / displayMetrics.density)
}

private fun getWidthEnum(widthSizeClass: WindowWidthSizeClass): WidthSizeClassEnum {
return when (widthSizeClass) {
WindowWidthSizeClass.COMPACT -> WidthSizeClassEnum.COMPACT
WindowWidthSizeClass.MEDIUM -> WidthSizeClassEnum.MEDIUM
else -> WidthSizeClassEnum.EXPANDED
}
}

private fun getHeightEnum(heightSizeClass: WindowHeightSizeClass): HeightSizeClassEnum {
return when (heightSizeClass) {
WindowHeightSizeClass.COMPACT -> HeightSizeClassEnum.COMPACT
WindowHeightSizeClass.MEDIUM -> HeightSizeClassEnum.MEDIUM
else -> HeightSizeClassEnum.EXPANDED
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# Description:
# Size classes
# TODO(b/236387720): Replace these classes with core window module size classes when available

load("//build_extensions:kt_android_library.bzl", "kt_android_library")

Expand All @@ -18,5 +17,7 @@ kt_android_library(
testonly = 1,
srcs = glob(["*.kt"]),
deps = [
"//runner/monitor",
"@maven//:androidx_window_window_core",
],
)
Original file line number Diff line number Diff line change
Expand Up @@ -16,48 +16,27 @@

package androidx.test.espresso.device.sizeclass

import androidx.test.platform.app.InstrumentationRegistry
import androidx.window.core.layout.WindowHeightSizeClass
import androidx.window.core.layout.WindowSizeClass

/**
* A class to create buckets for the height of a window.
*
* For details on window size classes, see
* https://developer.android.com/guide/topics/large-screens/support-different-screen-sizes.
*/
public class HeightSizeClass
private constructor(
private val lowerBound: Int,
internal val upperBound: Int,
private val description: String
) {
override fun toString(): String {
return description
}

override fun equals(other: Any?): Boolean {
if (this === other) return true
if (javaClass != other?.javaClass) return false
other as HeightSizeClass
if (lowerBound != other.lowerBound) return false
if (upperBound != other.upperBound) return false
if (description != other.description) return false
return true
}

override fun hashCode(): Int {
var result = lowerBound
result = 31 * result + upperBound
result = 31 * result + description.hashCode()
return result
}

private constructor(private val windowHeightSizeClass: WindowHeightSizeClass) {
public companion object {
/** A bucket to represent a compact height. One use-case is a phone that is in landscape. */
@JvmField public val COMPACT: HeightSizeClass = HeightSizeClass(0, 480, "COMPACT")
@JvmField public val COMPACT: HeightSizeClass = HeightSizeClass(WindowHeightSizeClass.COMPACT)
/** A bucket to represent a medium height. One use-case is a phone in portrait or a tablet. */
@JvmField public val MEDIUM: HeightSizeClass = HeightSizeClass(480, 900, "MEDIUM")
@JvmField public val MEDIUM: HeightSizeClass = HeightSizeClass(WindowHeightSizeClass.MEDIUM)
/**
* A bucket to represent an expanded height window. One use-case is a tablet or a desktop app.
*/
@JvmField public val EXPANDED: HeightSizeClass = HeightSizeClass(900, Int.MAX_VALUE, "EXPANDED")
@JvmField public val EXPANDED: HeightSizeClass = HeightSizeClass(WindowHeightSizeClass.EXPANDED)

/**
* Returns a recommended [HeightSizeClass] for the height of a window given the height in DP.
Expand All @@ -68,12 +47,17 @@ private constructor(
*/
@JvmStatic
public fun compute(dpHeight: Int): HeightSizeClass {
return when {
dpHeight < COMPACT.lowerBound -> {
throw IllegalArgumentException("Negative size: $dpHeight")
}
dpHeight < COMPACT.upperBound -> COMPACT
dpHeight < MEDIUM.upperBound -> MEDIUM
if (dpHeight < 0) {
throw IllegalArgumentException("Negative size: $dpHeight")
}
val instrumentation = InstrumentationRegistry.getInstrumentation()
val displayMetrics = instrumentation.getTargetContext().getResources().displayMetrics
val dpWidth = displayMetrics.widthPixels / displayMetrics.density
val heightSizeClass =
WindowSizeClass.compute(dpWidth, dpHeight.toFloat()).windowHeightSizeClass
return when (heightSizeClass) {
WindowHeightSizeClass.COMPACT -> COMPACT
WindowHeightSizeClass.MEDIUM -> MEDIUM
else -> EXPANDED
}
}
Expand Down Expand Up @@ -111,7 +95,7 @@ private constructor(
public enum class HeightSizeClassEnum(val description: String) {
COMPACT("COMPACT"),
MEDIUM("MEDIUM"),
EXPANDED("EXPANDED")
EXPANDED("EXPANDED"),
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,49 +16,29 @@

package androidx.test.espresso.device.sizeclass

import androidx.test.platform.app.InstrumentationRegistry
import androidx.window.core.layout.WindowSizeClass
import androidx.window.core.layout.WindowWidthSizeClass

/**
* A class to create buckets for the width of a window.
*
* For details on window size classes, see
* https://developer.android.com/guide/topics/large-screens/support-different-screen-sizes.
*/
public class WidthSizeClass
private constructor(
private val lowerBound: Int,
internal val upperBound: Int,
private val description: String
) {
override fun toString(): String {
return description
}

override fun equals(other: Any?): Boolean {
if (this === other) return true
if (javaClass != other?.javaClass) return false
other as WidthSizeClass
if (lowerBound != other.lowerBound) return false
if (upperBound != other.upperBound) return false
if (description != other.description) return false
return true
}

override fun hashCode(): Int {
var result = lowerBound
result = 31 * result + upperBound
result = 31 * result + description.hashCode()
return result
}
private constructor(private val windowWidthSizeClass: WindowWidthSizeClass) {

public companion object {
/** A bucket to represent a compact width window. One use-case is a phone in portrait. */
@JvmField public val COMPACT: WidthSizeClass = WidthSizeClass(0, 600, "COMPACT")
@JvmField public val COMPACT: WidthSizeClass = WidthSizeClass(WindowWidthSizeClass.COMPACT)
/**
* A bucket to represent a medium width window. Some use-cases are a phone in landscape or a
* tablet.
*/
@JvmField public val MEDIUM: WidthSizeClass = WidthSizeClass(600, 840, "MEDIUM")
@JvmField public val MEDIUM: WidthSizeClass = WidthSizeClass(WindowWidthSizeClass.MEDIUM)
/** A bucket to represent an expanded width window. One use-case is a desktop app. */
@JvmField public val EXPANDED: WidthSizeClass = WidthSizeClass(840, Int.MAX_VALUE, "EXPANDED")
@JvmField public val EXPANDED: WidthSizeClass = WidthSizeClass(WindowWidthSizeClass.EXPANDED)

/**
* Returns a recommended [WidthSizeClass] for the width of a window given the width in DP.
Expand All @@ -69,12 +49,16 @@ private constructor(
*/
@JvmStatic
public fun compute(dpWidth: Int): WidthSizeClass {
return when {
dpWidth < COMPACT.lowerBound -> {
throw IllegalArgumentException("Negative size: $dpWidth")
}
dpWidth < COMPACT.upperBound -> COMPACT
dpWidth < MEDIUM.upperBound -> MEDIUM
if (dpWidth < 0) {
throw IllegalArgumentException("Negative size: $dpWidth")
}
val instrumentation = InstrumentationRegistry.getInstrumentation()
val displayMetrics = instrumentation.getTargetContext().getResources().displayMetrics
val dpHeight = displayMetrics.heightPixels / displayMetrics.density
val widthSizeClass = WindowSizeClass.compute(dpWidth.toFloat(), dpHeight).windowWidthSizeClass
return when (widthSizeClass) {
WindowWidthSizeClass.COMPACT -> COMPACT
WindowWidthSizeClass.MEDIUM -> MEDIUM
else -> EXPANDED
}
}
Expand Down
Loading

0 comments on commit 071a189

Please sign in to comment.