Rewrite TextView onSaveInstanceState CTS tests

The previous tests directly access the SaveState parcel, and make
assumptions about its internal layout, which is not valid. This patch
replaces those tests with new ones that actually save and restore
instance state to a real TextView, using stable public interfaces.

The save state also contains state related to undo, but coverage for
that was already added.

Bug: 21209641
Change-Id: I9e9a3b37afb8356021e7b7232ee36823a0ce63d5
diff --git a/tests/tests/widget/src/android/widget/cts/TextViewTest.java b/tests/tests/widget/src/android/widget/cts/TextViewTest.java
index ec098f8..1d4be86 100644
--- a/tests/tests/widget/src/android/widget/cts/TextViewTest.java
+++ b/tests/tests/widget/src/android/widget/cts/TextViewTest.java
@@ -1764,6 +1764,39 @@
     }
 
     @UiThreadTest
+    public void testSaveInstanceState() {
+        TextView originalTextView = new TextView(mActivity);
+        final String text = "This is a string";
+        originalTextView.setText(text);
+        originalTextView.setFreezesText(true);  // needed to actually save state
+        Parcelable state = originalTextView.onSaveInstanceState();
+
+        TextView restoredTextView = new TextView(mActivity);
+        restoredTextView.onRestoreInstanceState(state);
+        assertEquals(text, restoredTextView.getText().toString());
+    }
+
+    @UiThreadTest
+    public void testSaveInstanceStateSelection() {
+        TextView originalTextView = new TextView(mActivity);
+        final String text = "This is a string";
+        final Spannable spannable = new SpannableString(text);
+        originalTextView.setText(spannable);
+        originalTextView.setTextIsSelectable(true);
+        Selection.setSelection((Spannable) originalTextView.getText(), 5, 7);
+        originalTextView.setFreezesText(true);  // needed to actually save state
+        Parcelable state = originalTextView.onSaveInstanceState();
+
+        TextView restoredTextView = new TextView(mActivity);
+        // Setting a selection only has an effect on a TextView when it is selectable.
+        restoredTextView.setTextIsSelectable(true);
+        restoredTextView.onRestoreInstanceState(state);
+        assertEquals(text, restoredTextView.getText().toString());
+        assertEquals(5, restoredTextView.getSelectionStart());
+        assertEquals(7, restoredTextView.getSelectionEnd());
+    }
+
+    @UiThreadTest
     public void testSetText() {
         TextView tv = findTextView(R.id.textview_text);
 
diff --git a/tests/tests/widget/src/android/widget/cts/TextView_SaveStateTest.java b/tests/tests/widget/src/android/widget/cts/TextView_SaveStateTest.java
deleted file mode 100644
index 801a6fd..0000000
--- a/tests/tests/widget/src/android/widget/cts/TextView_SaveStateTest.java
+++ /dev/null
@@ -1,70 +0,0 @@
-/*
- * Copyright (C) 2008 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.cts;
-
-
-import android.os.Parcel;
-import android.test.InstrumentationTestCase;
-import android.text.TextUtils;
-import android.view.AbsSavedState;
-import android.widget.TextView;
-
-/**
- * Test {@link TextView.SavedState}.
- */
-public class TextView_SaveStateTest extends InstrumentationTestCase {
-    public void testToString() {
-        Parcel source = creatTestParcel(0, 0, true, "This is content");
-        TextView.SavedState state = TextView.SavedState.CREATOR.createFromParcel(source);
-
-        assertNotNull(state.toString());
-
-        source = creatTestParcel(5, 10, false, "This is another content");
-        state = TextView.SavedState.CREATOR.createFromParcel(source);
-
-        assertNotNull(state.toString());
-    }
-
-    public void testWriteToParcel() {
-        Parcel source = creatTestParcel(0, 0, true, "This is content");
-        TextView.SavedState state = TextView.SavedState.CREATOR.createFromParcel(source);
-        assertNotNull(state);
-    }
-
-    /**
-     * Gets the parcel.
-     *
-     * @param start the start
-     * @param end the end
-     * @param frozenWithFocus the frozen with focus
-     * @param text the text
-     * @return the parcel
-     */
-    private Parcel creatTestParcel(int start, int end, boolean frozenWithFocus, String text) {
-        Parcel source = Parcel.obtain();
-
-        source.writeParcelable(AbsSavedState.EMPTY_STATE, 0);
-        source.writeInt(start);
-        source.writeInt(end);
-        source.writeInt(frozenWithFocus ? 1 : 0);
-        TextView textView = new TextView(getInstrumentation().getTargetContext());
-        textView.setText(text);
-        TextUtils.writeToParcel(textView.getText(), source, 0);
-        source.setDataPosition(0);
-        return source;
-    }
-}