|
| 1 | +package io.flutter.plugin.text; |
| 2 | + |
| 3 | +import static org.junit.Assert.assertEquals; |
| 4 | +import static org.mockito.Mockito.any; |
| 5 | +import static org.mockito.Mockito.anyInt; |
| 6 | +import static org.mockito.Mockito.mock; |
| 7 | +import static org.mockito.Mockito.times; |
| 8 | +import static org.mockito.Mockito.verify; |
| 9 | +import static org.mockito.Mockito.when; |
| 10 | + |
| 11 | +import android.app.Activity; |
| 12 | +import android.content.Intent; |
| 13 | +import android.content.pm.ActivityInfo; |
| 14 | +import android.content.pm.PackageItemInfo; |
| 15 | +import android.content.pm.PackageManager; |
| 16 | +import android.content.pm.ResolveInfo; |
| 17 | +import androidx.test.ext.junit.runners.AndroidJUnit4; |
| 18 | +import io.flutter.embedding.engine.dart.DartExecutor; |
| 19 | +import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding; |
| 20 | +import io.flutter.embedding.engine.systemchannels.ProcessTextChannel; |
| 21 | +import io.flutter.plugin.common.BinaryMessenger; |
| 22 | +import io.flutter.plugin.common.MethodCall; |
| 23 | +import io.flutter.plugin.common.MethodChannel; |
| 24 | +import io.flutter.plugin.common.StandardMethodCodec; |
| 25 | +import java.lang.reflect.Field; |
| 26 | +import java.nio.ByteBuffer; |
| 27 | +import java.util.ArrayList; |
| 28 | +import java.util.Arrays; |
| 29 | +import java.util.List; |
| 30 | +import java.util.Map; |
| 31 | +import org.junit.Test; |
| 32 | +import org.junit.runner.RunWith; |
| 33 | +import org.mockito.ArgumentCaptor; |
| 34 | + |
| 35 | +@RunWith(AndroidJUnit4.class) |
| 36 | +public class ProcessTextPluginTest { |
| 37 | + |
| 38 | + private static void sendToBinaryMessageHandler( |
| 39 | + BinaryMessenger.BinaryMessageHandler binaryMessageHandler, String method, Object args) { |
| 40 | + MethodCall methodCall = new MethodCall(method, args); |
| 41 | + ByteBuffer encodedMethodCall = StandardMethodCodec.INSTANCE.encodeMethodCall(methodCall); |
| 42 | + binaryMessageHandler.onMessage( |
| 43 | + (ByteBuffer) encodedMethodCall.flip(), mock(BinaryMessenger.BinaryReply.class)); |
| 44 | + } |
| 45 | + |
| 46 | + @SuppressWarnings("deprecation") |
| 47 | + // setMessageHandler is deprecated. |
| 48 | + @Test |
| 49 | + public void respondsToProcessTextChannelMessage() { |
| 50 | + ArgumentCaptor<BinaryMessenger.BinaryMessageHandler> binaryMessageHandlerCaptor = |
| 51 | + ArgumentCaptor.forClass(BinaryMessenger.BinaryMessageHandler.class); |
| 52 | + DartExecutor mockBinaryMessenger = mock(DartExecutor.class); |
| 53 | + ProcessTextChannel.ProcessTextMethodHandler mockHandler = |
| 54 | + mock(ProcessTextChannel.ProcessTextMethodHandler.class); |
| 55 | + PackageManager mockPackageManager = mock(PackageManager.class); |
| 56 | + ProcessTextChannel processTextChannel = |
| 57 | + new ProcessTextChannel(mockBinaryMessenger, mockPackageManager); |
| 58 | + |
| 59 | + processTextChannel.setMethodHandler(mockHandler); |
| 60 | + |
| 61 | + verify(mockBinaryMessenger, times(1)) |
| 62 | + .setMessageHandler(any(String.class), binaryMessageHandlerCaptor.capture()); |
| 63 | + |
| 64 | + BinaryMessenger.BinaryMessageHandler binaryMessageHandler = |
| 65 | + binaryMessageHandlerCaptor.getValue(); |
| 66 | + |
| 67 | + sendToBinaryMessageHandler(binaryMessageHandler, "ProcessText.queryTextActions", null); |
| 68 | + |
| 69 | + verify(mockHandler).queryTextActions(); |
| 70 | + } |
| 71 | + |
| 72 | + @SuppressWarnings("deprecation") |
| 73 | + // setMessageHandler is deprecated. |
| 74 | + @Test |
| 75 | + public void performQueryTextActions() { |
| 76 | + DartExecutor mockBinaryMessenger = mock(DartExecutor.class); |
| 77 | + PackageManager mockPackageManager = mock(PackageManager.class); |
| 78 | + ProcessTextChannel processTextChannel = |
| 79 | + new ProcessTextChannel(mockBinaryMessenger, mockPackageManager); |
| 80 | + |
| 81 | + // Set up mocked result for PackageManager.queryIntentActivities. |
| 82 | + ResolveInfo action1 = mock(ResolveInfo.class); |
| 83 | + when(action1.loadLabel(mockPackageManager)).thenReturn("Action1"); |
| 84 | + ResolveInfo action2 = mock(ResolveInfo.class); |
| 85 | + when(action2.loadLabel(mockPackageManager)).thenReturn("Action2"); |
| 86 | + List<ResolveInfo> infos = new ArrayList<ResolveInfo>(Arrays.asList(action1, action2)); |
| 87 | + Intent intent = new Intent().setAction(Intent.ACTION_PROCESS_TEXT).setType("text/plain"); |
| 88 | + when(mockPackageManager.queryIntentActivities( |
| 89 | + any(Intent.class), any(PackageManager.ResolveInfoFlags.class))) |
| 90 | + .thenReturn(infos); |
| 91 | + |
| 92 | + // ProcessTextPlugin should retrieve the mocked text actions. |
| 93 | + ProcessTextPlugin processTextPlugin = new ProcessTextPlugin(processTextChannel); |
| 94 | + Map<Integer, String> textActions = processTextPlugin.queryTextActions(); |
| 95 | + final int action1Id = 0; |
| 96 | + final int action2Id = 1; |
| 97 | + assertEquals(textActions, Map.of(action1Id, "Action1", action2Id, "Action2")); |
| 98 | + } |
| 99 | + |
| 100 | + @SuppressWarnings("deprecation") |
| 101 | + // setMessageHandler is deprecated. |
| 102 | + @Test |
| 103 | + public void performProcessTextActionWithNoReturnedValue() { |
| 104 | + DartExecutor mockBinaryMessenger = mock(DartExecutor.class); |
| 105 | + PackageManager mockPackageManager = mock(PackageManager.class); |
| 106 | + ProcessTextChannel processTextChannel = |
| 107 | + new ProcessTextChannel(mockBinaryMessenger, mockPackageManager); |
| 108 | + |
| 109 | + // Set up mocked result for PackageManager.queryIntentActivities. |
| 110 | + ResolveInfo action1 = createFakeResolveInfo("Action1", mockPackageManager); |
| 111 | + ResolveInfo action2 = createFakeResolveInfo("Action2", mockPackageManager); |
| 112 | + List<ResolveInfo> infos = new ArrayList<ResolveInfo>(Arrays.asList(action1, action2)); |
| 113 | + when(mockPackageManager.queryIntentActivities( |
| 114 | + any(Intent.class), any(PackageManager.ResolveInfoFlags.class))) |
| 115 | + .thenReturn(infos); |
| 116 | + |
| 117 | + // ProcessTextPlugin should retrieve the mocked text actions. |
| 118 | + ProcessTextPlugin processTextPlugin = new ProcessTextPlugin(processTextChannel); |
| 119 | + Map<Integer, String> textActions = processTextPlugin.queryTextActions(); |
| 120 | + final int action1Id = 0; |
| 121 | + final int action2Id = 1; |
| 122 | + assertEquals(textActions, Map.of(action1Id, "Action1", action2Id, "Action2")); |
| 123 | + |
| 124 | + // Set up activity binding. |
| 125 | + ActivityPluginBinding mockActivityPluginBinding = mock(ActivityPluginBinding.class); |
| 126 | + Activity mockActivity = mock(Activity.class); |
| 127 | + when(mockActivityPluginBinding.getActivity()).thenReturn(mockActivity); |
| 128 | + processTextPlugin.onAttachedToActivity(mockActivityPluginBinding); |
| 129 | + |
| 130 | + // Execute first action. |
| 131 | + String textToBeProcessed = "Flutter!"; |
| 132 | + MethodChannel.Result result = mock(MethodChannel.Result.class); |
| 133 | + processTextPlugin.processTextAction(action1Id, textToBeProcessed, false, result); |
| 134 | + |
| 135 | + // Activity.startActivityForResult should have been called. |
| 136 | + ArgumentCaptor<Intent> intentCaptor = ArgumentCaptor.forClass(Intent.class); |
| 137 | + verify(mockActivity, times(1)).startActivityForResult(intentCaptor.capture(), anyInt()); |
| 138 | + Intent intent = intentCaptor.getValue(); |
| 139 | + assertEquals(intent.getStringExtra(Intent.EXTRA_PROCESS_TEXT), textToBeProcessed); |
| 140 | + |
| 141 | + // Simulate an Android activity answer which does not return any value. |
| 142 | + Intent resultIntent = new Intent(); |
| 143 | + processTextPlugin.onActivityResult(result.hashCode(), Activity.RESULT_OK, resultIntent); |
| 144 | + |
| 145 | + // Success with no returned value is expected. |
| 146 | + verify(result).success(null); |
| 147 | + } |
| 148 | + |
| 149 | + @SuppressWarnings("deprecation") |
| 150 | + // setMessageHandler is deprecated. |
| 151 | + @Test |
| 152 | + public void performProcessTextActionWithReturnedValue() { |
| 153 | + DartExecutor mockBinaryMessenger = mock(DartExecutor.class); |
| 154 | + PackageManager mockPackageManager = mock(PackageManager.class); |
| 155 | + ProcessTextChannel processTextChannel = |
| 156 | + new ProcessTextChannel(mockBinaryMessenger, mockPackageManager); |
| 157 | + |
| 158 | + // Set up mocked result for PackageManager.queryIntentActivities. |
| 159 | + ResolveInfo action1 = createFakeResolveInfo("Action1", mockPackageManager); |
| 160 | + ResolveInfo action2 = createFakeResolveInfo("Action2", mockPackageManager); |
| 161 | + List<ResolveInfo> infos = new ArrayList<ResolveInfo>(Arrays.asList(action1, action2)); |
| 162 | + when(mockPackageManager.queryIntentActivities( |
| 163 | + any(Intent.class), any(PackageManager.ResolveInfoFlags.class))) |
| 164 | + .thenReturn(infos); |
| 165 | + |
| 166 | + // ProcessTextPlugin should retrieve the mocked text actions. |
| 167 | + ProcessTextPlugin processTextPlugin = new ProcessTextPlugin(processTextChannel); |
| 168 | + Map<Integer, String> textActions = processTextPlugin.queryTextActions(); |
| 169 | + final int action1Id = 0; |
| 170 | + final int action2Id = 1; |
| 171 | + assertEquals(textActions, Map.of(action1Id, "Action1", action2Id, "Action2")); |
| 172 | + |
| 173 | + // Set up activity binding. |
| 174 | + ActivityPluginBinding mockActivityPluginBinding = mock(ActivityPluginBinding.class); |
| 175 | + Activity mockActivity = mock(Activity.class); |
| 176 | + when(mockActivityPluginBinding.getActivity()).thenReturn(mockActivity); |
| 177 | + processTextPlugin.onAttachedToActivity(mockActivityPluginBinding); |
| 178 | + |
| 179 | + // Execute first action. |
| 180 | + String textToBeProcessed = "Flutter!"; |
| 181 | + MethodChannel.Result result = mock(MethodChannel.Result.class); |
| 182 | + processTextPlugin.processTextAction(action1Id, textToBeProcessed, false, result); |
| 183 | + |
| 184 | + // Activity.startActivityForResult should have been called. |
| 185 | + ArgumentCaptor<Intent> intentCaptor = ArgumentCaptor.forClass(Intent.class); |
| 186 | + verify(mockActivity, times(1)).startActivityForResult(intentCaptor.capture(), anyInt()); |
| 187 | + Intent intent = intentCaptor.getValue(); |
| 188 | + assertEquals(intent.getStringExtra(Intent.EXTRA_PROCESS_TEXT), textToBeProcessed); |
| 189 | + |
| 190 | + // Simulate an Android activity answer which returns a transformed text. |
| 191 | + String processedText = "Flutter!!!"; |
| 192 | + Intent resultIntent = new Intent(); |
| 193 | + resultIntent.putExtra(Intent.EXTRA_PROCESS_TEXT, processedText); |
| 194 | + processTextPlugin.onActivityResult(result.hashCode(), Activity.RESULT_OK, resultIntent); |
| 195 | + |
| 196 | + // Success with the transformed text is expected. |
| 197 | + verify(result).success(processedText); |
| 198 | + } |
| 199 | + |
| 200 | + private ResolveInfo createFakeResolveInfo(String label, PackageManager mockPackageManager) { |
| 201 | + ResolveInfo resolveInfo = mock(ResolveInfo.class); |
| 202 | + ActivityInfo activityInfo = new ActivityInfo(); |
| 203 | + when(resolveInfo.loadLabel(mockPackageManager)).thenReturn(label); |
| 204 | + |
| 205 | + // Use Java reflection to set required member variables. |
| 206 | + try { |
| 207 | + Field activityField = ResolveInfo.class.getDeclaredField("activityInfo"); |
| 208 | + activityField.setAccessible(true); |
| 209 | + activityField.set(resolveInfo, activityInfo); |
| 210 | + Field packageNameField = PackageItemInfo.class.getDeclaredField("packageName"); |
| 211 | + packageNameField.setAccessible(true); |
| 212 | + packageNameField.set(activityInfo, "mockActivityPackageName"); |
| 213 | + Field nameField = PackageItemInfo.class.getDeclaredField("name"); |
| 214 | + nameField.setAccessible(true); |
| 215 | + nameField.set(activityInfo, "mockActivityName"); |
| 216 | + } catch (Exception ex) { |
| 217 | + // Test will failed if reflection APIs throw. |
| 218 | + } |
| 219 | + |
| 220 | + return resolveInfo; |
| 221 | + } |
| 222 | +} |
0 commit comments