Merge "Fix View reset of layoutDirection and textDirection"
diff --git a/api/current.txt b/api/current.txt
index 559672f..2923805 100644
--- a/api/current.txt
+++ b/api/current.txt
@@ -22740,7 +22740,7 @@
     method public void requestDisallowInterceptTouchEvent(boolean);
     method public boolean requestSendAccessibilityEvent(android.view.View, android.view.accessibility.AccessibilityEvent);
     method public void requestTransparentRegion(android.view.View);
-    method protected void resetLayoutDirectionResolution();
+    method protected void resetResolvedLayoutDirection();
     method public void scheduleLayoutAnimation();
     method public void setAddStatesFromChildren(boolean);
     method public void setAlwaysDrawnWithCacheEnabled(boolean);
@@ -26417,7 +26417,7 @@
     method protected void onTextChanged(java.lang.CharSequence, int, int, int);
     method public boolean onTextContextMenuItem(int);
     method public void removeTextChangedListener(android.text.TextWatcher);
-    method protected void resetLayoutDirectionResolution();
+    method protected void resetResolvedLayoutDirection();
     method public void setAllCaps(boolean);
     method public final void setAutoLinkMask(int);
     method public void setCompoundDrawablePadding(int);
diff --git a/core/java/android/view/View.java b/core/java/android/view/View.java
index 1245898..74dc100 100644
--- a/core/java/android/view/View.java
+++ b/core/java/android/view/View.java
@@ -4474,7 +4474,7 @@
     @RemotableViewMethod
     public void setLayoutDirection(int layoutDirection) {
         if (getLayoutDirection() != layoutDirection) {
-            resetLayoutDirectionResolution();
+            resetResolvedLayoutDirection();
             // Setting the flag will also request a layout.
             setFlags(layoutDirection, LAYOUT_DIRECTION_MASK);
         }
@@ -9043,10 +9043,8 @@
             mPrivateFlags &= ~AWAKEN_SCROLL_BARS_ON_ATTACH;
         }
         jumpDrawablesToCurrentState();
-        resetLayoutDirectionResolution();
         resolveLayoutDirectionIfNeeded();
         resolvePadding();
-        resetResolvedTextDirection();
         resolveTextDirection();
         if (isFocused()) {
             InputMethodManager imm = InputMethodManager.peekInstance();
@@ -9143,7 +9141,7 @@
      *
      * @hide
      */
-    protected void resetLayoutDirectionResolution() {
+    protected void resetResolvedLayoutDirection() {
         // Reset the current View resolution
         mPrivateFlags2 &= ~LAYOUT_DIRECTION_RESOLVED;
     }
@@ -9190,6 +9188,9 @@
         }
 
         mCurrentAnimation = null;
+
+        resetResolvedLayoutDirection();
+        resetResolvedTextDirection();
     }
 
     /**
diff --git a/core/java/android/view/ViewGroup.java b/core/java/android/view/ViewGroup.java
index e6fdb17..752fd5a 100644
--- a/core/java/android/view/ViewGroup.java
+++ b/core/java/android/view/ViewGroup.java
@@ -4999,15 +4999,15 @@
     }
 
     @Override
-    protected void resetLayoutDirectionResolution() {
-        super.resetLayoutDirectionResolution();
+    protected void resetResolvedLayoutDirection() {
+        super.resetResolvedLayoutDirection();
 
         // Take care of resetting the children resolution too
         final int count = getChildCount();
         for (int i = 0; i < count; i++) {
             final View child = getChildAt(i);
             if (child.getLayoutDirection() == LAYOUT_DIRECTION_INHERIT) {
-                child.resetLayoutDirectionResolution();
+                child.resetResolvedLayoutDirection();
             }
         }
     }
diff --git a/core/java/android/widget/TextView.java b/core/java/android/widget/TextView.java
index 1e63e26..e350ec4 100644
--- a/core/java/android/widget/TextView.java
+++ b/core/java/android/widget/TextView.java
@@ -5571,8 +5571,8 @@
     }
 
     @Override
-    protected void resetLayoutDirectionResolution() {
-        super.resetLayoutDirectionResolution();
+    protected void resetResolvedLayoutDirection() {
+        super.resetResolvedLayoutDirection();
 
         if (mLayoutAlignment != null &&
                 (mTextAlign == TextAlign.VIEW_START ||
diff --git a/core/tests/coretests/AndroidManifest.xml b/core/tests/coretests/AndroidManifest.xml
index 8e2d925..40fa552 100644
--- a/core/tests/coretests/AndroidManifest.xml
+++ b/core/tests/coretests/AndroidManifest.xml
@@ -1051,6 +1051,13 @@
             </intent-filter>
         </activity>
 
+        <activity android:name="android.widget.TextViewTestActivity" android:label="TextViewTestActivity">
+            <intent-filter>
+                <action android:name="android.intent.action.MAIN" />
+                <category android:name="android.intent.category.FRAMEWORK_INSTRUMENTATION_TEST" />
+            </intent-filter>
+        </activity>
+
 
 
         <!-- Activity-level metadata -->
diff --git a/core/tests/coretests/res/layout/textview_test.xml b/core/tests/coretests/res/layout/textview_test.xml
new file mode 100644
index 0000000..f0c7b9e
--- /dev/null
+++ b/core/tests/coretests/res/layout/textview_test.xml
@@ -0,0 +1,27 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (C) 2011 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.
+-->
+
+<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+              android:id="@+id/textviewtest_layout"
+              android:layout_width="fill_parent"
+              android:layout_height="fill_parent">
+
+    <TextView android:id="@+id/textviewtest_textview"
+              android:layout_height="wrap_content"
+              android:layout_width="wrap_content"
+              android:text="@string/textview_hebrew_text"/>
+
+</LinearLayout>
\ No newline at end of file
diff --git a/core/tests/coretests/res/values/strings.xml b/core/tests/coretests/res/values/strings.xml
index f51b08e..71f3520 100644
--- a/core/tests/coretests/res/values/strings.xml
+++ b/core/tests/coretests/res/values/strings.xml
@@ -129,4 +129,6 @@
     <string name="button8">Button8</string>
     <string name="button9">Button9</string>
 
+    <string name="textview_hebrew_text">&#x05DD;&#x05DE;ab?!</string>
+
 </resources>
diff --git a/core/tests/coretests/src/android/widget/TextViewTest.java b/core/tests/coretests/src/android/widget/TextViewTest.java
index 6db67c0..c54e4a1 100644
--- a/core/tests/coretests/src/android/widget/TextViewTest.java
+++ b/core/tests/coretests/src/android/widget/TextViewTest.java
@@ -16,23 +16,25 @@
 
 package android.widget;
 
-import com.google.android.collect.Lists;
-import com.google.android.collect.Maps;
+import com.android.frameworks.coretests.R;
 
-import android.test.AndroidTestCase;
+import android.test.ActivityInstrumentationTestCase2;
 import android.test.suitebuilder.annotation.SmallTest;
 import android.text.GetChars;
 import android.view.View;
-import android.widget.TextView;
 
 /**
  * TextViewTest tests {@link TextView}.
  */
