Properly setup autoMirror in VDC and AVDC.

Add a new test to cover it for API >= 17
Test: added new test, ran on GB Nexus S, KK Nexus 4 and Nougat Nexus 6p
b/31951281

Change-Id: I4d76e2ad81abae0959cad6ee2d6251bef426acaf
diff --git a/graphics/drawable/animated/src/android/support/graphics/drawable/AnimatedVectorDrawableCompat.java b/graphics/drawable/animated/src/android/support/graphics/drawable/AnimatedVectorDrawableCompat.java
index 26a8444..0b0522e 100644
--- a/graphics/drawable/animated/src/android/support/graphics/drawable/AnimatedVectorDrawableCompat.java
+++ b/graphics/drawable/animated/src/android/support/graphics/drawable/AnimatedVectorDrawableCompat.java
@@ -322,6 +322,23 @@
         return mAnimatedVectorState.mVectorDrawable.getIntrinsicHeight();
     }
 
+    @Override
+    public boolean isAutoMirrored() {
+        if (mDelegateDrawable != null) {
+            return DrawableCompat.isAutoMirrored(mDelegateDrawable);
+        }
+        return mAnimatedVectorState.mVectorDrawable.isAutoMirrored();
+    }
+
+    @Override
+    public void setAutoMirrored(boolean mirrored) {
+        if (mDelegateDrawable != null) {
+            mDelegateDrawable.setAutoMirrored(mirrored);
+            return;
+        }
+        mAnimatedVectorState.mVectorDrawable.setAutoMirrored(mirrored);
+    }
+
     /**
      * Obtains styled attributes from the theme, if available, or unstyled
      * resources if the theme is null.
diff --git a/graphics/drawable/static/src/android/support/graphics/drawable/VectorDrawableCommon.java b/graphics/drawable/static/src/android/support/graphics/drawable/VectorDrawableCommon.java
index 5f2dc6d..bcabd99 100644
--- a/graphics/drawable/static/src/android/support/graphics/drawable/VectorDrawableCommon.java
+++ b/graphics/drawable/static/src/android/support/graphics/drawable/VectorDrawableCommon.java
@@ -18,7 +18,6 @@
 import android.content.res.Resources;
 import android.content.res.TypedArray;
 import android.graphics.ColorFilter;
-import android.graphics.Outline;
 import android.graphics.PorterDuff;
 import android.graphics.Rect;
 import android.graphics.Region;
@@ -27,7 +26,6 @@
 import android.support.v4.graphics.drawable.DrawableCompat;
 import android.support.v4.graphics.drawable.TintAwareDrawable;
 import android.util.AttributeSet;
-import android.view.View;
 
 /**
  * Internal common delegation shared by VectorDrawableCompat and AnimatedVectorDrawableCompat
@@ -117,25 +115,6 @@
     }
 
     @Override
-    public void setAutoMirrored(boolean mirrored) {
-        // API >= 21 only.
-        if (mDelegateDrawable != null) {
-            DrawableCompat.setAutoMirrored(mDelegateDrawable, mirrored);
-
-            return;
-        }
-    }
-
-    @Override
-    public boolean isAutoMirrored() {
-        // API >= 21 only.
-        if (mDelegateDrawable != null) {
-            DrawableCompat.isAutoMirrored(mDelegateDrawable);
-        }
-        return false;
-    }
-
-    @Override
     public void applyTheme(Resources.Theme t) {
         // API >= 21 only.
         if (mDelegateDrawable != null) {
@@ -145,14 +124,6 @@
     }
 
     @Override
-    public int getLayoutDirection() {
-        if (mDelegateDrawable != null) {
-            DrawableCompat.getLayoutDirection(mDelegateDrawable);
-        }
-        return View.LAYOUT_DIRECTION_LTR;
-    }
-
-    @Override
     public void clearColorFilter() {
         if (mDelegateDrawable != null) {
             mDelegateDrawable.clearColorFilter();
diff --git a/graphics/drawable/static/src/android/support/graphics/drawable/VectorDrawableCompat.java b/graphics/drawable/static/src/android/support/graphics/drawable/VectorDrawableCompat.java
index 57c8c93..f4d1198 100644
--- a/graphics/drawable/static/src/android/support/graphics/drawable/VectorDrawableCompat.java
+++ b/graphics/drawable/static/src/android/support/graphics/drawable/VectorDrawableCompat.java
@@ -45,12 +45,16 @@
 import android.support.v4.graphics.drawable.DrawableCompat;
 import android.support.v4.util.ArrayMap;
 import android.util.AttributeSet;
+import android.util.LayoutDirection;
 import android.util.Log;
 import android.util.Xml;
 
 import org.xmlpull.v1.XmlPullParser;
 import org.xmlpull.v1.XmlPullParserException;
 
+import org.xmlpull.v1.XmlPullParser;
+import org.xmlpull.v1.XmlPullParserException;
+
 import java.io.IOException;
 import java.util.ArrayList;
 import java.util.Stack;
@@ -522,6 +526,22 @@
         return false;
     }
 
+    @Override
+    public boolean isAutoMirrored() {
+        if (mDelegateDrawable != null) {
+            return DrawableCompat.isAutoMirrored(mDelegateDrawable);
+        }
+        return mVectorState.mAutoMirrored;
+    }
+
+    @Override
+    public void setAutoMirrored(boolean mirrored) {
+        if (mDelegateDrawable != null) {
+            DrawableCompat.setAutoMirrored(mDelegateDrawable, mirrored);
+            return;
+        }
+        mVectorState.mAutoMirrored = mirrored;
+    }
     /**
      * The size of a pixel when scaled from the intrinsic dimension to the viewport dimension. This
      * is used to calculate the path animation accuracy.
@@ -825,7 +845,11 @@
 
     // We don't support RTL auto mirroring since the getLayoutDirection() is for API 17+.
     private boolean needMirroring() {
-        return false;
+        if (Build.VERSION.SDK_INT < 17) {
+            return false;
+        } else {
+            return isAutoMirrored() && getLayoutDirection() == LayoutDirection.RTL;
+        }
     }
 
     // Extra override functions for delegation for SDK >= 7.
diff --git a/v7/appcompat/tests/AndroidManifest.xml b/v7/appcompat/tests/AndroidManifest.xml
index 7288eb3..a667c50 100644
--- a/v7/appcompat/tests/AndroidManifest.xml
+++ b/v7/appcompat/tests/AndroidManifest.xml
@@ -94,6 +94,9 @@
                 android:name="android.support.v7.app.NightModeActivity"
                 android:theme="@style/Theme.AppCompat.DayNight"/>
 
+        <activity
+            android:name="android.support.v7.app.AppCompatVectorDrawableIntegrationActivity"/>
+
     </application>
 
     <instrumentation
diff --git a/v7/appcompat/tests/res/drawable/test_vector_left_white_right_black.xml b/v7/appcompat/tests/res/drawable/test_vector_left_white_right_black.xml
new file mode 100644
index 0000000..b36a2b7
--- /dev/null
+++ b/v7/appcompat/tests/res/drawable/test_vector_left_white_right_black.xml
@@ -0,0 +1,34 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (C) 2016 The Android Open Source Project
+
+     Licensed under the Apache License, Version 2.0 (the "License");
+     you may not use this file except in compliance with the License.
+     You may obtain a copy of the License at
+
+          http://www.apache.org/licenses/LICENSE-2.0
+
+     Unless required by applicable law or agreed to in writing, software
+     distributed under the License is distributed on an "AS IS" BASIS,
+     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+     See the License for the specific language governing permissions and
+     limitations under the License.
+-->
+
+<vector xmlns:android="http://schemas.android.com/apk/res/android"
+    android:width="32dp"
+    android:height="32dp"
+    android:viewportHeight="32"
+    android:viewportWidth="32"
+    android:autoMirrored="true">
+    <group>
+        <path
+            android:name="left_white"
+            android:fillColor="#FFFFFFFF"
+            android:pathData="M 0 0 h 16 v 32 h -16 Z" />
+        <path
+            android:name="right_black"
+            android:fillColor="#FF000000"
+            android:pathData="M 16 0 h 16 v 32 h -16 Z" />
+
+    </group>
+</vector>
\ No newline at end of file
diff --git a/v7/appcompat/tests/res/layout/appcompat_vectordrawable_integration.xml b/v7/appcompat/tests/res/layout/appcompat_vectordrawable_integration.xml
new file mode 100644
index 0000000..22c65a7
--- /dev/null
+++ b/v7/appcompat/tests/res/layout/appcompat_vectordrawable_integration.xml
@@ -0,0 +1,35 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (C) 2016 The Android Open Source Project
+
+     Licensed under the Apache License, Version 2.0 (the "License");
+     you may not use this file except in compliance with the License.
+     You may obtain a copy of the License at
+
+          http://www.apache.org/licenses/LICENSE-2.0
+
+     Unless required by applicable law or agreed to in writing, software
+     distributed under the License is distributed on an "AS IS" BASIS,
+     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+     See the License for the specific language governing permissions and
+     limitations under the License.
+-->
+
+<ScrollView
+    xmlns:android="http://schemas.android.com/apk/res/android"
+    xmlns:app="http://schemas.android.com/apk/res-auto"
+    android:layout_width="match_parent"
+    android:layout_height="match_parent">
+    <LinearLayout
+        android:layout_width="match_parent"
+        android:layout_height="wrap_content"
+        android:orientation="vertical">
+
+        <android.support.v7.widget.AppCompatImageView
+            android:id="@+id/view_vector_1"
+            android:layout_width="match_parent"
+            android:layout_height="wrap_content"
+            app:srcCompat="@drawable/test_vector_left_white_right_black"/>
+
+    </LinearLayout>
+
+</ScrollView>
diff --git a/v7/appcompat/tests/src/android/support/v7/app/AppCompatVectorDrawableIntegrationActivity.java b/v7/appcompat/tests/src/android/support/v7/app/AppCompatVectorDrawableIntegrationActivity.java
new file mode 100644
index 0000000..7b9d75f
--- /dev/null
+++ b/v7/appcompat/tests/src/android/support/v7/app/AppCompatVectorDrawableIntegrationActivity.java
@@ -0,0 +1,26 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package android.support.v7.app;
+
+import android.support.v7.appcompat.test.R;
+import android.support.v7.testutils.BaseTestActivity;
+
+public class AppCompatVectorDrawableIntegrationActivity extends BaseTestActivity {
+    @Override
+    protected int getContentViewLayoutResId() {
+        return R.layout.appcompat_vectordrawable_integration;
+    }
+}
diff --git a/v7/appcompat/tests/src/android/support/v7/app/AppCompatVectorDrawableIntegrationTest.java b/v7/appcompat/tests/src/android/support/v7/app/AppCompatVectorDrawableIntegrationTest.java
new file mode 100644
index 0000000..b021ade
--- /dev/null
+++ b/v7/appcompat/tests/src/android/support/v7/app/AppCompatVectorDrawableIntegrationTest.java
@@ -0,0 +1,88 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package android.support.v7.app;
+
+import static org.junit.Assert.assertEquals;
+
+import android.app.Activity;
+import android.graphics.Bitmap;
+import android.graphics.Canvas;
+import android.graphics.Color;
+import android.graphics.drawable.Drawable;
+import android.os.Build;
+import android.support.test.annotation.UiThreadTest;
+import android.support.v7.appcompat.test.R;
+import android.test.suitebuilder.annotation.SmallTest;
+import android.view.View;
+import android.widget.ImageView;
+
+import org.junit.Before;
+import org.junit.Test;
+
+@SmallTest
+public class AppCompatVectorDrawableIntegrationTest
+        extends BaseInstrumentationTestCase<AppCompatVectorDrawableIntegrationActivity> {
+
+    private Bitmap mBitmap;
+    private Canvas mCanvas;
+
+    private static final int WIDTH = 64;
+    private static final int HEIGHT = 64;
+    private static final int LEFT_CENTER_X = WIDTH / 4;
+    private static final int RIGHT_CENTER_X = WIDTH * 3 / 4;
+    private static final int CENTER_Y = HEIGHT / 2;
+
+    public AppCompatVectorDrawableIntegrationTest() {
+        super(AppCompatVectorDrawableIntegrationActivity.class);
+    }
+
+    @Before
+    public void setup() {
+        mBitmap = Bitmap.createBitmap(WIDTH, HEIGHT, Bitmap.Config.ARGB_8888);
+        mCanvas = new Canvas(mBitmap);
+    }
+
+    @Test
+    @UiThreadTest
+    public void testVectorDrawableAutoMirrored() {
+        Activity activity = mActivityTestRule.getActivity();
+        ImageView view1 = (ImageView) activity.findViewById(R.id.view_vector_1);
+        Drawable vectorDrawable = view1.getDrawable();
+        // We update the bounds here for writing into the bitmap correctly.
+        vectorDrawable.setBounds(0, 0, WIDTH, HEIGHT);
+        vectorDrawable.draw(mCanvas);
+
+        int leftColor = mBitmap.getPixel(LEFT_CENTER_X, CENTER_Y);
+        int rightColor = mBitmap.getPixel(RIGHT_CENTER_X, CENTER_Y);
+
+        // Gingerbread seems treating the alpha differently, so checking red channel only here.
+        // It is enough to tell the difference.
+        assertEquals("Left side should be white", Color.red(leftColor), 255);
+        assertEquals("Right side should be black", Color.red(rightColor), 0);
+
+        if (Build.VERSION.SDK_INT >= 17) {
+            // setLayoutDirection is only available after API 17.
+            view1.setLayoutDirection(View.LAYOUT_DIRECTION_RTL);
+            vectorDrawable.draw(mCanvas);
+
+            leftColor = mBitmap.getPixel(LEFT_CENTER_X, CENTER_Y);
+            rightColor = mBitmap.getPixel(RIGHT_CENTER_X, CENTER_Y);
+
+            assertEquals("Left side should be black", Color.red(leftColor), 0);
+            assertEquals("Right side should be white", Color.red(rightColor), 255);
+        }
+    }
+}