Home | History | Annotate | Download | only in settings
      1 /*
      2  * Copyright (C) 2016 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;
     17 
     18 import android.app.Dialog;
     19 import android.app.Fragment;
     20 
     21 import org.junit.Before;
     22 import org.junit.Test;
     23 import org.junit.runner.RunWith;
     24 import org.mockito.Mock;
     25 import org.mockito.MockitoAnnotations;
     26 import org.robolectric.annotation.Config;
     27 
     28 import static org.junit.Assert.fail;
     29 import static org.mockito.Mockito.times;
     30 import static org.mockito.Mockito.verify;
     31 import static org.mockito.Mockito.when;
     32 
     33 import com.android.settings.testutils.SettingsRobolectricTestRunner;
     34 
     35 @RunWith(SettingsRobolectricTestRunner.class)
     36 @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
     37 public class SettingsDialogFragmentTest {
     38 
     39     private static final int DIALOG_ID = 15;
     40 
     41     @Mock
     42     private DialogCreatableFragment mDialogCreatable;
     43     private SettingsPreferenceFragment.SettingsDialogFragment mDialogFragment;
     44 
     45     @Before
     46     public void setUp() {
     47         MockitoAnnotations.initMocks(this);
     48     }
     49 
     50     @Test
     51     public void testGetMetrics_shouldGetMetricFromDialogCreatable() {
     52         when(mDialogCreatable.getDialogMetricsCategory(DIALOG_ID)).thenReturn(1);
     53 
     54         mDialogFragment =
     55                 new SettingsPreferenceFragment.SettingsDialogFragment(mDialogCreatable, DIALOG_ID);
     56         mDialogFragment.getMetricsCategory();
     57 
     58         // getDialogMetricsCategory called in constructor, and explicitly in test.
     59         verify(mDialogCreatable, times(2)).getDialogMetricsCategory(DIALOG_ID);
     60     }
     61 
     62     @Test
     63     public void testGetInvalidMetricsValue_shouldCrash() {
     64         when(mDialogCreatable.getDialogMetricsCategory(DIALOG_ID)).thenReturn(-1);
     65 
     66         try {
     67             mDialogFragment = new SettingsPreferenceFragment.SettingsDialogFragment(
     68                     mDialogCreatable, DIALOG_ID);
     69         } catch (IllegalStateException e) {
     70             // getDialogMetricsCategory called in constructor
     71             verify(mDialogCreatable).getDialogMetricsCategory(DIALOG_ID);
     72             return;
     73         }
     74         fail("Should fail with IllegalStateException");
     75     }
     76 
     77     public static class DialogCreatableFragment extends Fragment implements DialogCreatable {
     78 
     79         @Override
     80         public Dialog onCreateDialog(int dialogId) {
     81             return null;
     82         }
     83 
     84         @Override
     85         public int getDialogMetricsCategory(int dialogId) {
     86             return 0;
     87         }
     88     }
     89 }
     90