Improve Animator CTS test to rotate w/o activity recreation

This CL also changes test not to fail if rotation fails. It voids
the test but better than blocking devices that don't support
rotation.

Bug: 18709201
Change-Id: I6640c8bc1a101169cc8e347ab0f0e37b67cf0739
diff --git a/tests/tests/view/AndroidManifest.xml b/tests/tests/view/AndroidManifest.xml
index 6806d29..a36e6fd 100644
--- a/tests/tests/view/AndroidManifest.xml
+++ b/tests/tests/view/AndroidManifest.xml
@@ -84,7 +84,7 @@
         </activity>
         
         <activity android:name="android.view.animation.cts.AnimationTestCtsActivity"
-            android:label="AnimationTestCtsActivity">
+            android:label="AnimationTestCtsActivity" android:configChanges="orientation|screenSize">
             <intent-filter>
                 <action android:name="android.intent.action.MAIN"/>
                 <category android:name="android.intent.category.FRAMEWORK_INSTRUMENTATION_TEST" />
diff --git a/tests/tests/view/src/android/view/animation/cts/AnimationTestCtsActivity.java b/tests/tests/view/src/android/view/animation/cts/AnimationTestCtsActivity.java
index 48692f1..1ef9e48 100644
--- a/tests/tests/view/src/android/view/animation/cts/AnimationTestCtsActivity.java
+++ b/tests/tests/view/src/android/view/animation/cts/AnimationTestCtsActivity.java
@@ -20,11 +20,41 @@
 
 import android.app.Activity;
 import android.os.Bundle;
+import android.util.Log;
+
+import java.util.concurrent.TimeUnit;
 
 public class AnimationTestCtsActivity extends Activity {
+    final static long VISIBLE_TIMEOUT = TimeUnit.SECONDS.toNanos(3);
+    private boolean mIsVisible;
+
+    public boolean isVisible() {
+        return mIsVisible;
+    }
+
     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.anim_layout);
     }
+
+    @Override
+    protected void onResume() {
+        super.onResume();
+        mIsVisible = true;
+    }
+
+    @Override
+    protected void onPause() {
+        super.onPause();
+        mIsVisible = false;
+    }
+
+    public boolean waitUntilVisible() throws InterruptedException {
+        long start = System.nanoTime();
+        while (!mIsVisible && (System.nanoTime() - start) < VISIBLE_TIMEOUT) {
+            Thread.sleep(100);
+        }
+        return mIsVisible;
+    }
 }
diff --git a/tests/tests/view/src/android/view/animation/cts/AnimatorInflaterTest.java b/tests/tests/view/src/android/view/animation/cts/AnimatorInflaterTest.java
index eeda5a3..cc8ada0 100644
--- a/tests/tests/view/src/android/view/animation/cts/AnimatorInflaterTest.java
+++ b/tests/tests/view/src/android/view/animation/cts/AnimatorInflaterTest.java
@@ -21,13 +21,15 @@
 import android.animation.AnimatorSet;
 import android.animation.ObjectAnimator;
 import android.animation.StateListAnimator;
-import android.app.Activity;
 import android.app.Instrumentation;
-import android.content.pm.ActivityInfo;
-import android.content.res.Configuration;
-import android.os.Debug;
+import android.app.UiAutomation;
+import android.content.Context;
+import android.util.Log;
 import android.test.ActivityInstrumentationTestCase2;
+import android.view.Display;
+import android.view.Surface;
 import android.view.View;
+import android.view.WindowManager;
 
 import java.util.HashSet;
 import java.util.Set;
