1 /* 2 * Copyright (C) 2021 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 com.android.queryable.util; 18 19 import static com.google.common.truth.Truth.assertThat; 20 21 import android.os.Bundle; 22 import android.os.Parcelable; 23 24 import com.android.queryable.util.SerializableParcelWrapper; 25 26 import org.junit.Test; 27 import org.junit.runner.RunWith; 28 import org.junit.runners.JUnit4; 29 30 import java.io.ByteArrayInputStream; 31 import java.io.ByteArrayOutputStream; 32 import java.io.ObjectInputStream; 33 import java.io.ObjectOutputStream; 34 35 @RunWith(JUnit4.class) 36 public class SerializableParcelWrapperTest { 37 38 private static final String KEY = "Key"; 39 private static final String STRING_VALUE = "String"; 40 41 private final Bundle mBundle = new Bundle(); 42 43 @Test serialize_deserialize_isEqual()44 public void serialize_deserialize_isEqual() throws Exception { 45 mBundle.putString(KEY, STRING_VALUE); 46 SerializableParcelWrapper<Bundle> serializableParcelWrapper 47 = new SerializableParcelWrapper<>(mBundle); 48 49 byte[] serializedBytes = serialize(serializableParcelWrapper); 50 SerializableParcelWrapper<Bundle> unserializedWrapper = deserialize(serializedBytes, Bundle.class); 51 52 assertThat(unserializedWrapper.get().getString(KEY)) 53 .isEqualTo(serializableParcelWrapper.get().getString(KEY)); 54 } 55 serialize(SerializableParcelWrapper<?> wrapper)56 private byte[] serialize(SerializableParcelWrapper<?> wrapper) throws Exception { 57 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 58 try (ObjectOutputStream outputStream = new ObjectOutputStream(baos)) { 59 outputStream.writeObject(wrapper); 60 } 61 baos.close(); 62 return baos.toByteArray(); 63 } 64 deserialize( byte[] bytes, Class<E> type)65 private <E extends Parcelable> SerializableParcelWrapper<E> deserialize( 66 byte[] bytes, Class<E> type) throws Exception { 67 try(ByteArrayInputStream bais = new ByteArrayInputStream(bytes); 68 ObjectInputStream inputStream = new ObjectInputStream(bais)) { 69 return (SerializableParcelWrapper<E>) inputStream.readObject(); 70 } 71 } 72 73 @Test equals_areEqual_returnsTrue()74 public void equals_areEqual_returnsTrue() { 75 Bundle bundle = new Bundle(); 76 77 SerializableParcelWrapper<Bundle> serializableParcelWrapper1 78 = new SerializableParcelWrapper<>(bundle); 79 SerializableParcelWrapper<Bundle> serializableParcelWrapper2 80 = new SerializableParcelWrapper<>(bundle); 81 82 assertThat(serializableParcelWrapper1.equals(serializableParcelWrapper2)).isTrue(); 83 } 84 85 @Test equals_areNotEqual_returnsFalse()86 public void equals_areNotEqual_returnsFalse() { 87 Bundle bundle1 = new Bundle(); 88 Bundle bundle2 = new Bundle(); 89 bundle1.putString(KEY, STRING_VALUE); 90 91 SerializableParcelWrapper<Bundle> serializableParcelWrapper1 92 = new SerializableParcelWrapper<>(bundle1); 93 SerializableParcelWrapper<Bundle> serializableParcelWrapper2 94 = new SerializableParcelWrapper<>(bundle2); 95 96 assertThat(serializableParcelWrapper1.equals(serializableParcelWrapper2)).isFalse(); 97 } 98 99 @Test hashcode_matchesContainedHashcode()100 public void hashcode_matchesContainedHashcode() { 101 SerializableParcelWrapper<Bundle> serializableParcelWrapper 102 = new SerializableParcelWrapper<>(mBundle); 103 104 assertThat(serializableParcelWrapper.hashCode()).isEqualTo(mBundle.hashCode()); 105 } 106 } 107