1 /* 2 * Copyright (C) 2017 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; 18 19 import static android.provider.Settings.Secure.AUTOFILL_USER_DATA_MAX_CATEGORY_COUNT; 20 import static android.provider.Settings.Secure.AUTOFILL_USER_DATA_MAX_FIELD_CLASSIFICATION_IDS_SIZE; 21 import static android.provider.Settings.Secure.AUTOFILL_USER_DATA_MAX_USER_DATA_SIZE; 22 import static android.provider.Settings.Secure.AUTOFILL_USER_DATA_MAX_VALUE_LENGTH; 23 import static android.provider.Settings.Secure.AUTOFILL_USER_DATA_MIN_VALUE_LENGTH; 24 25 import static com.google.common.truth.Truth.assertThat; 26 27 import static org.testng.Assert.assertThrows; 28 29 import android.autofillservice.cts.common.SettingsStateChangerRule; 30 import android.content.Context; 31 import android.platform.test.annotations.AppModeFull; 32 import android.service.autofill.UserData; 33 import android.support.test.InstrumentationRegistry; 34 35 import com.google.common.base.Strings; 36 37 import org.junit.Before; 38 import org.junit.ClassRule; 39 import org.junit.Test; 40 import org.junit.runner.RunWith; 41 import org.mockito.junit.MockitoJUnitRunner; 42 43 @RunWith(MockitoJUnitRunner.class) 44 @AppModeFull // Unit test 45 public class UserDataTest { 46 47 private static final Context sContext = InstrumentationRegistry.getContext(); 48 49 @ClassRule 50 public static final SettingsStateChangerRule sUserDataMaxFcSizeChanger = 51 new SettingsStateChangerRule(sContext, 52 AUTOFILL_USER_DATA_MAX_FIELD_CLASSIFICATION_IDS_SIZE, "10"); 53 54 @ClassRule 55 public static final SettingsStateChangerRule sUserDataMaxCategoriesSizeChanger = 56 new SettingsStateChangerRule(sContext, AUTOFILL_USER_DATA_MAX_CATEGORY_COUNT, "2"); 57 58 @ClassRule 59 public static final SettingsStateChangerRule sUserDataMaxUserSizeChanger = 60 new SettingsStateChangerRule(sContext, AUTOFILL_USER_DATA_MAX_USER_DATA_SIZE, "4"); 61 62 @ClassRule 63 public static final SettingsStateChangerRule sUserDataMinValueChanger = 64 new SettingsStateChangerRule(sContext, AUTOFILL_USER_DATA_MIN_VALUE_LENGTH, "4"); 65 66 @ClassRule 67 public static final SettingsStateChangerRule sUserDataMaxValueChanger = 68 new SettingsStateChangerRule(sContext, AUTOFILL_USER_DATA_MAX_VALUE_LENGTH, "50"); 69 70 71 private final String mShortValue = Strings.repeat("k", UserData.getMinValueLength() - 1); 72 private final String mLongValue = "LONG VALUE, Y U NO SHORTER" 73 + Strings.repeat("?", UserData.getMaxValueLength()); 74 private final String mId = "4815162342"; 75 private final String mCategoryId = "id1"; 76 private final String mCategoryId2 = "id2"; 77 private final String mCategoryId3 = "id3"; 78 private final String mValue = mShortValue + "-1"; 79 private final String mValue2 = mShortValue + "-2"; 80 private final String mValue3 = mShortValue + "-3"; 81 private final String mValue4 = mShortValue + "-4"; 82 private final String mValue5 = mShortValue + "-5"; 83 84 private UserData.Builder mBuilder; 85 86 @Before 87 public void setFixtures() { 88 mBuilder = new UserData.Builder(mId, mValue, mCategoryId); 89 } 90 91 @Test 92 public void testBuilder_invalid() { 93 assertThrows(NullPointerException.class, 94 () -> new UserData.Builder(null, mValue, mCategoryId)); 95 assertThrows(IllegalArgumentException.class, 96 () -> new UserData.Builder("", mValue, mCategoryId)); 97 assertThrows(NullPointerException.class, 98 () -> new UserData.Builder(mId, null, mCategoryId)); 99 assertThrows(IllegalArgumentException.class, 100 () -> new UserData.Builder(mId, "", mCategoryId)); 101 assertThrows(IllegalArgumentException.class, 102 () -> new UserData.Builder(mId, mShortValue, mCategoryId)); 103 assertThrows(IllegalArgumentException.class, 104 () -> new UserData.Builder(mId, mLongValue, mCategoryId)); 105 assertThrows(NullPointerException.class, () -> new UserData.Builder(mId, mValue, null)); 106 assertThrows(IllegalArgumentException.class, () -> new UserData.Builder(mId, mValue, "")); 107 } 108 109 @Test 110 public void testAdd_invalid() { 111 assertThrows(NullPointerException.class, () -> mBuilder.add(null, mCategoryId)); 112 assertThrows(IllegalArgumentException.class, () -> mBuilder.add("", mCategoryId)); 113 assertThrows(IllegalArgumentException.class, () -> mBuilder.add(mShortValue, mCategoryId)); 114 assertThrows(IllegalArgumentException.class, () -> mBuilder.add(mLongValue, mCategoryId)); 115 assertThrows(NullPointerException.class, () -> mBuilder.add(mValue, null)); 116 assertThrows(IllegalArgumentException.class, () -> mBuilder.add(mValue, "")); 117 } 118 119 @Test 120 public void testAdd_duplicatedValue() { 121 assertThrows(IllegalStateException.class, () -> mBuilder.add(mValue, mCategoryId)); 122 assertThrows(IllegalStateException.class, () -> mBuilder.add(mValue, mCategoryId2)); 123 } 124 125 @Test 126 public void testAdd_maximumCategoriesReached() { 127 // Max is 2; one was added in the constructor 128 mBuilder.add(mValue2, mCategoryId2); 129 assertThrows(IllegalStateException.class, () -> mBuilder.add(mValue3, mCategoryId3)); 130 } 131 132 @Test 133 public void testAdd_maximumUserDataReached() { 134 // Max is 4; one was added in the constructor 135 mBuilder.add(mValue2, mCategoryId); 136 mBuilder.add(mValue3, mCategoryId); 137 mBuilder.add(mValue4, mCategoryId2); 138 assertThrows(IllegalStateException.class, () -> mBuilder.add(mValue5, mCategoryId2)); 139 } 140 141 @Test 142 public void testSetFcAlgorithm() { 143 final UserData userData = mBuilder.setFieldClassificationAlgorithm("algo_mas", null) 144 .build(); 145 assertThat(userData.getFieldClassificationAlgorithm()).isEqualTo("algo_mas"); 146 } 147 148 @Test 149 public void testBuild_valid() { 150 final UserData userData = mBuilder.build(); 151 assertThat(userData).isNotNull(); 152 assertThat(userData.getId()).isEqualTo(mId); 153 assertThat(userData.getFieldClassificationAlgorithm()).isNull(); 154 } 155 156 @Test 157 public void testNoMoreInteractionsAfterBuild() { 158 testBuild_valid(); 159 160 assertThrows(IllegalStateException.class, () -> mBuilder.add(mValue, mCategoryId2)); 161 assertThrows(IllegalStateException.class, 162 () -> mBuilder.setFieldClassificationAlgorithm("algo_mas", null)); 163 assertThrows(IllegalStateException.class, () -> mBuilder.build()); 164 } 165 } 166