Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 package org.chromium.content.browser.input; | |
| 6 | |
| 7 import static org.junit.Assert.assertEquals; | |
| 8 import static org.junit.Assert.assertFalse; | |
| 9 import static org.junit.Assert.assertNull; | |
| 10 import static org.junit.Assert.assertTrue; | |
| 11 import static org.junit.Assert.fail; | |
| 12 import static org.mockito.Mockito.inOrder; | |
| 13 import static org.mockito.Mockito.never; | |
| 14 import static org.mockito.Mockito.when; | |
| 15 | |
| 16 import android.os.Handler; | |
| 17 | |
| 18 import org.chromium.base.ThreadUtils; | |
| 19 import org.chromium.base.test.util.Feature; | |
| 20 import org.chromium.testing.local.LocalRobolectricTestRunner; | |
| 21 import org.junit.Before; | |
| 22 import org.junit.Test; | |
| 23 import org.junit.runner.RunWith; | |
| 24 import org.mockito.InOrder; | |
| 25 import org.mockito.Mock; | |
| 26 import org.mockito.Mockito; | |
| 27 import org.mockito.MockitoAnnotations; | |
| 28 import org.robolectric.annotation.Config; | |
| 29 | |
| 30 /** | |
| 31 * Unit tests for {@ThreadedInputConnection}. | |
| 32 */ | |
| 33 @RunWith(LocalRobolectricTestRunner.class) | |
| 34 @Config(manifest = Config.NONE) | |
| 35 public class ThreadedInputConnectionTest { | |
| 36 @Mock ImeAdapter mImeAdapter; | |
| 37 | |
| 38 ThreadedInputConnection mConnection; | |
| 39 InOrder mInOrder; | |
| 40 | |
| 41 @Before | |
| 42 public void setUp() throws Exception { | |
| 43 MockitoAnnotations.initMocks(this); | |
| 44 | |
| 45 mImeAdapter = Mockito.mock(ImeAdapter.class); | |
| 46 mInOrder = inOrder(mImeAdapter); | |
| 47 // Let's create Handler for test thread and pretend that it is running o n IME thread. | |
| 48 mConnection = new ThreadedInputConnection(mImeAdapter, new Handler()); | |
| 49 } | |
| 50 | |
| 51 @Test | |
| 52 @Feature({"TextInput"}) | |
| 53 public void testComposeGetTextFinishGetText() { | |
| 54 // IME app calls setComposingText(). | |
| 55 mConnection.setComposingText("hello", 1); | |
| 56 mInOrder.verify(mImeAdapter).sendCompositionToNative("hello", 1, false); | |
| 57 | |
| 58 // Renderer updates states asynchronously. | |
| 59 mConnection.updateStateOnUiThread("hello", 5, 5, 0, 5, true, true); | |
| 60 mInOrder.verify(mImeAdapter).updateSelection(5, 5, 0, 5); | |
| 61 assertEquals(0, mConnection.getQueueForTest().size()); | |
| 62 | |
| 63 // Prepare to call requestTextInputStateUpdate. | |
| 64 mConnection.updateStateOnUiThread("hello", 5, 5, 0, 5, true, false); | |
| 65 assertEquals(1, mConnection.getQueueForTest().size()); | |
| 66 when(mImeAdapter.requestTextInputStateUpdate()).thenReturn(true); | |
| 67 | |
| 68 // IME app calls getTextBeforeCursor(). | |
| 69 assertEquals("hello", mConnection.getTextBeforeCursor(20, 0)); | |
| 70 | |
| 71 // IME app calls finishComposingText(). | |
| 72 mConnection.finishComposingText(); | |
| 73 mInOrder.verify(mImeAdapter).finishComposingText(); | |
| 74 mConnection.updateStateOnUiThread("hello", 5, 5, -1, -1, true, true); | |
| 75 mInOrder.verify(mImeAdapter).updateSelection(5, 5, -1, -1); | |
| 76 | |
| 77 // Prepare to call requestTextInputStateUpdate. | |
| 78 mConnection.updateStateOnUiThread("hello", 5, 5, -1, -1, true, false); | |
| 79 assertEquals(1, mConnection.getQueueForTest().size()); | |
| 80 when(mImeAdapter.requestTextInputStateUpdate()).thenReturn(true); | |
| 81 | |
| 82 // IME app calls getTextBeforeCursor(). | |
| 83 assertEquals("hello", mConnection.getTextBeforeCursor(20, 0)); | |
| 84 | |
| 85 assertEquals(0, mConnection.getQueueForTest().size()); | |
| 86 } | |
| 87 | |
| 88 @Test | |
| 89 @Feature({"TextInput"}) | |
| 90 public void testRenderChangeUpdatesSelection() { | |
| 91 // User moves the cursor. | |
| 92 mConnection.updateStateOnUiThread("hello", 4, 4, -1, -1, true, true); | |
| 93 mInOrder.verify(mImeAdapter).updateSelection(4, 4, -1, -1); | |
| 94 assertEquals(0, mConnection.getQueueForTest().size()); | |
| 95 } | |
| 96 | |
| 97 @Test | |
| 98 @Feature({"TextInput"}) | |
| 99 public void testBatchEdit() { | |
| 100 // IME app calls beginBatchEdit(). | |
| 101 assertTrue(mConnection.beginBatchEdit()); | |
| 102 // Type hello real fast. | |
| 103 mConnection.commitText("hello", 1); | |
| 104 mInOrder.verify(mImeAdapter).sendCompositionToNative("hello", 1, true); | |
| 105 | |
| 106 // Renderer updates states asynchronously. | |
| 107 mConnection.updateStateOnUiThread("hello", 5, 5, -1, -1, true, true); | |
| 108 mInOrder.verify(mImeAdapter, never()).updateSelection(5, 5, -1, -1); | |
| 109 assertEquals(0, mConnection.getQueueForTest().size()); | |
| 110 | |
| 111 { | |
| 112 // Nest another batch edit. | |
| 113 assertTrue(mConnection.beginBatchEdit()); | |
| 114 // Move the cursor to the left. | |
| 115 mConnection.setSelection(4, 4); | |
| 116 assertTrue(mConnection.endBatchEdit()); | |
| 117 } | |
| 118 // We still have one outer batch edit, so should not update selection ye t. | |
| 119 mInOrder.verify(mImeAdapter, never()).updateSelection(4, 4, -1, -1); | |
| 120 | |
| 121 // Prepare to call requestTextInputStateUpdate. | |
| 122 mConnection.updateStateOnUiThread("hello", 4, 4, -1, -1, true, false); | |
| 123 assertEquals(1, mConnection.getQueueForTest().size()); | |
| 124 when(mImeAdapter.requestTextInputStateUpdate()).thenReturn(true); | |
| 125 | |
| 126 // IME app calls endBatchEdit(). | |
| 127 assertFalse(mConnection.endBatchEdit()); | |
| 128 // Batch edit is finished, now update selection. | |
| 129 mInOrder.verify(mImeAdapter).updateSelection(4, 4, -1, -1); | |
| 130 assertEquals(0, mConnection.getQueueForTest().size()); | |
| 131 } | |
| 132 | |
| 133 @Test | |
| 134 @Feature({"TextInput"}) | |
| 135 public void testFailToRequestToRenderer() { | |
| 136 when(mImeAdapter.requestTextInputStateUpdate()).thenReturn(false); | |
| 137 // Should not hang here. Return null to indicate failure. | |
| 138 assertNull(null, mConnection.getTextBeforeCursor(10, 0)); | |
| 139 } | |
| 140 | |
| 141 @Test | |
| 142 @Feature({"TextInput"}) | |
| 143 public void testRendererCannotUpdateState() { | |
| 144 when(mImeAdapter.requestTextInputStateUpdate()).thenReturn(true); | |
| 145 // We found that renderer cannot update state, e.g., due to a crash. | |
| 146 ThreadUtils.postOnUiThread(new Runnable() { | |
| 147 @Override | |
| 148 public void run() { | |
| 149 try { | |
| 150 Thread.sleep(1000); | |
|
Ted C
2016/02/24 19:09:02
Why the thread sleep at all? Are you trying to ma
Changwan Ryu
2016/02/24 23:36:09
Hmm... Since the test is using test thread as IME
| |
| 151 } catch (InterruptedException e) { | |
| 152 e.printStackTrace(); | |
| 153 fail(); | |
| 154 } | |
| 155 mConnection.unblockOnUiThread(); | |
| 156 } | |
| 157 }); | |
| 158 // Should not hang here. Return null to indicate failure. | |
| 159 assertEquals(null, mConnection.getTextBeforeCursor(10, 0)); | |
| 160 } | |
| 161 } | |
| OLD | NEW |