@@ -39,6 +41,7 @@
 public class AnimatorInflaterTest
         extends ActivityInstrumentationTestCase2<AnimationTestCtsActivity> {
 
+    private static final String TAG = "AnimatorInflaterTest";
     Set<Integer> identityHashes = new HashSet<Integer>();
 
     public AnimatorInflaterTest() {
@@ -58,7 +61,9 @@
     public void testLoadAnimatorWithDifferentInterpolators() throws Throwable {
         Animator anim1 = AnimatorInflater
                 .loadAnimator(getActivity(), R.anim.changing_test_animator);
-        rotate();
+        if (!rotate()) {
+            return;//cancel test
+        }
         Animator anim2 = AnimatorInflater
                 .loadAnimator(getActivity(), R.anim.changing_test_animator);
         assertNotSame(anim1, anim2);
@@ -109,7 +114,9 @@
                 assertEquals(targetY, dummyObject.y);
             }
             if (i == 0) {
-                rotate();
+                if (!rotate()) {
+                    return;//cancel test
+                }
             }
             anim1 = AnimatorInflater.loadAnimator(getActivity(), R.anim.test_animator);
             anim2 = AnimatorInflater.loadAnimator(getActivity(), R.anim.test_animator);
@@ -117,27 +124,43 @@
         }
     }
 
-    private void rotate() throws Throwable {
-        final Activity activity = getActivity();
-        int orientation = activity.getResources().getConfiguration().orientation;
-        final int nextOrientation = orientation == Configuration.ORIENTATION_LANDSCAPE
-                ? ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
-                : ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
+    private boolean rotate() throws Throwable {
+        WindowManager mWindowManager = (WindowManager) getActivity()
+                .getSystemService(Context.WINDOW_SERVICE);
+        Display display = mWindowManager.getDefaultDisplay();
+
         Instrumentation.ActivityMonitor monitor = new Instrumentation.ActivityMonitor(
-                activity.getClass().getName(), null, false);
+                getActivity().getClass().getName(), null, false);
         getInstrumentation().addMonitor(monitor);
-        runTestOnUiThread(new Runnable() {
-            @Override
-            public void run() {
-                activity.setRequestedOrientation(nextOrientation);
-            }
-        });
-        getInstrumentation().waitForIdleSync();
-        Activity newAct = getInstrumentation()
-                .waitForMonitorWithTimeout(monitor, TimeUnit.SECONDS.toMillis(2));
-        if (newAct != null) {
-            setActivity(newAct);
+        int nextRotation = 0;
+        switch (display.getRotation()) {
+            case Surface.ROTATION_0:
+            case Surface.ROTATION_180:
+                nextRotation = UiAutomation.ROTATION_FREEZE_90;
+                break;
+            case Surface.ROTATION_90:
+            case Surface.ROTATION_270:
+                nextRotation = UiAutomation.ROTATION_FREEZE_0;
+                break;
+            default:
+                Log.e(TAG, "Cannot get rotation, test is canceled");
+                return false;
         }
+        boolean rotated = getInstrumentation().getUiAutomation().setRotation(nextRotation);
+        Thread.sleep(500);
+        if (!rotated) {
+            Log.e(TAG, "Rotation failed, test is canceled");
+        }
+        getInstrumentation().waitForIdleSync();
+        if (!getActivity().waitUntilVisible()) {
+            Log.e(TAG, "Activity failed to complete rotation, canceling test");
+            return false;
+        }
+        if (getActivity().getWindowManager().getDefaultDisplay().getRotation() != nextRotation) {
+            Log.e(TAG, "New activity orientation does not match. Canceling test");
+            return false;
+        }
+        return true;
     }
 
     /**
@@ -158,7 +181,10 @@
      */
     public void testLoadStateListAnimatorWithChangingResetState() throws Throwable {
         loadStateListAnimatorWithChangingResetStateTest();
-        rotate();
+        if (!rotate()) {
+            return;//cancel test
+        }
+
         loadStateListAnimatorWithChangingResetStateTest();
     }
 
@@ -185,7 +211,9 @@
      */
     public void testLoadChangingStateListAnimator() throws Throwable {
         loadChangingStateListAnimatorTest();
-        rotate();
+        if (!rotate()) {
+            return;//cancel test
+        }
         loadChangingStateListAnimatorTest();
     }