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