Home | History | Annotate | Download | only in cts
      1 /*
      2  * Copyright (C) 2018 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 com.google.common.truth.Truth.assertThat;
     20 
     21 import static org.testng.Assert.assertThrows;
     22 
     23 import android.os.Bundle;
     24 import android.platform.test.annotations.AppModeFull;
     25 import android.service.autofill.CompositeUserData;
     26 import android.service.autofill.UserData;
     27 import android.util.ArrayMap;
     28 
     29 import com.google.common.base.Strings;
     30 
     31 import org.junit.Test;
     32 import org.junit.runner.RunWith;
     33 import org.mockito.junit.MockitoJUnitRunner;
     34 
     35 @RunWith(MockitoJUnitRunner.class)
     36 @AppModeFull(reason = "Unit test")
     37 public class CompositeUserDataTest {
     38 
     39     private final String mShortValue = Strings.repeat("k", UserData.getMinValueLength() - 1);
     40     private final String mLongValue = "LONG VALUE, Y U NO SHORTER"
     41             + Strings.repeat("?", UserData.getMaxValueLength());
     42     private final String mId = "4815162342";
     43     private final String mId2 = "4815162343";
     44     private final String mCategoryId = "id1";
     45     private final String mCategoryId2 = "id2";
     46     private final String mCategoryId3 = "id3";
     47     private final String mValue = mShortValue + "-1";
     48     private final String mValue2 = mShortValue + "-2";
     49     private final String mValue3 = mShortValue + "-3";
     50     private final String mValue4 = mShortValue + "-4";
     51     private final String mValue5 = mShortValue + "-5";
     52     private final String mAlgo = "algo";
     53     private final String mAlgo2 = "algo2";
     54     private final String mAlgo3 = "algo3";
     55     private final String mAlgo4 = "algo4";
     56 
     57     private final UserData mEmptyGenericUserData = new UserData.Builder(mId, mValue, mCategoryId)
     58             .build();
     59     private final UserData mLoadedGenericUserData = new UserData.Builder(mId, mValue, mCategoryId)
     60             .add(mValue2, mCategoryId2)
     61             .setFieldClassificationAlgorithm(mAlgo, createBundle(false))
     62             .setFieldClassificationAlgorithmForCategory(mCategoryId2, mAlgo2, createBundle(false))
     63             .build();
     64     private final UserData mEmptyPackageUserData = new UserData.Builder(mId2, mValue3, mCategoryId3)
     65             .build();
     66     private final UserData mLoadedPackageUserData = new UserData
     67             .Builder(mId2, mValue3, mCategoryId3)
     68             .add(mValue4, mCategoryId2)
     69             .setFieldClassificationAlgorithm(mAlgo3, createBundle(true))
     70             .setFieldClassificationAlgorithmForCategory(mCategoryId2, mAlgo4, createBundle(true))
     71             .build();
     72 
     73 
     74     @Test
     75     public void testMergeInvalid_bothNull() {
     76         assertThrows(NullPointerException.class, () -> new CompositeUserData(null, null));
     77     }
     78 
     79     @Test
     80     public void testMergeInvalid_nullPackageUserData() {
     81         assertThrows(NullPointerException.class,
     82                 () -> new CompositeUserData(mEmptyGenericUserData, null));
     83     }
     84 
     85     @Test
     86     public void testMerge_nullGenericUserData() {
     87         final CompositeUserData userData = new CompositeUserData(null, mEmptyPackageUserData);
     88 
     89         final String[] categoryIds = userData.getCategoryIds();
     90         assertThat(categoryIds.length).isEqualTo(1);
     91         assertThat(categoryIds[0]).isEqualTo(mCategoryId3);
     92 
     93         final String[] values = userData.getValues();
     94         assertThat(values.length).isEqualTo(1);
     95         assertThat(values[0]).isEqualTo(mValue3);
     96 
     97         assertThat(userData.getFieldClassificationAlgorithm()).isNull();
     98         assertThat(userData.getDefaultFieldClassificationArgs()).isNull();
     99     }
    100 
    101     @Test
    102     public void testMerge_bothEmpty() {
    103         final CompositeUserData userData = new CompositeUserData(mEmptyGenericUserData,
    104                 mEmptyPackageUserData);
    105 
    106         final String[] categoryIds = userData.getCategoryIds();
    107         assertThat(categoryIds.length).isEqualTo(2);
    108         assertThat(categoryIds[0]).isEqualTo(mCategoryId3);
    109         assertThat(categoryIds[1]).isEqualTo(mCategoryId);
    110 
    111         final String[] values = userData.getValues();
    112         assertThat(values.length).isEqualTo(2);
    113         assertThat(values[0]).isEqualTo(mValue3);
    114         assertThat(values[1]).isEqualTo(mValue);
    115 
    116         assertThat(userData.getFieldClassificationAlgorithm()).isNull();
    117         assertThat(userData.getDefaultFieldClassificationArgs()).isNull();
    118     }
    119 
    120     @Test
    121     public void testMerge_emptyGenericUserData() {
    122         final CompositeUserData userData = new CompositeUserData(mEmptyGenericUserData,
    123                 mLoadedPackageUserData);
    124 
    125         final String[] categoryIds = userData.getCategoryIds();
    126         assertThat(categoryIds.length).isEqualTo(3);
    127         assertThat(categoryIds[0]).isEqualTo(mCategoryId3);
    128         assertThat(categoryIds[1]).isEqualTo(mCategoryId2);
    129         assertThat(categoryIds[2]).isEqualTo(mCategoryId);
    130 
    131         final String[] values = userData.getValues();
    132         assertThat(values.length).isEqualTo(3);
    133         assertThat(values[0]).isEqualTo(mValue3);
    134         assertThat(values[1]).isEqualTo(mValue4);
    135         assertThat(values[2]).isEqualTo(mValue);
    136 
    137         assertThat(userData.getFieldClassificationAlgorithm()).isEqualTo(mAlgo3);
    138 
    139         final Bundle defaultArgs = userData.getDefaultFieldClassificationArgs();
    140         assertThat(defaultArgs).isNotNull();
    141         assertThat(defaultArgs.getBoolean("isPackage")).isTrue();
    142         assertThat(userData.getFieldClassificationAlgorithmForCategory(mCategoryId2))
    143                 .isEqualTo(mAlgo4);
    144 
    145         final ArrayMap<String, Bundle> args = userData.getFieldClassificationArgs();
    146         assertThat(args.size()).isEqualTo(1);
    147         assertThat(args.containsKey(mCategoryId2)).isTrue();
    148         assertThat(args.get(mCategoryId2)).isNotNull();
    149         assertThat(args.get(mCategoryId2).getBoolean("isPackage")).isTrue();
    150     }
    151 
    152     @Test
    153     public void testMerge_emptyPackageUserData() {
    154         final CompositeUserData userData = new CompositeUserData(mLoadedGenericUserData,
    155                 mEmptyPackageUserData);
    156 
    157         final String[] categoryIds = userData.getCategoryIds();
    158         assertThat(categoryIds.length).isEqualTo(3);
    159         assertThat(categoryIds[0]).isEqualTo(mCategoryId3);
    160         assertThat(categoryIds[1]).isEqualTo(mCategoryId);
    161         assertThat(categoryIds[2]).isEqualTo(mCategoryId2);
    162 
    163         final String[] values = userData.getValues();
    164         assertThat(values.length).isEqualTo(3);
    165         assertThat(values[0]).isEqualTo(mValue3);
    166         assertThat(values[1]).isEqualTo(mValue);
    167         assertThat(values[2]).isEqualTo(mValue2);
    168 
    169         assertThat(userData.getFieldClassificationAlgorithm()).isEqualTo(mAlgo);
    170 
    171         final Bundle defaultArgs = userData.getDefaultFieldClassificationArgs();
    172         assertThat(defaultArgs).isNotNull();
    173         assertThat(defaultArgs.getBoolean("isPackage")).isFalse();
    174         assertThat(userData.getFieldClassificationAlgorithmForCategory(mCategoryId2))
    175                 .isEqualTo(mAlgo2);
    176 
    177         final ArrayMap<String, Bundle> args = userData.getFieldClassificationArgs();
    178         assertThat(args.size()).isEqualTo(1);
    179         assertThat(args.containsKey(mCategoryId2)).isTrue();
    180         assertThat(args.get(mCategoryId2)).isNotNull();
    181         assertThat(args.get(mCategoryId2).getBoolean("isPackage")).isFalse();
    182     }
    183 
    184 
    185     @Test
    186     public void testMerge_bothHaveData() {
    187         final CompositeUserData userData = new CompositeUserData(mLoadedGenericUserData,
    188                 mLoadedPackageUserData);
    189 
    190         final String[] categoryIds = userData.getCategoryIds();
    191         assertThat(categoryIds.length).isEqualTo(3);
    192         assertThat(categoryIds[0]).isEqualTo(mCategoryId3);
    193         assertThat(categoryIds[1]).isEqualTo(mCategoryId2);
    194         assertThat(categoryIds[2]).isEqualTo(mCategoryId);
    195 
    196         final String[] values = userData.getValues();
    197         assertThat(values.length).isEqualTo(3);
    198         assertThat(values[0]).isEqualTo(mValue3);
    199         assertThat(values[1]).isEqualTo(mValue4);
    200         assertThat(values[2]).isEqualTo(mValue);
    201 
    202         assertThat(userData.getFieldClassificationAlgorithm()).isEqualTo(mAlgo3);
    203         assertThat(userData.getDefaultFieldClassificationArgs()).isNotNull();
    204         assertThat(userData.getFieldClassificationAlgorithmForCategory(mCategoryId2))
    205                 .isEqualTo(mAlgo4);
    206 
    207         final Bundle defaultArgs = userData.getDefaultFieldClassificationArgs();
    208         assertThat(defaultArgs).isNotNull();
    209         assertThat(defaultArgs.getBoolean("isPackage")).isTrue();
    210         assertThat(userData.getFieldClassificationAlgorithmForCategory(mCategoryId2))
    211                 .isEqualTo(mAlgo4);
    212 
    213         final ArrayMap<String, Bundle> args = userData.getFieldClassificationArgs();
    214         assertThat(args.size()).isEqualTo(1);
    215         assertThat(args.containsKey(mCategoryId2)).isTrue();
    216         assertThat(args.get(mCategoryId2)).isNotNull();
    217         assertThat(args.get(mCategoryId2).getBoolean("isPackage")).isTrue();
    218     }
    219 
    220     private Bundle createBundle(Boolean isPackageBundle) {
    221         final Bundle bundle = new Bundle();
    222         bundle.putBoolean("isPackage", isPackageBundle);
    223         return bundle;
    224     }
    225 }
    226