• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 package com.android.settings.bluetooth;
17 
18 import static com.google.common.truth.Truth.assertThat;
19 
20 import android.view.inputmethod.EditorInfo;
21 import android.widget.TextView;
22 
23 import org.junit.Before;
24 import org.junit.Test;
25 import org.junit.runner.RunWith;
26 import org.mockito.MockitoAnnotations;
27 import org.robolectric.RobolectricTestRunner;
28 import org.robolectric.RuntimeEnvironment;
29 
30 @RunWith(RobolectricTestRunner.class)
31 public class BluetoothNameDialogFragmentTest {
32 
33     private TestBluetoothNameDialogFragment mBluetoothNameDialogFragment;
34     private TextView mTextView;
35 
36     private static final String NAME_FOR_TEST = "test_device_name";
37 
38     @Before
setUp()39     public void setUp() {
40         MockitoAnnotations.initMocks(this);
41 
42         mBluetoothNameDialogFragment = new TestBluetoothNameDialogFragment();
43         mTextView = new TextView(RuntimeEnvironment.application);
44     }
45 
46     @Test
onEditorAction_dialogNull_shouldNotCrash()47     public void onEditorAction_dialogNull_shouldNotCrash() {
48         mBluetoothNameDialogFragment.mAlertDialog = null;
49 
50         // Should not crash
51         assertThat(
52                 mBluetoothNameDialogFragment.onEditorAction(mTextView, EditorInfo.IME_ACTION_DONE,
53                         null)).isTrue();
54     }
55 
56     @Test
onEditorAction_ImeNull_setsDeviceName()57     public void onEditorAction_ImeNull_setsDeviceName() {
58 
59 
60         mTextView.setText(NAME_FOR_TEST);
61         assertThat(
62                 mBluetoothNameDialogFragment.onEditorAction(mTextView, EditorInfo.IME_NULL,
63                         null)).isTrue();
64         assertThat(mBluetoothNameDialogFragment.getDeviceName()).isEqualTo(NAME_FOR_TEST);
65     }
66 
67     /**
68      * Test fragment for {@link BluetoothNameDialogFragment} to test common methods
69      */
70     public static class TestBluetoothNameDialogFragment extends BluetoothNameDialogFragment {
71 
72         private String mName;
73 
74         @Override
getDialogTitle()75         protected int getDialogTitle() {
76             return 0;
77         }
78 
79         @Override
getDeviceName()80         protected String getDeviceName() {
81             return mName;
82         }
83 
84         @Override
setDeviceName(String deviceName)85         protected void setDeviceName(String deviceName) {
86             mName = deviceName;
87         }
88 
89         @Override
getMetricsCategory()90         public int getMetricsCategory() {
91             return 0;
92         }
93     }
94 }
95