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.test; 18 19import android.content.Context; 20import android.test.InstrumentationTestCase; 21import android.test.suitebuilder.annotation.SmallTest; 22import android.view.LayoutInflater; 23import android.view.View; 24import android.widget.TextView; 25 26import com.android.setupwizardlib.TemplateLayout; 27 28public class TemplateLayoutTest extends InstrumentationTestCase { 29 30 private Context mContext; 31 32 @Override 33 protected void setUp() throws Exception { 34 super.setUp(); 35 mContext = getInstrumentation().getContext(); 36 } 37 38 @SmallTest 39 public void testAddView() { 40 TemplateLayout layout = new TemplateLayout(mContext, R.layout.test_template, 41 R.id.suw_layout_content); 42 TextView tv = new TextView(mContext); 43 tv.setId(R.id.test_view_id); 44 layout.addView(tv); 45 View view = layout.findViewById(R.id.test_view_id); 46 assertSame("The view added should be the same text view", tv, view); 47 } 48 49 @SmallTest 50 public void testInflateFromXml() { 51 LayoutInflater inflater = LayoutInflater.from(mContext); 52 TemplateLayout layout = 53 (TemplateLayout) inflater.inflate(R.layout.test_template_layout, null); 54 View content = layout.findViewById(R.id.test_content); 55 assertTrue("@id/test_content should be a TextView", content instanceof TextView); 56 } 57 58 @SmallTest 59 public void testTemplate() { 60 TemplateLayout layout = new TemplateLayout(mContext, R.layout.test_template, 61 R.id.suw_layout_content); 62 View templateView = layout.findViewById(R.id.test_template_view); 63 assertNotNull("@id/test_template_view should exist in template", templateView); 64 65 TextView tv = new TextView(mContext); 66 tv.setId(R.id.test_view_id); 67 layout.addView(tv); 68 69 templateView = layout.findViewById(R.id.test_template_view); 70 assertNotNull("@id/test_template_view should exist in template", templateView); 71 View contentView = layout.findViewById(R.id.test_view_id); 72 assertSame("The view added should be the same text view", tv, contentView); 73 } 74 75 @SmallTest 76 public void testNoTemplate() { 77 try { 78 new TemplateLayout(mContext, 0, 0); 79 fail("Inflating TemplateLayout without template should throw exception"); 80 } catch (IllegalArgumentException e) { 81 // Expected IllegalArgumentException 82 } 83 } 84} 85