Home | History | Annotate | Download | only in cts
      1 /*
      2  * Copyright (C) 2011 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.holo.cts;
     18 
     19 import com.android.cts.holo.R;
     20 
     21 import android.holo.cts.modifiers.CalendarViewModifier;
     22 import android.holo.cts.modifiers.SearchViewModifier;
     23 import android.holo.cts.modifiers.TabHostModifier;
     24 import android.holo.cts.modifiers.TimePickerModifier;
     25 import android.view.LayoutInflater;
     26 import android.view.View;
     27 import android.view.ViewGroup;
     28 import android.widget.BaseAdapter;
     29 import android.widget.TextView;
     30 
     31 import java.util.ArrayList;
     32 import java.util.Calendar;
     33 import java.util.List;
     34 
     35 /**
     36  * {@link BaseAdapter} for all the layouts used to test the Holo theme.
     37  */
     38 class LayoutAdapter extends BaseAdapter {
     39 
     40     /** Mode where we are just viewing all the layouts. */
     41     static final int MODE_VIEWING = 0;
     42 
     43     /** Mode where we are testing and may not include some layouts based on the device state. */
     44     static final int MODE_TESTING = 1;
     45 
     46     /** No timeout for widgets in layouts that aren't changed after inflation.  */
     47     private static final int NO_TIMEOUT_MS = 0;
     48 
     49     /** Small timeout for letting things like button presses to be handled. */
     50     private static final int SHORT_TIMEOUT_MS = 500;
     51 
     52     /** Longer timeout for widgets that have one-time animations. */
     53     private static final int LONG_TIMEOUT_MS = 3000;
     54 
     55     static class LayoutInfo {
     56         private final int mDisplayName;
     57         private final String mFileName;
     58         private final int mLayout;
     59         private final LayoutModifier mModifier;
     60         private final long mTimeoutMs;
     61 
     62         private LayoutInfo(int displayName, String fileName, int layout, LayoutModifier modifier,
     63                 long timeoutMs) {
     64             mDisplayName = displayName;
     65             mFileName = fileName;
     66             mLayout = layout;
     67             mModifier = modifier;
     68             mTimeoutMs = timeoutMs;
     69         }
     70 
     71         /** @return file name to use for creating reference and failure bitmaps. */
     72         public String getBitmapName() {
     73             return mFileName;
     74         }
     75 
     76         /** @return layout that will be inflated and captured to a bitmap */
     77         public int getLayout() {
     78             return mLayout;
     79         }
     80 
     81         /** @return whether or non the layout has a layout modifier */
     82         public boolean hasModifier() {
     83             return mModifier != null;
     84         }
     85 
     86         /** @return null or modifier that will manipulate the layout after it is inflated */
     87         public LayoutModifier getModifier() {
     88             return mModifier;
     89         }
     90 
     91         /** @return timeout before taking a snapshot of the layout */
     92         public long getTimeoutMs() {
     93             return mTimeoutMs;
     94         }
     95     }
     96 
     97     private final List<LayoutInfo> mLayoutInfos = new ArrayList<LayoutInfo>();
     98 
     99     private final LayoutInflater mInflater;
    100 
    101     LayoutAdapter(LayoutInflater inflater, int adapterMode) {
    102         mInflater = inflater;
    103 
    104         // Widgets
    105 
    106         addLayout(R.string.button, "button",
    107                 R.layout.button, null, NO_TIMEOUT_MS);
    108 
    109 //        addLayout(R.string.button_pressed, "button_pressed",
    110 //                R.layout.button, new ViewPressedModifier(), LONG_TIMEOUT_MS);
    111 
    112         addCalendarLayouts(adapterMode);
    113 
    114         addLayout(R.string.checkbox, "checkbox",
    115                 R.layout.checkbox, null, NO_TIMEOUT_MS);
    116 
    117         addLayout(R.string.checkbox_checked, "checkbox_checked",
    118                 R.layout.checkbox_checked, null, SHORT_TIMEOUT_MS);
    119 
    120         addLayout(R.string.chronometer, "chronometer",
    121                 R.layout.chronometer, null, NO_TIMEOUT_MS);
    122 
    123         // TODO: DatePicker has a blinking cursor that can be captured in the bitmap...
    124         // addLayout(R.string.datepicker, "datepicker",
    125         //        R.layout.datepicker, new DatePickerModifier(), LONG_TIMEOUT_MS);
    126 
    127         addLayout(R.string.edittext, "edittext",
    128                 R.layout.edittext, null, NO_TIMEOUT_MS);
    129 
    130         addLayout(R.string.progressbar, "progressbar",
    131                 R.layout.progressbar, null, NO_TIMEOUT_MS);
    132 
    133         addLayout(R.string.progressbar_small, "progressbar_small",
    134                 R.layout.progressbar_small, null, NO_TIMEOUT_MS);
    135 
    136         addLayout(R.string.progressbar_large, "progressbar_large",
    137                 R.layout.progressbar_large, null, NO_TIMEOUT_MS);
    138 
    139         addLayout(R.string.progressbar_horizontal_0, "progressbar_horizontal_0",
    140                 R.layout.progressbar_horizontal_0, null, NO_TIMEOUT_MS);
    141 
    142         addLayout(R.string.progressbar_horizontal_50, "progressbar_horizontal_50",
    143                 R.layout.progressbar_horizontal_50, null, NO_TIMEOUT_MS);
    144 
    145         addLayout(R.string.progressbar_horizontal_100, "progressbar_horizontal_100",
    146                 R.layout.progressbar_horizontal_100, null, NO_TIMEOUT_MS);
    147 
    148         addLayout(R.string.radiobutton, "radio_button",
    149                 R.layout.radiobutton, null, NO_TIMEOUT_MS);
    150 
    151         addLayout(R.string.radiobutton_checked, "radio_button_checked",
    152                 R.layout.radiobutton_checked, null, NO_TIMEOUT_MS);
    153 
    154         addLayout(R.string.radiogroup_horizontal, "radiogroup_horizontal",
    155                 R.layout.radiogroup_horizontal, null, NO_TIMEOUT_MS);
    156 
    157         addLayout(R.string.radiogroup_vertical, "radiogroup_vertical",
    158                 R.layout.radiogroup_vertical, null, NO_TIMEOUT_MS);
    159 
    160         addLayout(R.string.ratingbar_0, "ratingbar_0",
    161                 R.layout.ratingbar_0, null, NO_TIMEOUT_MS);
    162 
    163         addLayout(R.string.ratingbar_2point5, "ratingbar_2point5",
    164                 R.layout.ratingbar_2point5, null, NO_TIMEOUT_MS);
    165 
    166         addLayout(R.string.ratingbar_5, "ratingbar_5",
    167                 R.layout.ratingbar_5, null, NO_TIMEOUT_MS);
    168 
    169 //        addLayout(R.string.ratingbar_0_pressed, "ratingbar_0_pressed",
    170 //                R.layout.ratingbar_0, new ViewPressedModifier(), LONG_TIMEOUT_MS);
    171 
    172 //        addLayout(R.string.ratingbar_2point5_pressed, "ratingbar_2point5_pressed",
    173 //                R.layout.ratingbar_2point5, new ViewPressedModifier(), LONG_TIMEOUT_MS);
    174 
    175 //        addLayout(R.string.ratingbar_5_pressed, "ratingbar_5_pressed",
    176 //                R.layout.ratingbar_5, new ViewPressedModifier(), LONG_TIMEOUT_MS);
    177 
    178         addLayout(R.string.searchview, "searchview",
    179                 R.layout.searchview, null, NO_TIMEOUT_MS);
    180 
    181         addLayout(R.string.searchview_query, "searchview_query",
    182                 R.layout.searchview, new SearchViewModifier(SearchViewModifier.QUERY),
    183                 SHORT_TIMEOUT_MS);
    184 
    185         addLayout(R.string.searchview_query_hint, "searchview_query_hint",
    186                 R.layout.searchview, new SearchViewModifier(SearchViewModifier.QUERY_HINT),
    187                 SHORT_TIMEOUT_MS);
    188 
    189         addLayout(R.string.seekbar_0, "seekbar_0",
    190                 R.layout.seekbar_0, null, NO_TIMEOUT_MS);
    191 
    192         addLayout(R.string.seekbar_50, "seekbar_50",
    193                 R.layout.seekbar_50, null, NO_TIMEOUT_MS);
    194 
    195         addLayout(R.string.seekbar_100, "seekbar_100",
    196                 R.layout.seekbar_100, null, NO_TIMEOUT_MS);
    197 
    198         addLayout(R.string.spinner, "spinner",
    199                 R.layout.spinner, null, NO_TIMEOUT_MS);
    200 
    201         addLayout(R.string.switch_button, "switch",
    202                 R.layout.switch_button, null, NO_TIMEOUT_MS);
    203 
    204         addLayout(R.string.switch_button_checked, "switch_checked",
    205                 R.layout.switch_button_checked, null, NO_TIMEOUT_MS);
    206 
    207         addLayout(R.string.tabhost, "tabhost",
    208                 R.layout.tabhost, new TabHostModifier(), SHORT_TIMEOUT_MS);
    209 
    210         addLayout(R.string.textview, "textview",
    211                 R.layout.textview, null, NO_TIMEOUT_MS);
    212 
    213         addLayout(R.string.timepicker, "timepicker",
    214                 R.layout.timepicker, new TimePickerModifier(), LONG_TIMEOUT_MS);
    215 
    216         addLayout(R.string.togglebutton, "toggle_button",
    217                 R.layout.togglebutton, null, NO_TIMEOUT_MS);
    218 
    219         addLayout(R.string.togglebutton_checked, "toggle_button_checked",
    220                 R.layout.togglebutton_checked, null, NO_TIMEOUT_MS);
    221 
    222 
    223         // TODO: Zoom control hasn't been styled for Holo so don't test them.
    224 
    225 //        addLayout(R.string.zoomcontrols, "zoomcontrols",
    226 //                R.layout.zoomcontrols, null, NO_TIMEOUT_MS);
    227 
    228         // Dialogs
    229 
    230 
    231         // TODO: Dialogs are changing sizes depending on screen sizes, so we can't test these.
    232 
    233 //        addLayout(R.string.alertdialog_onebutton, "alertdialog_onebutton", R.layout.empty,
    234 //                new DialogModifier(new AlertDialogBuilder(AlertDialogBuilder.ONE_BUTTON)),
    235 //                SHORT_TIMEOUT_MS);
    236 //
    237 //        addLayout(R.string.alertdialog_twobuttons, "alertdialog_twobuttons", R.layout.empty,
    238 //                new DialogModifier(new AlertDialogBuilder(AlertDialogBuilder.TWO_BUTTONS)),
    239 //                SHORT_TIMEOUT_MS);
    240 //
    241 //        addLayout(R.string.alertdialog_threebuttons, "alertdialog_threebuttons", R.layout.empty,
    242 //                new DialogModifier(new AlertDialogBuilder(AlertDialogBuilder.THREE_BUTTONS)),
    243 //                SHORT_TIMEOUT_MS);
    244 //
    245 //        addLayout(R.string.alertdialog_list, "alertdialog_list", R.layout.empty,
    246 //                new DialogModifier(new AlertDialogBuilder(AlertDialogBuilder.LIST)),
    247 //                SHORT_TIMEOUT_MS);
    248 //
    249 //        addLayout(R.string.alertdialog_singlechoice, "alertdialog_singlechoice", R.layout.empty,
    250 //                new DialogModifier(new AlertDialogBuilder(AlertDialogBuilder.SINGLE_CHOICE)),
    251 //                SHORT_TIMEOUT_MS);
    252 //
    253 //        addLayout(R.string.alertdialog_multichoice, "alertdialog_multichoice", R.layout.empty,
    254 //                new DialogModifier(new AlertDialogBuilder(AlertDialogBuilder.MULTI_CHOICE)),
    255 //                SHORT_TIMEOUT_MS);
    256 
    257         // TODO: We can't test the spinner, because there is no way to halt the animation.
    258         // addLayout(R.string.progressdialog_spinner, "progressdialog_spinner", R.layout.empty,
    259         //      new DialogModifier(new ProgressDialogBuilder(ProgressDialog.STYLE_SPINNER)));
    260 
    261 //        addLayout(R.string.progressdialog_horizontal, "progressdialog_horizontal", R.layout.empty,
    262 //                new DialogModifier(new ProgressDialogBuilder(ProgressDialog.STYLE_HORIZONTAL)),
    263 //                SHORT_TIMEOUT_MS);
    264 
    265         // Colors
    266 
    267         addLayout(R.string.color_blue_bright, "color_blue_bright",
    268                 R.layout.color_blue_bright, null, NO_TIMEOUT_MS);
    269 
    270         addLayout(R.string.color_blue_dark, "color_blue_dark",
    271                 R.layout.color_blue_dark, null, NO_TIMEOUT_MS);
    272 
    273         addLayout(R.string.color_blue_light, "color_blue_light",
    274                 R.layout.color_blue_light, null, NO_TIMEOUT_MS);
    275 
    276         addLayout(R.string.color_green_dark, "color_green_dark",
    277                 R.layout.color_green_dark, null, NO_TIMEOUT_MS);
    278 
    279         addLayout(R.string.color_green_light, "color_green_light",
    280                 R.layout.color_green_light, null, NO_TIMEOUT_MS);
    281 
    282         addLayout(R.string.color_orange_dark, "color_orange_dark",
    283                 R.layout.color_orange_dark, null, NO_TIMEOUT_MS);
    284 
    285         addLayout(R.string.color_orange_light, "color_orange_light",
    286                 R.layout.color_orange_light, null, NO_TIMEOUT_MS);
    287 
    288         addLayout(R.string.color_purple, "color_purple",
    289                 R.layout.color_purple, null, NO_TIMEOUT_MS);
    290 
    291         addLayout(R.string.color_red_dark, "color_red_dark",
    292                 R.layout.color_red_dark, null, NO_TIMEOUT_MS);
    293 
    294         addLayout(R.string.color_red_light, "color_red_light",
    295                 R.layout.color_red_light, null, NO_TIMEOUT_MS);
    296     }
    297 
    298     private void addLayout(int displayName, String fileName, int layout, LayoutModifier modifier,
    299             long timeoutMs) {
    300         addLayout(new LayoutInfo(displayName, fileName, layout, modifier, timeoutMs));
    301     }
    302 
    303     private void addLayout(LayoutInfo info) {
    304         mLayoutInfos.add(info);
    305     }
    306 
    307     private void addCalendarLayouts(int adapterMode) {
    308         if (adapterMode == MODE_VIEWING || !CalendarViewModifier.isMonth(Calendar.JANUARY)) {
    309             addLayout(getCalendarLayoutInfo());
    310         }
    311 
    312         if (adapterMode == MODE_VIEWING || !CalendarViewModifier.isMonth(Calendar.FEBRUARY)) {
    313             addLayout(getFebruaryCalendarLayoutInfo());
    314         }
    315     }
    316 
    317     private LayoutInfo getCalendarLayoutInfo() {
    318         return new LayoutInfo(R.string.calendarview_jan, "calendar_view",
    319             R.layout.calendarview, new CalendarViewModifier(true), SHORT_TIMEOUT_MS);
    320     }
    321 
    322     private LayoutInfo getFebruaryCalendarLayoutInfo() {
    323         return new LayoutInfo(R.string.calendarview_feb, "calendar_view_feb",
    324             R.layout.calendarview, new CalendarViewModifier(false), SHORT_TIMEOUT_MS);
    325     }
    326 
    327     @Override
    328     public int getCount() {
    329         return mLayoutInfos.size();
    330     }
    331 
    332     @Override
    333     public LayoutInfo getItem(int position) {
    334         return mLayoutInfos.get(position);
    335     }
    336 
    337     @Override
    338     public long getItemId(int position) {
    339         return getItem(position).mLayout;
    340     }
    341 
    342     @Override
    343     public View getView(int position, View convertView, ViewGroup parent) {
    344         TextView textView;
    345         if (convertView != null) {
    346             textView = (TextView) convertView;
    347         } else {
    348             textView = (TextView) mInflater.inflate(android.R.layout.simple_list_item_1,
    349                     parent, false);
    350         }
    351         LayoutInfo layoutInfo = getItem(position);
    352         textView.setText(layoutInfo.mDisplayName);
    353         return textView;
    354     }
    355 }
    356