1 /*
2  * Copyright (C) 2023 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 android.app.appsearch.safeparcel;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 
21 import android.app.PendingIntent;
22 import android.content.Context;
23 import android.content.Intent;
24 import android.os.Parcel;
25 
26 import androidx.test.core.app.ApplicationProvider;
27 import androidx.test.ext.junit.runners.AndroidJUnit4;
28 
29 import org.junit.After;
30 import org.junit.Before;
31 import org.junit.Test;
32 import org.junit.runner.RunWith;
33 
34 /** Test for reading and writing PendingIntents to Parcel. */
35 @RunWith(AndroidJUnit4.class)
36 public final class SafeParcelablePendingIntentTest {
37     private static final String CALLBACK_INTENT_ACTION =
38             SafeParcelablePendingIntentTest.class.getName() + "_INTENT";
39 
40     private Parcel parcel;
41 
42     @Before
setup()43     public void setup() {
44         parcel = Parcel.obtain();
45     }
46 
47     @After
tearDown()48     public void tearDown() {
49         parcel.recycle();
50     }
51 
52     @Test
testWriteRead_success()53     public void testWriteRead_success() {
54         Intent intent = new Intent(CALLBACK_INTENT_ACTION);
55 
56         Context context = ApplicationProvider.getApplicationContext();
57         PendingIntent pendingIntent =
58                 PendingIntent.getBroadcast(
59                         context,
60                         1234,
61                         intent,
62                         PendingIntent.FLAG_ONE_SHOT | PendingIntent.FLAG_IMMUTABLE);
63 
64         // Writing pending intent
65         parcel.setDataPosition(0);
66         SafeParcelWriter.writePendingIntent(parcel, 1, pendingIntent, false);
67 
68         // Reading pending intent
69         parcel.setDataPosition(0);
70 
71         int header = SafeParcelReader.readHeader(parcel);
72         PendingIntent resultIntent = SafeParcelReader.readPendingIntent(parcel, header);
73         assertThat(resultIntent).isEqualTo(pendingIntent);
74     }
75 
76     @Test
testWriteReadNull_success()77     public void testWriteReadNull_success() {
78 
79         // Writing pending intent
80         parcel.setDataPosition(0);
81         int pos = 0;
82         SafeParcelWriter.writePendingIntent(parcel, 1, null, false);
83 
84         assertThat(parcel.dataPosition()).isEqualTo(pos);
85 
86         // Reading pending intent
87         parcel.setDataPosition(0);
88         SafeParcelWriter.writePendingIntent(parcel, 1, null, true);
89         assertThat(parcel.dataPosition()).isNotEqualTo(pos);
90 
91         parcel.setDataPosition(0);
92         int header = SafeParcelReader.readHeader(parcel);
93         PendingIntent resultIntent = SafeParcelReader.readPendingIntent(parcel, header);
94 
95         assertThat(resultIntent).isNull();
96     }
97 }
98