From 9651e04bf0c1037b42aed0fb8bbf9dfd3f09dec2 Mon Sep 17 00:00:00 2001 From: Wayne Ma Date: Tue, 23 May 2023 15:10:51 +0800 Subject: [PATCH] Add isUsageSupportedForUpstreamType() helper method This new helper method provides a convenient way to validate and determine whether tethering metrics' data usage can be collected for a given upstream type. Bug: 282861165 Test: atest TetheringMetricsTest Change-Id: I7527ce56815e1f7e9382be1a54b84c91552e1129 --- .../tethering/metrics/TetheringMetrics.java | 18 ++++++++++++++++++ .../metrics/TetheringMetricsTest.java | 18 ++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/Tethering/src/com/android/networkstack/tethering/metrics/TetheringMetrics.java b/Tethering/src/com/android/networkstack/tethering/metrics/TetheringMetrics.java index 4a116ec5f4..136dfb1da7 100644 --- a/Tethering/src/com/android/networkstack/tethering/metrics/TetheringMetrics.java +++ b/Tethering/src/com/android/networkstack/tethering/metrics/TetheringMetrics.java @@ -401,4 +401,22 @@ public class TetheringMetrics { return UpstreamType.UT_UNKNOWN; } + + /** + * Check whether tethering metrics' data usage can be collected for a given upstream type. + * + * @param type the upstream type + */ + public static boolean isUsageSupportedForUpstreamType(@NonNull UpstreamType type) { + switch(type) { + case UT_CELLULAR: + case UT_WIFI: + case UT_BLUETOOTH: + case UT_ETHERNET: + return true; + default: + break; + } + return false; + } } diff --git a/Tethering/tests/unit/src/com/android/networkstack/tethering/metrics/TetheringMetricsTest.java b/Tethering/tests/unit/src/com/android/networkstack/tethering/metrics/TetheringMetricsTest.java index 0046ce1df7..7cef9cbf0c 100644 --- a/Tethering/tests/unit/src/com/android/networkstack/tethering/metrics/TetheringMetricsTest.java +++ b/Tethering/tests/unit/src/com/android/networkstack/tethering/metrics/TetheringMetricsTest.java @@ -46,6 +46,7 @@ import static android.net.TetheringManager.TETHER_ERROR_UNKNOWN_TYPE; import static android.net.TetheringManager.TETHER_ERROR_UNSUPPORTED; import static android.net.TetheringManager.TETHER_ERROR_UNTETHER_IFACE_ERROR; +import static org.junit.Assert.assertEquals; import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.verify; @@ -374,4 +375,21 @@ public final class TetheringMetricsTest { UserType.USER_SETTINGS, upstreamEvents, currentTimeMillis() - wifiTetheringStartTime); } + + private void runUsageSupportedForUpstreamTypeTest(final UpstreamType upstreamType, + final boolean isSupported) { + final boolean result = TetheringMetrics.isUsageSupportedForUpstreamType(upstreamType); + assertEquals(isSupported, result); + } + + @Test + public void testUsageSupportedForUpstreamTypeTest() { + runUsageSupportedForUpstreamTypeTest(UpstreamType.UT_CELLULAR, true /* isSupported */); + runUsageSupportedForUpstreamTypeTest(UpstreamType.UT_WIFI, true /* isSupported */); + runUsageSupportedForUpstreamTypeTest(UpstreamType.UT_BLUETOOTH, true /* isSupported */); + runUsageSupportedForUpstreamTypeTest(UpstreamType.UT_ETHERNET, true /* isSupported */); + runUsageSupportedForUpstreamTypeTest(UpstreamType.UT_WIFI_AWARE, false /* isSupported */); + runUsageSupportedForUpstreamTypeTest(UpstreamType.UT_LOWPAN, false /* isSupported */); + runUsageSupportedForUpstreamTypeTest(UpstreamType.UT_UNKNOWN, false /* isSupported */); + } }