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.assertFalse; 21 import static org.junit.Assert.assertSame; 22 import static org.junit.Assert.assertTrue; 23 24 import android.Manifest; 25 import android.app.Activity; 26 import android.os.SystemClock; 27 import android.util.AttributeSet; 28 import android.util.Xml; 29 import android.view.View; 30 import android.widget.TextView; 31 import android.widget.ViewFlipper; 32 33 import androidx.test.annotation.UiThreadTest; 34 import androidx.test.filters.LargeTest; 35 import androidx.test.filters.MediumTest; 36 import androidx.test.rule.ActivityTestRule; 37 import androidx.test.runner.AndroidJUnit4; 38 39 import com.android.compatibility.common.util.AdoptShellPermissionsRule; 40 41 import org.junit.Before; 42 import org.junit.Rule; 43 import org.junit.Test; 44 import org.junit.runner.RunWith; 45 import org.xmlpull.v1.XmlPullParser; 46 47 /** 48 * Test {@link ViewFlipper}. 49 */ 50 @MediumTest 51 @RunWith(AndroidJUnit4.class) 52 public class ViewFlipperTest { 53 private Activity mActivity; 54 55 @Rule(order = 0) 56 public AdoptShellPermissionsRule mAdoptShellPermissionsRule = new AdoptShellPermissionsRule( 57 androidx.test.platform.app.InstrumentationRegistry 58 .getInstrumentation().getUiAutomation(), 59 Manifest.permission.START_ACTIVITIES_FROM_SDK_SANDBOX); 60 61 @Rule(order = 1) 62 public ActivityTestRule<ViewFlipperCtsActivity> mActivityRule = 63 new ActivityTestRule<>(ViewFlipperCtsActivity.class); 64 65 @Before setup()66 public void setup() { 67 mActivity = mActivityRule.getActivity(); 68 } 69 70 @UiThreadTest 71 @Test testConstructor()72 public void testConstructor() { 73 new ViewFlipper(mActivity); 74 75 new ViewFlipper(mActivity, null); 76 77 XmlPullParser parser = mActivity.getResources().getXml(R.layout.viewflipper_layout); 78 AttributeSet attrs = Xml.asAttributeSet(parser); 79 new ViewFlipper(mActivity, attrs); 80 } 81 82 @UiThreadTest 83 @Test(expected=NullPointerException.class) testConstructorNullContext()84 public void testConstructorNullContext() { 85 new ViewFlipper(null, null); 86 } 87 88 @UiThreadTest 89 @Test testSetFlipInterval()90 public void testSetFlipInterval() { 91 ViewFlipper viewFlipper = new ViewFlipper(mActivity); 92 viewFlipper.setFlipInterval(0); 93 viewFlipper.setFlipInterval(-1); 94 } 95 96 @LargeTest 97 @Test testViewFlipper()98 public void testViewFlipper() throws Throwable { 99 final int flipInterval = mActivity.getResources().getInteger( 100 R.integer.view_flipper_interval); 101 102 mActivityRule.runOnUiThread(() -> { 103 ViewFlipper viewFlipper = 104 (ViewFlipper) mActivity.findViewById(R.id.viewflipper_test); 105 106 TextView iv1 = (TextView) mActivity.findViewById(R.id.viewflipper_textview1); 107 TextView iv2 = (TextView) mActivity.findViewById(R.id.viewflipper_textview2); 108 109 assertFalse(viewFlipper.isFlipping()); 110 assertSame(iv1, viewFlipper.getCurrentView()); 111 112 viewFlipper.startFlipping(); 113 assertTrue(viewFlipper.isFlipping()); 114 assertSame(iv1, viewFlipper.getCurrentView()); 115 assertEquals(View.VISIBLE, iv1.getVisibility()); 116 assertEquals(View.GONE, iv2.getVisibility()); 117 }); 118 119 // wait for a longer time to make sure the view flipping is completed. 120 SystemClock.sleep(flipInterval + 200); 121 mActivityRule.runOnUiThread(() -> { 122 ViewFlipper viewFlipper = 123 (ViewFlipper) mActivity.findViewById(R.id.viewflipper_test); 124 125 TextView iv1 = (TextView) mActivity.findViewById(R.id.viewflipper_textview1); 126 TextView iv2 = (TextView) mActivity.findViewById(R.id.viewflipper_textview2); 127 128 assertSame(iv2, viewFlipper.getCurrentView()); 129 assertEquals(View.GONE, iv1.getVisibility()); 130 assertEquals(View.VISIBLE, iv2.getVisibility()); 131 }); 132 133 SystemClock.sleep(flipInterval + 200); 134 mActivityRule.runOnUiThread(() -> { 135 ViewFlipper viewFlipper = 136 (ViewFlipper) mActivity.findViewById(R.id.viewflipper_test); 137 138 TextView iv1 = (TextView) mActivity.findViewById(R.id.viewflipper_textview1); 139 TextView iv2 = (TextView) mActivity.findViewById(R.id.viewflipper_textview2); 140 141 assertSame(iv1, viewFlipper.getCurrentView()); 142 assertEquals(View.VISIBLE, iv1.getVisibility()); 143 assertEquals(View.GONE, iv2.getVisibility()); 144 145 viewFlipper.stopFlipping(); 146 assertFalse(viewFlipper.isFlipping()); 147 }); 148 } 149 } 150