1 /*
2  * Copyright (C) 2022 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.server.companion.virtual;
18 
19 import static android.content.Context.DEVICE_ID_DEFAULT;
20 import static android.content.Context.DEVICE_ID_INVALID;
21 
22 import static com.google.common.truth.Truth.assertThat;
23 
24 import static org.junit.Assert.assertThrows;
25 
26 import android.companion.virtual.VirtualDevice;
27 import android.os.Parcel;
28 import android.platform.test.annotations.Presubmit;
29 
30 import androidx.test.ext.junit.runners.AndroidJUnit4;
31 
32 import org.junit.Test;
33 import org.junit.runner.RunWith;
34 
35 @Presubmit
36 @RunWith(AndroidJUnit4.class)
37 public class VirtualDeviceTest {
38 
39     private static final int VIRTUAL_DEVICE_ID = 42;
40     private static final String VIRTUAL_DEVICE_NAME = "VirtualDeviceName";
41 
42     @Test
build_invalidId_shouldThrowIllegalArgumentException()43     public void build_invalidId_shouldThrowIllegalArgumentException() {
44         assertThrows(
45                 IllegalArgumentException.class,
46                 () -> new VirtualDevice(DEVICE_ID_INVALID, VIRTUAL_DEVICE_NAME));
47     }
48 
49     @Test
build_defaultId_shouldThrowIllegalArgumentException()50     public void build_defaultId_shouldThrowIllegalArgumentException() {
51         assertThrows(
52                 IllegalArgumentException.class,
53                 () -> new VirtualDevice(DEVICE_ID_DEFAULT, VIRTUAL_DEVICE_NAME));
54     }
55 
56     @Test
build_nameIsOptional()57     public void build_nameIsOptional() {
58         VirtualDevice virtualDevice =
59                 new VirtualDevice(VIRTUAL_DEVICE_ID, /* name= */ null);
60         assertThat(virtualDevice.getDeviceId()).isEqualTo(VIRTUAL_DEVICE_ID);
61         assertThat(virtualDevice.getName()).isNull();
62     }
63 
64     @Test
parcelable_shouldRecreateSuccessfully()65     public void parcelable_shouldRecreateSuccessfully() {
66         VirtualDevice originalDevice =
67                 new VirtualDevice(VIRTUAL_DEVICE_ID, VIRTUAL_DEVICE_NAME);
68         Parcel parcel = Parcel.obtain();
69         originalDevice.writeToParcel(parcel, 0);
70         parcel.setDataPosition(0);
71 
72         VirtualDevice device = VirtualDevice.CREATOR.createFromParcel(parcel);
73         assertThat(device).isEqualTo(originalDevice);
74         assertThat(device.getDeviceId()).isEqualTo(VIRTUAL_DEVICE_ID);
75         assertThat(device.getName()).isEqualTo(VIRTUAL_DEVICE_NAME);
76     }
77 }
78