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.setupwizardlib;
18
19import static org.junit.Assert.assertNotNull;
20import static org.junit.Assert.assertNull;
21import static org.junit.Assert.assertSame;
22import static org.junit.Assert.assertTrue;
23import static org.junit.Assert.fail;
24
25import android.content.Context;
26import android.support.test.InstrumentationRegistry;
27import android.support.test.filters.SmallTest;
28import android.support.test.runner.AndroidJUnit4;
29import android.view.LayoutInflater;
30import android.view.View;
31import android.widget.TextView;
32
33import com.android.setupwizardlib.template.HeaderMixin;
34import com.android.setupwizardlib.test.R;
35
36import org.junit.Before;
37import org.junit.Test;
38import org.junit.runner.RunWith;
39
40@RunWith(AndroidJUnit4.class)
41@SmallTest
42public class TemplateLayoutTest {
43
44    private Context mContext;
45
46    @Before
47    public void setUp() throws Exception {
48        mContext = InstrumentationRegistry.getContext();
49    }
50
51    @Test
52    public void testAddView() {
53        TemplateLayout layout = new TemplateLayout(mContext, R.layout.test_template,
54                R.id.suw_layout_content);
55        TextView tv = new TextView(mContext);
56        tv.setId(R.id.test_view_id);
57        layout.addView(tv);
58        View view = layout.findViewById(R.id.test_view_id);
59        assertSame("The view added should be the same text view", tv, view);
60    }
61
62    @Test
63    public void testInflateFromXml() {
64        LayoutInflater inflater = LayoutInflater.from(mContext);
65        TemplateLayout layout =
66                (TemplateLayout) inflater.inflate(R.layout.test_template_layout, null);
67        View content = layout.findViewById(R.id.test_content);
68        assertTrue("@id/test_content should be a TextView", content instanceof TextView);
69    }
70
71    @Test
72    public void testTemplate() {
73        TemplateLayout layout = new TemplateLayout(mContext, R.layout.test_template,
74                R.id.suw_layout_content);
75        View templateView = layout.findViewById(R.id.test_template_view);
76        assertNotNull("@id/test_template_view should exist in template", templateView);
77
78        TextView tv = new TextView(mContext);
79        tv.setId(R.id.test_view_id);
80        layout.addView(tv);
81
82        templateView = layout.findViewById(R.id.test_template_view);
83        assertNotNull("@id/test_template_view should exist in template", templateView);
84        View contentView = layout.findViewById(R.id.test_view_id);
85        assertSame("The view added should be the same text view", tv, contentView);
86    }
87
88    @Test
89    public void testNoTemplate() {
90        try {
91            new TemplateLayout(mContext, 0, 0);
92            fail("Inflating TemplateLayout without template should throw exception");
93        } catch (IllegalArgumentException e) {
94            // Expected IllegalArgumentException
95        }
96    }
97
98    @Test
99    public void testGetMixin() {
100        TemplateLayout layout = new TemplateLayout(mContext, R.layout.test_template,
101                R.id.suw_layout_content);
102        final HeaderMixin mixin = layout.getMixin(HeaderMixin.class);
103        assertNull("getMixin for a mixin that doesn't exist should return null", mixin);
104    }
105}
106