1 /* 2 * Copyright (C) 2021 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 static android.provider.DeviceConfig.NAMESPACE_APP_HIBERNATION; 20 21 import static com.android.settings.Utils.PROPERTY_APP_HIBERNATION_ENABLED; 22 import static com.android.settings.core.BasePreferenceController.AVAILABLE; 23 24 import static com.google.common.truth.Truth.assertThat; 25 26 import static org.mockito.Mockito.mock; 27 import static org.mockito.Mockito.spy; 28 import static org.mockito.Mockito.when; 29 30 import android.app.usage.IUsageStatsManager; 31 import android.app.usage.UsageStatsManager; 32 import android.apphibernation.AppHibernationManager; 33 import android.content.Context; 34 import android.content.pm.PackageManager; 35 import android.os.Looper; 36 import android.provider.DeviceConfig; 37 38 import androidx.preference.Preference; 39 import androidx.preference.PreferenceManager; 40 import androidx.preference.PreferenceScreen; 41 import androidx.test.core.app.ApplicationProvider; 42 import androidx.test.ext.junit.runners.AndroidJUnit4; 43 44 import org.junit.Before; 45 import org.junit.Test; 46 import org.junit.runner.RunWith; 47 import org.mockito.Mock; 48 import org.mockito.MockitoAnnotations; 49 50 @RunWith(AndroidJUnit4.class) 51 public class HibernatedAppsPreferenceControllerTest { 52 53 @Mock 54 PackageManager mPackageManager; 55 @Mock 56 AppHibernationManager mAppHibernationManager; 57 @Mock 58 IUsageStatsManager mIUsageStatsManager; 59 PreferenceScreen mPreferenceScreen; 60 private static final String KEY = "key"; 61 private Context mContext; 62 private HibernatedAppsPreferenceController mController; 63 64 @Before setUp()65 public void setUp() { 66 if (Looper.myLooper() == null) { 67 Looper.prepare(); 68 } 69 MockitoAnnotations.initMocks(this); 70 DeviceConfig.setProperty(NAMESPACE_APP_HIBERNATION, PROPERTY_APP_HIBERNATION_ENABLED, 71 "true", false); 72 mContext = spy(ApplicationProvider.getApplicationContext()); 73 when(mContext.getPackageManager()).thenReturn(mPackageManager); 74 when(mContext.getSystemService(AppHibernationManager.class)) 75 .thenReturn(mAppHibernationManager); 76 when(mContext.getSystemService(UsageStatsManager.class)).thenReturn( 77 new UsageStatsManager(mContext, mIUsageStatsManager)); 78 79 PreferenceManager manager = new PreferenceManager(mContext); 80 mPreferenceScreen = manager.createPreferenceScreen(mContext); 81 Preference preference = mock(Preference.class); 82 when(preference.getKey()).thenReturn(KEY); 83 mPreferenceScreen.addPreference(preference); 84 85 mController = new HibernatedAppsPreferenceController(mContext, KEY, 86 command -> command.run()); 87 } 88 89 @Test getAvailabilityStatus_featureDisabled_shouldNotReturnAvailable()90 public void getAvailabilityStatus_featureDisabled_shouldNotReturnAvailable() { 91 DeviceConfig.setProperty(NAMESPACE_APP_HIBERNATION, PROPERTY_APP_HIBERNATION_ENABLED, 92 "false", true); 93 94 assertThat((mController).getAvailabilityStatus()).isNotEqualTo(AVAILABLE); 95 } 96 } 97