Remove the need of accessing handler in NSS unit test

Currently, to wait for handler becomes idle, specific message
is used and the test would wait for condition variable to be
open when the message is processed.

However, this is already done in the HandlerUtils. Thus,
there is no need to post such message manually in the handler.

Test: atest FrameworksNetTests
Bug: 150664039

Change-Id: Iab32b2dbab01634ca159dcb90fc9f929d1fed1a2
This commit is contained in:
junyulai
2020-03-03 15:47:13 +08:00
parent d07db8a8da
commit 3123092fd5

View File

@@ -163,7 +163,6 @@ public class NetworkStatsServiceTest extends NetworkStatsBaseTest {
private @Mock IBinder mBinder; private @Mock IBinder mBinder;
private @Mock AlarmManager mAlarmManager; private @Mock AlarmManager mAlarmManager;
private HandlerThread mHandlerThread; private HandlerThread mHandlerThread;
private Handler mHandler;
private NetworkStatsService mService; private NetworkStatsService mService;
private INetworkStatsSession mSession; private INetworkStatsSession mSession;
@@ -199,8 +198,8 @@ public class NetworkStatsServiceTest extends NetworkStatsBaseTest {
mHandlerThread = new HandlerThread("HandlerThread"); mHandlerThread = new HandlerThread("HandlerThread");
mHandlerThread.start(); mHandlerThread.start();
Handler.Callback callback = new NetworkStatsService.HandlerCallback(mService); Handler.Callback callback = new NetworkStatsService.HandlerCallback(mService);
mHandler = new Handler(mHandlerThread.getLooper(), callback); final Handler handler = new Handler(mHandlerThread.getLooper(), callback);
mService.setHandler(mHandler, callback); mService.setHandler(handler, callback);
mElapsedRealtime = 0L; mElapsedRealtime = 0L;
@@ -939,9 +938,7 @@ public class NetworkStatsServiceTest extends NetworkStatsBaseTest {
long minThresholdInBytes = 2 * 1024 * 1024; // 2 MB long minThresholdInBytes = 2 * 1024 * 1024; // 2 MB
assertEquals(minThresholdInBytes, request.thresholdInBytes); assertEquals(minThresholdInBytes, request.thresholdInBytes);
// Send dummy message to make sure that any previous message has been handled HandlerUtilsKt.waitForIdle(mHandlerThread, WAIT_TIMEOUT);
mHandler.sendMessage(mHandler.obtainMessage(-1));
HandlerUtilsKt.waitForIdle(mHandler, WAIT_TIMEOUT);
// Make sure that the caller binder gets connected // Make sure that the caller binder gets connected
verify(mBinder).linkToDeath(any(IBinder.DeathRecipient.class), anyInt()); verify(mBinder).linkToDeath(any(IBinder.DeathRecipient.class), anyInt());
@@ -1077,7 +1074,7 @@ public class NetworkStatsServiceTest extends NetworkStatsBaseTest {
// Simulates alert quota of the provider has been reached. // Simulates alert quota of the provider has been reached.
cb.onAlertReached(); cb.onAlertReached();
HandlerUtilsKt.waitForIdle(mHandler, WAIT_TIMEOUT); HandlerUtilsKt.waitForIdle(mHandlerThread, WAIT_TIMEOUT);
// Verifies that polling is triggered by alert reached. // Verifies that polling is triggered by alert reached.
provider.expectStatsUpdate(0 /* unused */); provider.expectStatsUpdate(0 /* unused */);
@@ -1294,9 +1291,7 @@ public class NetworkStatsServiceTest extends NetworkStatsBaseTest {
private void forcePollAndWaitForIdle() { private void forcePollAndWaitForIdle() {
mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL)); mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL));
// Send dummy message to make sure that any previous message has been handled HandlerUtilsKt.waitForIdle(mHandlerThread, WAIT_TIMEOUT);
mHandler.sendMessage(mHandler.obtainMessage(-1));
HandlerUtilsKt.waitForIdle(mHandler, WAIT_TIMEOUT);
} }
static class LatchedHandler extends Handler { static class LatchedHandler extends Handler {