BackupSettingsActivityTest.java revision f8b3154586c48dd26fac3545b614e1ff6fc5cf70
1f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov/*
2f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov * Copyright (C) 2017 The Android Open Source Project
3f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov *
4f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov * Licensed under the Apache License, Version 2.0 (the "License");
5f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov * you may not use this file except in compliance with the License.
6f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov * You may obtain a copy of the License at
7f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov *
8f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov *      http://www.apache.org/licenses/LICENSE-2.0
9f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov *
10f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov * Unless required by applicable law or agreed to in writing, software
11f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov * distributed under the License is distributed on an "AS IS" BASIS,
12f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov * See the License for the specific language governing permissions and
14f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov * limitations under the License
15f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov */
16f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov
17f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippovpackage com.android.settings.backup;
18f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov
19f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippovimport static org.mockito.Matchers.any;
20f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippovimport static org.mockito.Matchers.anyInt;
21f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippovimport static org.mockito.Matchers.isA;
22f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippovimport static org.mockito.Mockito.doReturn;
23f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippovimport static org.mockito.Mockito.mock;
24f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippovimport static org.mockito.Mockito.spy;
25f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippovimport static org.mockito.Mockito.verify;
26f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippovimport static org.robolectric.Shadows.shadowOf;
27f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov
28f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippovimport android.app.Application;
29f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippovimport android.app.Fragment;
30f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippovimport android.app.FragmentManager;
31f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippovimport android.app.FragmentTransaction;
32f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippovimport android.app.backup.IBackupManager;
33f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippovimport android.content.ComponentName;
34f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippovimport android.content.Context;
35f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippovimport android.content.Intent;
36f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippovimport android.content.pm.PackageManager;
37f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippovimport android.os.IBinder;
38f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov
39f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippovimport com.android.settings.SettingsRobolectricTestRunner;
40f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippovimport com.android.settings.TestConfig;
41f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov
42f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippovimport org.junit.Before;
43f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippovimport org.junit.Test;
44f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippovimport org.junit.runner.RunWith;
45f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippovimport org.mockito.Mock;
46f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippovimport org.mockito.MockitoAnnotations;
47f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippovimport org.robolectric.Robolectric;
48f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippovimport org.robolectric.RuntimeEnvironment;
49f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippovimport org.robolectric.annotation.Config;
50f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippovimport org.robolectric.annotation.Implementation;
51f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippovimport org.robolectric.annotation.Implements;
52f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippovimport org.robolectric.res.builder.RobolectricPackageManager;
53f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippovimport org.robolectric.util.ActivityController;
54f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippovimport org.robolectric.shadows.ShadowActivity;
55f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov
56f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippovimport static com.google.common.truth.Truth.assertThat;
57f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov
58f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov
59f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov@RunWith(SettingsRobolectricTestRunner.class)
60f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION,
61f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov        shadows = {BackupSettingsActivityTest.ShadowBackupSettingsHelper.class})
62f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippovpublic class BackupSettingsActivityTest {
63f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov    private ActivityController<BackupSettingsActivity> mActivityController;
64f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov    private BackupSettingsActivity mActivity;
65f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov    private Application mApplication;
66f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov    private RobolectricPackageManager mPackageManager;
67f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov    private static boolean mIsBackupProvidedByOEM;
68f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov
69f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov    @Mock
70f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov    private FragmentManager mFragmentManager;
71f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov
72f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov    @Mock
73f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov    private FragmentTransaction mFragmentTransaction;
74f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov    @Mock
75f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov    private static Intent mIntent;
76f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov    @Mock
77f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov    private ComponentName mComponent;
78f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov
79f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov    @Before
80f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov    public void setUp() {
81f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov        MockitoAnnotations.initMocks(this);
82f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov
83f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov        mApplication = RuntimeEnvironment.application;
84f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov        mActivityController = Robolectric.buildActivity(BackupSettingsActivity.class);
85f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov        mActivity = mActivityController.get();
86f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov        mPackageManager = (RobolectricPackageManager) mApplication.getPackageManager();
87f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov        doReturn(mComponent).when(mIntent).getComponent();
88f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov
89f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov    }
90f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov
91f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov    @Test
92f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov    public void onCreate_launchActivity() {
93f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov        mIsBackupProvidedByOEM = false;
94f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov
95f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov        // Testing the scenario when the activity is disabled
96f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov        mPackageManager.setComponentEnabledSetting(mComponent,
97f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov                PackageManager.COMPONENT_ENABLED_STATE_DISABLED, 0);
98f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov
99f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov        mActivityController.create();
100f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov
101f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov        // Verify that the component to launch was enabled.
102f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov        assertThat(mPackageManager.getComponentState(mComponent).newState)
103f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov                .isEqualTo(PackageManager.COMPONENT_ENABLED_STATE_ENABLED);
104f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov
105f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov        // Verify that the intent returned by BackupSettingsHelper.getIntentForBackupSettings()
106f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov        // was launched.
107f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov        assertThat(shadowOf(mApplication).getNextStartedActivity()).isEqualTo(mIntent);
108f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov    }
109f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov
110f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov    @Test
111f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov    public void onCreate_hasManufacturerIntent() {
112f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov        mIsBackupProvidedByOEM = true;
113f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov
114f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov        // Fragments are tested separately, so mock out the manager.
115f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov        mActivity.setFragmentManager(mFragmentManager);
116f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov        doReturn(mFragmentTransaction).when(mFragmentTransaction).replace(anyInt(),
117f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov                any(Fragment.class));
118f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov        doReturn(mFragmentTransaction).when(mFragmentManager).beginTransaction();
119f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov
120f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov        mActivityController.create();
121f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov
122f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov        assertThat(shadowOf(mApplication).getNextStartedActivity()).isNull();
123f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov        verify(mFragmentTransaction).replace(anyInt(), isA(BackupSettingsFragment.class));
124f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov
125f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov    }
126f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov
127f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov    @Implements(BackupSettingsHelper.class)
128f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov    public static class ShadowBackupSettingsHelper {
129f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov        @Implementation
130f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov        public Intent getIntentForBackupSettings() {
131f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov            return mIntent;
132f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov        }
133f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov
134f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov        @Implementation
135f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov        public boolean isBackupProvidedByManufacturer() {
136f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov            return mIsBackupProvidedByOEM;
137f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov        }
138f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov    }
139f8b3154586c48dd26fac3545b614e1ff6fc5cf70Anton Philippov}
140