|
| 1 | +//TODO later// Copyright Epic Games, Inc. All Rights Reserved. |
| 2 | +package tbNames.tbNames_android_client; |
| 3 | + |
| 4 | +import tbNames.tbNames_android_client.NamEsClient; |
| 5 | + |
| 6 | +//import message type and parcelabe types |
| 7 | +import tbNames.tbNames_api.TbNamesTestHelper; |
| 8 | + |
| 9 | +import tbNames.tbNames_api.INamEsEventListener; |
| 10 | +import tbNames.tbNames_api.INamEs; |
| 11 | +import tbNames.tbNames_api.AbstractNamEs; |
| 12 | +import tbNames.tbNames_android_messenger.NamEsMessageType; |
| 13 | + |
| 14 | +import android.content.Context; |
| 15 | +import android.os.Bundle; |
| 16 | +import android.os.Handler; |
| 17 | +import android.os.IBinder; |
| 18 | +import android.os.Looper; |
| 19 | +import android.os.Message; |
| 20 | +import android.os.Messenger; |
| 21 | +import android.os.RemoteException; |
| 22 | +import android.util.Log; |
| 23 | + |
| 24 | +import java.util.concurrent.CompletableFuture; |
| 25 | +import java.util.concurrent.atomic.AtomicBoolean; |
| 26 | +import android.content.ComponentName; |
| 27 | + |
| 28 | +import static org.junit.Assert.assertEquals; |
| 29 | +import static org.junit.Assert.assertFalse; |
| 30 | +import static org.junit.Assert.assertTrue; |
| 31 | +import org.junit.After; |
| 32 | +import org.junit.Before; |
| 33 | +import org.junit.Test; |
| 34 | +import org.junit.runner.RunWith; |
| 35 | + |
| 36 | +import static org.mockito.Mockito.*; |
| 37 | +import org.mockito.ArgumentCaptor; |
| 38 | +import org.mockito.Mock; |
| 39 | +import org.mockito.InOrder; |
| 40 | + |
| 41 | +import org.robolectric.Robolectric; |
| 42 | +import org.robolectric.RobolectricTestRunner; |
| 43 | +import org.robolectric.annotation.Config; |
| 44 | +import org.robolectric.RuntimeEnvironment; |
| 45 | + |
| 46 | +import androidx.annotation.NonNull; |
| 47 | + |
| 48 | + |
| 49 | +interface INamEsClientMessageGetter |
| 50 | +{ |
| 51 | + public void getMessage(Message msg); |
| 52 | +} |
| 53 | + |
| 54 | + |
| 55 | +@Config(sdk = 33, manifest = Config.NONE) |
| 56 | +@RunWith(RobolectricTestRunner.class) |
| 57 | +public class NamEsClientTest |
| 58 | +{ |
| 59 | + |
| 60 | + @Mock |
| 61 | + private Context mMockContext; |
| 62 | + |
| 63 | + private NamEsClient testedClient; |
| 64 | + private INamEsEventListener listenerMock = mock(INamEsEventListener.class); |
| 65 | + private Messenger mServiceMessenger; |
| 66 | + private Messenger mClientMessenger; |
| 67 | + private Handler mServiceHandler ; |
| 68 | + private String mTestConnectionID1 = "MyTestClient"; |
| 69 | + InOrder inOrderServiceMessenger; |
| 70 | + InOrder inOrderEventListener; |
| 71 | + |
| 72 | + private INamEsClientMessageGetter serviceMessagesStorage = mock(INamEsClientMessageGetter.class); |
| 73 | + |
| 74 | + ArgumentCaptor<Message> messageCaptor = ArgumentCaptor.forClass(Message.class); |
| 75 | + @After |
| 76 | + public void tearDown() { |
| 77 | + |
| 78 | + testedClient.unbindFromService(); |
| 79 | + |
| 80 | + Robolectric.flushForegroundThreadScheduler(); |
| 81 | + |
| 82 | + inOrderServiceMessenger.verify(serviceMessagesStorage, times(1)).getMessage(messageCaptor.capture()); |
| 83 | + Message register_msg = messageCaptor.getValue(); |
| 84 | + assertEquals(NamEsMessageType.UNREGISTER_CLIENT.getValue(), register_msg.what); |
| 85 | + assertEquals(mTestConnectionID1, register_msg.getData().getString("connectionID", "")); |
| 86 | + |
| 87 | + inOrderEventListener.verify(listenerMock, times(1)).on_readyStatusChanged(false); |
| 88 | + |
| 89 | + testedClient.removeEventListener(listenerMock); |
| 90 | + } |
| 91 | + |
| 92 | + Handler createServiceHandlerMock(INamEsClientMessageGetter messageGetterMock) |
| 93 | + { |
| 94 | + return new Handler(Looper.getMainLooper()){ |
| 95 | + @Override |
| 96 | + public void handleMessage(Message msg) { |
| 97 | + Message copy = Message.obtain(); |
| 98 | + copy.copyFrom(msg); |
| 99 | + messageGetterMock.getMessage(copy); |
| 100 | + } |
| 101 | + }; |
| 102 | + } |
| 103 | + |
| 104 | + @Before |
| 105 | + public void setUp() throws RemoteException |
| 106 | + { |
| 107 | + inOrderServiceMessenger = inOrder(serviceMessagesStorage); |
| 108 | + inOrderEventListener = inOrder(listenerMock); |
| 109 | + mServiceHandler = createServiceHandlerMock(serviceMessagesStorage); |
| 110 | + mServiceMessenger = new Messenger(mServiceHandler); |
| 111 | + IBinder serviceBinder = mServiceMessenger.getBinder(); |
| 112 | + |
| 113 | + mMockContext = RuntimeEnvironment.getApplication(); |
| 114 | + |
| 115 | + |
| 116 | + testedClient = new NamEsClient(mMockContext, mTestConnectionID1); |
| 117 | + testedClient.addEventListener(listenerMock); |
| 118 | + ComponentName componentName = new ComponentName("tbNames.tbNames_android_service", "tbNames.tbNames_android_service.NamEsServiceAdapter"); |
| 119 | + testedClient.onServiceConnected(componentName, serviceBinder); |
| 120 | + |
| 121 | + Robolectric.flushForegroundThreadScheduler(); |
| 122 | + inOrderServiceMessenger.verify(serviceMessagesStorage, times(1)).getMessage(messageCaptor.capture()); |
| 123 | + Message register_msg = messageCaptor.getValue(); |
| 124 | + assertEquals(NamEsMessageType.REGISTER_CLIENT.getValue(), register_msg.what); |
| 125 | + mClientMessenger = register_msg.replyTo; |
| 126 | + assertEquals(mTestConnectionID1, register_msg.getData().getString("connectionID", "")); |
| 127 | + |
| 128 | + inOrderEventListener.verify(listenerMock, times(1)).on_readyStatusChanged(true); |
| 129 | + assertTrue(testedClient._isReady()); |
| 130 | + } |
| 131 | + |
| 132 | + @Test |
| 133 | + public void onInitReceive() throws RemoteException |
| 134 | + { |
| 135 | + //PREPARE message |
| 136 | + |
| 137 | + Message msg = Message.obtain(null, NamEsMessageType.INIT.getValue()); |
| 138 | + Bundle data = new Bundle(); |
| 139 | + boolean testSwitch = true; |
| 140 | + data.putBoolean("Switch", testSwitch); |
| 141 | + int testSOME_PROPERTY = 1; |
| 142 | + data.putInt("SOME_PROPERTY", testSOME_PROPERTY); |
| 143 | + int testSome_Poperty2 = 1; |
| 144 | + data.putInt("Some_Poperty2", testSome_Poperty2); |
| 145 | + |
| 146 | + //setup mock expectations |
| 147 | + msg.setData(data); |
| 148 | + mClientMessenger.send(msg); |
| 149 | + Robolectric.flushForegroundThreadScheduler(); |
| 150 | + inOrderEventListener.verify(listenerMock,times(1)).onSwitchChanged(testSwitch); |
| 151 | + inOrderEventListener.verify(listenerMock,times(1)).onSomePropertyChanged(testSOME_PROPERTY); |
| 152 | + inOrderEventListener.verify(listenerMock,times(1)).onSomePoperty2Changed(testSome_Poperty2); |
| 153 | + } |
| 154 | +//TODO do not add when a property is readonly |
| 155 | + @Test |
| 156 | + public void onReceiveSwitchPropertyChangeTest() throws RemoteException { |
| 157 | + // Create and send message |
| 158 | + Message msg = Message.obtain(null, NamEsMessageType.SET_Switch.getValue()); |
| 159 | + Bundle data = new Bundle(); |
| 160 | + boolean testSwitch = true; |
| 161 | + data.putBoolean("Switch", testSwitch); |
| 162 | + |
| 163 | + msg.setData(data); |
| 164 | + mClientMessenger.send(msg); |
| 165 | + Robolectric.flushForegroundThreadScheduler(); |
| 166 | + inOrderEventListener.verify(listenerMock,times(1)).onSwitchChanged(testSwitch); |
| 167 | + } |
| 168 | + |
| 169 | + @Test |
| 170 | + public void setPropertyRequestSwitch() |
| 171 | + { |
| 172 | + boolean testSwitch = true; |
| 173 | + |
| 174 | + testedClient.setSwitch(testSwitch); |
| 175 | + Robolectric.flushForegroundThreadScheduler(); |
| 176 | + |
| 177 | + inOrderServiceMessenger.verify(serviceMessagesStorage, times(1)).getMessage(messageCaptor.capture()); |
| 178 | + Message response = messageCaptor.getValue(); |
| 179 | + |
| 180 | + assertEquals(NamEsMessageType.PROP_Switch.getValue(), response.what); |
| 181 | + Bundle data = response.getData(); |
| 182 | + |
| 183 | + boolean receivedSwitch = data.getBoolean("Switch", false); |
| 184 | + assertEquals(receivedSwitch, testSwitch); |
| 185 | + } |
| 186 | +//TODO do not add when a property is readonly |
| 187 | + @Test |
| 188 | + public void onReceiveSOME_PROPERTYPropertyChangeTest() throws RemoteException { |
| 189 | + // Create and send message |
| 190 | + Message msg = Message.obtain(null, NamEsMessageType.SET_SomeProperty.getValue()); |
| 191 | + Bundle data = new Bundle(); |
| 192 | + int testSOME_PROPERTY = 1; |
| 193 | + data.putInt("SOME_PROPERTY", testSOME_PROPERTY); |
| 194 | + |
| 195 | + msg.setData(data); |
| 196 | + mClientMessenger.send(msg); |
| 197 | + Robolectric.flushForegroundThreadScheduler(); |
| 198 | + inOrderEventListener.verify(listenerMock,times(1)).onSomePropertyChanged(testSOME_PROPERTY); |
| 199 | + } |
| 200 | + |
| 201 | + @Test |
| 202 | + public void setPropertyRequestSOME_PROPERTY() |
| 203 | + { |
| 204 | + int testSOME_PROPERTY = 1; |
| 205 | + |
| 206 | + testedClient.setSomeProperty(testSOME_PROPERTY); |
| 207 | + Robolectric.flushForegroundThreadScheduler(); |
| 208 | + |
| 209 | + inOrderServiceMessenger.verify(serviceMessagesStorage, times(1)).getMessage(messageCaptor.capture()); |
| 210 | + Message response = messageCaptor.getValue(); |
| 211 | + |
| 212 | + assertEquals(NamEsMessageType.PROP_SomeProperty.getValue(), response.what); |
| 213 | + Bundle data = response.getData(); |
| 214 | + |
| 215 | + int receivedSOME_PROPERTY = data.getInt("SOME_PROPERTY", 0); |
| 216 | + assertEquals(receivedSOME_PROPERTY, testSOME_PROPERTY); |
| 217 | + } |
| 218 | +//TODO do not add when a property is readonly |
| 219 | + @Test |
| 220 | + public void onReceiveSome_Poperty2PropertyChangeTest() throws RemoteException { |
| 221 | + // Create and send message |
| 222 | + Message msg = Message.obtain(null, NamEsMessageType.SET_SomePoperty2.getValue()); |
| 223 | + Bundle data = new Bundle(); |
| 224 | + int testSome_Poperty2 = 1; |
| 225 | + data.putInt("Some_Poperty2", testSome_Poperty2); |
| 226 | + |
| 227 | + msg.setData(data); |
| 228 | + mClientMessenger.send(msg); |
| 229 | + Robolectric.flushForegroundThreadScheduler(); |
| 230 | + inOrderEventListener.verify(listenerMock,times(1)).onSomePoperty2Changed(testSome_Poperty2); |
| 231 | + } |
| 232 | + |
| 233 | + @Test |
| 234 | + public void setPropertyRequestSome_Poperty2() |
| 235 | + { |
| 236 | + int testSome_Poperty2 = 1; |
| 237 | + |
| 238 | + testedClient.setSomePoperty2(testSome_Poperty2); |
| 239 | + Robolectric.flushForegroundThreadScheduler(); |
| 240 | + |
| 241 | + inOrderServiceMessenger.verify(serviceMessagesStorage, times(1)).getMessage(messageCaptor.capture()); |
| 242 | + Message response = messageCaptor.getValue(); |
| 243 | + |
| 244 | + assertEquals(NamEsMessageType.PROP_SomePoperty2.getValue(), response.what); |
| 245 | + Bundle data = response.getData(); |
| 246 | + |
| 247 | + int receivedSome_Poperty2 = data.getInt("Some_Poperty2", 0); |
| 248 | + assertEquals(receivedSome_Poperty2, testSome_Poperty2); |
| 249 | + } |
| 250 | + @Test |
| 251 | + public void whenNotifiedSOME_SIGNAL() throws RemoteException |
| 252 | + { |
| 253 | + |
| 254 | + Message msg = Message.obtain(null, NamEsMessageType.SIG_SomeSignal.getValue()); |
| 255 | + Bundle data = new Bundle(); |
| 256 | + boolean testSOME_PARAM = true; |
| 257 | + data.putBoolean("SOME_PARAM", testSOME_PARAM); |
| 258 | + |
| 259 | + msg.setData(data); |
| 260 | + mClientMessenger.send(msg); |
| 261 | + Robolectric.flushForegroundThreadScheduler(); |
| 262 | + |
| 263 | + inOrderEventListener.verify(listenerMock,times(1)).onSomeSignal(testSOME_PARAM); |
| 264 | + |
| 265 | +} |
| 266 | + @Test |
| 267 | + public void whenNotifiedSome_Signal2() throws RemoteException |
| 268 | + { |
| 269 | + |
| 270 | + Message msg = Message.obtain(null, NamEsMessageType.SIG_SomeSignal2.getValue()); |
| 271 | + Bundle data = new Bundle(); |
| 272 | + boolean testSome_Param = true; |
| 273 | + data.putBoolean("Some_Param", testSome_Param); |
| 274 | + |
| 275 | + msg.setData(data); |
| 276 | + mClientMessenger.send(msg); |
| 277 | + Robolectric.flushForegroundThreadScheduler(); |
| 278 | + |
| 279 | + inOrderEventListener.verify(listenerMock,times(1)).onSomeSignal2(testSome_Param); |
| 280 | + |
| 281 | +} |
| 282 | + |
| 283 | + |
| 284 | + public void onSOME_FUNCTIONRequest() throws RemoteException { |
| 285 | + |
| 286 | + // Execute method |
| 287 | + boolean testSOME_PARAM = true; |
| 288 | + |
| 289 | + AtomicBoolean receivedResp = new AtomicBoolean(false); |
| 290 | + CompletableFuture<Void> resFuture = testedClient.someFunctionAsync(testSOME_PARAM); |
| 291 | + |
| 292 | + resFuture.thenAccept(result -> { |
| 293 | + receivedResp.set(true); |
| 294 | + }); |
| 295 | + Robolectric.flushForegroundThreadScheduler(); |
| 296 | + |
| 297 | + // Expect msg to be sent. |
| 298 | + inOrderServiceMessenger.verify(serviceMessagesStorage, times(1)).getMessage(messageCaptor.capture()); |
| 299 | + |
| 300 | + |
| 301 | + Message method_request = messageCaptor.getValue(); |
| 302 | + assertEquals(NamEsMessageType.RPC_SomeFunctionReq.getValue(), method_request.what); |
| 303 | + Bundle data = method_request.getData(); |
| 304 | + |
| 305 | + boolean receivedSOME_PARAM = data.getBoolean("SOME_PARAM", false); |
| 306 | + assertEquals(receivedSOME_PARAM, testSOME_PARAM); |
| 307 | + int returnedCallId = data.getInt("callId", -1); |
| 308 | + |
| 309 | + //Prepare response |
| 310 | + |
| 311 | + Message msg = Message.obtain(null, NamEsMessageType.RPC_SomeFunctionResp.getValue()); |
| 312 | + |
| 313 | + Bundle result_data = new Bundle(); |
| 314 | + result_data.putInt("callId", returnedCallId); |
| 315 | + |
| 316 | + msg.setData(result_data); |
| 317 | + method_request.replyTo.send(msg); |
| 318 | + Robolectric.flushForegroundThreadScheduler(); |
| 319 | + |
| 320 | + assertTrue(receivedResp.get()); |
| 321 | + |
| 322 | + } |
| 323 | + |
| 324 | + |
| 325 | + public void onSome_Function2Request() throws RemoteException { |
| 326 | + |
| 327 | + // Execute method |
| 328 | + boolean testSome_Param = true; |
| 329 | + |
| 330 | + AtomicBoolean receivedResp = new AtomicBoolean(false); |
| 331 | + CompletableFuture<Void> resFuture = testedClient.someFunction2Async(testSome_Param); |
| 332 | + |
| 333 | + resFuture.thenAccept(result -> { |
| 334 | + receivedResp.set(true); |
| 335 | + }); |
| 336 | + Robolectric.flushForegroundThreadScheduler(); |
| 337 | + |
| 338 | + // Expect msg to be sent. |
| 339 | + inOrderServiceMessenger.verify(serviceMessagesStorage, times(1)).getMessage(messageCaptor.capture()); |
| 340 | + |
| 341 | + |
| 342 | + Message method_request = messageCaptor.getValue(); |
| 343 | + assertEquals(NamEsMessageType.RPC_SomeFunction2Req.getValue(), method_request.what); |
| 344 | + Bundle data = method_request.getData(); |
| 345 | + |
| 346 | + boolean receivedSome_Param = data.getBoolean("Some_Param", false); |
| 347 | + assertEquals(receivedSome_Param, testSome_Param); |
| 348 | + int returnedCallId = data.getInt("callId", -1); |
| 349 | + |
| 350 | + //Prepare response |
| 351 | + |
| 352 | + Message msg = Message.obtain(null, NamEsMessageType.RPC_SomeFunction2Resp.getValue()); |
| 353 | + |
| 354 | + Bundle result_data = new Bundle(); |
| 355 | + result_data.putInt("callId", returnedCallId); |
| 356 | + |
| 357 | + msg.setData(result_data); |
| 358 | + method_request.replyTo.send(msg); |
| 359 | + Robolectric.flushForegroundThreadScheduler(); |
| 360 | + |
| 361 | + assertTrue(receivedResp.get()); |
| 362 | + |
| 363 | + } |
| 364 | + |
| 365 | +} |
0 commit comments