LinkSpanTest.java revision 2e634640f3ed1b101f3748849b415fa14d57991c
1dce2b2fa9d6b26414a8d5a55918e4d7ca2ab1baaElliott Hughes/* 2dce2b2fa9d6b26414a8d5a55918e4d7ca2ab1baaElliott Hughes * Copyright (C) 2016 The Android Open Source Project 3dce2b2fa9d6b26414a8d5a55918e4d7ca2ab1baaElliott Hughes * 4dce2b2fa9d6b26414a8d5a55918e4d7ca2ab1baaElliott Hughes * Licensed under the Apache License, Version 2.0 (the "License"); 5dce2b2fa9d6b26414a8d5a55918e4d7ca2ab1baaElliott Hughes * you may not use this file except in compliance with the License. 6dce2b2fa9d6b26414a8d5a55918e4d7ca2ab1baaElliott Hughes * You may obtain a copy of the License at 7dce2b2fa9d6b26414a8d5a55918e4d7ca2ab1baaElliott Hughes * 8dce2b2fa9d6b26414a8d5a55918e4d7ca2ab1baaElliott Hughes * http://www.apache.org/licenses/LICENSE-2.0 9dce2b2fa9d6b26414a8d5a55918e4d7ca2ab1baaElliott Hughes * 10dce2b2fa9d6b26414a8d5a55918e4d7ca2ab1baaElliott Hughes * Unless required by applicable law or agreed to in writing, software 11dce2b2fa9d6b26414a8d5a55918e4d7ca2ab1baaElliott Hughes * distributed under the License is distributed on an "AS IS" BASIS, 12dce2b2fa9d6b26414a8d5a55918e4d7ca2ab1baaElliott Hughes * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13dce2b2fa9d6b26414a8d5a55918e4d7ca2ab1baaElliott Hughes * See the License for the specific language governing permissions and 14dce2b2fa9d6b26414a8d5a55918e4d7ca2ab1baaElliott Hughes * limitations under the License. 15dce2b2fa9d6b26414a8d5a55918e4d7ca2ab1baaElliott Hughes */ 16dce2b2fa9d6b26414a8d5a55918e4d7ca2ab1baaElliott Hughes 17e5fea3d504609d22337a5311d3ce0e72314bceeeNarayan Kamathpackage com.android.setupwizardlib.span; 18dce2b2fa9d6b26414a8d5a55918e4d7ca2ab1baaElliott Hughes 19dce2b2fa9d6b26414a8d5a55918e4d7ca2ab1baaElliott Hughesimport static org.junit.Assert.assertSame; 20dce2b2fa9d6b26414a8d5a55918e4d7ca2ab1baaElliott Hughesimport static org.robolectric.RuntimeEnvironment.application; 21dce2b2fa9d6b26414a8d5a55918e4d7ca2ab1baaElliott Hughes 22dce2b2fa9d6b26414a8d5a55918e4d7ca2ab1baaElliott Hughesimport android.content.Context; 23dce2b2fa9d6b26414a8d5a55918e4d7ca2ab1baaElliott Hughesimport android.content.ContextWrapper; 24dce2b2fa9d6b26414a8d5a55918e4d7ca2ab1baaElliott Hughesimport android.widget.TextView; 25dce2b2fa9d6b26414a8d5a55918e4d7ca2ab1baaElliott Hughes 26dce2b2fa9d6b26414a8d5a55918e4d7ca2ab1baaElliott Hughesimport org.junit.Test; 27dce2b2fa9d6b26414a8d5a55918e4d7ca2ab1baaElliott Hughesimport org.junit.runner.RunWith; 28dce2b2fa9d6b26414a8d5a55918e4d7ca2ab1baaElliott Hughesimport org.robolectric.RobolectricTestRunner; 29dce2b2fa9d6b26414a8d5a55918e4d7ca2ab1baaElliott Hughes 30dce2b2fa9d6b26414a8d5a55918e4d7ca2ab1baaElliott Hughes@RunWith(RobolectricTestRunner.class) 31dce2b2fa9d6b26414a8d5a55918e4d7ca2ab1baaElliott Hughespublic class LinkSpanTest { 32dce2b2fa9d6b26414a8d5a55918e4d7ca2ab1baaElliott Hughes 33dce2b2fa9d6b26414a8d5a55918e4d7ca2ab1baaElliott Hughes @Test 34dce2b2fa9d6b26414a8d5a55918e4d7ca2ab1baaElliott Hughes public void testOnClick() { 35dce2b2fa9d6b26414a8d5a55918e4d7ca2ab1baaElliott Hughes final TestContext context = new TestContext(application); 36dce2b2fa9d6b26414a8d5a55918e4d7ca2ab1baaElliott Hughes final TextView textView = new TextView(context); 37dce2b2fa9d6b26414a8d5a55918e4d7ca2ab1baaElliott Hughes final LinkSpan linkSpan = new LinkSpan("test_id"); 38dce2b2fa9d6b26414a8d5a55918e4d7ca2ab1baaElliott Hughes 39dce2b2fa9d6b26414a8d5a55918e4d7ca2ab1baaElliott Hughes linkSpan.onClick(textView); 40dce2b2fa9d6b26414a8d5a55918e4d7ca2ab1baaElliott Hughes 41dce2b2fa9d6b26414a8d5a55918e4d7ca2ab1baaElliott Hughes assertSame("Clicked LinkSpan should be passed to setup", linkSpan, context.clickedSpan); 42dce2b2fa9d6b26414a8d5a55918e4d7ca2ab1baaElliott Hughes } 43dce2b2fa9d6b26414a8d5a55918e4d7ca2ab1baaElliott Hughes 44dce2b2fa9d6b26414a8d5a55918e4d7ca2ab1baaElliott Hughes @Test 45dce2b2fa9d6b26414a8d5a55918e4d7ca2ab1baaElliott Hughes public void testNonImplementingContext() { 46dce2b2fa9d6b26414a8d5a55918e4d7ca2ab1baaElliott Hughes final TextView textView = new TextView(application); 47dce2b2fa9d6b26414a8d5a55918e4d7ca2ab1baaElliott Hughes final LinkSpan linkSpan = new LinkSpan("test_id"); 48dce2b2fa9d6b26414a8d5a55918e4d7ca2ab1baaElliott Hughes 49dce2b2fa9d6b26414a8d5a55918e4d7ca2ab1baaElliott Hughes linkSpan.onClick(textView); 50dce2b2fa9d6b26414a8d5a55918e4d7ca2ab1baaElliott Hughes 51dce2b2fa9d6b26414a8d5a55918e4d7ca2ab1baaElliott Hughes // This would be no-op, because the context doesn't implement LinkSpan.OnClickListener. 52dce2b2fa9d6b26414a8d5a55918e4d7ca2ab1baaElliott Hughes // Just check that no uncaught exception here. 53dce2b2fa9d6b26414a8d5a55918e4d7ca2ab1baaElliott Hughes } 54dce2b2fa9d6b26414a8d5a55918e4d7ca2ab1baaElliott Hughes 55dce2b2fa9d6b26414a8d5a55918e4d7ca2ab1baaElliott Hughes private static class TestContext extends ContextWrapper implements LinkSpan.OnClickListener { 56dce2b2fa9d6b26414a8d5a55918e4d7ca2ab1baaElliott Hughes 57dce2b2fa9d6b26414a8d5a55918e4d7ca2ab1baaElliott Hughes public LinkSpan clickedSpan = null; 58dce2b2fa9d6b26414a8d5a55918e4d7ca2ab1baaElliott Hughes 59dce2b2fa9d6b26414a8d5a55918e4d7ca2ab1baaElliott Hughes TestContext(Context base) { 60dce2b2fa9d6b26414a8d5a55918e4d7ca2ab1baaElliott Hughes super(base); 61dce2b2fa9d6b26414a8d5a55918e4d7ca2ab1baaElliott Hughes } 62dce2b2fa9d6b26414a8d5a55918e4d7ca2ab1baaElliott Hughes 63dce2b2fa9d6b26414a8d5a55918e4d7ca2ab1baaElliott Hughes @Override 64dce2b2fa9d6b26414a8d5a55918e4d7ca2ab1baaElliott Hughes public void onClick(LinkSpan span) { 65dce2b2fa9d6b26414a8d5a55918e4d7ca2ab1baaElliott Hughes clickedSpan = span; 66dce2b2fa9d6b26414a8d5a55918e4d7ca2ab1baaElliott Hughes } 67dce2b2fa9d6b26414a8d5a55918e4d7ca2ab1baaElliott Hughes } 68dce2b2fa9d6b26414a8d5a55918e4d7ca2ab1baaElliott Hughes} 69dce2b2fa9d6b26414a8d5a55918e4d7ca2ab1baaElliott Hughes