diff options
Diffstat (limited to 'adservices/tests/cts/endtoends/topics/src/com/android/adservices/tests/cts/topics/TopicsManagerTest.java')
-rw-r--r-- | adservices/tests/cts/endtoends/topics/src/com/android/adservices/tests/cts/topics/TopicsManagerTest.java | 123 |
1 files changed, 86 insertions, 37 deletions
diff --git a/adservices/tests/cts/endtoends/topics/src/com/android/adservices/tests/cts/topics/TopicsManagerTest.java b/adservices/tests/cts/endtoends/topics/src/com/android/adservices/tests/cts/topics/TopicsManagerTest.java index 40dbe05a91..b0eaf7ddc6 100644 --- a/adservices/tests/cts/endtoends/topics/src/com/android/adservices/tests/cts/topics/TopicsManagerTest.java +++ b/adservices/tests/cts/endtoends/topics/src/com/android/adservices/tests/cts/topics/TopicsManagerTest.java @@ -55,6 +55,23 @@ public class TopicsManagerTest { // Default Epoch Period. private static final long TOPICS_EPOCH_JOB_PERIOD_MS = 7 * 86_400_000; // 7 days. + // Classifier test constants. + private static final int TEST_CLASSIFIER_NUMBER_OF_TOP_LABELS = 5; + // Each app is given topics with a confidence score between 0.0 to 1.0 float value. This + // denotes how confident are you that a particular topic t1 is related to the app x that is + // classified. + // Threshold value for classifier confidence set to 0 to allow all topics and avoid filtering. + private static final float TEST_CLASSIFIER_THRESHOLD = 0.0f; + // ON_DEVICE_CLASSIFIER + private static final int TEST_CLASSIFIER_TYPE = 1; + + // Classifier default constants. + private static final int DEFAULT_CLASSIFIER_NUMBER_OF_TOP_LABELS = 3; + // Threshold value for classifier confidence set back to the default. + private static final float DEFAULT_CLASSIFIER_THRESHOLD = 0.1f; + // PRECOMPUTED_THEN_ON_DEVICE_CLASSIFIER + private static final int DEFAULT_CLASSIFIER_TYPE = 3; + // Use 0 percent for random topic in the test so that we can verify the returned topic. private static final int TEST_TOPICS_PERCENTAGE_FOR_RANDOM_TOPIC = 0; private static final int TOPICS_PERCENTAGE_FOR_RANDOM_TOPIC = 5; @@ -72,16 +89,20 @@ public class TopicsManagerTest { // not be used for epoch retrieval. Thread.sleep(3 * TEST_EPOCH_JOB_PERIOD_MS); - overridingBeforeTest(); + overrideEpochPeriod(TEST_EPOCH_JOB_PERIOD_MS); + // We need to turn off random topic so that we can verify the returned topic. + overridePercentageForRandomTopic(TEST_TOPICS_PERCENTAGE_FOR_RANDOM_TOPIC); } @After public void teardown() { - overridingAfterTest(); + overrideEpochPeriod(TOPICS_EPOCH_JOB_PERIOD_MS); + overridePercentageForRandomTopic(TOPICS_PERCENTAGE_FOR_RANDOM_TOPIC); } @Test - public void testTopicsManager() throws Exception { + public void testTopicsManager_runDefaultClassifier() throws Exception { + // Default classifier uses the precomputed list first, then on-device classifier. // The Test App has 2 SDKs: sdk1 calls the Topics API and sdk2 does not. // Sdk1 calls the Topics API. AdvertisingTopicsClient advertisingTopicsClient1 = @@ -116,7 +137,7 @@ public class TopicsManagerTest { Topic topic = sdk1Result.getTopics().get(0); // topic is one of the 5 classification topics of the Test App. - assertThat(topic.getTopicId()).isIn(Arrays.asList(10147,10253,10175,10254,10333)); + assertThat(topic.getTopicId()).isIn(Arrays.asList(10147, 10253, 10175, 10254, 10333)); assertThat(topic.getModelVersion()).isAtLeast(1L); assertThat(topic.getTaxonomyVersion()).isAtLeast(1L); @@ -133,41 +154,78 @@ public class TopicsManagerTest { assertThat(sdk2Result2.getTopics()).isEmpty(); } - private void overridingBeforeTest() { - overridingAdservicesLoggingLevel("VERBOSE"); + @Test + public void testTopicsManager_runOnDeviceClassifier() throws Exception { + // Set classifier flag to use on-device classifier. + overrideClassifierType(TEST_CLASSIFIER_TYPE); + + // Set number of top labels returned by the on-device classifier to 5. + overrideClassifierNumberOfTopLabels(TEST_CLASSIFIER_NUMBER_OF_TOP_LABELS); + // Remove classifier threshold by setting it to 0. + overrideClassifierThreshold(TEST_CLASSIFIER_THRESHOLD); + + // The Test App has 1 SDK: sdk3 + // sdk3 calls the Topics API. + AdvertisingTopicsClient advertisingTopicsClient3 = + new AdvertisingTopicsClient.Builder() + .setContext(sContext) + .setSdkName("sdk3") + .setExecutor(CALLBACK_EXECUTOR) + .build(); - overrideDisableTopicsEnrollmentCheck("1"); - overrideEpochPeriod(TEST_EPOCH_JOB_PERIOD_MS); + // At beginning, Sdk3 receives no topic. + GetTopicsResponse sdk3Result = advertisingTopicsClient3.getTopics().get(); + assertThat(sdk3Result.getTopics()).isEmpty(); - // We need to turn off random topic so that we can verify the returned topic. - overridePercentageForRandomTopic(TEST_TOPICS_PERCENTAGE_FOR_RANDOM_TOPIC); + // Now force the Epoch Computation Job. This should be done in the same epoch for + // callersCanLearnMap to have the entry for processing. + forceEpochComputationJob(); - // We need to turn the Consent Manager into debug mode - overrideConsentManagerDebugMode(); + // Wait to the next epoch. We will not need to do this after we implement the fix in + // go/rb-topics-epoch-scheduling + Thread.sleep(TEST_EPOCH_JOB_PERIOD_MS); + + // Since the sdk3 called the Topics API in the previous Epoch, it should receive some topic. + sdk3Result = advertisingTopicsClient3.getTopics().get(); + assertThat(sdk3Result.getTopics()).isNotEmpty(); + + // We only have 5 topics classified by the on-device classifier. + // The app will be assigned one random topic from one of these 5 topics. + assertThat(sdk3Result.getTopics()).hasSize(1); + Topic topic = sdk3Result.getTopics().get(0); + + // Top 5 classifications for empty string with v2 model are [10230, 10253, 10227, 10250, + // 10257]. This is computed by running the model on the device for empty string. + // topic is one of the 5 classification topics of the Test App. + List<Integer> expectedTopTopicIds = Arrays.asList(10230, 10253, 10227, 10250, 10257); + assertThat(topic.getTopicId()).isIn(expectedTopTopicIds); + + assertThat(topic.getModelVersion()).isAtLeast(2L); + assertThat(topic.getTaxonomyVersion()).isAtLeast(2L); - // Turn off MDD to avoid model mismatching - disableMddBackgroundTasks(true); + // Set classifier flag back to default. + overrideClassifierType(DEFAULT_CLASSIFIER_TYPE); + + // Set number of top labels returned by the on-device classifier back to default. + overrideClassifierNumberOfTopLabels(DEFAULT_CLASSIFIER_NUMBER_OF_TOP_LABELS); + // Set classifier threshold back to default. + overrideClassifierThreshold(DEFAULT_CLASSIFIER_THRESHOLD); } - private void overridingAfterTest() { - overrideDisableTopicsEnrollmentCheck("0"); - overrideEpochPeriod(TOPICS_EPOCH_JOB_PERIOD_MS); - overridePercentageForRandomTopic(TOPICS_PERCENTAGE_FOR_RANDOM_TOPIC); - disableMddBackgroundTasks(false); - overridingAdservicesLoggingLevel("INFO"); + // Override the flag to select classifier type. + private void overrideClassifierType(int val) { + ShellUtils.runShellCommand("device_config put adservices classifier_type " + val); } - // Switch on/off for MDD service. Default value is false, which means MDD is enabled. - private void disableMddBackgroundTasks(boolean isSwitchedOff) { + // Override the flag to change the number of top labels returned by on-device classifier type. + private void overrideClassifierNumberOfTopLabels(int val) { ShellUtils.runShellCommand( - "setprop debug.adservices.mdd_background_task_kill_switch " + isSwitchedOff); + "device_config put adservices classifier_number_of_top_labels " + val); } - // Override the flag to disable Topics enrollment check. - private void overrideDisableTopicsEnrollmentCheck(String val) { - // Setting it to 1 here disables the Topics' enrollment check. - ShellUtils.runShellCommand( - "setprop debug.adservices.disable_topics_enrollment_check " + val); + // Override the flag to change the threshold for the classifier. + private void overrideClassifierThreshold(float val) { + ShellUtils.runShellCommand("device_config put adservices classifier_threshold " + val); } // Override the Epoch Period to shorten the Epoch Length in the test. @@ -183,21 +241,12 @@ public class TopicsManagerTest { + overridePercentage); } - // Override the Consent Manager behaviour - Consent Given - private void overrideConsentManagerDebugMode() { - ShellUtils.runShellCommand("setprop debug.adservices.consent_manager_debug_mode true"); - } - /** Forces JobScheduler to run the Epoch Computation job */ private void forceEpochComputationJob() { ShellUtils.runShellCommand( "cmd jobscheduler run -f" + " " + ADSERVICES_PACKAGE_NAME + " " + EPOCH_JOB_ID); } - private void overridingAdservicesLoggingLevel(String loggingLevel) { - ShellUtils.runShellCommand("setprop log.tag.adservices %s", loggingLevel); - } - // Used to get the package name. Copied over from com.android.adservices.AndroidServiceBinder private static String getAdServicesPackageName() { final Intent intent = new Intent(TOPICS_SERVICE_NAME); |