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.assertTrue; |
| 10 import static org.junit.Assert.fail; |
| 11 import static org.mockito.Mockito.inOrder; |
| 12 import static org.mockito.Mockito.never; |
| 13 import static org.mockito.Mockito.when; |
| 14 |
| 15 import android.os.Handler; |
| 16 |
| 17 import org.chromium.base.ThreadUtils; |
| 18 import org.chromium.base.test.util.Feature; |
| 19 import org.chromium.content.browser.input.ChromiumBaseInputConnection.ThreadMana
ger; |
| 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 import java.util.concurrent.Callable; |
| 31 |
| 32 /** |
| 33 * Unit tests for {@ThreadedInputConnection}. |
| 34 */ |
| 35 @RunWith(LocalRobolectricTestRunner.class) |
| 36 @Config(manifest = Config.NONE) |
| 37 public class ThreadedInputConnectionTest { |
| 38 |
| 39 @Mock ImeAdapter mImeAdapter; |
| 40 ThreadManager mThreadManager; |
| 41 ThreadedInputConnection mConnection; |
| 42 InOrder mInOrder; |
| 43 |
| 44 @Before |
| 45 public void setUp() throws Exception { |
| 46 MockitoAnnotations.initMocks(this); |
| 47 |
| 48 mImeAdapter = Mockito.mock(ImeAdapter.class); |
| 49 mInOrder = inOrder(mImeAdapter); |
| 50 // Let's create ThreadManager for test thread and pretend that it is an
IME thread. |
| 51 mThreadManager = new ThreadManager(new Handler()); |
| 52 // You should create connection on UI thread. |
| 53 mConnection = ThreadUtils.runOnUiThreadBlocking(new Callable<ThreadedInp
utConnection>() { |
| 54 @Override |
| 55 public ThreadedInputConnection call() throws Exception { |
| 56 return new ThreadedInputConnection(mImeAdapter, mThreadManager); |
| 57 } |
| 58 }); |
| 59 } |
| 60 |
| 61 @Test |
| 62 @Feature({"TextInput"}) |
| 63 public void testComposeGetTextFinishGetText() { |
| 64 // IME app calls setComposingText(). |
| 65 mConnection.setComposingText("hello", 1); |
| 66 mInOrder.verify(mImeAdapter).sendCompositionToNative("hello", 1, false); |
| 67 |
| 68 // Renderer updates states asynchronously. |
| 69 mConnection.updateStateOnUiThread("hello", 5, 5, 0, 5, true, true); |
| 70 mInOrder.verify(mImeAdapter).updateSelection(5, 5, 0, 5); |
| 71 assertEquals(0, mConnection.getQueueForTest().size()); |
| 72 |
| 73 // Prepare to call requestTextInputStateUpdate. |
| 74 mConnection.updateStateOnUiThread("hello", 5, 5, 0, 5, true, false); |
| 75 assertEquals(1, mConnection.getQueueForTest().size()); |
| 76 when(mImeAdapter.requestTextInputStateUpdate()).thenReturn(true); |
| 77 |
| 78 // IME app calls getTextBeforeCursor(). |
| 79 assertEquals("hello", mConnection.getTextBeforeCursor(20, 0)); |
| 80 |
| 81 // IME app calls finishComposingText(). |
| 82 mConnection.finishComposingText(); |
| 83 mInOrder.verify(mImeAdapter).finishComposingText(); |
| 84 mConnection.updateStateOnUiThread("hello", 5, 5, -1, -1, true, true); |
| 85 mInOrder.verify(mImeAdapter).updateSelection(5, 5, -1, -1); |
| 86 |
| 87 // Prepare to call requestTextInputStateUpdate. |
| 88 mConnection.updateStateOnUiThread("hello", 5, 5, -1, -1, true, false); |
| 89 assertEquals(1, mConnection.getQueueForTest().size()); |
| 90 when(mImeAdapter.requestTextInputStateUpdate()).thenReturn(true); |
| 91 |
| 92 // IME app calls getTextBeforeCursor(). |
| 93 assertEquals("hello", mConnection.getTextBeforeCursor(20, 0)); |
| 94 |
| 95 assertEquals(0, mConnection.getQueueForTest().size()); |
| 96 } |
| 97 |
| 98 @Test |
| 99 @Feature({"TextInput"}) |
| 100 public void testRenderChangeUpdatesSelection() { |
| 101 // User moves the cursor. |
| 102 mConnection.updateStateOnUiThread("hello", 4, 4, -1, -1, true, true); |
| 103 mInOrder.verify(mImeAdapter).updateSelection(4, 4, -1, -1); |
| 104 assertEquals(0, mConnection.getQueueForTest().size()); |
| 105 } |
| 106 |
| 107 @Test |
| 108 @Feature({"TextInput"}) |
| 109 public void testBatchEdit() { |
| 110 // IME app calls beginBatchEdit(). |
| 111 assertTrue(mConnection.beginBatchEdit()); |
| 112 // Type hello real fast. |
| 113 mConnection.commitText("hello", 1); |
| 114 mInOrder.verify(mImeAdapter).sendCompositionToNative("hello", 1, true); |
| 115 |
| 116 // Renderer updates states asynchronously. |
| 117 mConnection.updateStateOnUiThread("hello", 5, 5, -1, -1, true, true); |
| 118 mInOrder.verify(mImeAdapter, never()).updateSelection(5, 5, -1, -1); |
| 119 assertEquals(0, mConnection.getQueueForTest().size()); |
| 120 |
| 121 { |
| 122 // Nest another batch edit. |
| 123 assertTrue(mConnection.beginBatchEdit()); |
| 124 // Move the cursor to the left. |
| 125 mConnection.setSelection(4, 4); |
| 126 assertTrue(mConnection.endBatchEdit()); |
| 127 } |
| 128 // We still have one outer batch edit, so should not update selection ye
t. |
| 129 mInOrder.verify(mImeAdapter, never()).updateSelection(4, 4, -1, -1); |
| 130 |
| 131 // Prepare to call requestTextInputStateUpdate. |
| 132 mConnection.updateStateOnUiThread("hello", 4, 4, -1, -1, true, false); |
| 133 assertEquals(1, mConnection.getQueueForTest().size()); |
| 134 when(mImeAdapter.requestTextInputStateUpdate()).thenReturn(true); |
| 135 |
| 136 // IME app calls endBatchEdit(). |
| 137 assertFalse(mConnection.endBatchEdit()); |
| 138 // Batch edit is finished, now update selection. |
| 139 mInOrder.verify(mImeAdapter).updateSelection(4, 4, -1, -1); |
| 140 assertEquals(0, mConnection.getQueueForTest().size()); |
| 141 } |
| 142 |
| 143 @Test |
| 144 @Feature({"TextInput"}) |
| 145 public void testFailToRequestToRenderer() { |
| 146 when(mImeAdapter.requestTextInputStateUpdate()).thenReturn(false); |
| 147 // Should not hang here. Return null to indicate failure. |
| 148 assertEquals(null, mConnection.getTextBeforeCursor(10, 0)); |
| 149 } |
| 150 |
| 151 @Test |
| 152 @Feature({"TextInput"}) |
| 153 public void testRendererCannotUpdateState() { |
| 154 when(mImeAdapter.requestTextInputStateUpdate()).thenReturn(true); |
| 155 // We found that renderer cannot update state, e.g., due to a crash. |
| 156 ThreadUtils.postOnUiThread(new Runnable() { |
| 157 @Override |
| 158 public void run() { |
| 159 try { |
| 160 Thread.sleep(1000); |
| 161 } catch (InterruptedException e) { |
| 162 e.printStackTrace(); |
| 163 fail(); |
| 164 } |
| 165 mConnection.unblockOnUiThread(); |
| 166 } |
| 167 }); |
| 168 // Should not hang here. Return null to indicate failure. |
| 169 assertEquals(null, mConnection.getTextBeforeCursor(10, 0)); |
| 170 } |
| 171 } |
OLD | NEW |