1 /* 2 * Copyright (C) 2019 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.view.autofill; 18 19 import static android.view.autofill.AutofillId.NO_SESSION; 20 21 import static com.google.common.truth.Truth.assertThat; 22 import static com.google.common.truth.Truth.assertWithMessage; 23 24 import static org.testng.Assert.assertThrows; 25 26 import android.os.Parcel; 27 import android.view.View; 28 29 import org.junit.Test; 30 import org.junit.runner.RunWith; 31 import org.junit.runners.JUnit4; 32 33 @RunWith(JUnit4.class) 34 public class AutofillIdTest { 35 36 @Test testNonVirtual()37 public void testNonVirtual() { 38 final AutofillId id = new AutofillId(42); 39 assertNonVirtual(id, 42, NO_SESSION); 40 41 final AutofillId clone = cloneThroughParcel(id); 42 assertNonVirtual(clone, 42, NO_SESSION); 43 } 44 45 @Test testVirtual_int()46 public void testVirtual_int() { 47 final AutofillId id = new AutofillId(42, 108); 48 assertThat(id.getViewId()).isEqualTo(42); 49 assertThat(id.isVirtualInt()).isTrue(); 50 assertThat(id.isVirtualLong()).isFalse(); 51 assertThat(id.isNonVirtual()).isFalse(); 52 assertThat(id.getVirtualChildIntId()).isEqualTo(108); 53 assertThat(id.getVirtualChildLongId()).isEqualTo(View.NO_ID); 54 55 final AutofillId clone = cloneThroughParcel(id); 56 assertThat(clone.getViewId()).isEqualTo(42); 57 assertThat(clone.isVirtualLong()).isFalse(); 58 assertThat(clone.isVirtualInt()).isTrue(); 59 assertThat(clone.isNonVirtual()).isFalse(); 60 assertThat(clone.getVirtualChildIntId()).isEqualTo(108); 61 assertThat(clone.getVirtualChildLongId()).isEqualTo(View.NO_ID); 62 } 63 64 @Test testVirtual_long()65 public void testVirtual_long() { 66 final AutofillId id = new AutofillId(new AutofillId(42), 4815162342L, 108); 67 assertThat(id.getViewId()).isEqualTo(42); 68 assertThat(id.isVirtualLong()).isTrue(); 69 assertThat(id.isVirtualInt()).isFalse(); 70 assertThat(id.isNonVirtual()).isFalse(); 71 assertThat(id.getVirtualChildIntId()).isEqualTo(View.NO_ID); 72 assertThat(id.getVirtualChildLongId()).isEqualTo(4815162342L); 73 74 final AutofillId clone = cloneThroughParcel(id); 75 assertThat(clone.getViewId()).isEqualTo(42); 76 assertThat(clone.isVirtualLong()).isTrue(); 77 assertThat(clone.isVirtualInt()).isFalse(); 78 assertThat(clone.isNonVirtual()).isFalse(); 79 assertThat(clone.getVirtualChildIntId()).isEqualTo(View.NO_ID); 80 assertThat(clone.getVirtualChildLongId()).isEqualTo(4815162342L); 81 } 82 83 @Test testVirtual_parentObjectConstructor()84 public void testVirtual_parentObjectConstructor() { 85 assertThrows(NullPointerException.class, () -> new AutofillId(null, 108)); 86 87 final AutofillId id = new AutofillId(new AutofillId(42), 108); 88 assertThat(id.getViewId()).isEqualTo(42); 89 assertThat(id.isVirtualInt()).isTrue(); 90 assertThat(id.getVirtualChildIntId()).isEqualTo(108); 91 92 final AutofillId clone = cloneThroughParcel(id); 93 assertThat(clone.getViewId()).isEqualTo(42); 94 assertThat(clone.isVirtualInt()).isTrue(); 95 assertThat(clone.getVirtualChildIntId()).isEqualTo(108); 96 } 97 98 @Test testVirtual_withSession()99 public void testVirtual_withSession() { 100 final AutofillId id = new AutofillId(new AutofillId(42), 108L, 666); 101 assertThat(id.getViewId()).isEqualTo(42); 102 assertThat(id.isVirtualLong()).isTrue(); 103 assertThat(id.isVirtualInt()).isFalse(); 104 assertThat(id.isNonVirtual()).isFalse(); 105 assertThat(id.getVirtualChildLongId()).isEqualTo(108L); 106 assertThat(id.getVirtualChildIntId()).isEqualTo(View.NO_ID); 107 assertThat(id.getSessionId()).isEqualTo(666); 108 109 final AutofillId clone = cloneThroughParcel(id); 110 assertThat(clone.getViewId()).isEqualTo(42); 111 assertThat(clone.isVirtualLong()).isTrue(); 112 assertThat(clone.isVirtualInt()).isFalse(); 113 assertThat(clone.isNonVirtual()).isFalse(); 114 assertThat(clone.getVirtualChildLongId()).isEqualTo(108L); 115 assertThat(clone.getVirtualChildIntId()).isEqualTo(View.NO_ID); 116 assertThat(clone.getSessionId()).isEqualTo(666); 117 } 118 119 @Test testFactoryMethod_withoutSession()120 public void testFactoryMethod_withoutSession() { 121 final AutofillId id = new AutofillId(42); 122 id.setSessionId(108); 123 assertNonVirtual(id, 42, 108); 124 final AutofillId idWithoutSession = AutofillId.withoutSession(id); 125 assertNonVirtual(idWithoutSession, 42, NO_SESSION); 126 } 127 128 @Test testSetResetSession()129 public void testSetResetSession() { 130 final AutofillId id = new AutofillId(42); 131 assertNonVirtual(id, 42, NO_SESSION); 132 id.setSessionId(108); 133 assertNonVirtual(id, 42, 108); 134 135 final AutofillId clone1 = cloneThroughParcel(id); 136 assertNonVirtual(clone1, 42, 108); 137 138 id.resetSessionId(); 139 assertThat(id.getSessionId()).isEqualTo(NO_SESSION); 140 final AutofillId clone2 = cloneThroughParcel(id); 141 assertNonVirtual(clone2, 42, NO_SESSION); 142 } 143 144 @Test testEqualsHashCode_nonVirtual_same()145 public void testEqualsHashCode_nonVirtual_same() { 146 final AutofillId id = new AutofillId(42); 147 final AutofillId sameId = new AutofillId(42); 148 149 assertThat(id).isEqualTo(sameId); 150 assertThat(sameId).isEqualTo(id); 151 assertEqualsIgnoreSession(id, sameId); 152 assertEqualsIgnoreSession(sameId, id); 153 assertThat(id.hashCode()).isEqualTo(sameId.hashCode()); 154 } 155 156 @Test testEqualsHashCode_nonVirtual_other()157 public void testEqualsHashCode_nonVirtual_other() { 158 final AutofillId id = new AutofillId(42); 159 final AutofillId otherId = new AutofillId(108); 160 161 assertThat(id).isNotEqualTo(otherId); 162 assertThat(otherId).isNotEqualTo(id); 163 assertNotEqualsIgnoreSession(id, otherId); 164 assertNotEqualsIgnoreSession(otherId, id); 165 assertThat(id.hashCode()).isNotEqualTo(otherId.hashCode()); 166 } 167 168 @Test testEqualsHashCode_virtual_same()169 public void testEqualsHashCode_virtual_same() { 170 final AutofillId id = new AutofillId(42); 171 final AutofillId virtual = new AutofillId(42, 1); 172 final AutofillId sameVirtual = new AutofillId(42, 1); 173 174 assertThat(virtual).isEqualTo(sameVirtual); 175 assertThat(sameVirtual).isEqualTo(virtual); 176 assertEqualsIgnoreSession(virtual, sameVirtual); 177 assertEqualsIgnoreSession(sameVirtual, virtual); 178 assertThat(virtual.hashCode()).isEqualTo(sameVirtual.hashCode()); 179 assertThat(virtual).isNotEqualTo(id); 180 assertThat(id).isNotEqualTo(virtual); 181 assertNotEqualsIgnoreSession(id, virtual); 182 assertNotEqualsIgnoreSession(virtual, id); 183 } 184 185 @Test testEqualsHashCode_virtual_otherChild()186 public void testEqualsHashCode_virtual_otherChild() { 187 final AutofillId id = new AutofillId(42); 188 final AutofillId virtual = new AutofillId(42, 1); 189 final AutofillId virtualOtherChild = new AutofillId(42, 2); 190 191 assertThat(virtualOtherChild).isNotEqualTo(virtual); 192 assertThat(virtual).isNotEqualTo(virtualOtherChild); 193 assertNotEqualsIgnoreSession(virtualOtherChild, virtual); 194 assertNotEqualsIgnoreSession(virtual, virtualOtherChild); 195 assertThat(virtualOtherChild).isNotEqualTo(id); 196 assertThat(id).isNotEqualTo(virtualOtherChild); 197 assertNotEqualsIgnoreSession(virtualOtherChild, id); 198 assertNotEqualsIgnoreSession(id, virtualOtherChild); 199 } 200 201 @Test testEqualsHashCode_virtual_otherParent()202 public void testEqualsHashCode_virtual_otherParent() { 203 final AutofillId virtual = new AutofillId(42, 1); 204 final AutofillId virtualOtherParent = new AutofillId(108, 1); 205 final AutofillId virtualOtherChild = new AutofillId(42, 2); 206 207 assertThat(virtualOtherParent).isNotEqualTo(virtual); 208 assertThat(virtual).isNotEqualTo(virtualOtherParent); 209 assertNotEqualsIgnoreSession(virtualOtherParent, virtual); 210 assertNotEqualsIgnoreSession(virtual, virtualOtherParent); 211 assertThat(virtualOtherParent).isNotEqualTo(virtualOtherChild); 212 assertThat(virtualOtherChild).isNotEqualTo(virtualOtherParent); 213 assertNotEqualsIgnoreSession(virtualOtherParent, virtualOtherChild); 214 assertNotEqualsIgnoreSession(virtualOtherChild, virtualOtherParent); 215 } 216 217 @Test testEqualsHashCode_virtual_otherSession()218 public void testEqualsHashCode_virtual_otherSession() { 219 final AutofillId virtual = new AutofillId(42, 1); 220 final AutofillId virtualOtherSession = new AutofillId(42, 1); 221 virtualOtherSession.setSessionId(666); 222 223 assertThat(virtualOtherSession).isNotEqualTo(virtual); 224 assertThat(virtual).isNotEqualTo(virtualOtherSession); 225 assertEqualsIgnoreSession(virtualOtherSession, virtual); 226 assertEqualsIgnoreSession(virtual, virtualOtherSession); 227 } 228 229 @Test testEqualsHashCode_virtual_longId_same()230 public void testEqualsHashCode_virtual_longId_same() { 231 final AutofillId hostId = new AutofillId(42); 232 final AutofillId virtual = new AutofillId(hostId, 1L, 108); 233 final AutofillId sameVirtual = new AutofillId(hostId, 1L, 108); 234 235 assertThat(sameVirtual).isEqualTo(virtual); 236 assertThat(virtual).isEqualTo(sameVirtual); 237 assertEqualsIgnoreSession(sameVirtual, virtual); 238 assertEqualsIgnoreSession(virtual, sameVirtual); 239 assertThat(sameVirtual).isNotEqualTo(hostId); 240 assertThat(hostId).isNotEqualTo(sameVirtual); 241 assertNotEqualsIgnoreSession(sameVirtual, hostId); 242 assertNotEqualsIgnoreSession(hostId, sameVirtual); 243 } 244 245 @Test testEqualsHashCode_virtual_longId_otherChild()246 public void testEqualsHashCode_virtual_longId_otherChild() { 247 final AutofillId hostId = new AutofillId(42); 248 final AutofillId virtual = new AutofillId(hostId, 1L, 108); 249 final AutofillId virtualOtherChild = new AutofillId(hostId, 2L, 108); 250 251 assertThat(virtualOtherChild).isNotEqualTo(virtual); 252 assertThat(virtual).isNotEqualTo(virtualOtherChild); 253 assertNotEqualsIgnoreSession(virtualOtherChild, virtual); 254 assertNotEqualsIgnoreSession(virtual, virtualOtherChild); 255 assertThat(virtualOtherChild).isNotEqualTo(hostId); 256 assertThat(hostId).isNotEqualTo(virtualOtherChild); 257 assertNotEqualsIgnoreSession(virtualOtherChild, hostId); 258 assertNotEqualsIgnoreSession(hostId, virtualOtherChild); 259 } 260 261 @Test testEqualsHashCode_virtual_longId_otherParent()262 public void testEqualsHashCode_virtual_longId_otherParent() { 263 final AutofillId hostId = new AutofillId(42); 264 final AutofillId virtual = new AutofillId(hostId, 1L, 108); 265 final AutofillId virtualOtherParent = new AutofillId(new AutofillId(666), 1L, 108); 266 final AutofillId virtualOtherChild = new AutofillId(hostId, 2L, 108); 267 268 assertThat(virtualOtherParent).isNotEqualTo(virtual); 269 assertThat(virtual).isNotEqualTo(virtualOtherParent); 270 assertNotEqualsIgnoreSession(virtualOtherParent, virtual); 271 assertNotEqualsIgnoreSession(virtual, virtualOtherParent); 272 assertThat(virtualOtherParent).isNotEqualTo(virtualOtherChild); 273 assertThat(virtualOtherChild).isNotEqualTo(virtualOtherParent); 274 assertNotEqualsIgnoreSession(virtualOtherParent, virtualOtherChild); 275 assertNotEqualsIgnoreSession(virtualOtherChild, virtualOtherParent); 276 } 277 278 @Test testEqualsHashCode_virtual_longId_otherSession()279 public void testEqualsHashCode_virtual_longId_otherSession() { 280 final AutofillId hostId = new AutofillId(42); 281 final AutofillId virtual = new AutofillId(hostId, 1L, 108); 282 final AutofillId virtualOtherSession = new AutofillId(hostId, 1L, 666); 283 284 assertThat(virtualOtherSession).isNotEqualTo(virtual); 285 assertThat(virtual).isNotEqualTo(virtualOtherSession); 286 assertEqualsIgnoreSession(virtualOtherSession, virtual); 287 assertEqualsIgnoreSession(virtual, virtualOtherSession); 288 } 289 cloneThroughParcel(AutofillId id)290 private AutofillId cloneThroughParcel(AutofillId id) { 291 Parcel parcel = Parcel.obtain(); 292 293 try { 294 // Write to parcel 295 parcel.setDataPosition(0); // Sanity / paranoid check 296 id.writeToParcel(parcel, 0); 297 298 // Read from parcel 299 parcel.setDataPosition(0); 300 AutofillId clone = AutofillId.CREATOR.createFromParcel(parcel); 301 assertThat(clone).isNotNull(); 302 return clone; 303 } finally { 304 parcel.recycle(); 305 } 306 } 307 assertEqualsIgnoreSession(AutofillId id1, AutofillId id2)308 private void assertEqualsIgnoreSession(AutofillId id1, AutofillId id2) { 309 assertWithMessage("id1 is null").that(id1).isNotNull(); 310 assertWithMessage("id2 is null").that(id2).isNotNull(); 311 assertWithMessage("%s is not equal to %s", id1, id2).that(id1.equalsIgnoreSession(id2)) 312 .isTrue(); 313 } 314 assertNotEqualsIgnoreSession(AutofillId id1, AutofillId id2)315 private void assertNotEqualsIgnoreSession(AutofillId id1, AutofillId id2) { 316 assertWithMessage("id1 is null").that(id1).isNotNull(); 317 assertWithMessage("id2 is null").that(id2).isNotNull(); 318 assertWithMessage("%s is not equal to %s", id1, id2).that(id1.equalsIgnoreSession(id2)) 319 .isFalse(); 320 } 321 assertNonVirtual(AutofillId id, int expectedId, int expectSessionId)322 private void assertNonVirtual(AutofillId id, int expectedId, int expectSessionId) { 323 assertThat(id.getViewId()).isEqualTo(expectedId); 324 assertThat(id.isNonVirtual()).isTrue(); 325 assertThat(id.isVirtualInt()).isFalse(); 326 assertThat(id.isVirtualLong()).isFalse(); 327 assertThat(id.getVirtualChildIntId()).isEqualTo(View.NO_ID); 328 assertThat(id.getVirtualChildLongId()).isEqualTo(View.NO_ID); 329 assertThat(id.getSessionId()).isEqualTo(expectSessionId); 330 } 331 } 332