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