1 /* 2 * Copyright (C) 2021 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.google.common.truth.Truth.assertThat; 19 20 import android.app.appsearch.testutil.FakeAppSearchConfig; 21 22 import com.android.server.appsearch.icing.proto.GetOptimizeInfoResultProto; 23 import com.android.server.appsearch.icing.proto.StatusProto; 24 25 import org.junit.Test; 26 27 // NOTE: The tests in this class are based on the underlying assumption that 28 // time_optimize_threshold > min_time_optimize_threshold. This ensures that setting 29 // timeSinceLastOptimize to time_optimize_threshold - 1 does not make it lesser than 30 // min_time_optimize_threshold (otherwise shouldOptimize() would return false for test cases that 31 // check byteThreshold and docCountThreshold). 32 public class ServiceOptimizeStrategyTest { 33 ServiceAppSearchConfig mAppSearchConfig = new FakeAppSearchConfig(); 34 ServiceOptimizeStrategy mServiceOptimizeStrategy = 35 new ServiceOptimizeStrategy(mAppSearchConfig); 36 37 @Test testTimeOptimizeThreshold_isGreaterThan_minTimeOptimizeThreshold()38 public void testTimeOptimizeThreshold_isGreaterThan_minTimeOptimizeThreshold() { 39 assertThat(mAppSearchConfig.getCachedTimeOptimizeThresholdMs()) 40 .isGreaterThan(mAppSearchConfig.getCachedMinTimeOptimizeThresholdMs()); 41 } 42 43 @Test testShouldNotOptimize_underAllThresholds()44 public void testShouldNotOptimize_underAllThresholds() { 45 GetOptimizeInfoResultProto optimizeInfo = 46 GetOptimizeInfoResultProto.newBuilder() 47 .setTimeSinceLastOptimizeMs( 48 mAppSearchConfig.getCachedTimeOptimizeThresholdMs() - 1) 49 .setEstimatedOptimizableBytes( 50 mAppSearchConfig.getCachedBytesOptimizeThreshold() - 1) 51 .setOptimizableDocs( 52 mAppSearchConfig.getCachedDocCountOptimizeThreshold() - 1) 53 .setStatus(StatusProto.newBuilder().setCode(StatusProto.Code.OK).build()) 54 .build(); 55 assertThat(mServiceOptimizeStrategy.shouldOptimize(optimizeInfo)).isFalse(); 56 } 57 58 @Test testShouldOptimize_byteThreshold()59 public void testShouldOptimize_byteThreshold() { 60 GetOptimizeInfoResultProto optimizeInfo = 61 GetOptimizeInfoResultProto.newBuilder() 62 .setTimeSinceLastOptimizeMs( 63 mAppSearchConfig.getCachedTimeOptimizeThresholdMs() - 1) 64 .setEstimatedOptimizableBytes( 65 mAppSearchConfig.getCachedBytesOptimizeThreshold()) 66 .setOptimizableDocs( 67 mAppSearchConfig.getCachedDocCountOptimizeThreshold() - 1) 68 .setStatus(StatusProto.newBuilder().setCode(StatusProto.Code.OK).build()) 69 .build(); 70 assertThat(mServiceOptimizeStrategy.shouldOptimize(optimizeInfo)).isTrue(); 71 } 72 73 @Test testShouldOptimize_timeThreshold()74 public void testShouldOptimize_timeThreshold() { 75 GetOptimizeInfoResultProto optimizeInfo = 76 GetOptimizeInfoResultProto.newBuilder() 77 .setTimeSinceLastOptimizeMs( 78 mAppSearchConfig.getCachedTimeOptimizeThresholdMs()) 79 .setEstimatedOptimizableBytes( 80 mAppSearchConfig.getCachedBytesOptimizeThreshold() - 1) 81 .setOptimizableDocs( 82 mAppSearchConfig.getCachedDocCountOptimizeThreshold() - 1) 83 .setStatus(StatusProto.newBuilder().setCode(StatusProto.Code.OK).build()) 84 .build(); 85 assertThat(mServiceOptimizeStrategy.shouldOptimize(optimizeInfo)).isTrue(); 86 } 87 88 @Test testShouldOptimize_docCountThreshold()89 public void testShouldOptimize_docCountThreshold() { 90 GetOptimizeInfoResultProto optimizeInfo = 91 GetOptimizeInfoResultProto.newBuilder() 92 .setTimeSinceLastOptimizeMs( 93 mAppSearchConfig.getCachedTimeOptimizeThresholdMs() - 1) 94 .setEstimatedOptimizableBytes( 95 mAppSearchConfig.getCachedBytesOptimizeThreshold() - 1) 96 .setOptimizableDocs(mAppSearchConfig.getCachedDocCountOptimizeThreshold()) 97 .setStatus(StatusProto.newBuilder().setCode(StatusProto.Code.OK).build()) 98 .build(); 99 assertThat(mServiceOptimizeStrategy.shouldOptimize(optimizeInfo)).isTrue(); 100 } 101 102 @Test testShouldNotOptimize_underMinTimeThreshold()103 public void testShouldNotOptimize_underMinTimeThreshold() { 104 GetOptimizeInfoResultProto optimizeInfo = 105 GetOptimizeInfoResultProto.newBuilder() 106 .setTimeSinceLastOptimizeMs( 107 mAppSearchConfig.getCachedMinTimeOptimizeThresholdMs() - 1) 108 .setEstimatedOptimizableBytes( 109 mAppSearchConfig.getCachedBytesOptimizeThreshold()) 110 .setOptimizableDocs(mAppSearchConfig.getCachedDocCountOptimizeThreshold()) 111 .setStatus(StatusProto.newBuilder().setCode(StatusProto.Code.OK).build()) 112 .build(); 113 assertThat(mServiceOptimizeStrategy.shouldOptimize(optimizeInfo)).isFalse(); 114 } 115 } 116