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 android.app.cts; 18 19 import static com.google.common.truth.Truth.assertThat; 20 21 import static org.junit.Assert.assertNotNull; 22 23 import android.app.ActivityOptions; 24 import android.os.Bundle; 25 26 import androidx.test.ext.junit.runners.AndroidJUnit4; 27 28 import org.junit.Test; 29 import org.junit.runner.RunWith; 30 31 @RunWith(AndroidJUnit4.class) 32 public class ActivityOptionsTest { 33 34 @Test testActivityOptionsBundle_makeBasic()35 public void testActivityOptionsBundle_makeBasic() throws Throwable { 36 ActivityOptions options = ActivityOptions.makeBasic(); 37 Bundle bundle = options.toBundle(); 38 39 assertNotNull(bundle); 40 } 41 42 @Test testGetPendingIntentBackgroundActivityLaunchAllowedDefault()43 public void testGetPendingIntentBackgroundActivityLaunchAllowedDefault() { 44 ActivityOptions options = ActivityOptions.makeBasic(); 45 46 // backwards compatibility 47 checkPendingIntentBackgroundActivityStartModeBeforeAndAfterBundle(options, true, 48 ActivityOptions.MODE_BACKGROUND_ACTIVITY_START_SYSTEM_DEFINED); 49 } 50 51 @Test testGetSetPendingIntentBackgroundActivityLaunchAllowedTrue()52 public void testGetSetPendingIntentBackgroundActivityLaunchAllowedTrue() { 53 ActivityOptions options = ActivityOptions.makeBasic(); 54 options.setPendingIntentBackgroundActivityLaunchAllowed(true); 55 56 checkPendingIntentBackgroundActivityStartModeBeforeAndAfterBundle(options, true, 57 ActivityOptions.MODE_BACKGROUND_ACTIVITY_START_ALLOWED); 58 } 59 60 @Test testGetSetPendingIntentBackgroundActivityLaunchAllowedFalse()61 public void testGetSetPendingIntentBackgroundActivityLaunchAllowedFalse() { 62 ActivityOptions options = ActivityOptions.makeBasic(); 63 options.setPendingIntentBackgroundActivityLaunchAllowed(false); 64 65 checkPendingIntentBackgroundActivityStartModeBeforeAndAfterBundle(options, false, 66 ActivityOptions.MODE_BACKGROUND_ACTIVITY_START_DENIED); 67 } 68 69 @Test testGetSetPendingIntentBackgroundActivityStartModeAllowed()70 public void testGetSetPendingIntentBackgroundActivityStartModeAllowed() { 71 ActivityOptions options = ActivityOptions.makeBasic() 72 .setPendingIntentBackgroundActivityStartMode( 73 ActivityOptions.MODE_BACKGROUND_ACTIVITY_START_ALLOWED); 74 checkPendingIntentBackgroundActivityStartModeBeforeAndAfterBundle(options, true, 75 76 ActivityOptions.MODE_BACKGROUND_ACTIVITY_START_ALLOWED); 77 } 78 79 @Test testGetSetPendingIntentBackgroundActivityStartModeDenied()80 public void testGetSetPendingIntentBackgroundActivityStartModeDenied() { 81 ActivityOptions options = ActivityOptions.makeBasic() 82 .setPendingIntentBackgroundActivityStartMode( 83 ActivityOptions.MODE_BACKGROUND_ACTIVITY_START_DENIED); 84 85 checkPendingIntentBackgroundActivityStartModeBeforeAndAfterBundle(options, false, 86 ActivityOptions.MODE_BACKGROUND_ACTIVITY_START_DENIED); 87 } 88 checkPendingIntentBackgroundActivityStartModeBeforeAndAfterBundle( ActivityOptions options, boolean allowed, int mode)89 private void checkPendingIntentBackgroundActivityStartModeBeforeAndAfterBundle( 90 ActivityOptions options, boolean allowed, int mode) { 91 assertThat(options.isPendingIntentBackgroundActivityLaunchAllowed()).isEqualTo(allowed); 92 assertThat(options.getPendingIntentBackgroundActivityStartMode()).isEqualTo(mode); 93 94 Bundle bundle = options.toBundle(); 95 96 String key = "android.pendingIntent.backgroundActivityAllowed"; 97 if (mode == ActivityOptions.MODE_BACKGROUND_ACTIVITY_START_SYSTEM_DEFINED) { 98 assertThat(bundle.containsKey(key)).isFalse(); 99 } else { 100 assertThat(bundle.containsKey(key)).isTrue(); 101 assertThat(bundle.getInt(key)).isEqualTo(mode); 102 } 103 } 104 105 @Test testGetPendingIntentCreatorBackgroundActivityLaunchAllowedDefault()106 public void testGetPendingIntentCreatorBackgroundActivityLaunchAllowedDefault() { 107 ActivityOptions options = ActivityOptions.makeBasic(); 108 109 // backwards compatibility 110 checkPendingIntentCreatorBackgroundActivityStartModeBeforeAndAfterBundle(options, 111 ActivityOptions.MODE_BACKGROUND_ACTIVITY_START_SYSTEM_DEFINED); 112 } 113 114 @Test testGetPendingIntentCreatorBackgroundActivityStartModeAllowed()115 public void testGetPendingIntentCreatorBackgroundActivityStartModeAllowed() { 116 ActivityOptions options = ActivityOptions.makeBasic() 117 .setPendingIntentCreatorBackgroundActivityStartMode( 118 ActivityOptions.MODE_BACKGROUND_ACTIVITY_START_ALLOWED); 119 checkPendingIntentCreatorBackgroundActivityStartModeBeforeAndAfterBundle(options, 120 121 ActivityOptions.MODE_BACKGROUND_ACTIVITY_START_ALLOWED); 122 } 123 124 @Test testGetPendingIntentCreatorBackgroundActivityStartModeDenied()125 public void testGetPendingIntentCreatorBackgroundActivityStartModeDenied() { 126 ActivityOptions options = ActivityOptions.makeBasic() 127 .setPendingIntentCreatorBackgroundActivityStartMode( 128 ActivityOptions.MODE_BACKGROUND_ACTIVITY_START_DENIED); 129 130 checkPendingIntentCreatorBackgroundActivityStartModeBeforeAndAfterBundle(options, 131 ActivityOptions.MODE_BACKGROUND_ACTIVITY_START_DENIED); 132 } 133 checkPendingIntentCreatorBackgroundActivityStartModeBeforeAndAfterBundle( ActivityOptions options, int mode)134 private void checkPendingIntentCreatorBackgroundActivityStartModeBeforeAndAfterBundle( 135 ActivityOptions options, int mode) { 136 assertThat(options.getPendingIntentCreatorBackgroundActivityStartMode()).isEqualTo(mode); 137 138 Bundle bundle = options.toBundle(); 139 140 String key = "android.activity.pendingIntentCreatorBackgroundActivityStartMode"; 141 if (mode == ActivityOptions.MODE_BACKGROUND_ACTIVITY_START_SYSTEM_DEFINED) { 142 assertThat(bundle.containsKey(key)).isFalse(); 143 } else { 144 assertThat(bundle.containsKey(key)).isTrue(); 145 assertThat(bundle.getInt(key)).isEqualTo(mode); 146 } 147 } 148 149 } 150