summaryrefslogtreecommitdiff
path: root/adservices/tests/unittest/service-core/src/com/android/adservices/service/measurement/TestObjectProvider.java
blob: 07974cfb43e73fafea7c7987a70dfa53d13ab555 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/*
 * Copyright (C) 2022 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.adservices.service.measurement;

import static org.mockito.Mockito.any;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.spy;

import android.annotation.IntDef;
import android.test.mock.MockContentResolver;

import com.android.adservices.data.measurement.DatastoreManager;
import com.android.adservices.service.Flags;
import com.android.adservices.service.measurement.attribution.AttributionJobHandlerWrapper;
import com.android.adservices.service.measurement.inputverification.ClickVerifier;
import com.android.adservices.service.measurement.registration.SourceFetcher;
import com.android.adservices.service.measurement.registration.TriggerFetcher;

import org.mockito.stubbing.Answer;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.Collections;
import java.util.concurrent.TimeUnit;

class TestObjectProvider {
    private static final long ONE_HOUR_IN_MILLIS = TimeUnit.HOURS.toMillis(1);

    @IntDef(value = {
            Type.DENOISED,
            Type.NOISY,
    })
    @Retention(RetentionPolicy.SOURCE)
    @interface Type {
        int DENOISED = 1;
        int NOISY = 2;
    }

    static AttributionJobHandlerWrapper getAttributionJobHandler(
            DatastoreManager datastoreManager) {
        return new AttributionJobHandlerWrapper(datastoreManager);
    }

    static MeasurementImpl getMeasurementImpl(
            @Type int type,
            DatastoreManager datastoreManager,
            SourceFetcher sourceFetcher,
            TriggerFetcher triggerFetcher,
            ClickVerifier clickVerifier,
            Flags flags) {
        if (type == Type.DENOISED) {
            MeasurementImpl measurementImpl =
                    spy(
                            new MeasurementImpl(
                                    null,
                                    new MockContentResolver(),
                                    datastoreManager,
                                    sourceFetcher,
                                    triggerFetcher,
                                    clickVerifier));
            // Disable Impression Noise
            doReturn(Collections.emptyList()).when(measurementImpl).generateFakeEventReports(any());
            return measurementImpl;
        } else if (type == Type.NOISY) {
            MeasurementImpl measurementImpl =
                    spy(
                            new MeasurementImpl(
                                    null,
                                    new MockContentResolver(),
                                    datastoreManager,
                                    sourceFetcher,
                                    triggerFetcher,
                                    clickVerifier));
            // Create impression noise with 100% probability
            Answer<?> answerSourceEventReports =
                    invocation -> {
                        Source source = invocation.getArgument(0);
                        source.setAttributionMode(Source.AttributionMode.FALSELY);
                        return Collections.singletonList(
                                new EventReport.Builder()
                                        .setSourceId(source.getEventId())
                                        .setReportTime(source.getExpiryTime() + ONE_HOUR_IN_MILLIS)
                                        .setTriggerData(0)
                                        .setAttributionDestination(source.getAppDestination())
                                        .setEnrollmentId(source.getEnrollmentId())
                                        .setTriggerTime(0)
                                        .setTriggerPriority(0L)
                                        .setTriggerDedupKey(null)
                                        .setSourceType(source.getSourceType())
                                        .setStatus(EventReport.Status.PENDING)
                                        .build());
                    };
            doAnswer(answerSourceEventReports)
                    .when(measurementImpl)
                    .generateFakeEventReports(any());
            return measurementImpl;
        }

        return new MeasurementImpl(
                null,
                new MockContentResolver(),
                datastoreManager,
                sourceFetcher,
                triggerFetcher,
                clickVerifier);
    }
}