Disable animations in CTS theme ProgressBar & update ReferenceImageTest.java to androidx

cherry-pick from previous fix to problem of CTS theme ProgressBar not rendering in time causing test failure.
In addition, updated ReferenceImagesTest.java (to androidx so that LOCAL_SDK_VERSION of test_current can be ran.

Bug: 143049967
Change-Id: Iebd5ee02948e7c6f103a7faf575aca3a90b47b4a
Fix: 121330993
Test: atest android.theme.cts.ThemeHostTest#testThemes
Merged-In: Ibfb064a1c6a200a983e1e02df67f4bf82f11946c
diff --git a/hostsidetests/theme/app/Android.mk b/hostsidetests/theme/app/Android.mk
index 1d5a6f0..90999f8 100644
--- a/hostsidetests/theme/app/Android.mk
+++ b/hostsidetests/theme/app/Android.mk
@@ -38,6 +38,6 @@
 # Tag this module as a cts test artifact
 LOCAL_COMPATIBILITY_SUITE := cts vts general-tests
 
-LOCAL_SDK_VERSION := 23
+LOCAL_SDK_VERSION := test_current
 
 include $(BUILD_CTS_SUPPORT_PACKAGE)
diff --git a/hostsidetests/theme/app/AndroidManifest.xml b/hostsidetests/theme/app/AndroidManifest.xml
index 0f3c1de..a351a01 100755
--- a/hostsidetests/theme/app/AndroidManifest.xml
+++ b/hostsidetests/theme/app/AndroidManifest.xml
@@ -18,8 +18,6 @@
 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
     package="android.theme.app">
 
-    <uses-sdk android:minSdkVersion="17" />
-
     <uses-permission android:name="android.permission.DISABLE_KEYGUARD" />
     <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
 
diff --git a/hostsidetests/theme/app/src/android/theme/app/LayoutModifier.java b/hostsidetests/theme/app/src/android/theme/app/LayoutModifier.java
index 844c578..e007129 100644
--- a/hostsidetests/theme/app/src/android/theme/app/LayoutModifier.java
+++ b/hostsidetests/theme/app/src/android/theme/app/LayoutModifier.java
@@ -23,12 +23,19 @@
  */
 public interface LayoutModifier {
 
-    /** Actions to take before inflating the view. */
-    void prepare();
+    /**
+     * Modifies the view before it has been added to a parent. Useful for avoiding animations in
+     * response to setter calls.
+     *
+     * @param view the view inflated by the test activity
+     */
+    void modifyViewBeforeAdd(View view);
 
     /**
-     * @param view inflated by the test activity
-     * @return the same view or another view that will be snapshotted by the test
+     * Modifies the view after it has been added to a parent. Useful for running animations in
+     * response to setter calls.
+     *
+     * @param view the view inflated by the test activity
      */
-    View modifyView(View view);
+    void modifyViewAfterAdd(View view);
 }
diff --git a/hostsidetests/theme/app/src/android/theme/app/ReferenceImagesTest.java b/hostsidetests/theme/app/src/android/theme/app/ReferenceImagesTest.java
index 6e0731d..3707bb1 100644
--- a/hostsidetests/theme/app/src/android/theme/app/ReferenceImagesTest.java
+++ b/hostsidetests/theme/app/src/android/theme/app/ReferenceImagesTest.java
@@ -16,26 +16,47 @@
 
 package android.theme.app;
 
-import android.test.ActivityInstrumentationTestCase2;
+import static org.junit.Assert.assertTrue;
+
+import android.app.Instrumentation;
+
+import androidx.test.InstrumentationRegistry;
+import androidx.test.rule.ActivityTestRule;
+import androidx.test.runner.AndroidJUnit4;
+
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.runner.RunWith;
 
 import java.io.File;
 
 /**
- * Activity test case used to instrument generation of reference images.
+ * Test used to instrument generation of reference images.
  */
-public class ReferenceImagesTest extends ActivityInstrumentationTestCase2<GenerateImagesActivity> {
+@RunWith(AndroidJUnit4.class)
+public class ReferenceImagesTest {
+    private Instrumentation mInstrumentation;
+    private GenerateImagesActivity mActivity;
 
-    /** Overall test timeout is 30 minutes. Should only take about 5. */
+    // Overall test timeout is 30 minutes. Should only take about 5.
     private static final int TEST_RESULT_TIMEOUT = 30 * 60 * 1000;
 
-    public ReferenceImagesTest() {
-        super(GenerateImagesActivity.class);
+    @Rule
+    public ActivityTestRule<GenerateImagesActivity> mActivityRule =
+        new ActivityTestRule<>(GenerateImagesActivity.class);
+
+    @Before
+    public void setup() {
+        mInstrumentation = InstrumentationRegistry.getInstrumentation();
+        mInstrumentation.setInTouchMode(true);
+
+        mActivity = mActivityRule.getActivity();
     }
 
+    @Test
     public void testGenerateReferenceImages() throws Exception {
-        setActivityInitialTouchMode(true);
-
-        final GenerateImagesActivity activity = getActivity();
+        final GenerateImagesActivity activity = mActivity;
         assertTrue("Activity failed to complete within " + TEST_RESULT_TIMEOUT + " ms",
                 activity.waitForCompletion(TEST_RESULT_TIMEOUT));
         assertTrue(activity.getFinishReason(), activity.isFinishSuccess());
diff --git a/hostsidetests/theme/app/src/android/theme/app/ThemeDeviceActivity.java b/hostsidetests/theme/app/src/android/theme/app/ThemeDeviceActivity.java
index 9469a0c..b30ffd3 100644
--- a/hostsidetests/theme/app/src/android/theme/app/ThemeDeviceActivity.java
+++ b/hostsidetests/theme/app/src/android/theme/app/ThemeDeviceActivity.java
@@ -16,7 +16,9 @@
 
 package android.theme.app;
 
+import android.animation.ValueAnimator;
 import android.app.Activity;
+import android.content.Context;
 import android.content.Intent;
 import android.content.res.Configuration;
 import android.os.Build;
@@ -28,6 +30,7 @@
 import android.theme.app.modifiers.ViewCheckedModifier;
 import android.theme.app.modifiers.ViewPressedModifier;
 import android.util.Log;
+import android.view.LayoutInflater;
 import android.view.View;
 import android.view.WindowManager.LayoutParams;
 import android.widget.DatePicker;
@@ -76,15 +79,20 @@
         mOutputDir = new File(outputDir);
         mTheme = THEMES[themeIndex];
 
-        setTheme(mTheme.id);
+        // Disable animations.
+        ValueAnimator.setDurationScale(0);
 
         // Force text scaling to 1.0 regardless of system default.
         Configuration config = new Configuration();
         config.fontScale = 1.0f;
-        getResources().updateConfiguration(config, null);
-        setContentView(R.layout.theme_test);
 
-        mViewGroup = (ReferenceViewGroup) findViewById(R.id.reference_view_group);
+        Context inflationContext = createConfigurationContext(config);
+        inflationContext.setTheme(mTheme.id);
+
+        LayoutInflater layoutInflater = LayoutInflater.from(inflationContext);
+        setContentView(layoutInflater.inflate(R.layout.theme_test, null));
+
+        mViewGroup = findViewById(R.id.reference_view_group);
 
         getWindow().addFlags(LayoutParams.FLAG_KEEP_SCREEN_ON
                 | LayoutParams.FLAG_TURN_SCREEN_ON
@@ -139,24 +147,26 @@
         mViewGroup.removeAllViews();
 
         final Layout layout = LAYOUTS[mLayoutIndex++];
+        final LayoutModifier modifier = layout.modifier;
         final String layoutName = String.format("%s_%s", mTheme.name, layout.name);
-        final View view = getLayoutInflater().inflate(layout.id, mViewGroup, false);
-        if (layout.modifier != null) {
-            layout.modifier.modifyView(view);
+        final LayoutInflater layoutInflater = LayoutInflater.from(mViewGroup.getContext());
+        final View view = layoutInflater.inflate(layout.id, mViewGroup, false);
+        if (modifier != null) {
+            modifier.modifyViewBeforeAdd(view);
         }
+        view.setFocusable(false);
 
         mViewGroup.addView(view);
-        view.setFocusable(false);
+
+        if (modifier != null) {
+            modifier.modifyViewAfterAdd(view);
+        }
 
         Log.v(TAG, "Rendering layout " + layoutName
                 + " (" + mLayoutIndex + "/" + LAYOUTS.length + ")");
 
-        final Runnable generateBitmapRunnable = new Runnable() {
-            @Override
-            public void run() {
-                new BitmapTask(view, layoutName).execute();
-            }
-        };
+        final Runnable generateBitmapRunnable = () ->
+            new BitmapTask(view, layoutName, modifier).execute();
 
         if (view instanceof DatePicker && mTheme.spec == Theme.HOLO) {
             // The Holo-styled DatePicker uses a CalendarView that has a
@@ -168,8 +178,12 @@
     }
 
     private class BitmapTask extends GenerateBitmapTask {
-        public BitmapTask(View view, String name) {
+        private final LayoutModifier mLayoutModifier;
+
+        public BitmapTask(View view, String name, LayoutModifier modifier) {
             super(view, mOutputDir, name);
+
+            mLayoutModifier = modifier;
         }
 
         @Override
@@ -205,6 +219,7 @@
     }
 
     // List of themes to verify.
+    @SuppressWarnings("deprecation")
     static final Theme[] THEMES = {
             // Holo
             new Theme(Theme.HOLO, android.R.style.Theme_Holo,
diff --git a/hostsidetests/theme/app/src/android/theme/app/modifiers/AbstractLayoutModifier.java b/hostsidetests/theme/app/src/android/theme/app/modifiers/AbstractLayoutModifier.java
index 5c945ef..e9dca7a 100644
--- a/hostsidetests/theme/app/src/android/theme/app/modifiers/AbstractLayoutModifier.java
+++ b/hostsidetests/theme/app/src/android/theme/app/modifiers/AbstractLayoutModifier.java
@@ -17,13 +17,18 @@
 package android.theme.app.modifiers;
 
 import android.theme.app.LayoutModifier;
+import android.view.View;
 
 /**
- * {@link LayoutModifier} that does nothing in {@link #prepare()}.
+ * {@link LayoutModifier} that does nothing.
  */
 abstract class AbstractLayoutModifier implements LayoutModifier {
 
     @Override
-    public void prepare() {
+    public void modifyViewBeforeAdd(View view) {
+    }
+
+    @Override
+    public void modifyViewAfterAdd(View view) {
     }
 }
diff --git a/hostsidetests/theme/app/src/android/theme/app/modifiers/DatePickerModifier.java b/hostsidetests/theme/app/src/android/theme/app/modifiers/DatePickerModifier.java
index 26ccd67..f155fcb 100644
--- a/hostsidetests/theme/app/src/android/theme/app/modifiers/DatePickerModifier.java
+++ b/hostsidetests/theme/app/src/android/theme/app/modifiers/DatePickerModifier.java
@@ -26,9 +26,8 @@
 public class DatePickerModifier extends AbstractLayoutModifier {
 
     @Override
-    public View modifyView(View view) {
+    public void modifyViewAfterAdd(View view) {
         DatePicker tp = (DatePicker) view;
         tp.updateDate(2011, 4, 20);
-        return view;
     }
 }
diff --git a/hostsidetests/theme/app/src/android/theme/app/modifiers/ProgressBarModifier.java b/hostsidetests/theme/app/src/android/theme/app/modifiers/ProgressBarModifier.java
index 9849a64..dcd8c89 100644
--- a/hostsidetests/theme/app/src/android/theme/app/modifiers/ProgressBarModifier.java
+++ b/hostsidetests/theme/app/src/android/theme/app/modifiers/ProgressBarModifier.java
@@ -16,6 +16,7 @@
 
 package android.theme.app.modifiers;
 
+import android.animation.ValueAnimator;
 import android.view.View;
 import android.view.animation.Interpolator;
 import android.widget.ProgressBar;
@@ -23,10 +24,9 @@
 public class ProgressBarModifier extends AbstractLayoutModifier {
 
     @Override
-    public View modifyView(View view) {
+    public void modifyViewBeforeAdd(View view) {
         ProgressBar pb = (ProgressBar) view;
         pb.setInterpolator(new ZeroInterpolator());
-        return pb;
     }
 
     private static class ZeroInterpolator implements Interpolator {
diff --git a/hostsidetests/theme/app/src/android/theme/app/modifiers/SearchViewModifier.java b/hostsidetests/theme/app/src/android/theme/app/modifiers/SearchViewModifier.java
index 75dd20a..de341ce 100644
--- a/hostsidetests/theme/app/src/android/theme/app/modifiers/SearchViewModifier.java
+++ b/hostsidetests/theme/app/src/android/theme/app/modifiers/SearchViewModifier.java
@@ -34,7 +34,7 @@
     }
 
     @Override
-    public View modifyView(View view) {
+    public void modifyViewBeforeAdd(View view) {
         SearchView searchView = (SearchView) view;
         Context context = view.getContext();
 
@@ -52,6 +52,5 @@
         }
 
         searchView.setIconifiedByDefault(false);
-        return searchView;
     }
 }
diff --git a/hostsidetests/theme/app/src/android/theme/app/modifiers/TimePickerModifier.java b/hostsidetests/theme/app/src/android/theme/app/modifiers/TimePickerModifier.java
index b2ed4ef..8e06ad8 100644
--- a/hostsidetests/theme/app/src/android/theme/app/modifiers/TimePickerModifier.java
+++ b/hostsidetests/theme/app/src/android/theme/app/modifiers/TimePickerModifier.java
@@ -22,11 +22,10 @@
 public class TimePickerModifier extends AbstractLayoutModifier {
 
     @Override
-    public View modifyView(View view) {
+    public void modifyViewBeforeAdd(View view) {
         TimePicker timePicker = (TimePicker) view;
         timePicker.setIs24HourView(true);
         timePicker.setCurrentHour(13);
         timePicker.setCurrentMinute(37);
-        return view;
     }
 }
diff --git a/hostsidetests/theme/app/src/android/theme/app/modifiers/ViewCheckedModifier.java b/hostsidetests/theme/app/src/android/theme/app/modifiers/ViewCheckedModifier.java
index f55f057..7810219 100644
--- a/hostsidetests/theme/app/src/android/theme/app/modifiers/ViewCheckedModifier.java
+++ b/hostsidetests/theme/app/src/android/theme/app/modifiers/ViewCheckedModifier.java
@@ -22,8 +22,7 @@
 public class ViewCheckedModifier extends AbstractLayoutModifier {
 
     @Override
-    public View modifyView(View view) {
+    public void modifyViewBeforeAdd(View view) {
         ((CheckBox) view).setChecked(true);
-        return view;
     }
 }
diff --git a/hostsidetests/theme/app/src/android/theme/app/modifiers/ViewPressedModifier.java b/hostsidetests/theme/app/src/android/theme/app/modifiers/ViewPressedModifier.java
index a94962d..78b329c 100644
--- a/hostsidetests/theme/app/src/android/theme/app/modifiers/ViewPressedModifier.java
+++ b/hostsidetests/theme/app/src/android/theme/app/modifiers/ViewPressedModifier.java
@@ -21,8 +21,7 @@
 public class ViewPressedModifier extends AbstractLayoutModifier {
 
     @Override
-    public View modifyView(View view) {
+    public void modifyViewBeforeAdd(View view) {
         view.setPressed(true);
-        return view;
     }
 }