13487313ff582d8a7ae2bf58e7379c4577522dd61Scott Su/*
23487313ff582d8a7ae2bf58e7379c4577522dd61Scott Su * Copyright (C) 2008 The Android Open Source Project
33487313ff582d8a7ae2bf58e7379c4577522dd61Scott Su *
43487313ff582d8a7ae2bf58e7379c4577522dd61Scott Su * Licensed under the Apache License, Version 2.0 (the "License");
53487313ff582d8a7ae2bf58e7379c4577522dd61Scott Su * you may not use this file except in compliance with the License.
63487313ff582d8a7ae2bf58e7379c4577522dd61Scott Su * You may obtain a copy of the License at
73487313ff582d8a7ae2bf58e7379c4577522dd61Scott Su *
83487313ff582d8a7ae2bf58e7379c4577522dd61Scott Su *      http://www.apache.org/licenses/LICENSE-2.0
93487313ff582d8a7ae2bf58e7379c4577522dd61Scott Su *
103487313ff582d8a7ae2bf58e7379c4577522dd61Scott Su * Unless required by applicable law or agreed to in writing, software
113487313ff582d8a7ae2bf58e7379c4577522dd61Scott Su * distributed under the License is distributed on an "AS IS" BASIS,
123487313ff582d8a7ae2bf58e7379c4577522dd61Scott Su * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
133487313ff582d8a7ae2bf58e7379c4577522dd61Scott Su * See the License for the specific language governing permissions and
143487313ff582d8a7ae2bf58e7379c4577522dd61Scott Su * limitations under the License.
153487313ff582d8a7ae2bf58e7379c4577522dd61Scott Su */
163487313ff582d8a7ae2bf58e7379c4577522dd61Scott Su
173487313ff582d8a7ae2bf58e7379c4577522dd61Scott Supackage android.text.method.cts;
183487313ff582d8a7ae2bf58e7379c4577522dd61Scott Su
193487313ff582d8a7ae2bf58e7379c4577522dd61Scott Suimport com.android.cts.stub.R;
203487313ff582d8a7ae2bf58e7379c4577522dd61Scott Su
213487313ff582d8a7ae2bf58e7379c4577522dd61Scott Su
223487313ff582d8a7ae2bf58e7379c4577522dd61Scott Suimport android.app.Activity;
233487313ff582d8a7ae2bf58e7379c4577522dd61Scott Suimport android.app.Instrumentation;
243487313ff582d8a7ae2bf58e7379c4577522dd61Scott Suimport android.test.ActivityInstrumentationTestCase2;
253487313ff582d8a7ae2bf58e7379c4577522dd61Scott Suimport android.text.InputType;
263487313ff582d8a7ae2bf58e7379c4577522dd61Scott Suimport android.text.method.DateTimeKeyListener;
27300d755fcaa7274f1570f53d34c7a657bc420ab6Brian Muramatsuimport android.view.KeyCharacterMap;
283487313ff582d8a7ae2bf58e7379c4577522dd61Scott Suimport android.view.KeyEvent;
293487313ff582d8a7ae2bf58e7379c4577522dd61Scott Suimport android.widget.TextView;
303487313ff582d8a7ae2bf58e7379c4577522dd61Scott Su
313487313ff582d8a7ae2bf58e7379c4577522dd61Scott Su/**
323487313ff582d8a7ae2bf58e7379c4577522dd61Scott Su * Test {@link DateTimeKeyListener}.
333487313ff582d8a7ae2bf58e7379c4577522dd61Scott Su */
343487313ff582d8a7ae2bf58e7379c4577522dd61Scott Supublic class DateTimeKeyListenerTest extends
353487313ff582d8a7ae2bf58e7379c4577522dd61Scott Su        ActivityInstrumentationTestCase2<KeyListenerStubActivity> {
363487313ff582d8a7ae2bf58e7379c4577522dd61Scott Su    private Activity mActivity;
373487313ff582d8a7ae2bf58e7379c4577522dd61Scott Su    private Instrumentation mInstrumentation;
383487313ff582d8a7ae2bf58e7379c4577522dd61Scott Su    private TextView mTextView;
393487313ff582d8a7ae2bf58e7379c4577522dd61Scott Su
403487313ff582d8a7ae2bf58e7379c4577522dd61Scott Su    public DateTimeKeyListenerTest(){
413487313ff582d8a7ae2bf58e7379c4577522dd61Scott Su        super("com.android.cts.stub", KeyListenerStubActivity.class);
423487313ff582d8a7ae2bf58e7379c4577522dd61Scott Su    }
433487313ff582d8a7ae2bf58e7379c4577522dd61Scott Su
443487313ff582d8a7ae2bf58e7379c4577522dd61Scott Su    @Override
453487313ff582d8a7ae2bf58e7379c4577522dd61Scott Su    protected void setUp() throws Exception {
463487313ff582d8a7ae2bf58e7379c4577522dd61Scott Su        super.setUp();
473487313ff582d8a7ae2bf58e7379c4577522dd61Scott Su
483487313ff582d8a7ae2bf58e7379c4577522dd61Scott Su        mActivity = getActivity();
493487313ff582d8a7ae2bf58e7379c4577522dd61Scott Su        mInstrumentation = getInstrumentation();
503487313ff582d8a7ae2bf58e7379c4577522dd61Scott Su        mTextView = (TextView) mActivity.findViewById(R.id.keylistener_textview);
513487313ff582d8a7ae2bf58e7379c4577522dd61Scott Su    }
523487313ff582d8a7ae2bf58e7379c4577522dd61Scott Su
533487313ff582d8a7ae2bf58e7379c4577522dd61Scott Su    public void testConstructor() {
543487313ff582d8a7ae2bf58e7379c4577522dd61Scott Su        new DateTimeKeyListener();
553487313ff582d8a7ae2bf58e7379c4577522dd61Scott Su    }
563487313ff582d8a7ae2bf58e7379c4577522dd61Scott Su
573487313ff582d8a7ae2bf58e7379c4577522dd61Scott Su    public void testGetInstance() {
583487313ff582d8a7ae2bf58e7379c4577522dd61Scott Su        DateTimeKeyListener listener1 = DateTimeKeyListener.getInstance();
593487313ff582d8a7ae2bf58e7379c4577522dd61Scott Su        DateTimeKeyListener listener2 = DateTimeKeyListener.getInstance();
603487313ff582d8a7ae2bf58e7379c4577522dd61Scott Su
613487313ff582d8a7ae2bf58e7379c4577522dd61Scott Su        assertNotNull(listener1);
623487313ff582d8a7ae2bf58e7379c4577522dd61Scott Su        assertNotNull(listener2);
633487313ff582d8a7ae2bf58e7379c4577522dd61Scott Su        assertSame(listener1, listener2);
643487313ff582d8a7ae2bf58e7379c4577522dd61Scott Su    }
653487313ff582d8a7ae2bf58e7379c4577522dd61Scott Su
663487313ff582d8a7ae2bf58e7379c4577522dd61Scott Su    public void testGetAcceptedChars() {
673487313ff582d8a7ae2bf58e7379c4577522dd61Scott Su        MyDateTimeKeyListener dataTimeKeyListener = new MyDateTimeKeyListener();
683487313ff582d8a7ae2bf58e7379c4577522dd61Scott Su
693487313ff582d8a7ae2bf58e7379c4577522dd61Scott Su        TextMethodUtils.assertEquals(DateTimeKeyListener.CHARACTERS,
703487313ff582d8a7ae2bf58e7379c4577522dd61Scott Su                dataTimeKeyListener.getAcceptedChars());
713487313ff582d8a7ae2bf58e7379c4577522dd61Scott Su    }
723487313ff582d8a7ae2bf58e7379c4577522dd61Scott Su
733487313ff582d8a7ae2bf58e7379c4577522dd61Scott Su    public void testGetInputType() {
743487313ff582d8a7ae2bf58e7379c4577522dd61Scott Su        DateTimeKeyListener listener = DateTimeKeyListener.getInstance();
753487313ff582d8a7ae2bf58e7379c4577522dd61Scott Su
763487313ff582d8a7ae2bf58e7379c4577522dd61Scott Su        int expected = InputType.TYPE_CLASS_DATETIME
773487313ff582d8a7ae2bf58e7379c4577522dd61Scott Su                | InputType.TYPE_DATETIME_VARIATION_NORMAL;
783487313ff582d8a7ae2bf58e7379c4577522dd61Scott Su        assertEquals(expected, listener.getInputType());
793487313ff582d8a7ae2bf58e7379c4577522dd61Scott Su    }
803487313ff582d8a7ae2bf58e7379c4577522dd61Scott Su
813487313ff582d8a7ae2bf58e7379c4577522dd61Scott Su    /**
823487313ff582d8a7ae2bf58e7379c4577522dd61Scott Su     * Scenario description:
833487313ff582d8a7ae2bf58e7379c4577522dd61Scott Su     * 1. Press '1' key and check if the content of TextView becomes "1"
843487313ff582d8a7ae2bf58e7379c4577522dd61Scott Su     * 2. Press '2' key and check if the content of TextView becomes "12"
85300d755fcaa7274f1570f53d34c7a657bc420ab6Brian Muramatsu     * 3. Press 'a' key if it is producible
86300d755fcaa7274f1570f53d34c7a657bc420ab6Brian Muramatsu     * 4. Press 'p' key if it is producible
87300d755fcaa7274f1570f53d34c7a657bc420ab6Brian Muramatsu     * 5. Press 'm' key if it is producible
88300d755fcaa7274f1570f53d34c7a657bc420ab6Brian Muramatsu     * 6. Press an unaccepted key if it exists. and this key will not be accepted.
89300d755fcaa7274f1570f53d34c7a657bc420ab6Brian Muramatsu     * 7. Remove DateKeyListener and Press '1' key, this key will not be accepted
903487313ff582d8a7ae2bf58e7379c4577522dd61Scott Su     */
913487313ff582d8a7ae2bf58e7379c4577522dd61Scott Su    public void testDateTimeKeyListener() {
923487313ff582d8a7ae2bf58e7379c4577522dd61Scott Su        final DateTimeKeyListener dateTimeKeyListener = DateTimeKeyListener.getInstance();
93300d755fcaa7274f1570f53d34c7a657bc420ab6Brian Muramatsu        String expectedText = "";
943487313ff582d8a7ae2bf58e7379c4577522dd61Scott Su
953487313ff582d8a7ae2bf58e7379c4577522dd61Scott Su        mActivity.runOnUiThread(new Runnable() {
963487313ff582d8a7ae2bf58e7379c4577522dd61Scott Su            public void run() {
973487313ff582d8a7ae2bf58e7379c4577522dd61Scott Su                mTextView.setKeyListener(dateTimeKeyListener);
983487313ff582d8a7ae2bf58e7379c4577522dd61Scott Su                mTextView.requestFocus();
993487313ff582d8a7ae2bf58e7379c4577522dd61Scott Su            }
1003487313ff582d8a7ae2bf58e7379c4577522dd61Scott Su        });
1013487313ff582d8a7ae2bf58e7379c4577522dd61Scott Su        mInstrumentation.waitForIdleSync();
102300d755fcaa7274f1570f53d34c7a657bc420ab6Brian Muramatsu        assertEquals(expectedText, mTextView.getText().toString());
1033487313ff582d8a7ae2bf58e7379c4577522dd61Scott Su
1043487313ff582d8a7ae2bf58e7379c4577522dd61Scott Su        // press '1' key.
105f176eafe119e8065c29f6eb99d62d01274581174Brian Muramatsu        mInstrumentation.sendStringSync("1");
106300d755fcaa7274f1570f53d34c7a657bc420ab6Brian Muramatsu        expectedText += "1";
107300d755fcaa7274f1570f53d34c7a657bc420ab6Brian Muramatsu        assertEquals(expectedText, mTextView.getText().toString());
1083487313ff582d8a7ae2bf58e7379c4577522dd61Scott Su
1093487313ff582d8a7ae2bf58e7379c4577522dd61Scott Su        // press '2' key.
110f176eafe119e8065c29f6eb99d62d01274581174Brian Muramatsu        mInstrumentation.sendStringSync("2");
111300d755fcaa7274f1570f53d34c7a657bc420ab6Brian Muramatsu        expectedText += "2";
112300d755fcaa7274f1570f53d34c7a657bc420ab6Brian Muramatsu        assertEquals(expectedText, mTextView.getText().toString());
113300d755fcaa7274f1570f53d34c7a657bc420ab6Brian Muramatsu
114300d755fcaa7274f1570f53d34c7a657bc420ab6Brian Muramatsu        // press 'a' key if producible
115a3712c065dd0ae4893aa9e9b715903f9ce353da3Brian Muramatsu        KeyCharacterMap kcm = KeyCharacterMap.load(KeyCharacterMap.VIRTUAL_KEYBOARD);
116300d755fcaa7274f1570f53d34c7a657bc420ab6Brian Muramatsu        if ('a' == kcm.getMatch(KeyEvent.KEYCODE_A, DateTimeKeyListener.CHARACTERS)) {
117300d755fcaa7274f1570f53d34c7a657bc420ab6Brian Muramatsu            expectedText += "a";
118300d755fcaa7274f1570f53d34c7a657bc420ab6Brian Muramatsu            mInstrumentation.sendKeyDownUpSync(KeyEvent.KEYCODE_A);
119300d755fcaa7274f1570f53d34c7a657bc420ab6Brian Muramatsu            assertEquals(expectedText, mTextView.getText().toString());
120300d755fcaa7274f1570f53d34c7a657bc420ab6Brian Muramatsu        }
121300d755fcaa7274f1570f53d34c7a657bc420ab6Brian Muramatsu
122300d755fcaa7274f1570f53d34c7a657bc420ab6Brian Muramatsu        // press 'p' key if producible
123300d755fcaa7274f1570f53d34c7a657bc420ab6Brian Muramatsu        if ('p' == kcm.getMatch(KeyEvent.KEYCODE_P, DateTimeKeyListener.CHARACTERS)) {
124300d755fcaa7274f1570f53d34c7a657bc420ab6Brian Muramatsu            expectedText += "p";
125300d755fcaa7274f1570f53d34c7a657bc420ab6Brian Muramatsu            mInstrumentation.sendKeyDownUpSync(KeyEvent.KEYCODE_P);
126300d755fcaa7274f1570f53d34c7a657bc420ab6Brian Muramatsu            assertEquals(expectedText, mTextView.getText().toString());
127300d755fcaa7274f1570f53d34c7a657bc420ab6Brian Muramatsu        }
1283487313ff582d8a7ae2bf58e7379c4577522dd61Scott Su
129300d755fcaa7274f1570f53d34c7a657bc420ab6Brian Muramatsu        // press 'm' key if producible
130300d755fcaa7274f1570f53d34c7a657bc420ab6Brian Muramatsu        if ('m' == kcm.getMatch(KeyEvent.KEYCODE_M, DateTimeKeyListener.CHARACTERS)) {
131300d755fcaa7274f1570f53d34c7a657bc420ab6Brian Muramatsu            expectedText += "m";
132300d755fcaa7274f1570f53d34c7a657bc420ab6Brian Muramatsu            mInstrumentation.sendKeyDownUpSync(KeyEvent.KEYCODE_M);
133300d755fcaa7274f1570f53d34c7a657bc420ab6Brian Muramatsu            assertEquals(expectedText, mTextView.getText().toString());
134300d755fcaa7274f1570f53d34c7a657bc420ab6Brian Muramatsu        }
1353487313ff582d8a7ae2bf58e7379c4577522dd61Scott Su
1363487313ff582d8a7ae2bf58e7379c4577522dd61Scott Su        // press an unaccepted key if it exists.
1373487313ff582d8a7ae2bf58e7379c4577522dd61Scott Su        int keyCode = TextMethodUtils.getUnacceptedKeyCode(DateTimeKeyListener.CHARACTERS);
1383487313ff582d8a7ae2bf58e7379c4577522dd61Scott Su        if (-1 != keyCode) {
1393487313ff582d8a7ae2bf58e7379c4577522dd61Scott Su            sendKeys(keyCode);
140300d755fcaa7274f1570f53d34c7a657bc420ab6Brian Muramatsu            assertEquals(expectedText, mTextView.getText().toString());
1413487313ff582d8a7ae2bf58e7379c4577522dd61Scott Su        }
1423487313ff582d8a7ae2bf58e7379c4577522dd61Scott Su
1433487313ff582d8a7ae2bf58e7379c4577522dd61Scott Su        // remove DateTimeKeyListener
1443487313ff582d8a7ae2bf58e7379c4577522dd61Scott Su        mActivity.runOnUiThread(new Runnable() {
1453487313ff582d8a7ae2bf58e7379c4577522dd61Scott Su            public void run() {
1463487313ff582d8a7ae2bf58e7379c4577522dd61Scott Su                mTextView.setKeyListener(null);
1473487313ff582d8a7ae2bf58e7379c4577522dd61Scott Su                mTextView.requestFocus();
1483487313ff582d8a7ae2bf58e7379c4577522dd61Scott Su            }
1493487313ff582d8a7ae2bf58e7379c4577522dd61Scott Su        });
1503487313ff582d8a7ae2bf58e7379c4577522dd61Scott Su        mInstrumentation.waitForIdleSync();
151300d755fcaa7274f1570f53d34c7a657bc420ab6Brian Muramatsu        assertEquals(expectedText, mTextView.getText().toString());
1523487313ff582d8a7ae2bf58e7379c4577522dd61Scott Su
153f176eafe119e8065c29f6eb99d62d01274581174Brian Muramatsu        mInstrumentation.sendStringSync("1");
154300d755fcaa7274f1570f53d34c7a657bc420ab6Brian Muramatsu        assertEquals(expectedText, mTextView.getText().toString());
1553487313ff582d8a7ae2bf58e7379c4577522dd61Scott Su    }
1563487313ff582d8a7ae2bf58e7379c4577522dd61Scott Su
1573487313ff582d8a7ae2bf58e7379c4577522dd61Scott Su    private class MyDateTimeKeyListener extends DateTimeKeyListener {
1583487313ff582d8a7ae2bf58e7379c4577522dd61Scott Su        @Override
1593487313ff582d8a7ae2bf58e7379c4577522dd61Scott Su        protected char[] getAcceptedChars() {
1603487313ff582d8a7ae2bf58e7379c4577522dd61Scott Su            return super.getAcceptedChars();
1613487313ff582d8a7ae2bf58e7379c4577522dd61Scott Su        }
1623487313ff582d8a7ae2bf58e7379c4577522dd61Scott Su    }
1633487313ff582d8a7ae2bf58e7379c4577522dd61Scott Su}
164