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.util.ArrayList; 24 25 /** Utility class for creating and testing {@link FrequencyCapFilters} objects. */ 26 public class FrequencyCapFiltersFixture { 27 public static final FrequencyCapFilters VALID_FREQUENCY_CAP_FILTERS = 28 getValidFrequencyCapFiltersBuilder().build(); 29 30 public static final FrequencyCapFilters VALID_FREQUENCY_CAP_FILTERS_ONLY_WIN = 31 new FrequencyCapFilters.Builder() 32 .setKeyedFrequencyCapsForWinEvents( 33 KeyedFrequencyCapFixture.VALID_KEYED_FREQUENCY_CAP_LIST) 34 .build(); 35 36 public static final FrequencyCapFilters VALID_FREQUENCY_CAP_FILTERS_ONLY_IMPRESSION = 37 new FrequencyCapFilters.Builder() 38 .setKeyedFrequencyCapsForImpressionEvents( 39 KeyedFrequencyCapFixture.VALID_KEYED_FREQUENCY_CAP_LIST) 40 .build(); 41 42 public static final FrequencyCapFilters VALID_FREQUENCY_CAP_FILTERS_ONLY_VIEW = 43 new FrequencyCapFilters.Builder() 44 .setKeyedFrequencyCapsForViewEvents( 45 KeyedFrequencyCapFixture.VALID_KEYED_FREQUENCY_CAP_LIST) 46 .build(); 47 48 public static final FrequencyCapFilters VALID_FREQUENCY_CAP_FILTERS_ONLY_CLICK = 49 new FrequencyCapFilters.Builder() 50 .setKeyedFrequencyCapsForClickEvents( 51 KeyedFrequencyCapFixture.VALID_KEYED_FREQUENCY_CAP_LIST) 52 .build(); 53 getValidFrequencyCapFiltersBuilder()54 public static FrequencyCapFilters.Builder getValidFrequencyCapFiltersBuilder() { 55 return new FrequencyCapFilters.Builder() 56 .setKeyedFrequencyCapsForWinEvents( 57 KeyedFrequencyCapFixture.VALID_KEYED_FREQUENCY_CAP_LIST) 58 .setKeyedFrequencyCapsForImpressionEvents( 59 KeyedFrequencyCapFixture.VALID_KEYED_FREQUENCY_CAP_LIST) 60 .setKeyedFrequencyCapsForViewEvents( 61 KeyedFrequencyCapFixture.VALID_KEYED_FREQUENCY_CAP_LIST) 62 .setKeyedFrequencyCapsForClickEvents( 63 KeyedFrequencyCapFixture.VALID_KEYED_FREQUENCY_CAP_LIST); 64 } 65 getFrequencyCapFiltersWithExcessiveNumFilters()66 public static FrequencyCapFilters getFrequencyCapFiltersWithExcessiveNumFilters() { 67 // Distribute the maximum number of filters across all event types 68 final int distributedNumFilters = FrequencyCapFilters.MAX_NUM_FREQUENCY_CAP_FILTERS / 4; 69 ImmutableList.Builder<KeyedFrequencyCap> listBuilder = ImmutableList.builder(); 70 for (int key = 0; key < distributedNumFilters; key++) { 71 listBuilder.add( 72 KeyedFrequencyCapFixture.getValidKeyedFrequencyCapBuilderOncePerDay(key) 73 .build()); 74 } 75 76 Parcel sourceParcel = Parcel.obtain(); 77 sourceParcel.writeTypedList(listBuilder.build()); 78 sourceParcel.writeTypedList(listBuilder.build()); 79 sourceParcel.writeTypedList(listBuilder.build()); 80 81 // Add extra filters to the final list so that the total is exceeded 82 final int numExtraFiltersToExceed = 83 FrequencyCapFilters.MAX_NUM_FREQUENCY_CAP_FILTERS - (4 * distributedNumFilters) + 1; 84 for (int key = 0; key < numExtraFiltersToExceed; key++) { 85 listBuilder.add( 86 KeyedFrequencyCapFixture.getValidKeyedFrequencyCapBuilderOncePerDay(key) 87 .build()); 88 } 89 90 sourceParcel.writeTypedList(listBuilder.build()); 91 sourceParcel.setDataPosition(0); 92 93 return FrequencyCapFilters.CREATOR.createFromParcel(sourceParcel); 94 } 95 getFrequencyCapFiltersWithNullCaps()96 public static FrequencyCapFilters getFrequencyCapFiltersWithNullCaps() { 97 ArrayList<KeyedFrequencyCap> capList = new ArrayList<>(); 98 for (int key = 0; key < 3; key++) { 99 capList.add( 100 KeyedFrequencyCapFixture.getValidKeyedFrequencyCapBuilderOncePerDay(key) 101 .build()); 102 } 103 104 // Each list ends with null 105 capList.add(null); 106 107 Parcel sourceParcel = Parcel.obtain(); 108 sourceParcel.writeTypedList(capList); 109 sourceParcel.writeTypedList(capList); 110 sourceParcel.writeTypedList(capList); 111 sourceParcel.writeTypedList(capList); 112 sourceParcel.setDataPosition(0); 113 114 return FrequencyCapFilters.CREATOR.createFromParcel(sourceParcel); 115 } 116 } 117