• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2024 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 android.content.ComponentName;
23 import android.content.ContentValues;
24 import android.content.Context;
25 
26 import androidx.test.annotation.UiThreadTest;
27 import androidx.test.filters.SmallTest;
28 import androidx.test.platform.app.InstrumentationRegistry;
29 import androidx.test.runner.AndroidJUnit4;
30 
31 import com.android.bluetooth.btservice.AdapterService;
32 
33 import org.junit.Rule;
34 import org.junit.Test;
35 import org.junit.runner.RunWith;
36 import org.mockito.junit.MockitoJUnit;
37 import org.mockito.junit.MockitoRule;
38 
39 @SmallTest
40 @RunWith(AndroidJUnit4.class)
41 public class BluetoothOppServiceCleanupTest {
42     @Rule public MockitoRule mockitoRule = MockitoJUnit.rule();
43 
44     private final Context mTargetContext =
45             InstrumentationRegistry.getInstrumentation().getTargetContext();
46 
47     @Test
48     @UiThreadTest
testStopAndCleanup()49     public void testStopAndCleanup() {
50         AdapterService adapterService = new AdapterService(mTargetContext);
51 
52         // Don't need to disable again since it will be handled in OppService.stop
53         enableBtOppProvider();
54 
55         // Add thousands of placeholder rows
56         for (int i = 0; i < 2000; i++) {
57             ContentValues values = new ContentValues();
58             mTargetContext.getContentResolver().insert(BluetoothShare.CONTENT_URI, values);
59         }
60 
61         try {
62             BluetoothOppService service = new BluetoothOppService(adapterService);
63             service.start();
64             service.setAvailable(true);
65 
66             // Call stop while UpdateThread is running.
67             service.stop();
68             service.cleanup();
69         } finally {
70             mTargetContext.getContentResolver().delete(BluetoothShare.CONTENT_URI, null, null);
71         }
72     }
73 
enableBtOppProvider()74     private void enableBtOppProvider() {
75         mTargetContext
76                 .getPackageManager()
77                 .setApplicationEnabledSetting(
78                         mTargetContext.getPackageName(),
79                         COMPONENT_ENABLED_STATE_ENABLED,
80                         DONT_KILL_APP);
81 
82         ComponentName providerName =
83                 new ComponentName(mTargetContext, BluetoothOppProvider.class.getCanonicalName());
84         mTargetContext
85                 .getPackageManager()
86                 .setComponentEnabledSetting(
87                         providerName, COMPONENT_ENABLED_STATE_ENABLED, DONT_KILL_APP);
88     }
89 }
90