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