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.text.method.cts;
18
19
20import android.app.Activity;
21import android.content.Context;
22import android.os.Bundle;
23import android.test.ActivityInstrumentationTestCase2;
24import android.text.Editable;
25import android.text.Selection;
26import android.text.method.CharacterPickerDialog;
27import android.view.View;
28import android.widget.AdapterView;
29import android.widget.Gallery;
30import android.widget.TextView;
31
32public class CharacterPickerDialogTest extends
33        ActivityInstrumentationTestCase2<StubActivity> {
34    private Activity mActivity;
35
36    public CharacterPickerDialogTest() {
37        super("com.android.cts.stub", StubActivity.class);
38    }
39
40    @Override
41    protected void setUp() throws Exception {
42        super.setUp();
43        mActivity = getActivity();
44    }
45
46    public void testConstructor() {
47        final CharSequence str = "123456";
48        final Editable content = Editable.Factory.getInstance().newEditable(str);
49        final View view = new TextView(mActivity);
50        new CharacterPickerDialog(view.getContext(), view, content, "\u00A1", false);
51
52        try {
53            new CharacterPickerDialog(null, view, content, "\u00A1", false);
54            fail("should throw NullPointerException.");
55        } catch (NullPointerException e) {
56            // expected.
57        }
58    }
59
60    public void testOnCreate() {
61        // Do not test. Implementation details.
62    }
63
64    public void testOnItemClick() {
65        final Gallery parent = new Gallery(mActivity);
66        final CharSequence str = "123456";
67        Editable text = Editable.Factory.getInstance().newEditable(str);
68        final View view = new TextView(mActivity);
69        CharacterPickerDialog replacePickerDialog =
70                new CharacterPickerDialog(view.getContext(), view, text, "abc", false);
71
72        // insert 'a' to the beginning of text
73        replacePickerDialog.show();
74        Selection.setSelection(text, 0, 0);
75        assertEquals(str, text.toString());
76        assertTrue(replacePickerDialog.isShowing());
77
78        replacePickerDialog.onItemClick(parent, view, 0, 0);
79        assertEquals("a123456", text.toString());
80        assertFalse(replacePickerDialog.isShowing());
81
82        // replace the second character '1' with 'c'
83        replacePickerDialog.show();
84        Selection.setSelection(text, 2, 2);
85        assertTrue(replacePickerDialog.isShowing());
86
87        replacePickerDialog.onItemClick(parent, view, 2, 0);
88        assertEquals("ac23456", text.toString());
89        assertFalse(replacePickerDialog.isShowing());
90
91        // insert character 'c' between '2' and '3'
92        text = Editable.Factory.getInstance().newEditable(str);
93        CharacterPickerDialog insertPickerDialog =
94            new CharacterPickerDialog(view.getContext(), view, text, "abc", true);
95        Selection.setSelection(text, 2, 2);
96        assertEquals(str, text.toString());
97        insertPickerDialog.show();
98        assertTrue(insertPickerDialog.isShowing());
99
100        insertPickerDialog.onItemClick(parent, view, 2, 0);
101        assertEquals("12c3456", text.toString());
102        assertFalse(insertPickerDialog.isShowing());
103    }
104
105    public void testOnClick() {
106        final CharSequence str = "123456";
107        final Editable content = Editable.Factory.getInstance().newEditable(str);
108        final View view = new TextView(mActivity);
109        CharacterPickerDialog characterPickerDialog =
110                new CharacterPickerDialog(view.getContext(), view, content, "\u00A1", false);
111
112        characterPickerDialog.show();
113        assertTrue(characterPickerDialog.isShowing());
114
115        // nothing to test here, just make sure onClick does not throw exception
116        characterPickerDialog.onClick(view);
117
118    }
119}
120