• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2024 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.devicelock;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 
21 import android.devicelock.ParcelableException;
22 import android.os.Parcel;
23 
24 import org.junit.Test;
25 import org.junit.runner.RunWith;
26 import org.robolectric.RobolectricTestRunner;
27 
28 /**
29  * Tests for {@link android.devicelock.ParcelableException}.
30  */
31 @RunWith(RobolectricTestRunner.class)
32 public final class ParcelableExceptionTest {
33     private static final String EXCEPTION_MESSAGE = "TEST_EXCEPTION_MESSAGE";
34 
35     @Test
parcelableExceptionShouldReturnOriginalException()36     public void parcelableExceptionShouldReturnOriginalException() {
37         Exception exception = new Exception(EXCEPTION_MESSAGE);
38         ParcelableException parcelableException = new ParcelableException(exception);
39 
40         Exception cause = parcelableException.getException();
41 
42         assertThat(cause).isNotNull();
43         assertThat(cause.getMessage()).isEqualTo(EXCEPTION_MESSAGE);
44     }
45 
46     @Test
parcelableExceptionShouldParcelAndUnparcel()47     public void parcelableExceptionShouldParcelAndUnparcel() {
48         Parcel parcel = Parcel.obtain();
49         try {
50             Exception exception = new Exception(EXCEPTION_MESSAGE);
51             ParcelableException inParcelable = new ParcelableException(exception);
52             parcel.writeParcelable(inParcelable, 0);
53             parcel.setDataPosition(0);
54             ParcelableException outParcelable = parcel.readParcelable(
55                     ParcelableException.class.getClassLoader(), ParcelableException.class);
56             assertThat(outParcelable).isNotNull();
57             assertThat(inParcelable.getException().getMessage())
58                     .isEqualTo(outParcelable.getException().getMessage());
59         } finally {
60             parcel.recycle();
61         }
62     }
63 }
64