Improve error message when testing network factory
Currently, when network factory is under testing, but failed without terminating the network factory. The mocked network factory will stay registered and trigger another assertion fail in teardown(). Thus, the test suite will only shows the callstack that generated in teardown() instead of the original fail. The error message is misleading and not useful at all. Thus, safely terminate and quit mocked network factory after testing to prevent assertion fail in teardown(). Test: atest ConnectivityServiceTest#testMobileDataAlwaysOn Bug: 175180558 Change-Id: I0f96332cc05221e576bd792c6cd26d9dccb4e228
This commit is contained in:
@@ -3622,51 +3622,55 @@ public class ConnectivityServiceTest {
|
|||||||
// Register the factory and expect it to start looking for a network.
|
// Register the factory and expect it to start looking for a network.
|
||||||
testFactory.expectAddRequestsWithScores(0); // Score 0 as the request is not served yet.
|
testFactory.expectAddRequestsWithScores(0); // Score 0 as the request is not served yet.
|
||||||
testFactory.register();
|
testFactory.register();
|
||||||
testFactory.waitForNetworkRequests(1);
|
|
||||||
assertTrue(testFactory.getMyStartRequested());
|
|
||||||
|
|
||||||
// Bring up wifi. The factory stops looking for a network.
|
try {
|
||||||
mWiFiNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
|
testFactory.waitForNetworkRequests(1);
|
||||||
// Score 60 - 40 penalty for not validated yet, then 60 when it validates
|
assertTrue(testFactory.getMyStartRequested());
|
||||||
testFactory.expectAddRequestsWithScores(20, 60);
|
|
||||||
mWiFiNetworkAgent.connect(true);
|
|
||||||
testFactory.waitForRequests();
|
|
||||||
assertFalse(testFactory.getMyStartRequested());
|
|
||||||
|
|
||||||
ContentResolver cr = mServiceContext.getContentResolver();
|
// Bring up wifi. The factory stops looking for a network.
|
||||||
|
mWiFiNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
|
||||||
|
// Score 60 - 40 penalty for not validated yet, then 60 when it validates
|
||||||
|
testFactory.expectAddRequestsWithScores(20, 60);
|
||||||
|
mWiFiNetworkAgent.connect(true);
|
||||||
|
testFactory.waitForRequests();
|
||||||
|
assertFalse(testFactory.getMyStartRequested());
|
||||||
|
|
||||||
// Turn on mobile data always on. The factory starts looking again.
|
ContentResolver cr = mServiceContext.getContentResolver();
|
||||||
testFactory.expectAddRequestsWithScores(0); // Always on requests comes up with score 0
|
|
||||||
setAlwaysOnNetworks(true);
|
|
||||||
testFactory.waitForNetworkRequests(2);
|
|
||||||
assertTrue(testFactory.getMyStartRequested());
|
|
||||||
|
|
||||||
// Bring up cell data and check that the factory stops looking.
|
// Turn on mobile data always on. The factory starts looking again.
|
||||||
assertLength(1, mCm.getAllNetworks());
|
testFactory.expectAddRequestsWithScores(0); // Always on requests comes up with score 0
|
||||||
mCellNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_CELLULAR);
|
setAlwaysOnNetworks(true);
|
||||||
testFactory.expectAddRequestsWithScores(10, 50); // Unvalidated, then validated
|
testFactory.waitForNetworkRequests(2);
|
||||||
mCellNetworkAgent.connect(true);
|
assertTrue(testFactory.getMyStartRequested());
|
||||||
cellNetworkCallback.expectAvailableThenValidatedCallbacks(mCellNetworkAgent);
|
|
||||||
testFactory.waitForNetworkRequests(2);
|
|
||||||
assertFalse(testFactory.getMyStartRequested()); // Because the cell network outscores us.
|
|
||||||
|
|
||||||
// Check that cell data stays up.
|
// Bring up cell data and check that the factory stops looking.
|
||||||
waitForIdle();
|
assertLength(1, mCm.getAllNetworks());
|
||||||
verifyActiveNetwork(TRANSPORT_WIFI);
|
mCellNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_CELLULAR);
|
||||||
assertLength(2, mCm.getAllNetworks());
|
testFactory.expectAddRequestsWithScores(10, 50); // Unvalidated, then validated
|
||||||
|
mCellNetworkAgent.connect(true);
|
||||||
|
cellNetworkCallback.expectAvailableThenValidatedCallbacks(mCellNetworkAgent);
|
||||||
|
testFactory.waitForNetworkRequests(2);
|
||||||
|
assertFalse(
|
||||||
|
testFactory.getMyStartRequested()); // Because the cell network outscores us.
|
||||||
|
|
||||||
// Turn off mobile data always on and expect the request to disappear...
|
// Check that cell data stays up.
|
||||||
testFactory.expectRemoveRequests(1);
|
waitForIdle();
|
||||||
setAlwaysOnNetworks(false);
|
verifyActiveNetwork(TRANSPORT_WIFI);
|
||||||
testFactory.waitForNetworkRequests(1);
|
assertLength(2, mCm.getAllNetworks());
|
||||||
|
|
||||||
// ... and cell data to be torn down.
|
// Turn off mobile data always on and expect the request to disappear...
|
||||||
cellNetworkCallback.expectCallback(CallbackEntry.LOST, mCellNetworkAgent);
|
testFactory.expectRemoveRequests(1);
|
||||||
assertLength(1, mCm.getAllNetworks());
|
setAlwaysOnNetworks(false);
|
||||||
|
testFactory.waitForNetworkRequests(1);
|
||||||
|
|
||||||
testFactory.terminate();
|
// ... and cell data to be torn down.
|
||||||
mCm.unregisterNetworkCallback(cellNetworkCallback);
|
cellNetworkCallback.expectCallback(CallbackEntry.LOST, mCellNetworkAgent);
|
||||||
handlerThread.quit();
|
assertLength(1, mCm.getAllNetworks());
|
||||||
|
} finally {
|
||||||
|
testFactory.terminate();
|
||||||
|
mCm.unregisterNetworkCallback(cellNetworkCallback);
|
||||||
|
handlerThread.quit();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|||||||
Reference in New Issue
Block a user