1 /* 2 * Copyright 2025 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.bluetooth.opp; 18 19 import static android.content.pm.PackageManager.COMPONENT_ENABLED_STATE_ENABLED; 20 import static android.content.pm.PackageManager.DONT_KILL_APP; 21 22 import static com.android.bluetooth.TestUtils.MockitoRule; 23 24 import android.content.ComponentName; 25 import android.content.ContentValues; 26 import android.content.Context; 27 28 import androidx.test.annotation.UiThreadTest; 29 import androidx.test.filters.SmallTest; 30 import androidx.test.platform.app.InstrumentationRegistry; 31 import androidx.test.runner.AndroidJUnit4; 32 33 import com.android.bluetooth.btservice.AdapterService; 34 35 import org.junit.Rule; 36 import org.junit.Test; 37 import org.junit.runner.RunWith; 38 39 /** Test cases for {@link BluetoothOppServiceCleanup}. */ 40 @SmallTest 41 @RunWith(AndroidJUnit4.class) 42 public class BluetoothOppServiceCleanupTest { 43 @Rule public final MockitoRule mMockitoRule = new MockitoRule(); 44 45 private final Context mTargetContext = 46 InstrumentationRegistry.getInstrumentation().getTargetContext(); 47 48 @Test 49 @UiThreadTest testCleanup()50 public void testCleanup() throws Exception { 51 AdapterService adapterService = new AdapterService(mTargetContext); 52 53 // Don't need to disable again since it will be handled in OppService.cleanup 54 enableBtOppProvider(); 55 56 // Add thousands of placeholder rows 57 for (int i = 0; i < 2000; i++) { 58 ContentValues values = new ContentValues(); 59 mTargetContext.getContentResolver().insert(BluetoothShare.CONTENT_URI, values); 60 } 61 62 BluetoothOppService service = null; 63 try { 64 service = new BluetoothOppService(adapterService); 65 service.setAvailable(true); 66 67 // Call cleanup while UpdateThread is running. 68 service.cleanup(); 69 } finally { 70 if (service != null) { 71 Thread updateNotificationThread = service.mNotifier.mUpdateNotificationThread; 72 if (updateNotificationThread != null) { 73 updateNotificationThread.join(); 74 } 75 } 76 mTargetContext.getContentResolver().delete(BluetoothShare.CONTENT_URI, null, null); 77 } 78 } 79 enableBtOppProvider()80 private void enableBtOppProvider() { 81 mTargetContext 82 .getPackageManager() 83 .setApplicationEnabledSetting( 84 mTargetContext.getPackageName(), 85 COMPONENT_ENABLED_STATE_ENABLED, 86 DONT_KILL_APP); 87 88 ComponentName providerName = 89 new ComponentName(mTargetContext, BluetoothOppProvider.class.getCanonicalName()); 90 mTargetContext 91 .getPackageManager() 92 .setComponentEnabledSetting( 93 providerName, COMPONENT_ENABLED_STATE_ENABLED, DONT_KILL_APP); 94 } 95 } 96