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 @RunWith(SettingsRobolectricTestRunner.class)
     34 @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
     35 public class SettingsDialogFragmentTest {
     36 
     37     private static final int DIALOG_ID = 15;
     38 
     39     @Mock
     40     private DialogCreatableFragment mDialogCreatable;
     41     private SettingsPreferenceFragment.SettingsDialogFragment mDialogFragment;
     42 
     43     @Before
     44     public void setUp() {
     45         MockitoAnnotations.initMocks(this);
     46     }
     47 
     48     @Test
     49     public void testGetMetrics_shouldGetMetricFromDialogCreatable() {
     50         when(mDialogCreatable.getDialogMetricsCategory(DIALOG_ID)).thenReturn(1);
     51 
     52         mDialogFragment =
     53                 new SettingsPreferenceFragment.SettingsDialogFragment(mDialogCreatable, DIALOG_ID);
     54         mDialogFragment.getMetricsCategory();
     55 
     56         // getDialogMetricsCategory called in constructor, and explicitly in test.
     57         verify(mDialogCreatable, times(2)).getDialogMetricsCategory(DIALOG_ID);
     58     }
     59 
     60     @Test
     61     public void testGetInvalidMetricsValue_shouldCrash() {
     62         when(mDialogCreatable.getDialogMetricsCategory(DIALOG_ID)).thenReturn(-1);
     63 
     64         try {
     65             mDialogFragment = new SettingsPreferenceFragment.SettingsDialogFragment(
     66                     mDialogCreatable, DIALOG_ID);
     67         } catch (IllegalStateException e) {
     68             // getDialogMetricsCategory called in constructor
     69             verify(mDialogCreatable).getDialogMetricsCategory(DIALOG_ID);
     70             return;
     71         }
     72         fail("Should fail with IllegalStateException");
     73     }
     74 
     75     public static class DialogCreatableFragment extends Fragment implements DialogCreatable {
     76 
     77         @Override
     78         public Dialog onCreateDialog(int dialogId) {
     79             return null;
     80         }
     81 
     82         @Override
     83         public int getDialogMetricsCategory(int dialogId) {
     84             return 0;
     85         }
     86     }
     87 }
     88