1 /* 2 * Copyright (C) 2021 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 com.android.wifitrackerlib; 18 19 import static com.google.common.truth.Truth.assertThat; 20 21 import static org.junit.Assert.assertEquals; 22 import static org.mockito.Mockito.mock; 23 import static org.mockito.Mockito.when; 24 25 import android.content.Context; 26 import android.net.NetworkCapabilities; 27 import android.net.vcn.VcnTransportInfo; 28 import android.net.wifi.WifiInfo; 29 import android.text.Annotation; 30 import android.text.SpannableString; 31 import android.text.SpannableStringBuilder; 32 import android.text.Spanned; 33 import android.text.style.ClickableSpan; 34 35 import org.junit.Test; 36 37 public class NonSdkApiWrapperTest { 38 @Test testLinkifyAnnotation_annotation_returnTextWithClickableSpan()39 public void testLinkifyAnnotation_annotation_returnTextWithClickableSpan() { 40 final String annotationId = "id"; 41 final CharSequence testText = "test text "; 42 final CharSequence testLink = "link"; 43 final CharSequence expectedText = "test text link"; 44 final SpannableStringBuilder builder = new SpannableStringBuilder(testText); 45 builder.append(testLink, new Annotation("key", annotationId), 46 Spanned.SPAN_INCLUSIVE_INCLUSIVE); 47 48 final CharSequence output = NonSdkApiWrapper.linkifyAnnotation( 49 mock(Context.class), builder, annotationId, "url"); 50 51 final SpannableString outputSpannableString = new SpannableString(output); 52 assertEquals(output.toString(), expectedText.toString()); 53 assertEquals(outputSpannableString.getSpans(0, outputSpannableString.length(), 54 ClickableSpan.class).length, 1); 55 } 56 57 @Test testLinkifyAnnotation_annotationWithEmptyUriString_returnOriginalText()58 public void testLinkifyAnnotation_annotationWithEmptyUriString_returnOriginalText() { 59 final String annotationId = "url"; 60 final CharSequence testText = "test text "; 61 final CharSequence testLink = "Learn More"; 62 final CharSequence expectedText = "test text Learn More"; 63 final SpannableStringBuilder builder = new SpannableStringBuilder(testText); 64 builder.append(testLink, new Annotation("key", annotationId), 65 Spanned.SPAN_INCLUSIVE_INCLUSIVE); 66 67 final CharSequence output = NonSdkApiWrapper.linkifyAnnotation( 68 mock(Context.class), builder, annotationId, ""); 69 70 final SpannableString outputSpannableString = new SpannableString(output); 71 assertEquals(output.toString(), expectedText.toString()); 72 assertEquals(outputSpannableString.getSpans(0, outputSpannableString.length(), 73 ClickableSpan.class).length, 0); 74 } 75 76 /** 77 * Verifies the functionality of {@link NonSdkApiWrapper#getVcnWifiInfo(NetworkCapabilities)} 78 */ 79 @Test testGetVcnWifiInfo()80 public void testGetVcnWifiInfo() { 81 NetworkCapabilities networkCapabilities = mock(NetworkCapabilities.class); 82 83 assertThat(NonSdkApiWrapper.getVcnWifiInfo(networkCapabilities)).isNull(); 84 85 VcnTransportInfo vcnTransportInfo = mock(VcnTransportInfo.class); 86 when(networkCapabilities.getTransportInfo()).thenReturn(vcnTransportInfo); 87 88 assertThat(NonSdkApiWrapper.getVcnWifiInfo(networkCapabilities)).isNull(); 89 90 WifiInfo wifiInfo = mock(WifiInfo.class); 91 when(vcnTransportInfo.getWifiInfo()).thenReturn(wifiInfo); 92 93 assertThat(NonSdkApiWrapper.getVcnWifiInfo(networkCapabilities)).isEqualTo(wifiInfo); 94 } 95 } 96