### Question:
LinkSpan extends ClickableSpan { @Override public void onClick(View view) { if (dispatchClick(view)) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { view.cancelPendingInputEvents(); } } else { Log.w(TAG, "Dropping click event. No listener attached."); } if (view instanceof TextView) { CharSequence text = ((TextView) view).getText(); if (text instanceof Spannable) { Selection.setSelection((Spannable) text, 0); } } } LinkSpan(String id); @Override void onClick(View view); @Override void updateDrawState(TextPaint drawState); String getId();  }### Answer:
@Test public void onClick_shouldCallListenerOnContext() { final TestContext context = new TestContext(application); final TextView textView = new TextView(context); final LinkSpan linkSpan = new LinkSpan("test_id"); linkSpan.onClick(textView); assertSame("Clicked LinkSpan should be passed to setup", linkSpan, context.clickedSpan); }
@Test public void onClick_contextDoesNotImplementOnClickListener_shouldBeNoOp() { final TextView textView = new TextView(application); final LinkSpan linkSpan = new LinkSpan("test_id"); linkSpan.onClick(textView); }
@Test public void onClick_contextWrapsOnClickListener_shouldCallWrappedListener() { final TestContext context = new TestContext(application); final Context wrapperContext = new ContextWrapper(context); final TextView textView = new TextView(wrapperContext); final LinkSpan linkSpan = new LinkSpan("test_id"); linkSpan.onClick(textView); assertSame("Clicked LinkSpan should be passed to setup", linkSpan, context.clickedSpan); }
@Test public void onClick_shouldClearSelection() { final TestContext context = new TestContext(application); final TextView textView = new TextView(context); textView.setMovementMethod(LinkMovementMethod.getInstance()); textView.setFocusable(true); textView.setFocusableInTouchMode(true); final LinkSpan linkSpan = new LinkSpan("test_id"); SpannableStringBuilder text = new SpannableStringBuilder("Lorem ipsum dolor sit"); textView.setText(text); text.setSpan(linkSpan, 0, 5, 0); Selection.setSelection(text, 0, 5); linkSpan.onClick(textView); assertThat(Selection.getSelectionStart(textView.getText())).isEqualTo(0); assertThat(Selection.getSelectionEnd(textView.getText())).isEqualTo(0); }
@Test public void onClick_shouldCallListenerOnContext() { final TestContext context = new TestContext(application); final TextView textView = new TextView(context); final LinkSpan linkSpan = new LinkSpan("test_id"); linkSpan.onClick(textView); assertWithMessage("Clicked LinkSpan should be passed to setup") .that(context.clickedSpan) .isSameAs(linkSpan); }
@Test public void onClick_contextWrapsOnClickListener_shouldCallWrappedListener() { final TestContext context = new TestContext(application); final Context wrapperContext = new ContextWrapper(context); final TextView textView = new TextView(wrapperContext); final LinkSpan linkSpan = new LinkSpan("test_id"); linkSpan.onClick(textView); assertWithMessage("Clicked LinkSpan should be passed to setup") .that(context.clickedSpan) .isSameAs(linkSpan); }