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 com.android.car; 18 19 import static com.google.common.truth.Truth.assertThat; 20 21 import android.car.hardware.CarPropertyValue; 22 import android.car.hardware.property.CarPropertyEvent; 23 import android.car.hardware.property.CarPropertyManager; 24 import android.os.Parcel; 25 import android.os.Parcelable; 26 27 import org.junit.After; 28 import org.junit.Test; 29 import org.junit.runner.RunWith; 30 import org.mockito.junit.MockitoJUnitRunner; 31 32 /** Unit tests for {@link android.car.hardware.property.CarPropertyEvent} */ 33 @RunWith(MockitoJUnitRunner.class) 34 public final class CarPropertyEventTest { 35 36 private static final int FAKE_PROPERTY_ID = 0x1101111; 37 private static final int FAKE_AREA_ID = 0x1; 38 private static final int FAKE_PROPERTY_VALUE = 5; 39 private final Parcel mParcel = Parcel.obtain(); 40 41 @After tearDown()42 public void tearDown() throws Exception { 43 mParcel.recycle(); 44 } 45 readFromParcel()46 private <T extends Parcelable> T readFromParcel() { 47 mParcel.setDataPosition(0); 48 return mParcel.readParcelable(null); 49 } 50 writeToParcel(Parcelable value)51 private void writeToParcel(Parcelable value) { 52 mParcel.writeParcelable(value, 0); 53 } 54 55 @Test testCreateErrorEvent()56 public void testCreateErrorEvent() { 57 CarPropertyEvent carPropertyEvent = CarPropertyEvent 58 .createErrorEventWithErrorCode(FAKE_PROPERTY_ID, FAKE_AREA_ID, 59 CarPropertyManager.CAR_SET_PROPERTY_ERROR_CODE_UNKNOWN); 60 61 assertThat(carPropertyEvent.getErrorCode()) 62 .isEqualTo(CarPropertyManager.CAR_SET_PROPERTY_ERROR_CODE_UNKNOWN); 63 assertThat(carPropertyEvent.getCarPropertyValue().getStatus()).isEqualTo( 64 CarPropertyValue.STATUS_ERROR); 65 assertThat(carPropertyEvent.getEventType()).isEqualTo( 66 CarPropertyEvent.PROPERTY_EVENT_ERROR); 67 assertThat(carPropertyEvent.describeContents()).isEqualTo(0); 68 } 69 70 @Test testWriteAndReadEvent()71 public void testWriteAndReadEvent() { 72 CarPropertyValue<Integer> value = new CarPropertyValue<Integer>(FAKE_PROPERTY_ID, 73 FAKE_AREA_ID, FAKE_PROPERTY_VALUE); 74 CarPropertyEvent carPropertyEvent = new CarPropertyEvent( 75 CarPropertyEvent.PROPERTY_EVENT_PROPERTY_CHANGE, value); 76 77 writeToParcel(carPropertyEvent); 78 CarPropertyEvent eventReadFromParcel = readFromParcel(); 79 80 assertThat(eventReadFromParcel.getCarPropertyValue().getAreaId()) 81 .isEqualTo(FAKE_AREA_ID); 82 assertThat(eventReadFromParcel.getCarPropertyValue().getPropertyId()) 83 .isEqualTo(FAKE_PROPERTY_ID); 84 assertThat(eventReadFromParcel.getCarPropertyValue().getValue()) 85 .isEqualTo(FAKE_PROPERTY_VALUE); 86 assertThat(eventReadFromParcel.getEventType()) 87 .isEqualTo(CarPropertyEvent.PROPERTY_EVENT_PROPERTY_CHANGE); 88 } 89 } 90