2 * Copyright (C) 2008 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package android
.widget
;
19 import android
.app
.Activity
;
20 import android
.content
.Intent
;
21 import android
.test
.AndroidTestCase
;
22 import android
.test
.suitebuilder
.annotation
.SmallTest
;
23 import android
.text
.GetChars
;
24 import android
.text
.Selection
;
25 import android
.text
.Spannable
;
28 * TextViewTest tests {@link TextView}.
30 public class TextViewTest
extends AndroidTestCase
{
33 public void testArray() throws Exception
{
34 TextView tv
= new TextView(mContext
);
36 char[] c
= new char[] { 'H', 'e', 'l', 'l', 'o', ' ',
37 'W', 'o', 'r', 'l', 'd', '!' };
40 CharSequence oldText
= tv
.getText();
43 CharSequence newText
= tv
.getText();
45 assertTrue(newText
== oldText
);
47 assertEquals(5, newText
.length());
48 assertEquals('o', newText
.charAt(0));
49 assertEquals("o Wor", newText
.toString());
51 assertEquals(" Wo", newText
.subSequence(1, 4));
53 char[] c2
= new char[7];
54 ((GetChars
) newText
).getChars(1, 4, c2
, 2);
55 assertEquals('\0', c2
[1]);
56 assertEquals(' ', c2
[2]);
57 assertEquals('W', c2
[3]);
58 assertEquals('o', c2
[4]);
59 assertEquals('\0', c2
[5]);
62 public void testProcessTextActivityResultNonEditable() {
63 TextView tv
= new TextView(mContext
);
64 CharSequence originalText
= "This is some text.";
65 tv
.setText(originalText
, TextView
.BufferType
.SPANNABLE
);
66 assertEquals(originalText
, tv
.getText().toString());
67 tv
.setTextIsSelectable(true);
68 Selection
.setSelection((Spannable
) tv
.getText(), 0, tv
.getText().length());
70 CharSequence newText
= "Text is replaced.";
71 Intent data
= new Intent();
72 data
.putExtra(Intent
.EXTRA_PROCESS_TEXT
, newText
);
73 tv
.onActivityResult(TextView
.PROCESS_TEXT_REQUEST_CODE
, Activity
.RESULT_OK
, data
);
75 // This is a TextView, which can't be modified. Hence no change should have been made.
76 assertEquals(originalText
, tv
.getText().toString());
79 public void testProcessTextActivityResultEditable() {
80 EditText tv
= new EditText(mContext
);
81 CharSequence originalText
= "This is some text.";
82 tv
.setText(originalText
, TextView
.BufferType
.SPANNABLE
);
83 assertEquals(originalText
, tv
.getText().toString());
84 tv
.setTextIsSelectable(true);
85 Selection
.setSelection(tv
.getText(), 0, tv
.getText().length());
87 CharSequence newText
= "Text is replaced.";
88 Intent data
= new Intent();
89 data
.putExtra(Intent
.EXTRA_PROCESS_TEXT
, newText
);
90 tv
.onActivityResult(TextView
.PROCESS_TEXT_REQUEST_CODE
, Activity
.RESULT_OK
, data
);
92 assertEquals(newText
, tv
.getText().toString());
95 public void testProcessTextActivityResultCancel() {
96 EditText tv
= new EditText(mContext
);
97 CharSequence originalText
= "This is some text.";
98 tv
.setText(originalText
, TextView
.BufferType
.SPANNABLE
);
99 assertEquals(originalText
, tv
.getText().toString());
100 tv
.setTextIsSelectable(true);
101 Selection
.setSelection(tv
.getText(), 0, tv
.getText().length());
103 CharSequence newText
= "Text is replaced.";
104 Intent data
= new Intent();
105 data
.putExtra(Intent
.EXTRA_PROCESS_TEXT
, newText
);
106 tv
.onActivityResult(TextView
.PROCESS_TEXT_REQUEST_CODE
, Activity
.RESULT_CANCELED
, data
);
108 assertEquals(originalText
, tv
.getText().toString());
111 public void testProcessTextActivityNoData() {
112 EditText tv
= new EditText(mContext
);
113 CharSequence originalText
= "This is some text.";
114 tv
.setText(originalText
, TextView
.BufferType
.SPANNABLE
);
115 assertEquals(originalText
, tv
.getText().toString());
116 tv
.setTextIsSelectable(true);
117 Selection
.setSelection(tv
.getText(), 0, tv
.getText().length());
119 tv
.onActivityResult(TextView
.PROCESS_TEXT_REQUEST_CODE
, Activity
.RESULT_OK
, null);
121 assertEquals(originalText
, tv
.getText().toString());