Add assumption that initial displaySize has changed

The test fails if the same display is used for
both CLOSED and OPENED state: there is no change in
display metrics so the non-resizable activity does
not enter app-compat mode so the restart button is
not shown.

Solution: determine display size before and after
changing to OPENED state and continue the test only
if the two sizes are different.

This is a cherry pick of https://android-review.googlesource.com/c/platform/cts/+/2110365

Fix: 233922949
Test: android.cts.statsdatom.sizecompatrestartbutton.SizeCompatRestartButtonStatsTests#testSizeCompatRestartButtonAppearedButNotClicked
Merged-In: Ib8234de39368660db7727c6e0f1f72769a48f880
Change-Id: If1add825be4dd21c3897b1e48fbd8fea431d62eb
diff --git a/hostsidetests/statsdatom/src/android/cts/statsdatom/sizecompatrestartbutton/SizeCompatRestartButtonStatsTests.java b/hostsidetests/statsdatom/src/android/cts/statsdatom/sizecompatrestartbutton/SizeCompatRestartButtonStatsTests.java
index b797e68..57a44ef 100644
--- a/hostsidetests/statsdatom/src/android/cts/statsdatom/sizecompatrestartbutton/SizeCompatRestartButtonStatsTests.java
+++ b/hostsidetests/statsdatom/src/android/cts/statsdatom/sizecompatrestartbutton/SizeCompatRestartButtonStatsTests.java
@@ -28,9 +28,11 @@
 import com.android.os.AtomsProto.SizeCompatRestartButtonEventReported.Event;
 import com.android.os.StatsLog;
 import com.android.tradefed.build.IBuildInfo;
+import com.android.tradefed.device.ITestDevice;
 import com.android.tradefed.log.LogUtil.CLog;
 import com.android.tradefed.testtype.DeviceTestCase;
 import com.android.tradefed.testtype.IBuildReceiver;
+import com.android.tradefed.util.Pair;
 
 import java.util.Arrays;
 import java.util.List;
@@ -103,6 +105,11 @@
             return;
         }
 
+        Pair<Integer, Integer> displaySizeClosed = getDisplayRealSize(getDevice());
+        if (displaySizeClosed == null) {
+            CLog.i("Could not determine display size while CLOSED.");
+            return;
+        }
         try (AutoCloseable a = DeviceUtils.withActivity(getDevice(),
                 DeviceUtils.STATSD_ATOM_TEST_PKG, NON_RESIZEABLE_PORTRAIT_ACTIVITY, "action",
                 "action.sleep_top")) {
@@ -111,6 +118,15 @@
             Thread.sleep(AtomTestUtils.WAIT_TIME_LONG);
         }
 
+        Pair<Integer, Integer> displaySizeOpened = getDisplayRealSize(getDevice());
+        if (displaySizeOpened == null) {
+            CLog.i("Could not determine display size while OPENED.");
+            return;
+        }
+        if (displaySizeClosed.equals(displaySizeOpened)) {
+            CLog.i("Display size has not changed.");
+            return;
+        }
         List<StatsLog.EventMetricData> data = ReportUtils.getEventMetricDataList(getDevice());
         assertThat(data.size()).isEqualTo(1);
 
@@ -126,5 +142,26 @@
                 .map(Integer::valueOf)
                 .anyMatch(state -> state == DEVICE_STATE_OPENED);
     }
+
+    /**
+     * Returns the physical size of the current display used.
+     */
+    private Pair<Integer, Integer> getDisplayRealSize(ITestDevice device) throws Exception {
+        final String physicalSize = "Physical size: ";
+        String str = device.executeShellCommand("wm size");
+        if (!str.isEmpty()) {
+            String[] lines = str.split(System.getProperty("line.separator"));
+            for (String s : lines) {
+                if (s.contains(physicalSize)) {
+                    String substring = s.substring(physicalSize.length());
+                    if (!substring.isEmpty()) {
+                        return Pair.create(Integer.parseInt(substring.split("x")[0]),
+                                Integer.parseInt(substring.split("x")[1]));
+                    }
+                }
+            }
+        }
+        return null;
+    }
 }