blob: 3a4e2e403ee08df5ec5ca6d087cdcbb235e4d2e9 [file] [log] [blame]
tmfang464da582018-07-04 15:59:21 +08001/*
2 * Copyright (C) 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
17package com.android.settingslib;
18
19import static org.mockito.Mockito.mock;
20import static org.mockito.Mockito.spy;
21import static org.mockito.Mockito.verify;
22import static org.mockito.Mockito.when;
23
24import android.content.Context;
25import android.view.View;
26import android.widget.EditText;
27
28import org.junit.Before;
29import org.junit.Test;
30import org.junit.runner.RunWith;
31import org.mockito.Mock;
32import org.mockito.MockitoAnnotations;
James Lemieuxec3ad9e2018-11-28 17:49:14 -080033import org.robolectric.RobolectricTestRunner;
tmfang464da582018-07-04 15:59:21 +080034import org.robolectric.RuntimeEnvironment;
35import org.robolectric.util.ReflectionHelpers;
36
James Lemieuxec3ad9e2018-11-28 17:49:14 -080037@RunWith(RobolectricTestRunner.class)
tmfang464da582018-07-04 15:59:21 +080038public class CustomEditTextPreferenceComaptTest {
39
40 @Mock
41 private View mView;
42
43 private TestPreference mPreference;
44
45 @Before
46 public void setUp() {
47 MockitoAnnotations.initMocks(this);
48 mPreference = new TestPreference(RuntimeEnvironment.application);
49 }
50
51 @Test
52 public void bindDialogView_shouldRequestFocus() {
53 final String testText = "";
54 final EditText editText = spy(new EditText(RuntimeEnvironment.application));
55 editText.setText(testText);
56 when(mView.findViewById(android.R.id.edit)).thenReturn(editText);
57
58 mPreference.onBindDialogView(mView);
59
60 verify(editText).requestFocus();
61 }
62
63 @Test
64 public void getEditText_noDialog_shouldNotCrash() {
65 ReflectionHelpers.setField(mPreference, "mFragment",
66 mock(CustomEditTextPreferenceCompat.CustomPreferenceDialogFragment.class));
67
68 mPreference.getEditText();
69
70 // no crash
71 }
72
73 private static class TestPreference extends CustomEditTextPreferenceCompat {
James Lemieuxec3ad9e2018-11-28 17:49:14 -080074 private TestPreference(Context context) {
tmfang464da582018-07-04 15:59:21 +080075 super(context);
76 }
77 }
78}