1/*
2 * Copyright (C) 2015 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 */
16package android.databinding.testapp;
17
18import android.databinding.testapp.databinding.AbsSpinnerAdapterTestBinding;
19import android.databinding.testapp.vo.AbsSpinnerBindingObject;
20import android.os.Build;
21import android.test.UiThreadTest;
22import android.widget.Spinner;
23import android.widget.SpinnerAdapter;
24
25import java.util.List;
26
27public class AbsSpinnerBindingAdapterTest
28        extends BindingAdapterTestBase<AbsSpinnerAdapterTestBinding, AbsSpinnerBindingObject> {
29
30    Spinner mView;
31
32    public AbsSpinnerBindingAdapterTest() {
33        super(AbsSpinnerAdapterTestBinding.class, AbsSpinnerBindingObject.class,
34                R.layout.abs_spinner_adapter_test);
35    }
36
37    @Override
38    protected void setUp() throws Exception {
39        super.setUp();
40        mView = mBinder.view;
41    }
42
43    @UiThreadTest
44    public void testEntries() throws Throwable {
45        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
46            validateEntries();
47
48            changeValues();
49
50            validateEntries();
51        }
52    }
53
54    @UiThreadTest
55    public void testList() throws Throwable {
56        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
57            validateList();
58
59            mBindingObject.getList().add(1, "Cruel");
60            mBinder.executePendingBindings();
61
62            validateList();
63        }
64    }
65
66    private void validateEntries() {
67        assertEquals(mBindingObject.getEntries().length, mView.getAdapter().getCount());
68        CharSequence[] entries = mBindingObject.getEntries();
69        SpinnerAdapter adapter = mView.getAdapter();
70        for (int i = 0; i < entries.length; i++) {
71            assertEquals(adapter.getItem(i), entries[i]);
72        }
73    }
74
75    private void validateList() {
76        List<String> entries = mBindingObject.getList();
77        SpinnerAdapter adapter = mBinder.view2.getAdapter();
78        assertEquals(entries.size(), adapter.getCount());
79        for (int i = 0; i < entries.size(); i++) {
80            assertEquals(adapter.getItem(i), entries.get(i));
81        }
82    }
83}
84