Merge changes Ia5fc3e53,Ie1ab6013,I1cd5acb3 into main

* changes:
  Unregister BroadcastReceiver and OnSubscriptionsChangedListener.
  Disable KeepaliveStatsTracker before T.
  Disable KeepaliveStatsTracker instead of throwing.
This commit is contained in:
Hansen Kurli
2023-07-28 11:20:21 +00:00
committed by Gerrit Code Review
3 changed files with 181 additions and 65 deletions

View File

@@ -25,10 +25,13 @@ import static com.android.testutils.HandlerUtils.visibleOnHandlerThread;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.ArgumentMatchers.argThat;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doCallRealMethod;
import static org.mockito.Mockito.doReturn;
@@ -118,16 +121,25 @@ public class KeepaliveStatsTrackerTest {
@Mock private KeepaliveStatsTracker.Dependencies mDependencies;
@Mock private SubscriptionManager mSubscriptionManager;
private void triggerBroadcastDefaultSubId(int subId) {
private BroadcastReceiver getBroadcastReceiver() {
final ArgumentCaptor<BroadcastReceiver> receiverCaptor =
ArgumentCaptor.forClass(BroadcastReceiver.class);
verify(mContext).registerReceiver(receiverCaptor.capture(), /* filter= */ any(),
/* broadcastPermission= */ any(), eq(mTestHandler));
verify(mContext).registerReceiver(
receiverCaptor.capture(),
argThat(intentFilter -> intentFilter.matchAction(
SubscriptionManager.ACTION_DEFAULT_SUBSCRIPTION_CHANGED)),
/* broadcastPermission= */ any(),
eq(mTestHandler));
return receiverCaptor.getValue();
}
private void triggerBroadcastDefaultSubId(int subId) {
final Intent intent =
new Intent(TelephonyManager.ACTION_SUBSCRIPTION_CARRIER_IDENTITY_CHANGED);
new Intent(SubscriptionManager.ACTION_DEFAULT_SUBSCRIPTION_CHANGED);
intent.putExtra(SubscriptionManager.EXTRA_SUBSCRIPTION_INDEX, subId);
receiverCaptor.getValue().onReceive(mContext, intent);
getBroadcastReceiver().onReceive(mContext, intent);
}
private OnSubscriptionsChangedListener getOnSubscriptionsChangedListener() {
@@ -433,6 +445,24 @@ public class KeepaliveStatsTrackerTest {
assertCarrierLifetimeMetrics(expectKeepaliveCarrierStatsArray, actualCarrierLifetime);
}
// The KeepaliveStatsTracker will be disabled when an error occurs with the keepalive states.
// Most tests should assert that the tracker is still active to ensure no errors occurred.
private void assertKeepaliveStatsTrackerActive() {
assertTrue(mKeepaliveStatsTracker.isEnabled());
}
private void assertKeepaliveStatsTrackerDisabled() {
assertFalse(mKeepaliveStatsTracker.isEnabled());
final OnSubscriptionsChangedListener listener = getOnSubscriptionsChangedListener();
// BackgroundThread will remove the OnSubscriptionsChangedListener.
HandlerUtils.waitForIdle(BackgroundThread.getHandler(), TIMEOUT_MS);
verify(mSubscriptionManager).removeOnSubscriptionsChangedListener(listener);
final BroadcastReceiver receiver = getBroadcastReceiver();
verify(mContext).unregisterReceiver(receiver);
}
@Test
public void testNoKeepalive() {
final int writeTime = 5000;
@@ -452,6 +482,7 @@ public class KeepaliveStatsTrackerTest {
expectRegisteredDurations,
expectActiveDurations,
new KeepaliveCarrierStats[0]);
assertKeepaliveStatsTrackerActive();
}
/*
@@ -485,6 +516,7 @@ public class KeepaliveStatsTrackerTest {
new KeepaliveCarrierStats[] {
getDefaultCarrierStats(expectRegisteredDurations[1], expectActiveDurations[1])
});
assertKeepaliveStatsTrackerActive();
}
/*
@@ -523,6 +555,7 @@ public class KeepaliveStatsTrackerTest {
new KeepaliveCarrierStats[] {
getDefaultCarrierStats(expectRegisteredDurations[1], expectActiveDurations[1])
});
assertKeepaliveStatsTrackerActive();
}
/*
@@ -567,6 +600,7 @@ public class KeepaliveStatsTrackerTest {
new KeepaliveCarrierStats[] {
getDefaultCarrierStats(expectRegisteredDurations[1], expectActiveDurations[1])
});
assertKeepaliveStatsTrackerActive();
}
/*
@@ -615,6 +649,7 @@ public class KeepaliveStatsTrackerTest {
new KeepaliveCarrierStats[] {
getDefaultCarrierStats(expectRegisteredDurations[1], expectActiveDurations[1])
});
assertKeepaliveStatsTrackerActive();
}
/*
@@ -657,6 +692,7 @@ public class KeepaliveStatsTrackerTest {
new KeepaliveCarrierStats[] {
getDefaultCarrierStats(expectRegisteredDurations[1], expectActiveDurations[1])
});
assertKeepaliveStatsTrackerActive();
}
/*
@@ -708,6 +744,7 @@ public class KeepaliveStatsTrackerTest {
new KeepaliveCarrierStats[] {
getDefaultCarrierStats(expectRegisteredDurations[1], expectActiveDurations[1])
});
assertKeepaliveStatsTrackerActive();
}
/*
@@ -788,6 +825,7 @@ public class KeepaliveStatsTrackerTest {
expectRegisteredDurations[1] + 2 * expectRegisteredDurations[2],
expectActiveDurations[1] + 2 * expectActiveDurations[2])
});
assertKeepaliveStatsTrackerActive();
}
/*
@@ -857,6 +895,7 @@ public class KeepaliveStatsTrackerTest {
new KeepaliveCarrierStats[] {
getDefaultCarrierStats(expectRegisteredDurations2[1], expectActiveDurations2[1])
});
assertKeepaliveStatsTrackerActive();
}
/*
@@ -946,6 +985,7 @@ public class KeepaliveStatsTrackerTest {
expectRegisteredDurations2,
expectActiveDurations2,
new KeepaliveCarrierStats[] {expectKeepaliveCarrierStats3});
assertKeepaliveStatsTrackerActive();
}
@Test
@@ -957,7 +997,10 @@ public class KeepaliveStatsTrackerTest {
onStartKeepalive(startTime1, TEST_SLOT);
// Attempt to use the same (network, slot)
assertThrows(IllegalArgumentException.class, () -> onStartKeepalive(startTime2, TEST_SLOT));
onStartKeepalive(startTime2, TEST_SLOT);
// Starting a 2nd keepalive on the same slot is unexpected and an error so the stats tracker
// is disabled.
assertKeepaliveStatsTrackerDisabled();
final DailykeepaliveInfoReported dailyKeepaliveInfoReported =
buildKeepaliveMetrics(writeTime);
@@ -1018,6 +1061,7 @@ public class KeepaliveStatsTrackerTest {
new KeepaliveCarrierStats[] {
getDefaultCarrierStats(expectRegisteredDurations[1], expectActiveDurations[1])
});
assertKeepaliveStatsTrackerActive();
}
@Test
@@ -1065,6 +1109,7 @@ public class KeepaliveStatsTrackerTest {
new KeepaliveCarrierStats[] {
expectKeepaliveCarrierStats1, expectKeepaliveCarrierStats2
});
assertKeepaliveStatsTrackerActive();
}
@Test
@@ -1106,6 +1151,7 @@ public class KeepaliveStatsTrackerTest {
/* expectRegisteredDurations= */ new int[] {startTime, writeTime - startTime},
/* expectActiveDurations= */ new int[] {startTime, writeTime - startTime},
new KeepaliveCarrierStats[] {expectKeepaliveCarrierStats});
assertKeepaliveStatsTrackerActive();
}
@Test
@@ -1148,6 +1194,7 @@ public class KeepaliveStatsTrackerTest {
writeTime * 3 - startTime1 - startTime2 - startTime3,
writeTime * 3 - startTime1 - startTime2 - startTime3)
});
assertKeepaliveStatsTrackerActive();
}
@Test
@@ -1197,6 +1244,7 @@ public class KeepaliveStatsTrackerTest {
new KeepaliveCarrierStats[] {
expectKeepaliveCarrierStats1, expectKeepaliveCarrierStats2
});
assertKeepaliveStatsTrackerActive();
}
@Test