• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.settings.fuelgauge;
17 
18 import static com.google.common.truth.Truth.assertThat;
19 
20 import static org.mockito.Matchers.anyInt;
21 import static org.mockito.Mockito.doReturn;
22 import static org.mockito.Mockito.spy;
23 import static org.mockito.Mockito.verify;
24 import static org.robolectric.Shadows.shadowOf;
25 
26 import android.app.AlertDialog;
27 import android.app.Fragment;
28 import android.content.Context;
29 import android.content.DialogInterface;
30 
31 import com.android.settings.R;
32 import com.android.settings.testutils.SettingsRobolectricTestRunner;
33 import com.android.settings.TestConfig;
34 import com.android.settings.testutils.shadow.ShadowEventLogWriter;
35 
36 import org.junit.Before;
37 import org.junit.Test;
38 import org.junit.runner.RunWith;
39 import org.mockito.Mock;
40 import org.mockito.MockitoAnnotations;
41 import org.robolectric.RuntimeEnvironment;
42 import org.robolectric.annotation.Config;
43 import org.robolectric.shadows.ShadowAlertDialog;
44 import org.robolectric.shadows.ShadowDialog;
45 import org.robolectric.util.FragmentTestUtil;
46 
47 @RunWith(SettingsRobolectricTestRunner.class)
48 @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION, shadows = {
49         ShadowEventLogWriter.class
50 })
51 public class ButtonActionDialogFragmentTest {
52     private static final int FORCE_STOP_ID = ButtonActionDialogFragment.DialogType.FORCE_STOP;
53     private static final int DISABLE_ID = ButtonActionDialogFragment.DialogType.DISABLE;
54     private static final int SPECIAL_DISABLE_ID =
55             ButtonActionDialogFragment.DialogType.SPECIAL_DISABLE;
56     @Mock
57     private TestFragment mTargetFragment;
58     private ButtonActionDialogFragment mFragment;
59     private Context mShadowContext;
60 
61     @Before
setUp()62     public void setUp() {
63         MockitoAnnotations.initMocks(this);
64         mShadowContext = RuntimeEnvironment.application;
65 
66         mFragment = spy(ButtonActionDialogFragment.newInstance(FORCE_STOP_ID));
67         doReturn(mShadowContext).when(mFragment).getContext();
68         mFragment.setTargetFragment(mTargetFragment, 0);
69     }
70 
71     @Test
testOnClick_handleToTargetFragment()72     public void testOnClick_handleToTargetFragment() {
73         mFragment.onClick(null, 0);
74 
75         verify(mTargetFragment).handleDialogClick(anyInt());
76     }
77 
78     @Test
testOnCreateDialog_forceStopDialog()79     public void testOnCreateDialog_forceStopDialog() {
80         ButtonActionDialogFragment fragment = ButtonActionDialogFragment.newInstance(FORCE_STOP_ID);
81 
82         FragmentTestUtil.startFragment(fragment);
83 
84         final AlertDialog dialog = (AlertDialog) ShadowDialog.getLatestDialog();
85         ShadowAlertDialog shadowDialog = shadowOf(dialog);
86 
87         assertThat(shadowDialog.getMessage()).isEqualTo(
88                 mShadowContext.getString(R.string.force_stop_dlg_text));
89         assertThat(shadowDialog.getTitle()).isEqualTo(
90                 mShadowContext.getString(R.string.force_stop_dlg_title));
91         assertThat(dialog.getButton(DialogInterface.BUTTON_POSITIVE).getText()).isEqualTo(
92                 mShadowContext.getString(R.string.dlg_ok));
93         assertThat(dialog.getButton(DialogInterface.BUTTON_NEGATIVE).getText()).isEqualTo(
94                 mShadowContext.getString(R.string.dlg_cancel));
95     }
96 
97     @Test
testOnCreateDialog_disableDialog()98     public void testOnCreateDialog_disableDialog() {
99         ButtonActionDialogFragment fragment = ButtonActionDialogFragment.newInstance(DISABLE_ID);
100 
101         FragmentTestUtil.startFragment(fragment);
102 
103         final AlertDialog dialog = (AlertDialog) ShadowDialog.getLatestDialog();
104         ShadowAlertDialog shadowDialog = shadowOf(dialog);
105 
106         assertThat(shadowDialog.getMessage()).isEqualTo(
107                 mShadowContext.getString(R.string.app_disable_dlg_text));
108         assertThat(dialog.getButton(DialogInterface.BUTTON_POSITIVE).getText()).isEqualTo(
109                 mShadowContext.getString(R.string.app_disable_dlg_positive));
110         assertThat(dialog.getButton(DialogInterface.BUTTON_NEGATIVE).getText()).isEqualTo(
111                 mShadowContext.getString(R.string.dlg_cancel));
112     }
113 
114     @Test
testOnCreateDialog_specialDisableDialog()115     public void testOnCreateDialog_specialDisableDialog() {
116         ButtonActionDialogFragment fragment = ButtonActionDialogFragment.newInstance(
117                 SPECIAL_DISABLE_ID);
118 
119         FragmentTestUtil.startFragment(fragment);
120 
121         final AlertDialog dialog = (AlertDialog) ShadowDialog.getLatestDialog();
122         ShadowAlertDialog shadowDialog = shadowOf(dialog);
123 
124         assertThat(shadowDialog.getMessage()).isEqualTo(
125                 mShadowContext.getString(R.string.app_disable_dlg_text));
126         assertThat(dialog.getButton(DialogInterface.BUTTON_POSITIVE).getText()).isEqualTo(
127                 mShadowContext.getString(R.string.app_disable_dlg_positive));
128         assertThat(dialog.getButton(DialogInterface.BUTTON_NEGATIVE).getText()).isEqualTo(
129                 mShadowContext.getString(R.string.dlg_cancel));
130     }
131 
132     /**
133      * Test fragment that used as the target fragment, it must implement the
134      * {@link com.android.settings.fuelgauge.ButtonActionDialogFragment.AppButtonsDialogListener}
135      */
136     public static class TestFragment extends Fragment implements
137             ButtonActionDialogFragment.AppButtonsDialogListener {
138 
139         @Override
handleDialogClick(int type)140         public void handleDialogClick(int type) {
141             // do nothing
142         }
143     }
144 }
145