• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2024 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 com.android.ondevicepersonalization.services;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 
21 import static org.junit.Assert.assertEquals;
22 import static org.junit.Assert.assertTrue;
23 
24 import android.adservices.ondevicepersonalization.CalleeMetadata;
25 import android.adservices.ondevicepersonalization.OnDevicePersonalizationManager;
26 import android.adservices.ondevicepersonalization.aidl.IIsFeatureEnabledCallback;
27 
28 import com.android.dx.mockito.inline.extended.ExtendedMockito;
29 import com.android.modules.utils.testing.ExtendedMockitoRule;
30 import com.android.ondevicepersonalization.internal.util.LoggerFactory;
31 
32 import org.junit.Before;
33 import org.junit.Rule;
34 import org.junit.Test;
35 import org.junit.runner.RunWith;
36 import org.junit.runners.JUnit4;
37 import org.mockito.quality.Strictness;
38 
39 import java.util.HashMap;
40 import java.util.HashSet;
41 import java.util.Map;
42 import java.util.Set;
43 import java.util.concurrent.CountDownLatch;
44 import java.util.function.Supplier;
45 
46 @RunWith(JUnit4.class)
47 public class FeatureStatusManagerTest {
48     private static final LoggerFactory.Logger sLogger = LoggerFactory.getLogger();
49     private static final String TAG = FeatureStatusManagerTest.class.getSimpleName();
50     private static final long SERVICE_ENTRY_TIME = 100L;
51     private final CountDownLatch mLatch = new CountDownLatch(1);
52     private volatile boolean mCallbackSuccess;
53     private volatile int mResult;
54 
55     @Rule
56     public final ExtendedMockitoRule mExtendedMockitoRule =
57             new ExtendedMockitoRule.Builder(this)
58                     .spyStatic(FlagsFactory.class)
59                     .setStrictness(Strictness.LENIENT)
60                     .build();
61 
62     @Before
setUp()63     public void setUp() {
64         ExtendedMockito.doReturn(new TestFlags() {}).when(FlagsFactory::getFlags);
65     }
66 
67     @Test
testEnabledNonFlaggedFeature()68     public void testEnabledNonFlaggedFeature() {
69         Set<String> nonFlaggedFeatures = new HashSet<>();
70         nonFlaggedFeatures.add("featureName");
71         FeatureStatusManager featureStatusManager =
72                 new FeatureStatusManager(
73                         FlagsFactory.getFlags(),
74                         new HashMap<>(),
75                         nonFlaggedFeatures);
76         assertThat(featureStatusManager.isFeatureEnabled("featureName")).isEqualTo(
77                 OnDevicePersonalizationManager.FEATURE_ENABLED);
78     }
79 
80     @Test
testEnabledFlaggedFeature()81     public void testEnabledFlaggedFeature() {
82         Map<String, Supplier<Boolean>> flaggedFeatures = new HashMap<>();
83 
84         flaggedFeatures.put("featureName", (new TestFlags() {})::getEnabledFeature);
85         FeatureStatusManager featureStatusManager =
86                 new FeatureStatusManager(
87                         FlagsFactory.getFlags(),
88                         flaggedFeatures,
89                         new HashSet<>());
90         assertThat(featureStatusManager.isFeatureEnabled("featureName")).isEqualTo(
91                 OnDevicePersonalizationManager.FEATURE_ENABLED);
92     }
93 
94     @Test
testDisabledFlaggedFeature()95     public void testDisabledFlaggedFeature() {
96         Map<String, Supplier<Boolean>> flaggedFeatures = new HashMap<>();
97 
98         flaggedFeatures.put("featureName", (new TestFlags() {})::getDisabledFeature);
99         FeatureStatusManager featureStatusManager =
100                 new FeatureStatusManager(
101                         FlagsFactory.getFlags(),
102                         flaggedFeatures,
103                         new HashSet<>());
104         assertThat(featureStatusManager.isFeatureEnabled("featureName")).isEqualTo(
105                 OnDevicePersonalizationManager.FEATURE_DISABLED);
106     }
107 
108     @Test
testUnsupportedFeature()109     public void testUnsupportedFeature() {
110         FeatureStatusManager featureStatusManager = new FeatureStatusManager(
111                 FlagsFactory.getFlags(),
112                 new HashMap<>(),
113                 new HashSet<>());
114         assertThat(featureStatusManager.isFeatureEnabled("featureName")).isEqualTo(
115                 OnDevicePersonalizationManager.FEATURE_UNSUPPORTED);
116     }
117 
118     @Test
testGetFeatureStatusAndSendResult()119     public void testGetFeatureStatusAndSendResult() throws InterruptedException {
120         FeatureStatusManager.getFeatureStatusAndSendResult(
121                 "featureName",
122                 SERVICE_ENTRY_TIME,
123                 new TestIsFeatureEnabledCallback());
124         mLatch.await();
125 
126         assertTrue(mCallbackSuccess);
127         assertEquals(mResult, OnDevicePersonalizationManager.FEATURE_UNSUPPORTED);
128     }
129 
130     class TestFlags implements Flags {
131 
getDisabledFeature()132         public boolean getDisabledFeature() {
133             return false;
134         }
135 
getEnabledFeature()136         public boolean getEnabledFeature() {
137             return true;
138         }
139     }
140 
141     class TestIsFeatureEnabledCallback extends IIsFeatureEnabledCallback.Stub {
142         @Override
onResult(int result, CalleeMetadata calleeMetadata)143         public void onResult(int result, CalleeMetadata calleeMetadata) {
144             sLogger.d(TAG + " : onResult callback.");
145             mCallbackSuccess = true;
146             mResult = result;
147             mLatch.countDown();
148         }
149     }
150 }
151