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 */
16
17package com.android.messaging.ui;
18
19import android.view.View;
20
21
22/**
23 * Base class for view tests. Derived class just has to provide a layout id. Tests can then just
24 * call getView() to get a created view and test its behavior.
25 */
26public abstract class ViewTest<T extends View> extends BugleActivityUnitTestCase<TestActivity> {
27    public ViewTest() {
28        super(TestActivity.class);
29    }
30
31    protected T mView;
32
33    @SuppressWarnings("unchecked")
34    @Override
35    protected void setUp() throws Exception {
36        super.setUp();
37
38        // Create activity
39        final ActivityInstrumentationTestCaseIntent intent =
40                new ActivityInstrumentationTestCaseIntent(getInstrumentation().getTargetContext(),
41                TestActivity.class);
42        startActivity(intent, null, null);
43    }
44
45    @SuppressWarnings("unchecked")
46    protected T getView() {
47        if (mView == null) {
48            // View creation deferred (typically until test time) so that factory/appcontext is
49            // ready.
50            mView = (T) getActivity().getLayoutInflater().inflate(getLayoutIdForView(), null);
51        }
52        return mView;
53    }
54
55    protected void clickButton(final View view) {
56        getActivity().runOnUiThread(new Runnable() {
57            @Override
58            public void run() {
59                view.performClick();
60            }
61        });
62        getInstrumentation().waitForIdleSync();
63    }
64
65    protected abstract int getLayoutIdForView();
66}
67