summaryrefslogtreecommitdiff
path: root/adservices/tests/unittest/service-core/src/com/android/adservices/service/topics/classifier/CommonClassifierHelperTest.java
blob: 010a69dfb5f5ec1d24b293045659de2b34fd9962 (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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
/*
 * 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.topics.classifier;

import static com.android.adservices.service.topics.classifier.CommonClassifierHelper.computeClassifierAssetChecksum;
import static com.android.adservices.service.topics.classifier.CommonClassifierHelper.getTopTopics;

import static com.google.common.truth.Truth.assertThat;

import static org.junit.Assert.assertThrows;

import android.content.Context;

import androidx.test.core.app.ApplicationProvider;

import com.android.adservices.MockRandom;
import com.android.adservices.data.topics.Topic;

import com.google.android.libraries.mobiledatadownload.file.SynchronousFileStorage;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.mobiledatadownload.ClientConfigProto.ClientFile;

import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.stream.Collectors;

/**
 * Tests for {@link CommonClassifierHelper}.
 *
 * <p><b> Note: Some tests in this test class are depend on the ordering of topicIds in
 * adservices/tests/unittest/service-core/assets/classifier/labels_test_topics.txt, because we will
 * use Random() or MockRandom() to generate random integer index to get random topicIds. Topics will
 * be selected from the topics list in order by their index in the topics list. </b>
 */
public class CommonClassifierHelperTest {
    private static final Context sContext = ApplicationProvider.getApplicationContext();
    private static final String TEST_LABELS_FILE_PATH = "classifier/labels_test_topics.txt";
    private static final String TEST_PRECOMPUTED_FILE_PATH =
            "classifier/precomputed_test_app_list.csv";
    private static final String TEST_CLASSIFIER_ASSETS_METADATA_PATH =
            "classifier/classifier_test_assets_metadata.json";
    private static final String PRODUCTION_LABELS_FILE_PATH = "classifier/labels_topics.txt";
    private static final String PRODUCTION_APPS_FILE_PATH = "classifier/precomputed_app_list.csv";
    private static final String PRODUCTION_CLASSIFIER_ASSETS_METADATA_PATH =
            "classifier/classifier_assets_metadata.json";
    private static final String BUNDLED_MODEL_FILE_PATH = "classifier/model.tflite";

    private ModelManager mTestModelManager;
    private ModelManager mProductionModelManager;
    private ImmutableList<Integer> testLabels;
    private ImmutableMap<String, ImmutableMap<String, String>> testClassifierAssetsMetadata;
    private long mTestTaxonomyVersion;
    private long mTestModelVersion;

    private ImmutableList<Integer> productionLabels;
    private ImmutableMap<String, ImmutableMap<String, String>> productionClassifierAssetsMetadata;
    private long mProductionTaxonomyVersion;
    private long mProductionModelVersion;

    @Mock SynchronousFileStorage mMockFileStorage;
    @Mock Map<String, ClientFile> mMockDownloadedFiles;

    @Before
    public void setUp() {
        MockitoAnnotations.initMocks(this);
        mTestModelManager =
                new ModelManager(
                        sContext,
                        TEST_LABELS_FILE_PATH,
                        TEST_PRECOMPUTED_FILE_PATH,
                        TEST_CLASSIFIER_ASSETS_METADATA_PATH,
                        BUNDLED_MODEL_FILE_PATH,
                        mMockFileStorage,
                        mMockDownloadedFiles);

        mProductionModelManager =
                new ModelManager(
                        sContext,
                        PRODUCTION_LABELS_FILE_PATH,
                        PRODUCTION_APPS_FILE_PATH,
                        PRODUCTION_CLASSIFIER_ASSETS_METADATA_PATH,
                        BUNDLED_MODEL_FILE_PATH,
                        mMockFileStorage,
                        mMockDownloadedFiles);

        testLabels = mTestModelManager.retrieveLabels();
        testClassifierAssetsMetadata = mTestModelManager.retrieveClassifierAssetsMetadata();
        mTestTaxonomyVersion =
                Long.parseLong(
                        testClassifierAssetsMetadata.get("labels_topics").get("asset_version"));
        mTestModelVersion =
                Long.parseLong(
                        testClassifierAssetsMetadata.get("tflite_model").get("asset_version"));

        productionLabels = mProductionModelManager.retrieveLabels();
        productionClassifierAssetsMetadata =
                mProductionModelManager.retrieveClassifierAssetsMetadata();
        mProductionTaxonomyVersion =
                Long.parseLong(
                        productionClassifierAssetsMetadata
                                .get("labels_topics")
                                .get("asset_version"));
        mProductionModelVersion =
                Long.parseLong(
                        productionClassifierAssetsMetadata
                                .get("tflite_model")
                                .get("asset_version"));
    }

    @Test
    public void testGetTopTopics_legalInput() {
        // construction the appTopics map so that when sorting by the number of occurrences,
        // the order of topics are:
        // topic1, topic2, topic3, topic4, topic5, ...,
        Map<String, List<Topic>> appTopics = new HashMap<>();
        appTopics.put("app1", getTestTopics(Arrays.asList(1, 2, 3, 4, 5)));
        appTopics.put("app2", getTestTopics(Arrays.asList(1, 2, 3, 4, 5)));
        appTopics.put("app3", getTestTopics(Arrays.asList(1, 2, 3, 4, 16)));
        appTopics.put("app4", getTestTopics(Arrays.asList(1, 2, 3, 13, 17)));
        appTopics.put("app5", getTestTopics(Arrays.asList(1, 2, 11, 14, 18)));
        appTopics.put("app6", getTestTopics(Arrays.asList(1, 10, 12, 15, 19)));

        // This test case should return top 5 topics from appTopics and 1 random topic
        List<Topic> testResponse =
                getTopTopics(
                        appTopics,
                        testLabels,
                        new Random(),
                        /* numberOfTopTopics = */ 5,
                        /* numberOfRandomTopics = */ 1);

        assertThat(testResponse.get(0)).isEqualTo(getTestTopic(1));
        assertThat(testResponse.get(1)).isEqualTo(getTestTopic(2));
        assertThat(testResponse.get(2)).isEqualTo(getTestTopic(3));
        assertThat(testResponse.get(3)).isEqualTo(getTestTopic(4));
        assertThat(testResponse.get(4)).isEqualTo(getTestTopic(5));
        // Check the random topic is not empty
        // The random topic is at the end
        assertThat(testResponse.get(5)).isNotNull();
    }

    @Test
    public void testGetTopTopics_largeTopTopicsInput() {
        Map<String, List<Topic>> appTopics = new HashMap<>();
        appTopics.put("app1", getTestTopics(Arrays.asList(1, 2, 3, 4, 5)));

        // We only have 5 topics but requesting for 15 topics,
        // so we will pad them with 10 random topics.
        List<Topic> testResponse =
                getTopTopics(
                        appTopics,
                        testLabels,
                        new Random(),
                        /* numberOfTopTopics = */ 15,
                        /* numberOfRandomTopics = */ 1);

        // The response body should contain 11 topics.
        assertThat(testResponse.size()).isEqualTo(16);
    }

    @Test
    public void testGetTopTopics_zeroTopTopics() {
        Map<String, List<Topic>> appTopics = new HashMap<>();
        appTopics.put("app1", getTestTopics(Arrays.asList(1, 2, 3, 4, 5)));

        // This test case should throw an IllegalArgumentException if numberOfTopTopics is 0.
        assertThrows(
                IllegalArgumentException.class,
                () ->
                        getTopTopics(
                                appTopics,
                                testLabels,
                                new Random(),
                                /* numberOfTopTopics = */ 0,
                                /* numberOfRandomTopics = */ 1));
    }

    @Test
    public void testGetTopTopics_zeroRandomTopics() {
        Map<String, List<Topic>> appTopics = new HashMap<>();
        appTopics.put("app1", getTestTopics(Arrays.asList(1, 2, 3, 4, 5)));
        // This test case should throw an IllegalArgumentException if numberOfRandomTopics is 0.
        assertThrows(
                IllegalArgumentException.class,
                () ->
                        getTopTopics(
                                appTopics,
                                testLabels,
                                new Random(),
                                /* numberOfTopTopics = */ 3,
                                /* numberOfRandomTopics = */ 0));
    }

    @Test
    public void testGetTopTopics_negativeTopTopics() {
        Map<String, List<Topic>> appTopics = new HashMap<>();
        appTopics.put("app1", getTestTopics(Arrays.asList(1, 2, 3, 4, 5)));

        // This test case should throw an IllegalArgumentException if numberOfTopTopics is negative.
        assertThrows(
                IllegalArgumentException.class,
                () ->
                        getTopTopics(
                                appTopics,
                                testLabels,
                                new Random(),
                                /* numberOfTopTopics = */ -5,
                                /* numberOfRandomTopics = */ 1));
    }

    @Test
    public void testGetTopTopics_negativeRandomTopics() {
        Map<String, List<Topic>> appTopics = new HashMap<>();
        appTopics.put("app1", getTestTopics(Arrays.asList(1, 2, 3, 4, 5)));

        // This test case should throw an IllegalArgumentException
        // if numberOfRandomTopics is negative.
        assertThrows(
                IllegalArgumentException.class,
                () ->
                        getTopTopics(
                                appTopics,
                                testLabels,
                                new Random(),
                                /* numberOfTopTopics = */ 3,
                                /* numberOfRandomTopics = */ -1));
    }

    @Test
    public void testGetTopTopics_emptyAppTopicsMap() {
        Map<String, List<Topic>> appTopics = new HashMap<>();

        // The device does not have an app, an empty top topics list should be returned.
        List<Topic> testResponse =
                getTopTopics(
                        appTopics,
                        testLabels,
                        new Random(),
                        /* numberOfTopTopics = */ 5,
                        /* numberOfRandomTopics = */ 1);

        // The response body should be empty.
        assertThat(testResponse).isEmpty();
    }

    @Test
    public void testGetTopTopics_emptyTopicInEachApp() {
        Map<String, List<Topic>> appTopics = new HashMap<>();

        // app1 and app2 do not have any classification topics.
        appTopics.put("app1", new ArrayList<>());
        appTopics.put("app2", new ArrayList<>());

        // The device have some apps but the topic corresponding to the app cannot be obtained.
        // In this test case, an empty top topics list should be returned.
        List<Topic> testResponse =
                getTopTopics(
                        appTopics,
                        testLabels,
                        new Random(),
                        /* numberOfTopTopics = */ 5,
                        /* numberOfRandomTopics = */ 1);

        // The response body should be empty
        assertThat(testResponse).isEmpty();
    }

    @Test
    public void testGetTopTopics_selectSingleRandomTopic() {
        // In this test, in order to make test result to be deterministic so CommonClassifierHelper
        // has to be mocked to get a random topic. However, real CommonClassifierHelper need to
        // be tested as well. Therefore, real methods will be called for the other top topics.
        //
        // Initialize MockRandom. Append 3 random positive integers (20, 100, 300) to MockRandom
        // array,
        // their corresponding topicIds in the topics list will not overlap with
        // the topicIds of app1 below.
        MockRandom mockRandom = new MockRandom(new long[] {20, 100, 300});

        Map<String, List<Topic>> testAppTopics = new HashMap<>();
        // We label app1 with the first 5 topics in topics list.
        testAppTopics.put("app1", getTestTopics(Arrays.asList(253, 146, 277, 59, 127)));

        // Test the random topic with labels file in test assets.
        List<Topic> testResponse =
                getTopTopics(
                        testAppTopics,
                        testLabels,
                        mockRandom,
                        /* numberOfTopTopics = */ 5,
                        /* numberOfRandomTopics = */ 1);

        // The response body should contain 5 topics + 1 random topic.
        assertThat(testResponse.size()).isEqualTo(6);

        // In the following test, we need to verify that the mock random integer index
        // can match the correct topic in classifier/precomputed_test_app_list_chrome_topics.csv.
        // "random = n, topicId = m" means this topicId m is from the nth (0-indexed)
        // topicId in the topics list.
        // random = 20, topicId = 10021
        assertThat(testResponse.get(5)).isEqualTo(getTestTopic(10021));

        Map<String, List<Topic>> productionAppTopics = new HashMap<>();
        // We label app1 with the same topic IDs as testAppTopics, but using production metadata.
        productionAppTopics.put("app1", getProductionTopics(Arrays.asList(253, 146, 277, 59, 127)));

        // Test the random topic with labels file in production assets.
        List<Topic> productionResponse =
                getTopTopics(
                        productionAppTopics,
                        productionLabels,
                        new MockRandom(new long[] {50, 100, 300}),
                        /* numberOfTopTopics = */ 5,
                        /* numberOfRandomTopics = */ 1);

        // The response body should contain 5 topics + 1 random topic.
        assertThat(productionResponse.size()).isEqualTo(6);

        // In the following test, we need to verify that the mock random integer index
        // can match the correct topic in classifier/precomputed_app_list_chrome_topics.csv.
        // "random = n, topicId = m" means this topicId m is from the nth (0-indexed)
        // topicId in the topics list.
        // random = 50, topicId = 10051
        assertThat(productionResponse.get(5)).isEqualTo(getProductionTopic(10051));
    }

    @Test
    public void testGetTopTopics_selectMultipleRandomTopic() {
        // In this test, in order to make test result to be deterministic so CommonClassifierHelper
        // has to be mocked to get some random topics. However, real CommonClassifierHelper need to
        // be tested as well. Therefore, real methods will be called for the other top topics.
        //
        // Initialize MockRandom. Randomly select 7 indices in MockRandom, their corresponding
        // topicIds in the topics list
        // will not overlap with the topicIds of app1 below. 500 in MockRandom exceeds the length
        // of topics list, so what it represents should be 151st (500 % 349 = 151) topicId
        // in topics list.
        MockRandom mockRandom = new MockRandom(new long[] {10, 20, 50, 75, 100, 300, 500});

        Map<String, List<Topic>> appTopics = new HashMap<>();
        // The topicId we use is verticals4 and its index range is from 0 to 1918.
        // We label app1 with the first 5 topicIds in topics list.
        appTopics.put("app1", getTestTopics(Arrays.asList(34, 89, 69, 349, 241)));

        List<Topic> testResponse =
                getTopTopics(
                        appTopics,
                        testLabels,
                        mockRandom,
                        /* numberOfTopTopics = */ 5,
                        /* numberOfRandomTopics = */ 7);

        // The response body should contain 5 topics + 7 random topic.
        assertThat(testResponse.size()).isEqualTo(12);

        // In the following tests, we need to verify that the mock random integer index
        // can match the correct topic in classifier/precomputed_test_app_list_chrome_topics.csv.
        // "random = n, topicId = m" means this topicId m is from the nth (0-indexed)
        // topicId in the topics list.
        // random = 10, topicId = 10011
        assertThat(testResponse.get(5)).isEqualTo(getTestTopic(10011));

        // random = 20, topicId = 10021
        assertThat(testResponse.get(6)).isEqualTo(getTestTopic(10021));

        // random = 50, topicId = 10051
        assertThat(testResponse.get(7)).isEqualTo(getTestTopic(10051));

        // random = 75, topicId = 10076
        assertThat(testResponse.get(8)).isEqualTo(getTestTopic(10076));

        // random = 100, topicId = 10101
        assertThat(testResponse.get(9)).isEqualTo(getTestTopic(10101));

        // random = 300, topicId = 10301
        assertThat(testResponse.get(10)).isEqualTo(getTestTopic(10301));

        // random = 500, size of labels list is 446,
        // index should be 500 % 446 = 54, topicId = 10055
        assertThat(testResponse.get(11)).isEqualTo(getTestTopic(10055));
    }

    @Test
    public void testGetTopTopics_selectDuplicateRandomTopic() {
        // In this test, in order to make test result to be deterministic so CommonClassifierHelper
        // has to be mocked to get a random topic. However, real CommonClassifierHelper need to
        // be tested as well. Therefore, real methods will be called for the other top topics.
        //
        // Initialize MockRandom. Randomly select 6 indices in MockRandom, their first 5
        // corresponding topicIds
        // in the topics list will overlap with the topicIds of app1 below.
        MockRandom mockRandom = new MockRandom(new long[] {1, 5, 10, 25, 100, 300});

        Map<String, List<Topic>> appTopics = new HashMap<>();

        // If the random topic duplicates with the real topic, then pick another random
        // one until no duplicates. In this test, we will let app1 have five topicIds of
        // 2, 6, 11, 26, 101. These topicIds are the same as the topicIds in the
        // classifier/precomputed_test_app_list_chrome_topics.csv corresponding to
        // the first five indices in the MockRandomArray.
        appTopics.put("app1", getTestTopics(Arrays.asList(2, 6, 11, 26, 101)));

        List<Topic> testResponse =
                getTopTopics(
                        appTopics,
                        testLabels,
                        mockRandom,
                        /* numberOfTopTopics = */ 5,
                        /* numberOfRandomTopics = */ 1);

        // The response body should contain 5 topics + 1 random topic
        assertThat(testResponse.size()).isEqualTo(6);

        // In the following tests, we need to verify that the mock random integer index
        // can match the correct topic in classifier/precomputed_test_app_list_chrome_topics.csv.
        // "random = n, topicId = m" means this topicId m is from the nth (0-indexed)
        // topicId in the topics list.
        // In this test, if we want to select a random topic that does not repeat,
        // we should select the one corresponding to the sixth index
        // in the MockRandom array topicId, i.e. random = 1, topicId = 10002
        assertThat(testResponse.get(5)).isEqualTo(getTestTopic(10002));
    }

    @Test
    public void testComputeTestAssetChecksum() {
        // Compute SHA256 checksum of labels topics file in test assets and check the result
        // can match the checksum saved in the test classifier assets metadata file.
        String labelsTestTopicsChecksum =
                computeClassifierAssetChecksum(sContext.getAssets(), TEST_LABELS_FILE_PATH);
        assertThat(labelsTestTopicsChecksum)
                .isEqualTo(testClassifierAssetsMetadata.get("labels_topics").get("checksum"));

        // Compute SHA256 checksum of precomputed apps topics file in test assets
        // and check the result can match the checksum saved in the classifier assets metadata file.
        String precomputedAppsTestChecksum =
                computeClassifierAssetChecksum(sContext.getAssets(), TEST_PRECOMPUTED_FILE_PATH);
        assertThat(precomputedAppsTestChecksum)
                .isEqualTo(
                        testClassifierAssetsMetadata.get("precomputed_app_list").get("checksum"));
    }

    @Test
    public void testComputeProductionAssetChecksum() {
        // Compute SHA256 checksum of labels topics file in production assets and check the result
        // can match the checksum saved in the production classifier assets metadata file.
        String labelsProductionTopicsChecksum =
                computeClassifierAssetChecksum(sContext.getAssets(), PRODUCTION_LABELS_FILE_PATH);
        assertThat(labelsProductionTopicsChecksum)
                .isEqualTo(productionClassifierAssetsMetadata.get("labels_topics").get("checksum"));

        // Compute SHA256 checksum of precomputed apps topics file in production assets
        // and check the result can match the checksum saved in the classifier assets metadata file.
        String precomputedAppsProductionChecksum =
                computeClassifierAssetChecksum(sContext.getAssets(), PRODUCTION_APPS_FILE_PATH);
        assertThat(precomputedAppsProductionChecksum)
                .isEqualTo(
                        productionClassifierAssetsMetadata
                                .get("precomputed_app_list")
                                .get("checksum"));
    }

    private Topic getTestTopic(int topicId) {
        return Topic.create(topicId, mTestTaxonomyVersion, mTestModelVersion);
    }

    private List<Topic> getTestTopics(List<Integer> topicIds) {
        return topicIds.stream().map(this::getTestTopic).collect(Collectors.toList());
    }

    private Topic getProductionTopic(int topicId) {
        return Topic.create(topicId, mProductionTaxonomyVersion, mProductionModelVersion);
    }

    private List<Topic> getProductionTopics(List<Integer> topicIds) {
        return topicIds.stream().map(this::getProductionTopic).collect(Collectors.toList());
    }
}