Home | History | Annotate | Download | only in defaultapps
      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 
     17 package com.android.settings.applications.defaultapps;
     18 
     19 import static org.mockito.Matchers.any;
     20 import static org.mockito.Matchers.eq;
     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.mockito.Mockito.when;
     25 
     26 import android.app.Activity;
     27 import android.content.Context;
     28 import android.os.UserManager;
     29 
     30 import com.android.settings.testutils.SettingsRobolectricTestRunner;
     31 import com.android.settingslib.wrapper.PackageManagerWrapper;
     32 
     33 import org.junit.Before;
     34 import org.junit.Test;
     35 import org.junit.runner.RunWith;
     36 import org.mockito.Answers;
     37 import org.mockito.Mock;
     38 import org.mockito.MockitoAnnotations;
     39 import org.robolectric.RuntimeEnvironment;
     40 import org.robolectric.util.ReflectionHelpers;
     41 
     42 @RunWith(SettingsRobolectricTestRunner.class)
     43 public class DefaultSmsPickerTest {
     44 
     45     private static final String TEST_APP_KEY = "com.android.settings/PickerTest";
     46 
     47     @Mock(answer = Answers.RETURNS_DEEP_STUBS)
     48     private Activity mActivity;
     49     @Mock
     50     private UserManager mUserManager;
     51     @Mock
     52     private DefaultSmsPicker.DefaultKeyUpdater mDefaultKeyUpdater;
     53     @Mock
     54     private PackageManagerWrapper mPackageManager;
     55 
     56     private DefaultSmsPicker mPicker;
     57 
     58     @Before
     59     public void setUp() {
     60         MockitoAnnotations.initMocks(this);
     61         when(mActivity.getSystemService(Context.USER_SERVICE)).thenReturn(mUserManager);
     62         mPicker = spy(new DefaultSmsPicker());
     63         mPicker.onAttach((Context) mActivity);
     64 
     65         ReflectionHelpers.setField(mPicker, "mPm", mPackageManager);
     66         ReflectionHelpers.setField(mPicker, "mDefaultKeyUpdater", mDefaultKeyUpdater);
     67         doReturn(RuntimeEnvironment.application).when(mPicker).getContext();
     68     }
     69 
     70     @Test
     71     public void setDefaultAppKey_shouldUpdateDefault() {
     72         mPicker.setDefaultKey(TEST_APP_KEY);
     73 
     74         verify(mDefaultKeyUpdater).setDefaultApplication(any(Context.class), eq(TEST_APP_KEY));
     75     }
     76 
     77     @Test
     78     public void getDefaultAppKey_shouldReturnDefault() {
     79         mPicker.getDefaultKey();
     80 
     81         verify(mDefaultKeyUpdater).getDefaultApplication(any(Context.class));
     82     }
     83 }
     84