summaryrefslogtreecommitdiff
path: root/sdksandbox/tests/unittest/src/com/android/sdksandbox/SdkSandboxTest.java
diff options
context:
space:
mode:
Diffstat (limited to 'sdksandbox/tests/unittest/src/com/android/sdksandbox/SdkSandboxTest.java')
-rw-r--r--sdksandbox/tests/unittest/src/com/android/sdksandbox/SdkSandboxTest.java131
1 files changed, 38 insertions, 93 deletions
diff --git a/sdksandbox/tests/unittest/src/com/android/sdksandbox/SdkSandboxTest.java b/sdksandbox/tests/unittest/src/com/android/sdksandbox/SdkSandboxTest.java
index 0e7701b5eb..815596ec6e 100644
--- a/sdksandbox/tests/unittest/src/com/android/sdksandbox/SdkSandboxTest.java
+++ b/sdksandbox/tests/unittest/src/com/android/sdksandbox/SdkSandboxTest.java
@@ -18,8 +18,11 @@ package com.android.sdksandbox;
import static com.google.common.truth.Truth.assertThat;
+import static org.junit.Assert.assertThrows;
+
import android.app.sdksandbox.LoadSdkException;
import android.app.sdksandbox.SandboxedSdk;
+import android.app.sdksandbox.SdkSandboxLocalSingleton;
import android.app.sdksandbox.SharedPreferencesKey;
import android.app.sdksandbox.SharedPreferencesUpdate;
import android.app.sdksandbox.testutils.StubSdkToServiceLink;
@@ -30,10 +33,8 @@ import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Binder;
import android.os.Bundle;
-import android.os.IBinder;
import android.os.Looper;
import android.os.Process;
-import android.preference.PreferenceManager;
import android.view.SurfaceControlViewHost;
import androidx.test.platform.app.InstrumentationRegistry;
@@ -131,7 +132,27 @@ public class SdkSandboxTest {
@After
public void teardown() throws Exception {
- getClientSharedPreference().edit().clear().commit();
+ mService.getClientSharedPreferences().edit().clear().commit();
+ }
+
+ @Test
+ public void testSandboxInitialization_initializesSdkSandboxLocalSingleTon() throws Exception {
+ assertThrows(
+ IllegalStateException.class, () -> SdkSandboxLocalSingleton.getExistingInstance());
+
+ mService.initialize(new StubSdkToServiceLink());
+
+ assertThat(SdkSandboxLocalSingleton.getExistingInstance()).isNotNull();
+ }
+
+ @Test
+ public void testSandboxInitialization_clearsSyncedData() throws Exception {
+ // First write some data
+ mService.syncDataFromClient(TEST_UPDATE);
+
+ mService.initialize(new StubSdkToServiceLink());
+
+ assertThat(mService.getClientSharedPreferences().getAll()).isEmpty();
}
@Test
@@ -140,7 +161,6 @@ public class SdkSandboxTest {
RemoteCode mRemoteCode = new RemoteCode(latch);
mService.loadSdk(
CLIENT_PACKAGE_NAME,
- new Binder(),
mApplicationInfo,
SDK_NAME,
SDK_PROVIDER_CLASS,
@@ -148,8 +168,7 @@ public class SdkSandboxTest {
null,
new Bundle(),
mRemoteCode,
- SANDBOX_LATENCY_INFO,
- new StubSdkToServiceLink());
+ SANDBOX_LATENCY_INFO);
assertThat(latch.await(1, TimeUnit.MINUTES)).isTrue();
assertThat(mRemoteCode.mSuccessful).isTrue();
}
@@ -160,10 +179,8 @@ public class SdkSandboxTest {
RemoteCode mRemoteCode1 = new RemoteCode(latch1);
CountDownLatch latch2 = new CountDownLatch(1);
RemoteCode mRemoteCode2 = new RemoteCode(latch2);
- IBinder duplicateToken = new Binder();
mService.loadSdk(
CLIENT_PACKAGE_NAME,
- duplicateToken,
mApplicationInfo,
SDK_NAME,
SDK_PROVIDER_CLASS,
@@ -171,13 +188,11 @@ public class SdkSandboxTest {
null,
new Bundle(),
mRemoteCode1,
- SANDBOX_LATENCY_INFO,
- new StubSdkToServiceLink());
+ SANDBOX_LATENCY_INFO);
assertThat(latch1.await(1, TimeUnit.MINUTES)).isTrue();
assertThat(mRemoteCode1.mSuccessful).isTrue();
mService.loadSdk(
CLIENT_PACKAGE_NAME,
- duplicateToken,
mApplicationInfo,
SDK_NAME,
SDK_PROVIDER_CLASS,
@@ -185,8 +200,7 @@ public class SdkSandboxTest {
null,
new Bundle(),
mRemoteCode2,
- SANDBOX_LATENCY_INFO,
- new StubSdkToServiceLink());
+ SANDBOX_LATENCY_INFO);
assertThat(latch2.await(1, TimeUnit.MINUTES)).isTrue();
assertThat(mRemoteCode2.mSuccessful).isFalse();
assertThat(mRemoteCode2.mErrorCode)
@@ -194,49 +208,11 @@ public class SdkSandboxTest {
}
@Test
- public void testLoadingMultiple() throws Exception {
- CountDownLatch latch1 = new CountDownLatch(1);
- RemoteCode mRemoteCode1 = new RemoteCode(latch1);
- CountDownLatch latch2 = new CountDownLatch(1);
- RemoteCode mRemoteCode2 = new RemoteCode(latch2);
- StubSdkToServiceLink sdkToServiceLink = new StubSdkToServiceLink();
- mService.loadSdk(
- CLIENT_PACKAGE_NAME,
- new Binder(),
- mApplicationInfo,
- SDK_NAME,
- SDK_PROVIDER_CLASS,
- null,
- null,
- new Bundle(),
- mRemoteCode1,
- SANDBOX_LATENCY_INFO,
- sdkToServiceLink);
- mService.loadSdk(
- CLIENT_PACKAGE_NAME,
- new Binder(),
- mApplicationInfo,
- SDK_NAME,
- SDK_PROVIDER_CLASS,
- null,
- null,
- new Bundle(),
- mRemoteCode2,
- SANDBOX_LATENCY_INFO,
- sdkToServiceLink);
- assertThat(latch1.await(1, TimeUnit.MINUTES)).isTrue();
- assertThat(mRemoteCode1.mSuccessful).isTrue();
- assertThat(latch2.await(1, TimeUnit.MINUTES)).isTrue();
- assertThat(mRemoteCode2.mSuccessful).isTrue();
- }
-
- @Test
public void testRequestSurfacePackage() throws Exception {
CountDownLatch latch = new CountDownLatch(1);
RemoteCode mRemoteCode = new RemoteCode(latch);
mService.loadSdk(
CLIENT_PACKAGE_NAME,
- new Binder(),
mApplicationInfo,
SDK_NAME,
SDK_PROVIDER_CLASS,
@@ -244,8 +220,7 @@ public class SdkSandboxTest {
null,
new Bundle(),
mRemoteCode,
- SANDBOX_LATENCY_INFO,
- new StubSdkToServiceLink());
+ SANDBOX_LATENCY_INFO);
assertThat(latch.await(1, TimeUnit.MINUTES)).isTrue();
CountDownLatch surfaceLatch = new CountDownLatch(1);
@@ -271,7 +246,6 @@ public class SdkSandboxTest {
RemoteCode mRemoteCode = new RemoteCode(latch);
mService.loadSdk(
CLIENT_PACKAGE_NAME,
- new Binder(),
mApplicationInfo,
SDK_NAME,
SDK_PROVIDER_CLASS,
@@ -279,8 +253,7 @@ public class SdkSandboxTest {
null,
new Bundle(),
mRemoteCode,
- SANDBOX_LATENCY_INFO,
- new StubSdkToServiceLink());
+ SANDBOX_LATENCY_INFO);
assertThat(latch.await(1, TimeUnit.MINUTES)).isTrue();
CountDownLatch surfaceLatch = new CountDownLatch(1);
@@ -305,10 +278,6 @@ public class SdkSandboxTest {
@Test
public void testDump_NoSdk() {
- Mockito.doNothing()
- .when(mContext)
- .enforceCallingPermission(
- Mockito.eq("android.permission.DUMP"), Mockito.anyString());
final StringWriter stringWriter = new StringWriter();
mService.dump(new FileDescriptor(), new PrintWriter(stringWriter), new String[0]);
assertThat(stringWriter.toString()).contains("mHeldSdk is empty");
@@ -316,14 +285,8 @@ public class SdkSandboxTest {
@Test
public void testDump_WithSdk() {
- Mockito.doNothing()
- .when(mContext)
- .enforceCallingPermission(
- Mockito.eq("android.permission.DUMP"), Mockito.anyString());
-
mService.loadSdk(
CLIENT_PACKAGE_NAME,
- new Binder(),
mApplicationInfo,
SDK_NAME,
SDK_PROVIDER_CLASS,
@@ -331,8 +294,7 @@ public class SdkSandboxTest {
null,
new Bundle(),
new RemoteCode(new CountDownLatch(1)),
- SANDBOX_LATENCY_INFO,
- new StubSdkToServiceLink());
+ SANDBOX_LATENCY_INFO);
final StringWriter stringWriter = new StringWriter();
mService.dump(new FileDescriptor(), new PrintWriter(stringWriter), new String[0]);
@@ -363,17 +325,12 @@ public class SdkSandboxTest {
assertThat(callback.isDisabled()).isFalse();
}
- @Test(expected = SecurityException.class)
- public void testDump_WithoutPermission() {
- mService.dump(new FileDescriptor(), new PrintWriter(new StringWriter()), new String[0]);
- }
-
@Test
public void testSyncDataFromClient_StoresInClientSharedPreference() throws Exception {
mService.syncDataFromClient(TEST_UPDATE);
// Verify that ClientSharedPreference contains the synced data
- SharedPreferences pref = getClientSharedPreference();
+ SharedPreferences pref = mService.getClientSharedPreferences();
assertThat(pref.getAll().keySet()).containsExactlyElementsIn(TEST_DATA.keySet());
assertThat(pref.getAll().values()).containsExactlyElementsIn(TEST_DATA.values());
}
@@ -402,7 +359,7 @@ public class SdkSandboxTest {
mService.syncDataFromClient(update);
// Verify that ClientSharedPreference contains the synced data
- SharedPreferences pref = getClientSharedPreference();
+ SharedPreferences pref = mService.getClientSharedPreferences();
assertThat(pref.getAll().keySet()).containsExactlyElementsIn(bundle.keySet());
assertThat(pref.getString("string", "")).isEqualTo("value");
assertThat(pref.getBoolean("boolean", false)).isEqualTo(true);
@@ -425,7 +382,7 @@ public class SdkSandboxTest {
mService.syncDataFromClient(newUpdate);
// Verify that ClientSharedPreference contains the synced data
- SharedPreferences pref = getClientSharedPreference();
+ SharedPreferences pref = mService.getClientSharedPreferences();
assertThat(pref.getAll().keySet()).containsExactlyElementsIn(TEST_DATA.keySet());
assertThat(pref.getString(KEY_TO_UPDATE, "")).isEqualTo("update");
}
@@ -441,7 +398,7 @@ public class SdkSandboxTest {
mService.syncDataFromClient(newUpdate);
// Verify that ClientSharedPreference contains the synced data
- SharedPreferences pref = getClientSharedPreference();
+ SharedPreferences pref = mService.getClientSharedPreferences();
assertThat(pref.getAll().keySet()).doesNotContain(KEY_TO_UPDATE);
}
@@ -460,7 +417,6 @@ public class SdkSandboxTest {
mService.loadSdk(
CLIENT_PACKAGE_NAME,
- new Binder(),
mApplicationInfo,
SDK_NAME,
SDK_PROVIDER_CLASS,
@@ -468,8 +424,7 @@ public class SdkSandboxTest {
null,
new Bundle(),
mRemoteCode,
- SANDBOX_LATENCY_INFO,
- new StubSdkToServiceLink());
+ SANDBOX_LATENCY_INFO);
assertThat(latch.await(1, TimeUnit.MINUTES)).isTrue();
assertThat(mRemoteCode.mSandboxLatencyInfo.getLatencySystemServerToSandbox())
.isEqualTo(
@@ -505,12 +460,9 @@ public class SdkSandboxTest {
TIME_SDK_CALL_COMPLETED,
TIME_SANDBOX_CALLED_SYSTEM_SERVER);
- final IBinder sdkToken = new Binder();
-
final CountDownLatch latch = new CountDownLatch(1);
mService.loadSdk(
CLIENT_PACKAGE_NAME,
- sdkToken,
mApplicationInfo,
SDK_NAME,
SDK_PROVIDER_CLASS,
@@ -518,12 +470,11 @@ public class SdkSandboxTest {
null,
new Bundle(),
new RemoteCode(latch),
- SANDBOX_LATENCY_INFO,
- new StubSdkToServiceLink());
+ SANDBOX_LATENCY_INFO);
assertThat(latch.await(1, TimeUnit.MINUTES)).isTrue();
final UnloadSdkCallbackImpl unloadSdkCallback = new UnloadSdkCallbackImpl();
- mService.unloadSdk(sdkToken, unloadSdkCallback, SANDBOX_LATENCY_INFO);
+ mService.unloadSdk(SDK_NAME, unloadSdkCallback, SANDBOX_LATENCY_INFO);
final SandboxLatencyInfo sandboxLatencyInfo = unloadSdkCallback.getSandboxLatencyInfo();
@@ -561,7 +512,6 @@ public class SdkSandboxTest {
mService.loadSdk(
CLIENT_PACKAGE_NAME,
- new Binder(),
mApplicationInfo,
SDK_NAME,
SDK_PROVIDER_CLASS,
@@ -569,8 +519,7 @@ public class SdkSandboxTest {
null,
new Bundle(),
mRemoteCode,
- SANDBOX_LATENCY_INFO,
- new StubSdkToServiceLink());
+ SANDBOX_LATENCY_INFO);
assertThat(latch.await(1, TimeUnit.MINUTES)).isTrue();
CountDownLatch surfaceLatch = new CountDownLatch(1);
@@ -663,10 +612,6 @@ public class SdkSandboxTest {
return bundle;
}
- private SharedPreferences getClientSharedPreference() {
- return PreferenceManager.getDefaultSharedPreferences(mContext);
- }
-
private static class RequestSurfacePackageCallbackImpl
extends IRequestSurfacePackageFromSdkCallback.Stub {
private CountDownLatch mLatch;