Home | History | Annotate | Download | only in cts
      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 com.google.common.truth.Truth.assertThat;
     20 
     21 import static org.mockito.Mockito.mock;
     22 import static org.testng.Assert.assertThrows;
     23 
     24 import android.platform.test.annotations.AppModeFull;
     25 import android.service.autofill.BatchUpdates;
     26 import android.service.autofill.CustomDescription;
     27 import android.service.autofill.InternalOnClickAction;
     28 import android.service.autofill.InternalTransformation;
     29 import android.service.autofill.InternalValidator;
     30 import android.service.autofill.OnClickAction;
     31 import android.service.autofill.Transformation;
     32 import android.service.autofill.Validator;
     33 import android.util.SparseArray;
     34 import android.widget.RemoteViews;
     35 
     36 import androidx.test.ext.junit.runners.AndroidJUnit4;
     37 
     38 import org.junit.Test;
     39 import org.junit.runner.RunWith;
     40 
     41 @RunWith(AndroidJUnit4.class)
     42 @AppModeFull(reason = "Unit test")
     43 public class CustomDescriptionUnitTest {
     44 
     45     private final CustomDescription.Builder mBuilder =
     46             new CustomDescription.Builder(mock(RemoteViews.class));
     47     private final BatchUpdates mValidUpdate =
     48             new BatchUpdates.Builder().updateTemplate(mock(RemoteViews.class)).build();
     49     private final Transformation mValidTransformation = mock(InternalTransformation.class);
     50     private final Validator mValidCondition = mock(InternalValidator.class);
     51     private final OnClickAction mValidAction = mock(InternalOnClickAction.class);
     52 
     53     @Test
     54     public void testNullConstructor() {
     55         assertThrows(NullPointerException.class, () ->  new CustomDescription.Builder(null));
     56     }
     57 
     58     @Test
     59     public void testAddChild_null() {
     60         assertThrows(IllegalArgumentException.class, () ->  mBuilder.addChild(42, null));
     61     }
     62 
     63     @Test
     64     public void testAddChild_invalidImplementation() {
     65         assertThrows(IllegalArgumentException.class,
     66                 () ->  mBuilder.addChild(42, mock(Transformation.class)));
     67     }
     68 
     69     @Test
     70     public void testBatchUpdate_nullCondition() {
     71         assertThrows(IllegalArgumentException.class,
     72                 () ->  mBuilder.batchUpdate(null, mValidUpdate));
     73     }
     74 
     75     @Test
     76     public void testBatchUpdate_invalidImplementation() {
     77         assertThrows(IllegalArgumentException.class,
     78                 () ->  mBuilder.batchUpdate(mock(Validator.class), mValidUpdate));
     79     }
     80 
     81     @Test
     82     public void testBatchUpdate_nullUpdates() {
     83         assertThrows(NullPointerException.class,
     84                 () ->  mBuilder.batchUpdate(mValidCondition, null));
     85     }
     86 
     87     @Test
     88     public void testSetOnClickAction_null() {
     89         assertThrows(IllegalArgumentException.class, () ->  mBuilder.addOnClickAction(42, null));
     90     }
     91 
     92     @Test
     93     public void testSetOnClickAction_invalidImplementation() {
     94         assertThrows(IllegalArgumentException.class,
     95                 () -> mBuilder.addOnClickAction(42, mock(OnClickAction.class)));
     96     }
     97 
     98     @Test
     99     public void testSetOnClickAction_thereCanBeOnlyOne() {
    100         final CustomDescription customDescription = mBuilder
    101                 .addOnClickAction(42, mock(InternalOnClickAction.class))
    102                 .addOnClickAction(42, mValidAction)
    103                 .build();
    104         final SparseArray<InternalOnClickAction> actions = customDescription.getActions();
    105         assertThat(actions.size()).isEqualTo(1);
    106         assertThat(actions.keyAt(0)).isEqualTo(42);
    107         assertThat(actions.valueAt(0)).isSameAs(mValidAction);
    108     }
    109 
    110     @Test
    111     public void testBuild_valid() {
    112         new CustomDescription.Builder(mock(RemoteViews.class)).build();
    113         new CustomDescription.Builder(mock(RemoteViews.class))
    114             .addChild(108, mValidTransformation)
    115             .batchUpdate(mValidCondition, mValidUpdate)
    116             .addOnClickAction(42, mValidAction)
    117             .build();
    118     }
    119 
    120     @Test
    121     public void testNoMoreInteractionsAfterBuild() {
    122         mBuilder.build();
    123 
    124         assertThrows(IllegalStateException.class, () -> mBuilder.build());
    125         assertThrows(IllegalStateException.class,
    126                 () -> mBuilder.addChild(108, mValidTransformation));
    127         assertThrows(IllegalStateException.class,
    128                 () -> mBuilder.batchUpdate(mValidCondition, mValidUpdate));
    129         assertThrows(IllegalStateException.class,
    130                 () -> mBuilder.addOnClickAction(42, mValidAction));
    131     }
    132 }
    133