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.text.style.cts; 18 19 import static org.junit.Assert.assertEquals; 20 import static org.junit.Assert.assertNotNull; 21 22 import android.app.Activity; 23 import android.app.Instrumentation; 24 import android.app.Instrumentation.ActivityMonitor; 25 import android.os.Parcel; 26 import android.support.test.InstrumentationRegistry; 27 import android.support.test.filters.LargeTest; 28 import android.support.test.filters.SmallTest; 29 import android.support.test.rule.ActivityTestRule; 30 import android.support.test.runner.AndroidJUnit4; 31 import android.text.cts.R; 32 import android.text.style.URLSpan; 33 import android.widget.TextView; 34 35 import org.junit.Before; 36 import org.junit.Rule; 37 import org.junit.Test; 38 import org.junit.runner.RunWith; 39 40 @SmallTest 41 @RunWith(AndroidJUnit4.class) 42 public class URLSpanTest { 43 // The scheme of TEST_URL must be "ctstesttext" to launch MockURLSpanTestActivity 44 private static final String TEST_URL = "ctstesttext://urlSpan/test"; 45 46 private Activity mActivity; 47 48 @Rule 49 public ActivityTestRule<URLSpanCtsActivity> mActivityRule = 50 new ActivityTestRule<>(URLSpanCtsActivity.class); 51 52 @Before 53 public void setup() { 54 mActivity = mActivityRule.getActivity(); 55 } 56 57 @Test 58 public void testConstructor() { 59 URLSpan urlSpan = new URLSpan(TEST_URL); 60 61 final Parcel p = Parcel.obtain(); 62 try { 63 urlSpan.writeToParcel(p, 0); 64 p.setDataPosition(0); 65 new URLSpan(p); 66 } finally { 67 p.recycle(); 68 } 69 } 70 71 @Test 72 public void testGetURL() { 73 URLSpan urlSpan = new URLSpan(TEST_URL); 74 assertEquals(TEST_URL, urlSpan.getURL()); 75 } 76 77 @LargeTest 78 @Test 79 public void testOnClick() throws Throwable { 80 final URLSpan urlSpan = new URLSpan(TEST_URL); 81 final TextView textView = (TextView) mActivity.findViewById(R.id.url); 82 83 Instrumentation instrumentation = InstrumentationRegistry.getInstrumentation(); 84 ActivityMonitor am = instrumentation.addMonitor(MockURLSpanTestActivity.class.getName(), 85 null, false); 86 87 mActivityRule.runOnUiThread(() -> urlSpan.onClick(textView)); 88 89 Activity newActivity = am.waitForActivityWithTimeout(5000); 90 assertNotNull(newActivity); 91 newActivity.finish(); 92 } 93 94 @Test(expected=NullPointerException.class) 95 public void testOnClickFailure() { 96 URLSpan urlSpan = new URLSpan(TEST_URL); 97 98 urlSpan.onClick(null); 99 } 100 101 @Test 102 public void testDescribeContents() { 103 URLSpan urlSpan = new URLSpan(TEST_URL); 104 urlSpan.describeContents(); 105 } 106 107 @Test 108 public void testGetSpanTypeId() { 109 URLSpan urlSpan = new URLSpan(TEST_URL); 110 urlSpan.getSpanTypeId(); 111 } 112 113 @Test 114 public void testWriteToParcel() { 115 Parcel p = Parcel.obtain(); 116 try { 117 URLSpan urlSpan = new URLSpan(TEST_URL); 118 urlSpan.writeToParcel(p, 0); 119 p.setDataPosition(0); 120 URLSpan u = new URLSpan(p); 121 assertEquals(TEST_URL, u.getURL()); 122 } finally { 123 p.recycle(); 124 } 125 } 126 } 127