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