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 17 package com.android.settingslib.applications; 18 19 import static com.google.common.truth.Truth.assertThat; 20 import static org.mockito.ArgumentMatchers.anyList; 21 import static org.mockito.Mockito.mock; 22 import static org.mockito.Mockito.times; 23 import static org.mockito.Mockito.verify; 24 25 import android.content.ComponentName; 26 import android.provider.Settings; 27 28 import com.android.settingslib.SettingsLibRobolectricTestRunner; 29 30 import org.junit.Before; 31 import org.junit.Test; 32 import org.junit.runner.RunWith; 33 import org.robolectric.RuntimeEnvironment; 34 35 @RunWith(SettingsLibRobolectricTestRunner.class) 36 public class ServiceListingTest { 37 38 private static final String TEST_SETTING = "testSetting"; 39 private static final String TEST_INTENT = "com.example.intent"; 40 41 private ServiceListing mServiceListing; 42 43 @Before 44 public void setUp() { 45 mServiceListing = new ServiceListing.Builder(RuntimeEnvironment.application) 46 .setTag("testTag") 47 .setSetting(TEST_SETTING) 48 .setNoun("testNoun") 49 .setIntentAction(TEST_INTENT) 50 .setPermission("testPermission") 51 .build(); 52 } 53 54 @Test 55 public void testCallback() { 56 ServiceListing.Callback callback = mock(ServiceListing.Callback.class); 57 mServiceListing.addCallback(callback); 58 mServiceListing.reload(); 59 verify(callback, times(1)).onServicesReloaded(anyList()); 60 mServiceListing.removeCallback(callback); 61 mServiceListing.reload(); 62 verify(callback, times(1)).onServicesReloaded(anyList()); 63 } 64 65 @Test 66 public void testSaveLoad() { 67 ComponentName testComponent1 = new ComponentName("testPackage1", "testClass1"); 68 ComponentName testComponent2 = new ComponentName("testPackage2", "testClass2"); 69 Settings.Secure.putString(RuntimeEnvironment.application.getContentResolver(), 70 TEST_SETTING, 71 testComponent1.flattenToString() + ":" + testComponent2.flattenToString()); 72 73 mServiceListing.reload(); 74 75 assertThat(mServiceListing.isEnabled(testComponent1)).isTrue(); 76 assertThat(mServiceListing.isEnabled(testComponent2)).isTrue(); 77 assertThat(Settings.Secure.getString(RuntimeEnvironment.application.getContentResolver(), 78 TEST_SETTING)).contains(testComponent1.flattenToString()); 79 assertThat(Settings.Secure.getString(RuntimeEnvironment.application.getContentResolver(), 80 TEST_SETTING)).contains(testComponent2.flattenToString()); 81 82 mServiceListing.setEnabled(testComponent1, false); 83 84 assertThat(mServiceListing.isEnabled(testComponent1)).isFalse(); 85 assertThat(mServiceListing.isEnabled(testComponent2)).isTrue(); 86 assertThat(Settings.Secure.getString(RuntimeEnvironment.application.getContentResolver(), 87 TEST_SETTING)).doesNotContain(testComponent1.flattenToString()); 88 assertThat(Settings.Secure.getString(RuntimeEnvironment.application.getContentResolver(), 89 TEST_SETTING)).contains(testComponent2.flattenToString()); 90 91 mServiceListing.setEnabled(testComponent1, true); 92 93 assertThat(mServiceListing.isEnabled(testComponent1)).isTrue(); 94 assertThat(mServiceListing.isEnabled(testComponent2)).isTrue(); 95 assertThat(Settings.Secure.getString(RuntimeEnvironment.application.getContentResolver(), 96 TEST_SETTING)).contains(testComponent1.flattenToString()); 97 assertThat(Settings.Secure.getString(RuntimeEnvironment.application.getContentResolver(), 98 TEST_SETTING)).contains(testComponent2.flattenToString()); 99 } 100 } 101