Cody Kesting | 8c0043e | 2019-12-17 08:51:32 -0800 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2020 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package android.net; |
| 18 | |
| 19 | import static android.net.ConnectivityDiagnosticsManager.ConnectivityReport; |
| 20 | |
| 21 | import static com.android.testutils.ParcelUtilsKt.assertParcelSane; |
| 22 | |
| 23 | import static org.junit.Assert.assertEquals; |
| 24 | import static org.junit.Assert.assertFalse; |
| 25 | import static org.junit.Assert.assertNotEquals; |
| 26 | import static org.junit.Assert.assertTrue; |
| 27 | |
| 28 | import android.os.PersistableBundle; |
| 29 | |
| 30 | import org.junit.Test; |
| 31 | import org.junit.runner.RunWith; |
| 32 | import org.junit.runners.JUnit4; |
| 33 | |
| 34 | @RunWith(JUnit4.class) |
| 35 | public class ConnectivityDiagnosticsManagerTest { |
| 36 | private static final int NET_ID = 1; |
| 37 | private static final long TIMESTAMP = 10L; |
| 38 | private static final String INTERFACE_NAME = "interface"; |
| 39 | private static final String BUNDLE_KEY = "key"; |
| 40 | private static final String BUNDLE_VALUE = "value"; |
| 41 | |
| 42 | private ConnectivityReport createSampleConnectivityReport() { |
| 43 | final LinkProperties linkProperties = new LinkProperties(); |
| 44 | linkProperties.setInterfaceName(INTERFACE_NAME); |
| 45 | |
| 46 | final NetworkCapabilities networkCapabilities = new NetworkCapabilities(); |
| 47 | networkCapabilities.addCapability(NetworkCapabilities.NET_CAPABILITY_IMS); |
| 48 | |
| 49 | final PersistableBundle bundle = new PersistableBundle(); |
| 50 | bundle.putString(BUNDLE_KEY, BUNDLE_VALUE); |
| 51 | |
| 52 | return new ConnectivityReport( |
| 53 | new Network(NET_ID), TIMESTAMP, linkProperties, networkCapabilities, bundle); |
| 54 | } |
| 55 | |
| 56 | private ConnectivityReport createDefaultConnectivityReport() { |
| 57 | return new ConnectivityReport( |
| 58 | new Network(0), |
| 59 | 0L, |
| 60 | new LinkProperties(), |
| 61 | new NetworkCapabilities(), |
| 62 | PersistableBundle.EMPTY); |
| 63 | } |
| 64 | |
| 65 | @Test |
| 66 | public void testPersistableBundleEquals() { |
| 67 | assertFalse( |
| 68 | ConnectivityDiagnosticsManager.persistableBundleEquals( |
| 69 | null, PersistableBundle.EMPTY)); |
| 70 | assertFalse( |
| 71 | ConnectivityDiagnosticsManager.persistableBundleEquals( |
| 72 | PersistableBundle.EMPTY, null)); |
| 73 | assertTrue( |
| 74 | ConnectivityDiagnosticsManager.persistableBundleEquals( |
| 75 | PersistableBundle.EMPTY, PersistableBundle.EMPTY)); |
| 76 | |
| 77 | final PersistableBundle a = new PersistableBundle(); |
| 78 | a.putString(BUNDLE_KEY, BUNDLE_VALUE); |
| 79 | |
| 80 | final PersistableBundle b = new PersistableBundle(); |
| 81 | b.putString(BUNDLE_KEY, BUNDLE_VALUE); |
| 82 | |
| 83 | final PersistableBundle c = new PersistableBundle(); |
| 84 | c.putString(BUNDLE_KEY, null); |
| 85 | |
| 86 | assertFalse( |
| 87 | ConnectivityDiagnosticsManager.persistableBundleEquals(PersistableBundle.EMPTY, a)); |
| 88 | assertFalse( |
| 89 | ConnectivityDiagnosticsManager.persistableBundleEquals(a, PersistableBundle.EMPTY)); |
| 90 | |
| 91 | assertTrue(ConnectivityDiagnosticsManager.persistableBundleEquals(a, b)); |
| 92 | assertTrue(ConnectivityDiagnosticsManager.persistableBundleEquals(b, a)); |
| 93 | |
| 94 | assertFalse(ConnectivityDiagnosticsManager.persistableBundleEquals(a, c)); |
| 95 | assertFalse(ConnectivityDiagnosticsManager.persistableBundleEquals(c, a)); |
| 96 | } |
| 97 | |
| 98 | @Test |
| 99 | public void testConnectivityReportEquals() { |
| 100 | assertEquals(createSampleConnectivityReport(), createSampleConnectivityReport()); |
| 101 | assertEquals(createDefaultConnectivityReport(), createDefaultConnectivityReport()); |
| 102 | |
| 103 | final LinkProperties linkProperties = new LinkProperties(); |
| 104 | linkProperties.setInterfaceName(INTERFACE_NAME); |
| 105 | |
| 106 | final NetworkCapabilities networkCapabilities = new NetworkCapabilities(); |
| 107 | networkCapabilities.addCapability(NetworkCapabilities.NET_CAPABILITY_IMS); |
| 108 | |
| 109 | final PersistableBundle bundle = new PersistableBundle(); |
| 110 | bundle.putString(BUNDLE_KEY, BUNDLE_VALUE); |
| 111 | |
| 112 | assertNotEquals( |
| 113 | createDefaultConnectivityReport(), |
| 114 | new ConnectivityReport( |
| 115 | new Network(NET_ID), |
| 116 | 0L, |
| 117 | new LinkProperties(), |
| 118 | new NetworkCapabilities(), |
| 119 | PersistableBundle.EMPTY)); |
| 120 | assertNotEquals( |
| 121 | createDefaultConnectivityReport(), |
| 122 | new ConnectivityReport( |
| 123 | new Network(0), |
| 124 | TIMESTAMP, |
| 125 | new LinkProperties(), |
| 126 | new NetworkCapabilities(), |
| 127 | PersistableBundle.EMPTY)); |
| 128 | assertNotEquals( |
| 129 | createDefaultConnectivityReport(), |
| 130 | new ConnectivityReport( |
| 131 | new Network(0), |
| 132 | 0L, |
| 133 | linkProperties, |
| 134 | new NetworkCapabilities(), |
| 135 | PersistableBundle.EMPTY)); |
| 136 | assertNotEquals( |
| 137 | createDefaultConnectivityReport(), |
| 138 | new ConnectivityReport( |
| 139 | new Network(0), |
| 140 | TIMESTAMP, |
| 141 | new LinkProperties(), |
| 142 | networkCapabilities, |
| 143 | PersistableBundle.EMPTY)); |
| 144 | assertNotEquals( |
| 145 | createDefaultConnectivityReport(), |
| 146 | new ConnectivityReport( |
| 147 | new Network(0), |
| 148 | TIMESTAMP, |
| 149 | new LinkProperties(), |
| 150 | new NetworkCapabilities(), |
| 151 | bundle)); |
| 152 | } |
| 153 | |
| 154 | @Test |
| 155 | public void testConnectivityReportParcelUnparcel() { |
| 156 | assertParcelSane(createSampleConnectivityReport(), 5); |
| 157 | } |
| 158 | } |