1/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.widget;
18
19import android.app.Activity;
20import android.content.Intent;
21import android.test.ActivityInstrumentationTestCase2;
22import android.test.suitebuilder.annotation.SmallTest;
23import android.text.GetChars;
24import android.text.Selection;
25import android.text.Spannable;
26
27/**
28 * TextViewTest tests {@link TextView}.
29 */
30public class TextViewTest extends ActivityInstrumentationTestCase2<TextViewActivity> {
31
32    public TextViewTest() {
33        super(TextViewActivity.class);
34    }
35
36    @SmallTest
37    public void testArray() throws Exception {
38        TextView tv = new TextView(getActivity());
39
40        char[] c = new char[] { 'H', 'e', 'l', 'l', 'o', ' ',
41                                'W', 'o', 'r', 'l', 'd', '!' };
42
43        tv.setText(c, 1, 4);
44        CharSequence oldText = tv.getText();
45
46        tv.setText(c, 4, 5);
47        CharSequence newText = tv.getText();
48
49        assertTrue(newText == oldText);
50
51        assertEquals(5, newText.length());
52        assertEquals('o', newText.charAt(0));
53        assertEquals("o Wor", newText.toString());
54
55        assertEquals(" Wo", newText.subSequence(1, 4));
56
57        char[] c2 = new char[7];
58        ((GetChars) newText).getChars(1, 4, c2, 2);
59        assertEquals('\0', c2[1]);
60        assertEquals(' ', c2[2]);
61        assertEquals('W', c2[3]);
62        assertEquals('o', c2[4]);
63        assertEquals('\0', c2[5]);
64    }
65
66    @SmallTest
67    public void testProcessTextActivityResultNonEditable() {
68        final TextView tv = new TextView(getActivity());
69        CharSequence originalText = "This is some text.";
70        tv.setText(originalText, TextView.BufferType.SPANNABLE);
71        assertEquals(originalText, tv.getText().toString());
72        tv.setTextIsSelectable(true);
73        Selection.setSelection((Spannable) tv.getText(), 0, tv.getText().length());
74
75        // We need to run this in the UI thread, as it will create a Toast.
76        getActivity().runOnUiThread(new Runnable() {
77            @Override
78            public void run() {
79                CharSequence newText = "Text is replaced.";
80                Intent data = new Intent();
81                data.putExtra(Intent.EXTRA_PROCESS_TEXT, newText);
82                tv.onActivityResult(TextView.PROCESS_TEXT_REQUEST_CODE, Activity.RESULT_OK, data);
83            }
84        });
85        getInstrumentation().waitForIdleSync();
86
87        // This is a TextView, which can't be modified. Hence no change should have been made.
88        assertEquals(originalText, tv.getText().toString());
89    }
90
91    @SmallTest
92    public void testProcessTextActivityResultEditable() {
93        EditText tv = new EditText(getActivity());
94        CharSequence originalText = "This is some text.";
95        tv.setText(originalText, TextView.BufferType.SPANNABLE);
96        assertEquals(originalText, tv.getText().toString());
97        tv.setTextIsSelectable(true);
98        Selection.setSelection(tv.getText(), 0, tv.getText().length());
99
100        CharSequence newText = "Text is replaced.";
101        Intent data = new Intent();
102        data.putExtra(Intent.EXTRA_PROCESS_TEXT, newText);
103        tv.onActivityResult(TextView.PROCESS_TEXT_REQUEST_CODE, Activity.RESULT_OK, data);
104
105        assertEquals(newText, tv.getText().toString());
106    }
107
108    @SmallTest
109    public void testProcessTextActivityResultCancel() {
110        EditText tv = new EditText(getActivity());
111        CharSequence originalText = "This is some text.";
112        tv.setText(originalText, TextView.BufferType.SPANNABLE);
113        assertEquals(originalText, tv.getText().toString());
114        tv.setTextIsSelectable(true);
115        Selection.setSelection(tv.getText(), 0, tv.getText().length());
116
117        CharSequence newText = "Text is replaced.";
118        Intent data = new Intent();
119        data.putExtra(Intent.EXTRA_PROCESS_TEXT, newText);
120        tv.onActivityResult(TextView.PROCESS_TEXT_REQUEST_CODE, Activity.RESULT_CANCELED, data);
121
122        assertEquals(originalText, tv.getText().toString());
123    }
124
125    @SmallTest
126    public void testProcessTextActivityNoData() {
127        EditText tv = new EditText(getActivity());
128        CharSequence originalText = "This is some text.";
129        tv.setText(originalText, TextView.BufferType.SPANNABLE);
130        assertEquals(originalText, tv.getText().toString());
131        tv.setTextIsSelectable(true);
132        Selection.setSelection(tv.getText(), 0, tv.getText().length());
133
134        tv.onActivityResult(TextView.PROCESS_TEXT_REQUEST_CODE, Activity.RESULT_OK, null);
135
136        assertEquals(originalText, tv.getText().toString());
137    }
138}
139