1 /* 2 * Copyright 2018 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 androidx.appcompat.widget; 18 19 import android.content.Context; 20 import android.support.test.annotation.UiThreadTest; 21 import android.support.test.filters.SmallTest; 22 import android.support.test.rule.ActivityTestRule; 23 import android.support.test.runner.AndroidJUnit4; 24 import android.text.Editable; 25 26 import androidx.appcompat.app.AppCompatActivity; 27 28 import org.junit.Rule; 29 import org.junit.Test; 30 import org.junit.runner.RunWith; 31 32 /** 33 * Tests for the {@link AppCompatEditText} class. 34 */ 35 @SmallTest 36 @RunWith(AndroidJUnit4.class) 37 public class AppCompatEditTextTest { 38 @Rule 39 public final ActivityTestRule<AppCompatActivity> mActivityTestRule = 40 new ActivityTestRule<>(AppCompatActivity.class); 41 42 @Test 43 @UiThreadTest 44 public void testGetTextNonEditable() { 45 // This subclass calls getText before the object is fully constructed. This should not cause 46 // a null pointer exception. 47 GetTextEditText editText = new GetTextEditText(mActivityTestRule.getActivity()); 48 } 49 50 private class GetTextEditText extends AppCompatEditText { 51 52 GetTextEditText(Context context) { 53 super(context); 54 } 55 56 @Override 57 public void setText(CharSequence text, BufferType type) { 58 Editable currentText = getText(); 59 super.setText(text, type); 60 } 61 } 62 63 @Test 64 @UiThreadTest 65 public void testGetTextBeforeConstructor() { 66 // This subclass calls getText before the TextView constructor. This should not cause 67 // a null pointer exception. 68 GetTextEditText2 editText = new GetTextEditText2(mActivityTestRule.getActivity()); 69 } 70 71 private class GetTextEditText2 extends AppCompatEditText { 72 73 GetTextEditText2(Context context) { 74 super(context); 75 } 76 77 @Override 78 public void setOverScrollMode(int overScrollMode) { 79 // This method is called by the View constructor before the TextView/EditText 80 // constructors. 81 Editable text = getText(); 82 } 83 } 84 } 85