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.development;
18
19import static com.android.settings.development.KeepActivitiesPreferenceController.SETTING_VALUE_OFF;
20import static org.mockito.Mockito.doReturn;
21import static org.mockito.Mockito.spy;
22import static org.mockito.Mockito.verify;
23import static org.mockito.Mockito.when;
24
25import android.app.IActivityManager;
26import android.content.Context;
27import android.os.RemoteException;
28import android.provider.Settings;
29import android.support.v14.preference.SwitchPreference;
30import android.support.v7.preference.PreferenceScreen;
31
32import com.android.settings.testutils.SettingsRobolectricTestRunner;
33
34import org.junit.Before;
35import org.junit.Test;
36import org.junit.runner.RunWith;
37import org.mockito.Mock;
38import org.mockito.MockitoAnnotations;
39import org.robolectric.RuntimeEnvironment;
40
41@RunWith(SettingsRobolectricTestRunner.class)
42public class KeepActivitiesPreferenceControllerTest {
43
44    private static final int SETTING_VALUE_ON = 1;
45
46    @Mock
47    private SwitchPreference mPreference;
48    @Mock
49    private PreferenceScreen mPreferenceScreen;
50    @Mock
51    private IActivityManager mActivityManager;
52
53    private Context mContext;
54    private KeepActivitiesPreferenceController mController;
55
56    @Before
57    public void setup() {
58        MockitoAnnotations.initMocks(this);
59        mContext = RuntimeEnvironment.application;
60        mController = spy(new KeepActivitiesPreferenceController(mContext));
61        doReturn(mActivityManager).when(mController).getActivityManager();
62        when(mPreferenceScreen.findPreference(mController.getPreferenceKey()))
63            .thenReturn(mPreference);
64        mController.displayPreference(mPreferenceScreen);
65    }
66
67    @Test
68    public void onPreferenceChanged_settingEnabled_turnOnDestroyActivities()
69            throws RemoteException {
70        mController.onPreferenceChange(mPreference, true /* new value */);
71
72        verify(mActivityManager).setAlwaysFinish(true);
73    }
74
75    @Test
76    public void onPreferenceChanged_settingDisabled_turnOffDestroyActivities()
77            throws RemoteException {
78        mController.onPreferenceChange(mPreference, false /* new value */);
79
80        verify(mActivityManager).setAlwaysFinish(false);
81    }
82
83    @Test
84    public void updateState_settingEnabled_preferenceShouldBeChecked() {
85        Settings.System.putInt(mContext.getContentResolver(),
86                Settings.Global.ALWAYS_FINISH_ACTIVITIES, SETTING_VALUE_ON);
87        mController.updateState(mPreference);
88
89        verify(mPreference).setChecked(true);
90    }
91
92    @Test
93    public void updateState_settingDisabled_preferenceShouldNotBeChecked() {
94        Settings.System.putInt(mContext.getContentResolver(),
95                Settings.Global.ALWAYS_FINISH_ACTIVITIES, SETTING_VALUE_OFF);
96        mController.updateState(mPreference);
97
98        verify(mPreference).setChecked(false);
99    }
100
101    @Test
102    public void onDeveloperOptionsDisabled_shouldDisablePreference() throws RemoteException {
103        mController.onDeveloperOptionsSwitchDisabled();
104
105        verify(mActivityManager).setAlwaysFinish(false);
106        verify(mPreference).setEnabled(false);
107        verify(mPreference).setChecked(false);
108    }
109}
110