1 /* 2 * Copyright (C) 2018 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 package com.android.cts.profileowner; 17 18 import android.app.PendingIntent; 19 import android.app.usage.UsageStatsManager; 20 import android.content.Intent; 21 22 import androidx.test.InstrumentationRegistry; 23 24 import java.time.Duration; 25 import java.util.concurrent.TimeUnit; 26 27 public class AppUsageObserverTest extends BaseProfileOwnerTest { 28 29 static final int OBSERVER_LIMIT = 1000; 30 testAppUsageObserver_MinTimeLimit()31 public void testAppUsageObserver_MinTimeLimit() throws Exception { 32 final String[] packages = {"not.real.package.name"}; 33 final int obsId = 0; 34 UsageStatsManager usm = mContext.getSystemService(UsageStatsManager.class); 35 36 Intent intent = new Intent(Intent.ACTION_MAIN).setPackage(mContext.getPackageName()); 37 PendingIntent pendingIntent = PendingIntent.getActivity( 38 InstrumentationRegistry.getContext(), 39 1, intent, PendingIntent.FLAG_MUTABLE); 40 41 usm.registerAppUsageObserver(obsId, packages, 60, TimeUnit.SECONDS, pendingIntent); 42 usm.unregisterAppUsageObserver(obsId); 43 try { 44 usm.registerAppUsageObserver(obsId, packages, 59, TimeUnit.SECONDS, pendingIntent); 45 fail("Should have thrown an IllegalArgumentException"); 46 } catch (IllegalArgumentException expected) { 47 // Do nothing. Exception is expected. 48 } 49 } 50 testUsageSessionObserver_MinTimeLimit()51 public void testUsageSessionObserver_MinTimeLimit() throws Exception { 52 final String[] packages = {"not.real.package.name"}; 53 final int obsId = 0; 54 UsageStatsManager usm = mContext.getSystemService(UsageStatsManager.class); 55 56 Intent intent = new Intent(Intent.ACTION_MAIN).setPackage(mContext.getPackageName()); 57 PendingIntent pendingIntent = PendingIntent.getActivity( 58 InstrumentationRegistry.getContext(), 59 1, intent, PendingIntent.FLAG_MUTABLE); 60 61 usm.registerUsageSessionObserver(obsId, packages, Duration.ofSeconds(60), 62 Duration.ofSeconds(10), pendingIntent, null); 63 usm.unregisterUsageSessionObserver(obsId); 64 try { 65 usm.registerUsageSessionObserver(obsId, packages, Duration.ofSeconds(59), 66 Duration.ofSeconds(10), pendingIntent, null); 67 fail("Should have thrown an IllegalArgumentException"); 68 } catch (IllegalArgumentException expected) { 69 // Do nothing. Exception is expected. 70 } 71 } 72 testObserverLimit()73 public void testObserverLimit() throws Exception { 74 final String[] packages = {"not.real.package.name"}; 75 UsageStatsManager usm = mContext.getSystemService(UsageStatsManager.class); 76 77 Intent intent = new Intent(Intent.ACTION_MAIN).setPackage(mContext.getPackageName()); 78 PendingIntent pendingIntent = PendingIntent.getActivity( 79 InstrumentationRegistry.getContext(), 80 1, intent, PendingIntent.FLAG_MUTABLE); 81 82 // Register too many AppUsageObservers 83 for (int obsId = 0; obsId < OBSERVER_LIMIT; obsId++) { 84 usm.registerAppUsageObserver(obsId, packages, 60, TimeUnit.MINUTES, pendingIntent); 85 } 86 try { 87 usm.registerAppUsageObserver(OBSERVER_LIMIT, packages, 60, TimeUnit.MINUTES, 88 pendingIntent); 89 fail("Should have thrown an IllegalStateException"); 90 } catch (IllegalStateException expected) { 91 // Do nothing. Exception is expected. 92 } 93 94 // Register too many UsageSessionObservers. 95 for (int obsId = 0; obsId < OBSERVER_LIMIT; obsId++) { 96 usm.registerUsageSessionObserver(obsId, packages, Duration.ofSeconds(60), 97 Duration.ofSeconds(10), pendingIntent, null); 98 } 99 try { 100 usm.registerUsageSessionObserver(OBSERVER_LIMIT, packages, Duration.ofSeconds(60), 101 Duration.ofSeconds(10), pendingIntent, null); 102 fail("Should have thrown an IllegalStateException"); 103 } catch (IllegalStateException expected) { 104 // Do nothing. Exception is expected. 105 } 106 107 for (int obsId = 0; obsId < OBSERVER_LIMIT; obsId++) { 108 usm.unregisterAppUsageObserver(obsId); 109 } 110 111 for (int obsId = 0; obsId < OBSERVER_LIMIT; obsId++) { 112 usm.unregisterUsageSessionObserver(obsId); 113 } 114 } 115 } 116