1 /* 2 * Copyright (C) 2017 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 package com.android.documentsui.inspector; 17 18 import static junit.framework.Assert.assertEquals; 19 import static junit.framework.Assert.assertTrue; 20 21 import android.content.Context; 22 import android.support.test.InstrumentationRegistry; 23 import android.support.test.runner.AndroidJUnit4; 24 25 import android.test.suitebuilder.annotation.SmallTest; 26 import android.text.Spannable; 27 import android.text.SpannableString; 28 import android.widget.TextView; 29 import com.android.documentsui.inspector.HeaderTextSelector.Selector; 30 import org.junit.Before; 31 import org.junit.Test; 32 import org.junit.runner.RunWith; 33 34 @RunWith(AndroidJUnit4.class) 35 @SmallTest 36 public class HeaderTextSelectorTest { 37 38 private Context mContext; 39 private TestTextView mTestTextView; 40 private TestSelector mTestSelector; 41 private HeaderTextSelector mSelector; 42 43 @Before 44 public void setUp() throws Exception { 45 mContext = InstrumentationRegistry.getTargetContext(); 46 mTestTextView = new TestTextView(mContext); 47 mTestSelector = new TestSelector(); 48 mSelector = new HeaderTextSelector(mTestTextView, mTestSelector); 49 } 50 51 @Test 52 public void testSimpleFileName() throws Exception { 53 CharSequence fileName = "filename"; 54 String extension = ".txt"; 55 String testSequence = fileName + extension; 56 57 mTestTextView.setText(testSequence); 58 mSelector.onCreateActionMode(null, null); 59 mTestSelector.assertCalled(); 60 61 CharSequence selectedSequence = 62 testSequence.subSequence(mTestSelector.mStart, mTestSelector.mStop); 63 assertEquals(selectedSequence, fileName); 64 } 65 66 67 @Test 68 public void testLotsOfPeriodsInFileName() throws Exception { 69 CharSequence fileName = "filename"; 70 String extension = ".jpg.zip.pdf.txt"; 71 String testSequence = fileName + extension; 72 73 mTestTextView.setText(testSequence); 74 mSelector.onCreateActionMode(null, null); 75 mTestSelector.assertCalled(); 76 77 CharSequence selectedSequence = 78 testSequence.subSequence(mTestSelector.mStart, mTestSelector.mStop); 79 assertEquals(selectedSequence, fileName); 80 } 81 82 @Test 83 public void testNoPeriodsInFileName() throws Exception { 84 String testSequence = "filename"; 85 86 mTestTextView.setText(testSequence); 87 mSelector.onCreateActionMode(null, null); 88 mTestSelector.assertCalled(); 89 90 CharSequence selectedSequence = 91 testSequence.subSequence(mTestSelector.mStart, mTestSelector.mStop); 92 assertEquals(selectedSequence, testSequence); 93 } 94 95 private static class TestTextView extends TextView { 96 97 public TestTextView(Context context) { 98 super(context); 99 } 100 101 private String textViewString; 102 103 public void setText(String text) { 104 textViewString = text; 105 } 106 107 @Override 108 public CharSequence getText() { 109 return new SpannableString(textViewString); 110 } 111 } 112 113 private static class TestSelector implements Selector { 114 115 private boolean mCalled; 116 private int mStart; 117 private int mStop; 118 119 public TestSelector() { 120 mCalled = false; 121 mStart = -1; 122 mStop = -1; 123 } 124 125 @Override 126 public void select(Spannable text, int start, int stop) { 127 mCalled = true; 128 mStart = start; 129 mStop = stop; 130 } 131 132 public void assertCalled() { 133 assertTrue(mCalled); 134 } 135 } 136 }