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.internal.util;
18 
19 import android.os.Parcel;
20 import android.platform.test.annotations.Presubmit;
21 
22 import static org.junit.Assert.assertEquals;
23 import static org.junit.Assert.assertNull;
24 
25 import androidx.test.filters.SmallTest;
26 
27 import com.android.internal.util.Parcelling.BuiltIn.ForInstant;
28 
29 import org.junit.Test;
30 import org.junit.runner.RunWith;
31 import org.junit.runners.JUnit4;
32 
33 import java.time.Instant;
34 
35 /** Tests for {@link Parcelling}. */
36 @SmallTest
37 @Presubmit
38 @RunWith(JUnit4.class)
39 public class ParcellingTests {
40 
41     private Parcel mParcel = Parcel.obtain();
42 
43     @Test
forInstant_normal()44     public void forInstant_normal() {
45         testForInstant(Instant.ofEpochSecond(500L, 10));
46     }
47 
48     @Test
forInstant_minimum()49     public void forInstant_minimum() {
50         testForInstant(Instant.MIN);
51     }
52 
53     @Test
forInstant_maximum()54     public void forInstant_maximum() {
55         testForInstant(Instant.MAX);
56     }
57 
58     @Test
forInstant_null()59     public void forInstant_null() {
60         testForInstant(null);
61     }
62 
testForInstant(Instant instant)63     private void testForInstant(Instant instant) {
64         Parcelling<Instant> parcelling = new ForInstant();
65         parcelling.parcel(instant, mParcel, 0);
66         mParcel.setDataPosition(0);
67 
68         Instant created = parcelling.unparcel(mParcel);
69 
70         if (instant == null) {
71             assertNull(created);
72         } else {
73             assertEquals(instant, created);
74         }
75     }
76 
77 }
78