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 import android.support.test.InstrumentationRegistry; 22 23 import java.util.concurrent.TimeUnit; 24 25 public class AppUsageObserverTest extends BaseProfileOwnerTest { 26 27 static final int OBSERVER_LIMIT = 1000; 28 testMinTimeLimit()29 public void testMinTimeLimit() throws Exception { 30 final String[] packages = {"not.real.package.name"}; 31 final int obsId = 0; 32 UsageStatsManager usm = mContext.getSystemService(UsageStatsManager.class); 33 34 Intent intent = new Intent(Intent.ACTION_MAIN); 35 PendingIntent pendingIntent = PendingIntent.getActivity( 36 InstrumentationRegistry.getContext(), 37 1, intent, 0); 38 39 usm.registerAppUsageObserver(obsId, packages, 60, TimeUnit.SECONDS, pendingIntent); 40 usm.unregisterAppUsageObserver(obsId); 41 try { 42 usm.registerAppUsageObserver(obsId, packages, 59, TimeUnit.SECONDS, pendingIntent); 43 fail("Should have thrown an IllegalArgumentException"); 44 } catch (IllegalArgumentException expected) { 45 // Do nothing. Exception is expected. 46 } 47 } 48 testObserverLimit()49 public void testObserverLimit() throws Exception { 50 final String[] packages = {"not.real.package.name"}; 51 UsageStatsManager usm = mContext.getSystemService(UsageStatsManager.class); 52 53 Intent intent = new Intent(Intent.ACTION_MAIN); 54 PendingIntent pendingIntent = PendingIntent.getActivity( 55 InstrumentationRegistry.getContext(), 56 1, intent, 0); 57 58 for (int obsId = 0; obsId < OBSERVER_LIMIT; obsId++) { 59 usm.registerAppUsageObserver(obsId, packages, 60, TimeUnit.MINUTES, pendingIntent); 60 } 61 try { 62 usm.registerAppUsageObserver(OBSERVER_LIMIT, packages, 60, TimeUnit.MINUTES, 63 pendingIntent); 64 fail("Should have thrown an IllegalStateException"); 65 } catch (IllegalStateException expected) { 66 // Do nothing. Exception is expected. 67 } 68 for (int obsId = 0; obsId < OBSERVER_LIMIT; obsId++) { 69 usm.unregisterAppUsageObserver(obsId); 70 } 71 } 72 } 73