From d29b62981c59b5eab7b93dc6b91565ba5cdce381 Mon Sep 17 00:00:00 2001 From: agrieve Date: Thu, 6 Jun 2024 11:40:19 -0700 Subject: [PATCH 1/5] Fix exceptions during @AfterClass not being reported via InstrumentationResultPrinter PiperOrigin-RevId: 640969268 --- runner/android_junit_runner/CHANGELOG.md | 2 + .../InstrumentationResultPrinter.java | 14 ++-- .../test/internal/runner/listener/BUILD | 7 -- .../InstrumentationResultPrinterTest.java | 69 +++++++++++++------ 4 files changed, 57 insertions(+), 35 deletions(-) diff --git a/runner/android_junit_runner/CHANGELOG.md b/runner/android_junit_runner/CHANGELOG.md index 0e374a29f..cc5a575a0 100644 --- a/runner/android_junit_runner/CHANGELOG.md +++ b/runner/android_junit_runner/CHANGELOG.md @@ -6,6 +6,8 @@ **Bug Fixes** +* Exceptions during `@AfterClass` were not being reported via `InstrumentationResultPrinter`. + **New Features** **Breaking Changes** diff --git a/runner/android_junit_runner/java/androidx/test/internal/runner/listener/InstrumentationResultPrinter.java b/runner/android_junit_runner/java/androidx/test/internal/runner/listener/InstrumentationResultPrinter.java index 7cb820220..5e230c8e7 100644 --- a/runner/android_junit_runner/java/androidx/test/internal/runner/listener/InstrumentationResultPrinter.java +++ b/runner/android_junit_runner/java/androidx/test/internal/runner/listener/InstrumentationResultPrinter.java @@ -44,7 +44,7 @@ */ public class InstrumentationResultPrinter extends InstrumentationRunListener { - private static final String TAG = "InstrumentationResultPrinter"; + private static final String TAG = "InstrResultPrinter"; /** * This value, if stored with key {@link android.app.Instrumentation#REPORT_KEY_IDENTIFIER}, @@ -152,14 +152,12 @@ public void testFinished(Description description) throws Exception { @Override public void testFailure(Failure failure) throws Exception { - boolean shouldCallFinish = false; - if (!isAnyTestStarted()) { - // Junit failed during initialization and testStarted was never called. For example, an - // exception was thrown in @BeforeClass method. We must artificially call testStarted and - // testFinished in order to print a descriptive result that external tools (like Studio) can - // understand. + // getMethodName() == null when an exception is thrown during @BeforeClass or @AfterClass. + // No matching testStart() / testFinish() is emitted, so simulate them here for the sake of + // instrumentation consumers. + boolean shouldCallFinish = failure.getDescription().getMethodName() == null; + if (shouldCallFinish) { testStarted(failure.getDescription()); - shouldCallFinish = true; } testResultCode = REPORT_VALUE_RESULT_FAILURE; reportFailure(failure); diff --git a/runner/android_junit_runner/javatests/androidx/test/internal/runner/listener/BUILD b/runner/android_junit_runner/javatests/androidx/test/internal/runner/listener/BUILD index 95c3103b2..7728121fe 100644 --- a/runner/android_junit_runner/javatests/androidx/test/internal/runner/listener/BUILD +++ b/runner/android_junit_runner/javatests/androidx/test/internal/runner/listener/BUILD @@ -44,15 +44,8 @@ axt_android_library_test( "//ext/junit", "//runner/android_junit_runner", "//runner/android_junit_runner/javatests/androidx/test/testing/fixtures", - "//runner/rules", - "//services/events/java/androidx/test/services/events", - "//services/storage", "@androidsdk//:legacy_test-34", - "@maven//:com_google_guava_guava", - "@maven//:com_google_truth_truth", "@maven//:junit_junit", - "@maven//:org_hamcrest_hamcrest_core", - "@maven//:org_mockito_mockito_core", ], ) diff --git a/runner/android_junit_runner/javatests/androidx/test/internal/runner/listener/InstrumentationResultPrinterTest.java b/runner/android_junit_runner/javatests/androidx/test/internal/runner/listener/InstrumentationResultPrinterTest.java index 037a8cad8..caa6e6371 100644 --- a/runner/android_junit_runner/javatests/androidx/test/internal/runner/listener/InstrumentationResultPrinterTest.java +++ b/runner/android_junit_runner/javatests/androidx/test/internal/runner/listener/InstrumentationResultPrinterTest.java @@ -23,11 +23,17 @@ import android.os.Bundle; import androidx.test.ext.junit.runners.AndroidJUnit4; import androidx.test.filters.SmallTest; +import java.util.ArrayList; +import java.util.List; import junit.framework.Assert; +import org.junit.AfterClass; +import org.junit.BeforeClass; import org.junit.Test; +import org.junit.runner.Computer; import org.junit.runner.Description; +import org.junit.runner.JUnitCore; +import org.junit.runner.Request; import org.junit.runner.RunWith; -import org.junit.runner.notification.Failure; @RunWith(AndroidJUnit4.class) @SmallTest @@ -62,27 +68,50 @@ public void sendStatus(int code, Bundle bundle) { assertTrue(resultBundle[0].containsKey(REPORT_KEY_STACK)); } - @Test - public void verifyFailureDescriptionPropagatedToStartAndFinishMethods() throws Exception { - Description[] descriptions = new Description[2]; - InstrumentationResultPrinter intrResultPrinter = - new InstrumentationResultPrinter() { - @Override - public void testStarted(Description description) throws Exception { - descriptions[0] = description; - } + private static class TestInstrumentationResultPrinter extends InstrumentationResultPrinter { + final List resultsLog = new ArrayList<>(); - @Override - public void testFinished(Description description) throws Exception { - descriptions[1] = description; - } - }; + @Override + public void sendStatus(int code, Bundle bundle) { + resultsLog.add( + String.format( + "code=%s, name=%s#%s", + code, + bundle.getString(REPORT_KEY_NAME_CLASS), + bundle.getString(REPORT_KEY_NAME_TEST))); + } + } + + public static class ThrowingTest { + @BeforeClass + public static void setUpClass() { + throw new RuntimeException(); + } + + @AfterClass + public static void tearDownClass() { + throw new RuntimeException(); + } - Description d = Description.createTestDescription(this.getClass(), "Failure Description"); - Failure testFailure = new Failure(d, new Exception()); - intrResultPrinter.testFailure(testFailure); + @Test + public void emptyTest() {} + } + + @Test + public void verifyBeforeClassExceptionsReported() throws Exception { + JUnitCore core = new JUnitCore(); + var intrResultPrinter = new TestInstrumentationResultPrinter(); + core.addListener(intrResultPrinter); + Request testRequest = Request.classes(new Computer(), new Class[] {ThrowingTest.class}); + core.run(testRequest); - assertEquals(d, descriptions[0]); - assertEquals(d, descriptions[1]); + String className = ThrowingTest.class.getName(); + assertEquals( + List.of( + "code=1, name=" + className + "#null", + "code=-2, name=" + className + "#null", + "code=1, name=" + className + "#null", + "code=-2, name=" + className + "#null"), + intrResultPrinter.resultsLog); } } From 8eeb94a8b50ee54a7068ae05ea4f7550c6ea7536 Mon Sep 17 00:00:00 2001 From: Paige McAuliffe Date: Mon, 10 Jun 2024 10:06:12 -0700 Subject: [PATCH 2/5] Use androidx.window size classes for HeightSizeClass and WidthSizeClass PiperOrigin-RevId: 641939424 --- WORKSPACE | 1 + espresso/device/CHANGELOG.md | 1 + .../device/action/DisplaySizeAction.kt | 31 +++++----- .../test/espresso/device/filter/BUILD | 2 + .../device/filter/RequiresDisplayFilter.kt | 38 +++++++++---- .../test/espresso/device/sizeclass/BUILD | 3 +- .../device/sizeclass/HeightSizeClass.kt | 56 +++++++------------ .../device/sizeclass/WidthSizeClass.kt | 52 ++++++----------- .../java/androidx/test/multiwindow/app/BUILD | 1 + .../multiwindow/app/MultiWindowActivity.java | 26 ++++++--- 10 files changed, 105 insertions(+), 106 deletions(-) diff --git a/WORKSPACE b/WORKSPACE index 0e6e71845..a4c901a07 100644 --- a/WORKSPACE +++ b/WORKSPACE @@ -104,6 +104,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", diff --git a/espresso/device/CHANGELOG.md b/espresso/device/CHANGELOG.md index 9f6097d76..585402284 100644 --- a/espresso/device/CHANGELOG.md +++ b/espresso/device/CHANGELOG.md @@ -14,6 +14,7 @@ **Breaking Changes** **API Changes** +* Update WidthSizeClass and HeightSizeClass to use androidx.window size classes **Breaking API Changes** diff --git a/espresso/device/java/androidx/test/espresso/device/action/DisplaySizeAction.kt b/espresso/device/java/androidx/test/espresso/device/action/DisplaySizeAction.kt index 059a87c18..64dddb6a7 100644 --- a/espresso/device/java/androidx/test/espresso/device/action/DisplaySizeAction.kt +++ b/espresso/device/java/androidx/test/espresso/device/action/DisplaySizeAction.kt @@ -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) { @@ -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 @@ -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() } @@ -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." ) diff --git a/espresso/device/java/androidx/test/espresso/device/filter/BUILD b/espresso/device/java/androidx/test/espresso/device/filter/BUILD index b3175e2d5..ac67e5844 100644 --- a/espresso/device/java/androidx/test/espresso/device/filter/BUILD +++ b/espresso/device/java/androidx/test/espresso/device/filter/BUILD @@ -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", ], ) @@ -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", ], ) diff --git a/espresso/device/java/androidx/test/espresso/device/filter/RequiresDisplayFilter.kt b/espresso/device/java/androidx/test/espresso/device/filter/RequiresDisplayFilter.kt index d613145dd..a4fe343e8 100644 --- a/espresso/device/java/androidx/test/espresso/device/filter/RequiresDisplayFilter.kt +++ b/espresso/device/java/androidx/test/espresso/device/filter/RequiresDisplayFilter.kt @@ -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 /** @@ -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 } @@ -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 + } } } diff --git a/espresso/device/java/androidx/test/espresso/device/sizeclass/BUILD b/espresso/device/java/androidx/test/espresso/device/sizeclass/BUILD index 035c7dba5..70572b320 100644 --- a/espresso/device/java/androidx/test/espresso/device/sizeclass/BUILD +++ b/espresso/device/java/androidx/test/espresso/device/sizeclass/BUILD @@ -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") @@ -18,5 +17,7 @@ kt_android_library( testonly = 1, srcs = glob(["*.kt"]), deps = [ + "//runner/monitor", + "@maven//:androidx_window_window_core", ], ) diff --git a/espresso/device/java/androidx/test/espresso/device/sizeclass/HeightSizeClass.kt b/espresso/device/java/androidx/test/espresso/device/sizeclass/HeightSizeClass.kt index ff1873a62..647e13062 100644 --- a/espresso/device/java/androidx/test/espresso/device/sizeclass/HeightSizeClass.kt +++ b/espresso/device/java/androidx/test/espresso/device/sizeclass/HeightSizeClass.kt @@ -16,6 +16,10 @@ 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. * @@ -23,41 +27,16 @@ package androidx.test.espresso.device.sizeclass * 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. @@ -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 } } @@ -111,7 +95,7 @@ private constructor( public enum class HeightSizeClassEnum(val description: String) { COMPACT("COMPACT"), MEDIUM("MEDIUM"), - EXPANDED("EXPANDED") + EXPANDED("EXPANDED"), } } } diff --git a/espresso/device/java/androidx/test/espresso/device/sizeclass/WidthSizeClass.kt b/espresso/device/java/androidx/test/espresso/device/sizeclass/WidthSizeClass.kt index 9b3e8d114..992e68af4 100644 --- a/espresso/device/java/androidx/test/espresso/device/sizeclass/WidthSizeClass.kt +++ b/espresso/device/java/androidx/test/espresso/device/sizeclass/WidthSizeClass.kt @@ -16,6 +16,10 @@ 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. * @@ -23,42 +27,18 @@ package androidx.test.espresso.device.sizeclass * 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. @@ -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 } } diff --git a/testapps/multiwindow_testapp/java/androidx/test/multiwindow/app/BUILD b/testapps/multiwindow_testapp/java/androidx/test/multiwindow/app/BUILD index 72fcc5888..10f80effb 100644 --- a/testapps/multiwindow_testapp/java/androidx/test/multiwindow/app/BUILD +++ b/testapps/multiwindow_testapp/java/androidx/test/multiwindow/app/BUILD @@ -27,6 +27,7 @@ jetify_android_library( deps = [ "//opensource/androidx:annotation", "@maven//:androidx_appcompat_appcompat", + "@maven//:androidx_window_window_core", ], ) diff --git a/testapps/multiwindow_testapp/java/androidx/test/multiwindow/app/MultiWindowActivity.java b/testapps/multiwindow_testapp/java/androidx/test/multiwindow/app/MultiWindowActivity.java index a0e2bd8d6..1b8dfd0c4 100644 --- a/testapps/multiwindow_testapp/java/androidx/test/multiwindow/app/MultiWindowActivity.java +++ b/testapps/multiwindow_testapp/java/androidx/test/multiwindow/app/MultiWindowActivity.java @@ -23,6 +23,9 @@ import android.view.View; import android.view.ViewGroup; import android.widget.TextView; +import androidx.window.core.layout.WindowHeightSizeClass; +import androidx.window.core.layout.WindowSizeClass; +import androidx.window.core.layout.WindowWidthSizeClass; /** Activity that updates a TextView when entering or exiting multi-window mode. */ public class MultiWindowActivity extends Activity { @@ -57,31 +60,36 @@ protected void onConfigurationChanged(Configuration newConfig) { private void computeWindowSizeClasses() { TextView screenWidthTextView = (TextView) findViewById(R.id.screen_width_display_size); + TextView screenHeightTextView = (TextView) findViewById(R.id.screen_height_display_size); + float width = this.getResources().getDisplayMetrics().widthPixels / this.getResources().getDisplayMetrics().density; + float height = + this.getResources().getDisplayMetrics().heightPixels + / this.getResources().getDisplayMetrics().density; + WindowSizeClass sizeClass = WindowSizeClass.compute(width, height); String screenWidthText; - if (width < 600f) { + WindowWidthSizeClass widthSizeClass = sizeClass.getWindowWidthSizeClass(); + if (widthSizeClass.equals(WindowWidthSizeClass.COMPACT)) { screenWidthText = "Compact width"; - } else if (width < 840f) { + } else if (widthSizeClass.equals(WindowWidthSizeClass.MEDIUM)) { screenWidthText = "Medium width"; } else { screenWidthText = "Expanded width"; } - screenWidthTextView.setText(screenWidthText); - TextView screenHeightTextView = (TextView) findViewById(R.id.screen_height_display_size); - float height = - this.getResources().getDisplayMetrics().heightPixels - / this.getResources().getDisplayMetrics().density; + WindowHeightSizeClass heightSizeClass = sizeClass.getWindowHeightSizeClass(); String screenHeightText; - if (height < 480f) { + if (heightSizeClass.equals(WindowHeightSizeClass.COMPACT)) { screenHeightText = "Compact height"; - } else if (height < 900f) { + } else if (heightSizeClass.equals(WindowHeightSizeClass.MEDIUM)) { screenHeightText = "Medium height"; } else { screenHeightText = "Expanded height"; } + + screenWidthTextView.setText(screenWidthText); screenHeightTextView.setText(screenHeightText); } } From 88792bbaece05eb35d27227596aab3d9839036e4 Mon Sep 17 00:00:00 2001 From: Brett Chabot Date: Mon, 24 Jun 2024 12:26:26 -0700 Subject: [PATCH 3/5] Update axt_released_versions.bzl. PiperOrigin-RevId: 646183347 --- build_extensions/axt_released_versions.bzl | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/build_extensions/axt_released_versions.bzl b/build_extensions/axt_released_versions.bzl index 14007e9a1..2606a1463 100644 --- a/build_extensions/axt_released_versions.bzl +++ b/build_extensions/axt_released_versions.bzl @@ -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" From 7cf2440030733787bb228545bf8acde8350cd62f Mon Sep 17 00:00:00 2001 From: Brett Chabot Date: Tue, 25 Jun 2024 09:07:22 -0700 Subject: [PATCH 4/5] Upgrade accessibilitytestframework version (back) to 3.1.2. Fixes #2244 PiperOrigin-RevId: 646499915 --- WORKSPACE | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/WORKSPACE b/WORKSPACE index a4c901a07..eab1cd8b4 100644 --- a/WORKSPACE +++ b/WORKSPACE @@ -67,6 +67,7 @@ load( "GUAVA_VERSION", "JUNIT_VERSION", "UIAUTOMATOR_VERSION", + "ATF_VERSION" ) # gRPC @@ -129,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", From 3e1d6df892b50db7af0f7d4bf99e416a518ba0a2 Mon Sep 17 00:00:00 2001 From: Brett Chabot Date: Wed, 26 Jun 2024 11:39:17 -0700 Subject: [PATCH 5/5] Catch and log exceptions when enabling tracing. Due to an android gradle bug calls to forceEnableAppTracing() may fail with NoSuchMethodError at runtime. Instead of crashing, just catch and log this exception. --- runner/monitor/CHANGELOG.md | 2 ++ .../java/androidx/test/platform/tracing/AndroidXTracer.java | 6 ++++++ 2 files changed, 8 insertions(+) diff --git a/runner/monitor/CHANGELOG.md b/runner/monitor/CHANGELOG.md index 81697fb4a..29ace4aa3 100644 --- a/runner/monitor/CHANGELOG.md +++ b/runner/monitor/CHANGELOG.md @@ -6,6 +6,8 @@ **Bug Fixes** +* Catch and log NoSuchMethodError on forceEnableAppTracing calls + **New Features** **Breaking Changes** diff --git a/runner/monitor/java/androidx/test/platform/tracing/AndroidXTracer.java b/runner/monitor/java/androidx/test/platform/tracing/AndroidXTracer.java index 1abdd4b66..261d4dafd 100644 --- a/runner/monitor/java/androidx/test/platform/tracing/AndroidXTracer.java +++ b/runner/monitor/java/androidx/test/platform/tracing/AndroidXTracer.java @@ -59,6 +59,12 @@ public AndroidXTracer enableTracing() { // The AndroidX call can fail if reflection is not allowed. // We want to log the error yet we should not break any test in this case. Log.e(TAG, "enableTracing failed", e); + } catch (NoSuchMethodError e) { + // This can occur if an androidx.tracing < 1.1.0 is put on classpath instead. + // See http://issuetracker.google.com/349628366). + // We want to log the error yet we should not break any test in this case. + Log.e(TAG, "enableTracing failed. " + + "You may need to upgrade your androidx.tracing:tracing version", e); } return this; }