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 package com.android.server.appsearch;
17 
18 import static com.android.internal.util.ConcurrentUtils.DIRECT_EXECUTOR;
19 
20 import static com.google.common.truth.Truth.assertThat;
21 
22 import android.provider.DeviceConfig;
23 
24 import com.android.modules.utils.testing.TestableDeviceConfig;
25 import com.android.server.appsearch.icing.proto.GetOptimizeInfoResultProto;
26 import com.android.server.appsearch.icing.proto.StatusProto;
27 
28 import org.junit.Rule;
29 import org.junit.Test;
30 
31 // This class tests the scenario time_optimize_threshold < min_time_optimize_threshold (which
32 // shouldn't be the case in an ideal world) as opposed to ServiceOptimizeStrategyTest which tests
33 // the scenario time_optimize_threshold > min_time_optimize_threshold.
34 public class MockingServiceOptimizeStrategyTest {
35     @Rule
36     public final TestableDeviceConfig.TestableDeviceConfigRule
37             mDeviceConfigRule = new TestableDeviceConfig.TestableDeviceConfigRule();
38 
39     @Test
testShouldNotOptimize_overOtherThresholds_underMinTimeThreshold()40     public void testShouldNotOptimize_overOtherThresholds_underMinTimeThreshold() {
41         // Create ServiceAppSearchConfig with min_time_optimize_threshold <
42         // time_optimize_threshold
43         DeviceConfig.setProperty(
44                 DeviceConfig.NAMESPACE_APPSEARCH,
45                 FrameworkServiceAppSearchConfig.KEY_BYTES_OPTIMIZE_THRESHOLD,
46                 Integer.toString(147147),
47                 false);
48         DeviceConfig.setProperty(
49                 DeviceConfig.NAMESPACE_APPSEARCH,
50                 FrameworkServiceAppSearchConfig.KEY_TIME_OPTIMIZE_THRESHOLD_MILLIS,
51                 Integer.toString(900),
52                 false);
53         DeviceConfig.setProperty(
54                 DeviceConfig.NAMESPACE_APPSEARCH,
55                 FrameworkServiceAppSearchConfig.KEY_DOC_COUNT_OPTIMIZE_THRESHOLD,
56                 Integer.toString(369369),
57                 false);
58         DeviceConfig.setProperty(
59                 DeviceConfig.NAMESPACE_APPSEARCH,
60                 FrameworkServiceAppSearchConfig.KEY_MIN_TIME_OPTIMIZE_THRESHOLD_MILLIS,
61                 Integer.toString(0),
62                 false);
63         ServiceAppSearchConfig appSearchConfig =
64                 FrameworkServiceAppSearchConfig.create(DIRECT_EXECUTOR);
65         ServiceOptimizeStrategy mServiceOptimizeStrategy =
66                 new ServiceOptimizeStrategy(appSearchConfig);
67         // Create optimizeInfo with all values above respective thresholds.
68         GetOptimizeInfoResultProto optimizeInfo =
69                 GetOptimizeInfoResultProto.newBuilder()
70                         .setTimeSinceLastOptimizeMs(
71                                 appSearchConfig.getCachedTimeOptimizeThresholdMs() + 1)
72                         .setEstimatedOptimizableBytes(
73                                 appSearchConfig.getCachedBytesOptimizeThreshold() + 1)
74                         .setOptimizableDocs(
75                                 appSearchConfig.getCachedDocCountOptimizeThreshold() + 1)
76                         .setStatus(StatusProto.newBuilder().setCode(StatusProto.Code.OK).build())
77                         .build();
78 
79         // Verify shouldOptimize() returns true when
80         // min_time_optimize_threshold(0) < time_optimize_threshold(900)
81         // < timeSinceLastOptimize(901)
82         assertThat(mServiceOptimizeStrategy.shouldOptimize(optimizeInfo)).isTrue();
83 
84         // Set min_time_optimize_threshold to a value greater than time_optimize_threshold
85         DeviceConfig.setProperty(
86                 DeviceConfig.NAMESPACE_APPSEARCH,
87                 FrameworkServiceAppSearchConfig.KEY_MIN_TIME_OPTIMIZE_THRESHOLD_MILLIS,
88                 Integer.toString(1000),
89                 false);
90 
91         // Verify shouldOptimize() returns false when
92         // min_time_optimize_threshold(1000) > timeSinceLastOptimize(901)
93         // > time_optimize_threshold(900)
94         assertThat(mServiceOptimizeStrategy.shouldOptimize(optimizeInfo)).isFalse();
95     }
96 }
97