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 17 package android.autofillservice.cts.servicebehavior; 18 19 import static android.service.autofill.SavedDatasetsInfo.TYPE_OTHER; 20 import static android.service.autofill.SavedDatasetsInfo.TYPE_PASSWORDS; 21 import static android.service.autofill.SavedDatasetsInfoCallback.ERROR_NEEDS_USER_ACTION; 22 import static android.service.autofill.SavedDatasetsInfoCallback.ERROR_OTHER; 23 24 import static androidx.test.platform.app.InstrumentationRegistry.getInstrumentation; 25 26 import static com.android.compatibility.common.util.ShellUtils.runShellCommand; 27 28 import static com.google.common.truth.Truth.assertThat; 29 30 import static org.testng.Assert.assertThrows; 31 32 import android.autofillservice.cts.commontests.AutoFillServiceTestCase; 33 import android.autofillservice.cts.testcore.InstrumentedAutoFillService; 34 import android.platform.test.annotations.AppModeFull; 35 import android.service.autofill.SavedDatasetsInfo; 36 import android.service.autofill.SavedDatasetsInfoCallback; 37 import android.util.ArraySet; 38 39 import com.google.common.collect.Lists; 40 41 import org.junit.Before; 42 import org.junit.Test; 43 44 import java.util.Collections; 45 46 /** 47 * Tests for 48 * {@link android.service.autofill.AutofillService#onSavedDatasetsInfoRequest(SavedDatasetsInfoCallback)}. 49 */ 50 // TODO: Use a TestAPI to test this. 51 @AppModeFull(reason = "Tests the service only") 52 public class SavedDatasetsInfoTest extends AutoFillServiceTestCase.ManualActivityLaunch { 53 54 @Before before()55 public void before() throws Exception { 56 enableService(); 57 } 58 59 @Test testUnimplemented()60 public void testUnimplemented() throws Exception { 61 assertThat(executePasswordCountRequest()).isEqualTo("resultCode=1"); 62 } 63 64 @Test testError()65 public void testError() throws Exception { 66 InstrumentedAutoFillService.setSavedDatasetsInfoReplier( 67 (callback) -> callback.onError(ERROR_NEEDS_USER_ACTION)); 68 assertThat(executePasswordCountRequest()).isEqualTo("resultCode=1"); 69 70 InstrumentedAutoFillService.setSavedDatasetsInfoReplier( 71 (callback) -> callback.onError(ERROR_OTHER)); 72 assertThat(executePasswordCountRequest()).isEqualTo("resultCode=1"); 73 } 74 75 @Test testSuccess()76 public void testSuccess() throws Exception { 77 InstrumentedAutoFillService.setSavedDatasetsInfoReplier( 78 (callback) -> callback.onSuccess( 79 new ArraySet<>(Lists.newArrayList( 80 new SavedDatasetsInfo(TYPE_OTHER, 99), 81 new SavedDatasetsInfo(TYPE_PASSWORDS, 5))))); 82 assertThat(executePasswordCountRequest()).isEqualTo("resultCode=0\nvalue=5"); 83 } 84 85 @Test testSuccess_withoutValidCounts()86 public void testSuccess_withoutValidCounts() throws Exception { 87 InstrumentedAutoFillService.setSavedDatasetsInfoReplier( 88 (callback) -> callback.onSuccess(Collections.emptySet())); 89 assertThat(executePasswordCountRequest()).isEqualTo("resultCode=1"); 90 91 InstrumentedAutoFillService.setSavedDatasetsInfoReplier((callback) -> 92 assertThrows(() -> 93 callback.onSuccess( 94 Collections.singleton( 95 new SavedDatasetsInfo(TYPE_PASSWORDS, -10))))); 96 } 97 executePasswordCountRequest()98 private String executePasswordCountRequest() { 99 return runShellCommand( 100 "cmd autofill get saved-password-count " 101 + getInstrumentation().getTargetContext().getUserId()); 102 } 103 } 104