aboutsummaryrefslogtreecommitdiff
path: root/java/test/com/android/i18n/addressinput/AddressWidgetUiComponentProviderTest.java
blob: d51972832e4b664ff8876f4eda05c137c962b699 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*
 * Copyright (C) 2014 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.i18n.addressinput;

import com.android.i18n.addressinput.testing.TestActivity;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.test.ActivityInstrumentationTestCase2;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;

import junit.framework.Assert;

/**
 * Test class for {@link AddressWidgetUiComponentProvider}.
 */
public class AddressWidgetUiComponentProviderTest
    extends ActivityInstrumentationTestCase2<TestActivity> {
  private AddressWidget widget;
  private AddressWidgetUiComponentProvider componentProvider;
  private LinearLayout container;
  private AddressData address;
  private Context context;
  private int customTextViewCounter;
  private int customProgressDialogCounter;

  public AddressWidgetUiComponentProviderTest() {
    super(TestActivity.class);
  }

  @Override
  protected void setUp() throws Exception {
    super.setUp();
    AddressData.Builder builder = new AddressData.Builder()
        .setCountry("US")
        .setLanguageCode("en")
        .setAddressLine1("1098 Alta Ave")
        .setAdminArea("CA");
    address = builder.build();
    context = getActivity();
    container = new LinearLayout(context);
  }

  public void testCustomWidgets() {
    customTextViewCounter = 0;
    customProgressDialogCounter = 0;
    componentProvider = new TestComponentProvider(context);
    widget = new AddressWidget(context, container, new FormOptions.Builder().build(),
        new SimpleClientCacheManager(), componentProvider);
    widget.renderFormWithSavedAddress(address);

    for (AddressField field : AddressField.values()) {
      if (field.equals(AddressField.COUNTRY)) {
        continue;
      }

      View view = widget.getViewForField(field);
      if (view instanceof EditText) {
        assertTrue("Field " + field + " does not use customized edit text widget.",
            view instanceof CustomEditText);
      } else if (view instanceof Spinner) {
        assertTrue("Field " + field + " does not use customized spinner widget.",
            view instanceof CustomSpinner);
        assertTrue("Field " + field + " does not use customized ArrayAdapter.",
            ((Spinner) view).getAdapter() instanceof CustomArrayAdapter);
      }
    }

    assertTrue("Custom TextView label not used.", customTextViewCounter > 0);
    assertTrue("Custom ProgressDialog not used.", customProgressDialogCounter > 0);
  }

  private void increaseTextViewCounter() {
    customTextViewCounter++;
  }

  private void increaseProgressDialogCounter() {
    customProgressDialogCounter++;
  }

  private class CustomEditText extends EditText {
    CustomEditText(Context context) {
      super(context);
    }
  }

  private class CustomSpinner extends Spinner {
    CustomSpinner(Context context) {
      super(context);
    }
  }

  private class CustomArrayAdapter<String> extends ArrayAdapter {
    CustomArrayAdapter(Context context, int id) {
      super(context, id);
    }
  }

  private class TestComponentProvider extends AddressWidgetUiComponentProvider {
    TestComponentProvider(Context context) {
      super(context);
    }

    protected TextView createUiLabel(CharSequence label, AddressField.WidthType widthType) {
      TextView result = new TextView(mContext);
      result.setText(label);
      AddressWidgetUiComponentProviderTest.this.increaseTextViewCounter();
      return result;
    }

    protected EditText createUiTextField(AddressField.WidthType widthType) {
      return new CustomEditText(mContext);
    }

    protected Spinner createUiPickerSpinner(AddressField.WidthType widthType) {
      return new CustomSpinner(mContext);
    }

    protected ArrayAdapter<String> createUiPickerAdapter(AddressField.WidthType widthType) {
      ArrayAdapter<String> result = new CustomArrayAdapter<String>(
          context, android.R.layout.simple_spinner_item);
      result.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
      return result;
    }

    protected ProgressDialog getUiActivityIndicatorView() {
      AddressWidgetUiComponentProviderTest.this.increaseProgressDialogCounter();
      return super.getUiActivityIndicatorView();
    }
  }
}