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 
17 package com.android.settings.applications;
18 
19 import android.app.Activity;
20 import android.content.pm.ApplicationInfo;
21 import android.content.pm.PackageManager;
22 import android.os.Looper;
23 import android.widget.TextView;
24 import com.android.settings.R;
25 import com.android.settings.Settings;
26 import com.android.settings.SettingsRobolectricTestRunner;
27 import com.android.settings.TestConfig;
28 import com.android.settings.core.lifecycle.Lifecycle;
29 import com.android.settings.testutils.shadow.ShadowDynamicIndexableContentMonitor;
30 import com.android.settings.testutils.shadow.SettingsShadowResources;
31 import com.android.settings.testutils.shadow.SettingsShadowResources.SettingsShadowTheme;
32 import com.android.settings.testutils.shadow.ShadowEventLogWriter;
33 import com.android.settingslib.applications.ApplicationsState;
34 import org.junit.Before;
35 import org.junit.Test;
36 import org.junit.runner.RunWith;
37 import org.mockito.Mock;
38 import org.mockito.MockitoAnnotations;
39 import org.robolectric.annotation.Config;
40 import org.robolectric.util.ReflectionHelpers;
41 
42 import static org.mockito.Matchers.any;
43 import static org.mockito.Mockito.mock;
44 import static org.mockito.Mockito.verify;
45 import static org.mockito.Mockito.when;
46 
47 /**
48  * Tests for {@link ManageApplications}.
49  */
50 @RunWith(SettingsRobolectricTestRunner.class)
51 // TODO: Consider making the shadow class set global using a robolectric.properties file.
52 @Config(manifest = TestConfig.MANIFEST_PATH,
53         sdk = TestConfig.SDK_VERSION,
54         shadows = {
55                 SettingsShadowResources.class,
56                 SettingsShadowTheme.class,
57                 ShadowDynamicIndexableContentMonitor.class,
58                 ShadowEventLogWriter.class
59         })
60 public class ManageApplicationsTest {
61 
62     @Mock private ApplicationsState mState;
63     @Mock private ApplicationsState.Session mSession;
64 
65     private Looper mBgLooper;
66     private ManageApplications mFragment;
67 
68     @Before
setUp()69     public void setUp() {
70         MockitoAnnotations.initMocks(this);
71 
72         ReflectionHelpers.setStaticField(ApplicationsState.class, "sInstance", mState);
73         when(mState.newSession(any())).thenReturn(mSession);
74         mBgLooper = Looper.myLooper();
75         when(mState.getBackgroundLooper()).thenReturn(mBgLooper);
76 
77         mFragment = new ManageApplications();
78         ReflectionHelpers.setField(mFragment, "mLifecycle", new Lifecycle());
79     }
80 
81     @Test
launchFragment()82     public void launchFragment() {
83         SettingsRobolectricTestRunner.startSettingsFragment(
84                 mFragment, Settings.ManageApplicationsActivity.class);
85     }
86 
87     @Test
updateDisableView_appDisabledUntilUsed_shouldSetDisabled()88     public void updateDisableView_appDisabledUntilUsed_shouldSetDisabled() {
89         final TextView view = mock(TextView.class);
90         final ApplicationInfo info = new ApplicationInfo();
91         info.flags = ApplicationInfo.FLAG_INSTALLED;
92         info.enabled = true;
93         info.enabledSetting = PackageManager.COMPONENT_ENABLED_STATE_DISABLED_UNTIL_USED;
94         ManageApplications fragment = mock(ManageApplications.class);
95         when(fragment.getActivity()).thenReturn(mock(Activity.class));
96         final ManageApplications.ApplicationsAdapter adapter =
97             new ManageApplications.ApplicationsAdapter(mState, fragment, 0);
98 
99         adapter.updateDisableView(view, info);
100 
101         verify(view).setText(R.string.disabled);
102     }
103 }
104