-public class TextViewTest extends AndroidTestCase {
+public class TextViewTest extends ActivityInstrumentationTestCase2<TextViewTestActivity> {
+
+    public TextViewTest() {
+        super(TextViewTestActivity.class);
+    }
 
     @SmallTest
     public void testArray() throws Exception {
-        TextView tv = new TextView(mContext);
+        TextView tv = new TextView(getActivity());
 
         char[] c = new char[] { 'H', 'e', 'l', 'l', 'o', ' ',
                                 'W', 'o', 'r', 'l', 'd', '!' };
@@ -62,13 +64,13 @@
 
     @SmallTest
     public void testTextDirectionDefault() {
-        TextView tv = new TextView(mContext);
+        TextView tv = new TextView(getActivity());
         assertEquals(View.TEXT_DIRECTION_INHERIT, tv.getTextDirection());
     }
 
     @SmallTest
     public void testSetGetTextDirection() {
-        TextView tv = new TextView(mContext);
+        TextView tv = new TextView(getActivity());
 
         tv.setTextDirection(View.TEXT_DIRECTION_FIRST_STRONG);
         assertEquals(View.TEXT_DIRECTION_FIRST_STRONG, tv.getTextDirection());
@@ -88,7 +90,7 @@
 
     @SmallTest
     public void testGetResolvedTextDirectionLtr() {
-        TextView tv = new TextView(mContext);
+        TextView tv = new TextView(getActivity());
         tv.setText("this is a test");
 
         tv.setTextDirection(View.TEXT_DIRECTION_FIRST_STRONG);
@@ -109,10 +111,10 @@
 
     @SmallTest
     public void testGetResolvedTextDirectionLtrWithInheritance() {
-        LinearLayout ll = new LinearLayout(mContext);
+        LinearLayout ll = new LinearLayout(getActivity());
         ll.setTextDirection(View.TEXT_DIRECTION_RTL);
 
-        TextView tv = new TextView(mContext);
+        TextView tv = new TextView(getActivity());
         tv.setText("this is a test");
         ll.addView(tv);
 
@@ -134,7 +136,7 @@
 
     @SmallTest
     public void testGetResolvedTextDirectionRtl() {
-        TextView tv = new TextView(mContext);
+        TextView tv = new TextView(getActivity());
         tv.setText("\u05DD\u05DE"); // hebrew
 
         tv.setTextDirection(View.TEXT_DIRECTION_FIRST_STRONG);
@@ -155,10 +157,9 @@
 
     @SmallTest
     public void testGetResolvedTextDirectionRtlWithInheritance() {
-        LinearLayout ll = new LinearLayout(mContext);
-        ll.setTextDirection(View.TEXT_DIRECTION_RTL);
+        LinearLayout ll = new LinearLayout(getActivity());
 
-        TextView tv = new TextView(mContext);
+        TextView tv = new TextView(getActivity());
         tv.setText("\u05DD\u05DE"); // hebrew
         ll.addView(tv);
 
@@ -169,6 +170,24 @@
         assertEquals(View.TEXT_DIRECTION_RTL, tv.getResolvedTextDirection());
 
         tv.setTextDirection(View.TEXT_DIRECTION_INHERIT);
+        assertEquals(View.TEXT_DIRECTION_FIRST_STRONG, tv.getResolvedTextDirection());
+
+        tv.setTextDirection(View.TEXT_DIRECTION_LTR);
+        assertEquals(View.TEXT_DIRECTION_LTR, tv.getResolvedTextDirection());
+
+        tv.setTextDirection(View.TEXT_DIRECTION_RTL);
+        assertEquals(View.TEXT_DIRECTION_RTL, tv.getResolvedTextDirection());
+
+        // Force to RTL text direction on the layout
+        ll.setTextDirection(View.TEXT_DIRECTION_RTL);
+
+        tv.setTextDirection(View.TEXT_DIRECTION_FIRST_STRONG);
+        assertEquals(View.TEXT_DIRECTION_RTL, tv.getResolvedTextDirection());
+
+        tv.setTextDirection(View.TEXT_DIRECTION_ANY_RTL);
+        assertEquals(View.TEXT_DIRECTION_RTL, tv.getResolvedTextDirection());
+
+        tv.setTextDirection(View.TEXT_DIRECTION_INHERIT);
         assertEquals(View.TEXT_DIRECTION_RTL, tv.getResolvedTextDirection());
 
         tv.setTextDirection(View.TEXT_DIRECTION_LTR);
@@ -180,10 +199,10 @@
 
     @SmallTest
     public void testCharCountHeuristic() {
-        LinearLayout ll = new LinearLayout(mContext);
+        LinearLayout ll = new LinearLayout(getActivity());
         ll.setLayoutDirection(View.LAYOUT_DIRECTION_RTL);
 
-        TextView tv = new TextView(mContext);
+        TextView tv = new TextView(getActivity());
         ll.addView(tv);
 
         tv.setTextDirection(View.TEXT_DIRECTION_CHAR_COUNT);
@@ -211,4 +230,23 @@
         tv.setText("ab \u05DD\u05DE"); // latin + hebrew at 50% each
         assertEquals(View.TEXT_DIRECTION_RTL, tv.getResolvedTextDirection());
     }
+
+    @SmallTest
+    public void testResetTextDirection() {
+        final TextViewTestActivity activity = getActivity();
+
+        final LinearLayout ll = (LinearLayout) activity.findViewById(R.id.textviewtest_layout);
+        final TextView tv = (TextView) activity.findViewById(R.id.textviewtest_textview);
+
+        getActivity().runOnUiThread(new Runnable() {
+            public void run() {
+                tv.setTextDirection(View.TEXT_DIRECTION_FIRST_STRONG);
+                assertEquals(View.TEXT_DIRECTION_RTL, tv.getResolvedTextDirection());
+                assertEquals(true, tv.isResolvedTextDirection());
+
+                ll.removeView(tv);
+                assertEquals(false, tv.isResolvedTextDirection());
+            }
+        });
+    }
 }
diff --git a/core/tests/coretests/src/android/widget/TextViewTestActivity.java b/core/tests/coretests/src/android/widget/TextViewTestActivity.java
new file mode 100644
index 0000000..1bb4d24
--- /dev/null
+++ b/core/tests/coretests/src/android/widget/TextViewTestActivity.java
@@ -0,0 +1,30 @@
+/*
+ * Copyright (C) 2011 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.widget;
+
+import android.app.Activity;
+import android.os.Bundle;
+
+import com.android.frameworks.coretests.R;
+
+public class TextViewTestActivity extends Activity {
+
+    @Override
+    protected void onCreate(Bundle savedInstanceState) {
+        super.onCreate(savedInstanceState);
+        setContentView(R.layout.textview_test);
+    }
+}