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 android.adservices.cts;
18 
19 import static android.adservices.adselection.AdSelectionFromOutcomesConfigFixture.SAMPLE_AD_SELECTION_ID_1;
20 import static android.adservices.adselection.AdSelectionFromOutcomesConfigFixture.SAMPLE_AD_SELECTION_ID_2;
21 import static android.adservices.adselection.AdSelectionFromOutcomesConfigFixture.SAMPLE_SELECTION_LOGIC_URI_1;
22 import static android.adservices.adselection.AdSelectionFromOutcomesConfigFixture.SAMPLE_SELECTION_LOGIC_URI_2;
23 import static android.adservices.adselection.AdSelectionFromOutcomesConfigFixture.SAMPLE_SELECTION_SIGNALS;
24 
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertThrows;
27 
28 import android.adservices.adselection.AdSelectionFromOutcomesConfig;
29 import android.adservices.adselection.AdSelectionFromOutcomesConfigFixture;
30 import android.adservices.common.CommonFixture;
31 import android.os.Parcel;
32 
33 import com.android.adservices.shared.testing.SdkLevelSupportRule;
34 
35 import org.junit.Rule;
36 import org.junit.Test;
37 
38 import java.util.Collections;
39 
40 public class AdSelectionFromOutcomesConfigTest {
41 
42     @Rule(order = 0)
43     public final SdkLevelSupportRule sdkLevel = SdkLevelSupportRule.forAtLeastS();
44 
45     @Test
testBuildValidAdSelectionFromOutcomesConfigSuccess()46     public void testBuildValidAdSelectionFromOutcomesConfigSuccess() {
47         AdSelectionFromOutcomesConfig config =
48                 AdSelectionFromOutcomesConfigFixture.anAdSelectionFromOutcomesConfig();
49 
50         assertEquals(1, config.getAdSelectionIds().size());
51         assertEquals(SAMPLE_AD_SELECTION_ID_1, (long) config.getAdSelectionIds().get(0));
52         assertEquals(SAMPLE_SELECTION_SIGNALS, config.getSelectionSignals());
53         assertEquals(SAMPLE_SELECTION_LOGIC_URI_1, config.getSelectionLogicUri());
54     }
55 
56     @Test
testParcelValidInputSuccess()57     public void testParcelValidInputSuccess() {
58         AdSelectionFromOutcomesConfig config =
59                 AdSelectionFromOutcomesConfigFixture.anAdSelectionFromOutcomesConfig();
60 
61         Parcel p = Parcel.obtain();
62         config.writeToParcel(p, 0);
63         p.setDataPosition(0);
64         AdSelectionFromOutcomesConfig fromParcel =
65                 AdSelectionFromOutcomesConfig.CREATOR.createFromParcel(p);
66 
67         assertEquals(config.getAdSelectionIds(), fromParcel.getAdSelectionIds());
68         assertEquals(config.getSelectionSignals(), fromParcel.getSelectionSignals());
69         assertEquals(config.getSelectionLogicUri(), fromParcel.getSelectionLogicUri());
70     }
71 
72     @Test
testBuildAdSelectionFromOutcomesConfigUnsetAdOutcomeIds()73     public void testBuildAdSelectionFromOutcomesConfigUnsetAdOutcomeIds() {
74         assertThrows(
75                 NullPointerException.class,
76                 () -> {
77                     new AdSelectionFromOutcomesConfig.Builder()
78                             .setSelectionSignals(SAMPLE_SELECTION_SIGNALS)
79                             .setSelectionLogicUri(SAMPLE_SELECTION_LOGIC_URI_1)
80                             .build();
81                 });
82     }
83 
84     @Test
testBuildAdSelectionFromOutcomesConfigUnsetSelectionSignals()85     public void testBuildAdSelectionFromOutcomesConfigUnsetSelectionSignals() {
86         assertThrows(
87                 NullPointerException.class,
88                 () -> {
89                     new AdSelectionFromOutcomesConfig.Builder()
90                             .setAdSelectionIds(Collections.singletonList(SAMPLE_AD_SELECTION_ID_1))
91                             .setSelectionLogicUri(SAMPLE_SELECTION_LOGIC_URI_1)
92                             .build();
93                 });
94     }
95 
96     @Test
testBuildAdSelectionFromOutcomesConfigUnsetSelectionUri()97     public void testBuildAdSelectionFromOutcomesConfigUnsetSelectionUri() {
98         assertThrows(
99                 NullPointerException.class,
100                 () -> {
101                     new AdSelectionFromOutcomesConfig.Builder()
102                             .setAdSelectionIds(Collections.singletonList(SAMPLE_AD_SELECTION_ID_1))
103                             .setSelectionSignals(SAMPLE_SELECTION_SIGNALS)
104                             .build();
105                 });
106     }
107 
108     @Test
testAdSelectionFromOutcomesConfigDescribeContents()109     public void testAdSelectionFromOutcomesConfigDescribeContents() {
110         AdSelectionFromOutcomesConfig obj =
111                 AdSelectionFromOutcomesConfigFixture.anAdSelectionFromOutcomesConfig();
112 
113         assertEquals(obj.describeContents(), 0);
114     }
115 
116     @Test
testEqualInputsHaveSameHashCode()117     public void testEqualInputsHaveSameHashCode() {
118         AdSelectionFromOutcomesConfig obj1 =
119                 AdSelectionFromOutcomesConfigFixture.anAdSelectionFromOutcomesConfig();
120         AdSelectionFromOutcomesConfig obj2 =
121                 AdSelectionFromOutcomesConfigFixture.anAdSelectionFromOutcomesConfig();
122 
123         CommonFixture.assertHaveSameHashCode(obj1, obj2);
124     }
125 
126     @Test
testNotEqualInputsHaveDifferentHashCode()127     public void testNotEqualInputsHaveDifferentHashCode() {
128         AdSelectionFromOutcomesConfig obj1 =
129                 AdSelectionFromOutcomesConfigFixture.anAdSelectionFromOutcomesConfig();
130         AdSelectionFromOutcomesConfig obj2 =
131                 AdSelectionFromOutcomesConfigFixture.anAdSelectionFromOutcomesConfig(
132                         Collections.singletonList(SAMPLE_AD_SELECTION_ID_2));
133         AdSelectionFromOutcomesConfig obj3 =
134                 AdSelectionFromOutcomesConfigFixture.anAdSelectionFromOutcomesConfig(
135                         SAMPLE_SELECTION_LOGIC_URI_2);
136 
137         CommonFixture.assertDifferentHashCode(obj1, obj2, obj3);
138     }
139 }
140