1 /* 2 * Copyright (C) 2019 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.settings.accessibility; 18 19 import static com.google.common.truth.Truth.assertThat; 20 21 import android.content.Context; 22 import android.text.Editable; 23 import android.text.Html; 24 import android.view.LayoutInflater; 25 import android.view.View; 26 27 import androidx.preference.PreferenceViewHolder; 28 29 import com.android.settings.R; 30 31 import org.junit.Before; 32 import org.junit.Test; 33 import org.junit.runner.RunWith; 34 import org.robolectric.RobolectricTestRunner; 35 import org.robolectric.RuntimeEnvironment; 36 import org.xml.sax.XMLReader; 37 38 /** Tests for {@link HtmlTextPreference} */ 39 @RunWith(RobolectricTestRunner.class) 40 public final class HtmlTextPreferenceTest { 41 42 private HtmlTextPreference mHtmlTextPreference; 43 private PreferenceViewHolder mPreferenceViewHolder; 44 private String mHandledTag; 45 private final Html.TagHandler mTagHandler = new Html.TagHandler() { 46 @Override 47 public void handleTag(boolean opening, String tag, Editable editable, XMLReader xmlReader) { 48 mHandledTag = tag; 49 } 50 }; 51 52 @Before setUp()53 public void setUp() { 54 final Context context = RuntimeEnvironment.application; 55 mHtmlTextPreference = new HtmlTextPreference(context); 56 57 final LayoutInflater inflater = LayoutInflater.from(context); 58 final View view = 59 inflater.inflate(R.layout.preference_static_text, null); 60 mPreferenceViewHolder = PreferenceViewHolder.createInstanceForTests(view); 61 } 62 63 @Test testTagHandler()64 public void testTagHandler() { 65 final String testStr = "<testTag>Real description</testTag>"; 66 67 mHtmlTextPreference.setSummary(testStr); 68 mHtmlTextPreference.setTagHandler(mTagHandler); 69 mHtmlTextPreference.onBindViewHolder(mPreferenceViewHolder); 70 71 assertThat(mHandledTag).isEqualTo("testTag"); 72 } 73 } 74