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.adservices.common;
18 
19 import android.os.Parcel;
20 
21 import com.google.common.collect.ImmutableList;
22 
23 import java.time.Duration;
24 import java.util.Arrays;
25 import java.util.List;
26 
27 /** Utility class for creating and testing {@link KeyedFrequencyCap} objects. */
28 public class KeyedFrequencyCapFixture {
29     public static final int KEY1 = 1;
30     public static final int KEY2 = 2;
31     public static final int KEY3 = 3;
32     public static final int KEY4 = 4;
33     public static final int VALID_COUNT = 10;
34     public static final int FILTER_COUNT = 1;
35     public static final int FILTER_UNDER_MAX_COUNT = FILTER_COUNT - 1;
36     public static final int FILTER_EXCEED_COUNT = FILTER_COUNT;
37     public static final Duration ONE_DAY_DURATION = Duration.ofDays(1);
38 
39     public static final ImmutableList<KeyedFrequencyCap> VALID_KEYED_FREQUENCY_CAP_LIST =
40             ImmutableList.of(
41                     getValidKeyedFrequencyCapBuilderOncePerDay(KEY1).build(),
42                     getValidKeyedFrequencyCapBuilderOncePerDay(KEY2).build(),
43                     getValidKeyedFrequencyCapBuilderOncePerDay(KEY3).build(),
44                     getValidKeyedFrequencyCapBuilderOncePerDay(KEY4).build());
45 
46     public static final List<KeyedFrequencyCap> KEYED_FREQUENCY_CAP_LIST_CONTAINING_NULL =
47             Arrays.asList(
48                     getValidKeyedFrequencyCapBuilderOncePerDay(KEY1).build(),
49                     getValidKeyedFrequencyCapBuilderOncePerDay(KEY2).build(),
50                     getValidKeyedFrequencyCapBuilderOncePerDay(KEY3).build(),
51                     null,
52                     getValidKeyedFrequencyCapBuilderOncePerDay(KEY4).build());
53 
getExcessiveNumberOfFrequencyCapsList()54     public static ImmutableList<KeyedFrequencyCap> getExcessiveNumberOfFrequencyCapsList() {
55         ImmutableList.Builder<KeyedFrequencyCap> listBuilder = ImmutableList.builder();
56 
57         // Add just one more than the limit
58         for (int key = 0; key <= FrequencyCapFilters.MAX_NUM_FREQUENCY_CAP_FILTERS; key++) {
59             listBuilder.add(getValidKeyedFrequencyCapBuilderOncePerDay(key).build());
60         }
61 
62         return listBuilder.build();
63     }
64 
getValidKeyedFrequencyCapBuilderOncePerDay(int key)65     public static KeyedFrequencyCap.Builder getValidKeyedFrequencyCapBuilderOncePerDay(int key) {
66         return new KeyedFrequencyCap.Builder(key, FILTER_COUNT, ONE_DAY_DURATION);
67     }
68 
getKeyedFrequencyCapWithFields( int adCounterKey, int maxCount, Duration interval)69     public static KeyedFrequencyCap getKeyedFrequencyCapWithFields(
70             int adCounterKey, int maxCount, Duration interval) {
71         Parcel sourceParcel = Parcel.obtain();
72         sourceParcel.writeInt(adCounterKey);
73         sourceParcel.writeInt(maxCount);
74         sourceParcel.writeLong(interval.getSeconds());
75         sourceParcel.setDataPosition(0);
76 
77         return KeyedFrequencyCap.CREATOR.createFromParcel(sourceParcel);
78     }
79 }
80