1e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com/*
2e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com * Copyright (C) 2014 Google Inc.
3e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com *
4e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com * Licensed under the Apache License, Version 2.0 (the "License");
5e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com * you may not use this file except in compliance with the License.
6e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com * You may obtain a copy of the License at
7e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com *
8e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com * http://www.apache.org/licenses/LICENSE-2.0
9e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com *
10e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com * Unless required by applicable law or agreed to in writing, software
11e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com * distributed under the License is distributed on an "AS IS" BASIS,
12e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com * See the License for the specific language governing permissions and
14e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com * limitations under the License.
15e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com */
16e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com
17e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.compackage com.android.i18n.addressinput;
18e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com
19e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.comimport com.android.i18n.addressinput.testing.TestActivity;
20e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com
21e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.comimport android.app.Activity;
22e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.comimport android.app.ProgressDialog;
23e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.comimport android.content.Context;
24e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.comimport android.test.ActivityInstrumentationTestCase2;
25e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.comimport android.view.View;
26e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.comimport android.widget.ArrayAdapter;
27e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.comimport android.widget.EditText;
28e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.comimport android.widget.LinearLayout;
29e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.comimport android.widget.Spinner;
30e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.comimport android.widget.TextView;
31e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com
32e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.comimport junit.framework.Assert;
33e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com
34e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com/**
35e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com * Test class for {@link AddressWidgetUiComponentProvider}.
36e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com */
37e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.compublic class AddressWidgetUiComponentProviderTest
38e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com        extends ActivityInstrumentationTestCase2<TestActivity> {
39e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com    private AddressWidget widget;
40e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com    private AddressWidgetUiComponentProvider componentProvider;
41e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com    private LinearLayout container;
42e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com    private AddressData address;
43e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com    private Context context;
44e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com    private int customTextViewCounter;
45e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com    private int customProgressDialogCounter;
46e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com
47e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com    public AddressWidgetUiComponentProviderTest() {
48e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com      super(TestActivity.class);
49e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com    }
50e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com
51e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com    @Override
52e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com    protected void setUp() throws Exception {
53e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com        super.setUp();
54e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com        AddressData.Builder builder = new AddressData.Builder()
55e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com                .setCountry("US")
56e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com                .setLanguageCode("en")
57e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com                .setAddressLine1("1098 Alta Ave")
58e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com                .setAdminArea("CA");
59e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com        address = builder.build();
60e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com        context = getActivity();
61e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com        container = new LinearLayout(context);
62e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com    }
63e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com
64e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com    public void testCustomWidgets() {
65e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com        customTextViewCounter = 0;
66e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com        customProgressDialogCounter = 0;
67e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com        componentProvider = new TestComponentProvider(context);
68e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com        widget = new AddressWidget(context, container, new FormOptions.Builder().build(),
69e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com            new SimpleClientCacheManager(), componentProvider);
70e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com        widget.renderFormWithSavedAddress(address);
71e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com
72e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com        for (AddressField field : AddressField.values()) {
73e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com            if (field.equals(AddressField.COUNTRY)) {
74e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com              continue;
75e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com            }
76e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com
77e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com            View view = widget.getViewForField(field);
78e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com            if (view instanceof EditText) {
79e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com                assertTrue("Field " + field + " does not use customized edit text widget.",
80e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com                        view instanceof CustomEditText);
81e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com            } else if (view instanceof Spinner) {
82e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com                assertTrue("Field " + field + " does not use customized spinner widget.",
83e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com                        view instanceof CustomSpinner);
84e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com                assertTrue("Field " + field + " does not use customized ArrayAdapter.",
85e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com                        ((Spinner) view).getAdapter() instanceof CustomArrayAdapter);
86e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com            }
87e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com        }
88e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com
89e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com        assertTrue("Custom TextView label not used.", customTextViewCounter > 0);
90e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com        assertTrue("Custom ProgressDialog not used.", customProgressDialogCounter > 0);
91e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com    }
92e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com
93e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com    private void increaseTextViewCounter() {
94e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com        customTextViewCounter++;
95e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com    }
96e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com
97e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com    private void increaseProgressDialogCounter() {
98e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com        customProgressDialogCounter++;
99e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com    }
100e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com
101e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com    private class CustomEditText extends EditText {
102e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com        CustomEditText(Context context) {
103e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com          super(context);
104e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com        }
105e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com    }
106e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com
107e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com    private class CustomSpinner extends Spinner {
108e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com        CustomSpinner(Context context) {
109e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com            super(context);
110e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com        }
111e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com    }
112e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com
113e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com    private class CustomArrayAdapter<String> extends ArrayAdapter {
114e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com        CustomArrayAdapter(Context context, int id) {
115e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com            super(context, id);
116e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com        }
117e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com    }
118e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com
119e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com    private class TestComponentProvider extends AddressWidgetUiComponentProvider {
120e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com        TestComponentProvider(Context context) {
121e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com            super(context);
122e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com        }
123e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com
124e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com        protected TextView createUiLabel(CharSequence label, AddressField.WidthType widthType) {
125e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com            TextView result = new TextView(mContext);
126e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com            result.setText(label);
127e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com            AddressWidgetUiComponentProviderTest.this.increaseTextViewCounter();
128e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com            return result;
129e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com        }
130e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com
131e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com        protected EditText createUiTextField(AddressField.WidthType widthType) {
132e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com            return new CustomEditText(mContext);
133e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com        }
134e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com
135e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com        protected Spinner createUiPickerSpinner(AddressField.WidthType widthType) {
136e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com            return new CustomSpinner(mContext);
137e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com        }
138e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com
139e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com        protected ArrayAdapter<String> createUiPickerAdapter(AddressField.WidthType widthType) {
140e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com            ArrayAdapter<String> result =
141e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com                    new CustomArrayAdapter<String>(
142e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com                            mContext, android.R.layout.simple_spinner_item);
143e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com            result.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
144e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com            return result;
145e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com        }
146e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com
147e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com        protected ProgressDialog getUiActivityIndicatorView() {
148e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com            AddressWidgetUiComponentProviderTest.this.increaseProgressDialogCounter();
149e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com            return super.getUiActivityIndicatorView();
150e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com        }
151e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com    }
152e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com}
153e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com