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 package android.autofillservice.cts;
     17 
     18 import static android.autofillservice.cts.AbstractDatePickerActivity.ID_DATE_PICKER;
     19 import static android.autofillservice.cts.AbstractDatePickerActivity.ID_OUTPUT;
     20 import static android.autofillservice.cts.Helper.assertDateValue;
     21 import static android.autofillservice.cts.Helper.assertNumberOfChildren;
     22 import static android.autofillservice.cts.Helper.assertTextAndValue;
     23 import static android.autofillservice.cts.Helper.assertTextIsSanitized;
     24 import static android.autofillservice.cts.Helper.findNodeByResourceId;
     25 import static android.service.autofill.SaveInfo.SAVE_DATA_TYPE_GENERIC;
     26 
     27 import static com.google.common.truth.Truth.assertWithMessage;
     28 
     29 import android.autofillservice.cts.CannedFillResponse.CannedDataset;
     30 import android.autofillservice.cts.InstrumentedAutoFillService.FillRequest;
     31 import android.autofillservice.cts.InstrumentedAutoFillService.SaveRequest;
     32 import android.icu.util.Calendar;
     33 
     34 import org.junit.Test;
     35 
     36 /**
     37  * Base class for {@link AbstractDatePickerActivity} tests.
     38  */
     39 abstract class DatePickerTestCase<T extends AbstractDatePickerActivity>
     40         extends AutoFillServiceTestCase {
     41 
     42     protected abstract T getDatePickerActivity();
     43 
     44     @Test
     45     public void testAutoFillAndSave() throws Exception {
     46         final T activity = getDatePickerActivity();
     47 
     48         // Set service.
     49         enableService();
     50 
     51         // Set expectations.
     52         final Calendar cal = Calendar.getInstance();
     53         cal.set(Calendar.YEAR, 2012);
     54         cal.set(Calendar.MONTH, Calendar.DECEMBER);
     55         cal.set(Calendar.DAY_OF_MONTH, 20);
     56 
     57         sReplier.addResponse(new CannedFillResponse.Builder()
     58                 .addDataset(new CannedDataset.Builder()
     59                     .setPresentation(createPresentation("The end of the world"))
     60                     .setField(ID_OUTPUT, "Y U NO CHANGE ME?")
     61                     .setField(ID_DATE_PICKER, cal.getTimeInMillis())
     62                     .build())
     63                 .setRequiredSavableIds(SAVE_DATA_TYPE_GENERIC, ID_OUTPUT, ID_DATE_PICKER)
     64                 .build());
     65         activity.expectAutoFill("2012/11/20", 2012, Calendar.DECEMBER, 20);
     66 
     67         // Trigger auto-fill.
     68         activity.onOutput((v) -> v.requestFocus());
     69         final FillRequest fillRequest = sReplier.getNextFillRequest();
     70 
     71         // Assert properties of DatePicker field.
     72         assertTextIsSanitized(fillRequest.structure, ID_DATE_PICKER);
     73         assertNumberOfChildren(fillRequest.structure, ID_DATE_PICKER, 0);
     74 
     75         // Auto-fill it.
     76         mUiBot.selectDataset("The end of the world");
     77 
     78         // Check the results.
     79         activity.assertAutoFilled();
     80 
     81         // Trigger save.
     82         activity.setDate(2010, Calendar.DECEMBER, 12);
     83         activity.tapOk();
     84 
     85         mUiBot.saveForAutofill(true, SAVE_DATA_TYPE_GENERIC);
     86         final SaveRequest saveRequest = sReplier.getNextSaveRequest();
     87         assertWithMessage("onSave() not called").that(saveRequest).isNotNull();
     88 
     89         // Assert sanitization on save: everything should be available!
     90         assertDateValue(findNodeByResourceId(saveRequest.structure, ID_DATE_PICKER), 2010, 11, 12);
     91         assertTextAndValue(findNodeByResourceId(saveRequest.structure, ID_OUTPUT), "2010/11/12");
     92     }
     93 }
     94