1 /* 2 * Copyright (C) 2008 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 android.widget.cts; 18 19 import static org.junit.Assert.assertEquals; 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.support.test.InstrumentationRegistry; 27 import android.support.test.filters.SmallTest; 28 import android.support.test.runner.AndroidJUnit4; 29 import android.util.AttributeSet; 30 import android.util.Xml; 31 import android.view.View; 32 import android.view.ViewGroup; 33 import android.widget.ListView; 34 import android.widget.ViewSwitcher; 35 import android.widget.ViewSwitcher.ViewFactory; 36 37 import org.junit.Before; 38 import org.junit.Test; 39 import org.junit.runner.RunWith; 40 import org.xmlpull.v1.XmlPullParser; 41 42 /** 43 * Test {@link ViewSwitcher}. 44 */ 45 @SmallTest 46 @RunWith(AndroidJUnit4.class) 47 public class ViewSwitcherTest { 48 private Context mContext; 49 50 @Before 51 public void setup() { 52 mContext = InstrumentationRegistry.getTargetContext(); 53 } 54 55 @Test 56 public void testConstructor() { 57 new ViewSwitcher(mContext); 58 59 new ViewSwitcher(mContext, null); 60 61 XmlPullParser parser = mContext.getResources().getXml(R.layout.viewswitcher_layout); 62 AttributeSet attrs = Xml.asAttributeSet(parser); 63 new ViewSwitcher(mContext, attrs); 64 } 65 66 @Test 67 public void testSetFactory() { 68 final ViewSwitcher viewSwitcher = new ViewSwitcher(mContext); 69 70 MockViewFactory factory = new MockViewFactory(); 71 viewSwitcher.setFactory(factory); 72 assertTrue(factory.hasMakeViewCalled()); 73 } 74 75 @Test 76 public void testReset() { 77 final ViewSwitcher viewSwitcher = new ViewSwitcher(mContext); 78 79 ListView lv1 = new ListView(mContext); 80 ListView lv2 = new ListView(mContext); 81 assertEquals(View.VISIBLE, lv1.getVisibility()); 82 assertEquals(View.VISIBLE, lv2.getVisibility()); 83 viewSwitcher.addView(lv1, 0); 84 viewSwitcher.addView(lv2, 1); 85 86 viewSwitcher.reset(); 87 assertEquals(View.GONE, lv1.getVisibility()); 88 assertEquals(View.GONE, lv2.getVisibility()); 89 } 90 91 @Test 92 public void testGetNextView() { 93 final ViewSwitcher viewSwitcher = new ViewSwitcher(mContext); 94 95 ListView lv1 = new ListView(mContext); 96 ListView lv2 = new ListView(mContext); 97 viewSwitcher.addView(lv1, 0, new ViewGroup.LayoutParams(20, 25)); 98 assertSame(lv1, viewSwitcher.getChildAt(0)); 99 assertNull(viewSwitcher.getNextView()); 100 101 viewSwitcher.addView(lv2, 1, new ViewGroup.LayoutParams(20, 25)); 102 assertSame(lv2, viewSwitcher.getChildAt(1)); 103 assertSame(lv2, viewSwitcher.getNextView()); 104 105 viewSwitcher.setDisplayedChild(1); 106 assertSame(lv1, viewSwitcher.getNextView()); 107 108 try { 109 ListView lv3 = new ListView(mContext); 110 viewSwitcher.addView(lv3, 2, null); 111 fail("Should throw IllegalStateException here."); 112 } catch (IllegalStateException e) { 113 } 114 } 115 116 private class MockViewFactory implements ViewFactory { 117 private boolean mMakeViewCalled = false; 118 119 public View makeView() { 120 mMakeViewCalled = true; 121 return new ListView(mContext); 122 } 123 124 public boolean hasMakeViewCalled() { 125 return mMakeViewCalled; 126 } 127 } 128 } 129