OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 package org.chromium.chrome.browser; |
| 6 |
| 7 import android.app.Service; |
| 8 import android.content.Context; |
| 9 import android.os.RemoteException; |
| 10 import android.test.InstrumentationTestCase; |
| 11 import android.test.suitebuilder.annotation.MediumTest; |
| 12 |
| 13 import org.chromium.base.library_loader.LibraryLoader; |
| 14 import org.chromium.base.library_loader.LibraryProcessType; |
| 15 import org.chromium.base.test.util.CommandLineFlags; |
| 16 import org.chromium.base.test.util.Feature; |
| 17 import org.chromium.content.browser.ChildProcessConnectionImpl; |
| 18 import org.chromium.content.browser.ChildProcessLauncher; |
| 19 import org.chromium.content.browser.test.ChildProcessLauncherTestHelper; |
| 20 import org.chromium.content.browser.test.util.Criteria; |
| 21 import org.chromium.content.browser.test.util.CriteriaHelper; |
| 22 import org.chromium.webapk.lib.common.WebApkSandboxedProcessService; |
| 23 |
| 24 /** |
| 25 * Instrumentation tests for ChildProcessLauncher. |
| 26 */ |
| 27 public class ChromeChildProcessLauncherTest extends InstrumentationTestCase { |
| 28 private static final String TESTING_WEB_APK_PACKAGE_NAME = "org.chromium.web
apk.template.test"; |
| 29 |
| 30 /** |
| 31 * Tests service number of connections for WebAPKs and regular tabs are assi
gned properly, |
| 32 * i.e. from different ChildConnectionAllocators. |
| 33 */ |
| 34 @MediumTest |
| 35 @Feature({"ProcessManagement", "webapk"}) |
| 36 public void testServiceNumberAllocation() throws InterruptedException, Remot
eException { |
| 37 Context appContext = getInstrumentation().getTargetContext(); |
| 38 assertEquals(0, ChildProcessLauncher.allocatedSandboxedConnectionsCountF
orTesting( |
| 39 appContext, TESTING_WEB_APK_PACKAGE_NAME)); |
| 40 assertEquals(0, ChildProcessLauncherTestHelper.allocatedChromeSandboxedC
onnectionsCount( |
| 41 appContext)); |
| 42 |
| 43 // Start and connect to a new service of a WebAPK. |
| 44 ChildProcessConnectionImpl webapkConnection = |
| 45 startConnection(TESTING_WEB_APK_PACKAGE_NAME, WebApkSandboxedPro
cessService.class); |
| 46 // Start and connect to a new service for a regular tab. |
| 47 ChildProcessConnectionImpl tabConnection = |
| 48 startConnection(appContext.getPackageName(), null); |
| 49 |
| 50 // Verify that one connection is allocated for a WebAPK and a regular ta
b respectively. |
| 51 assertEquals(1, ChildProcessLauncher.allocatedSandboxedConnectionsCountF
orTesting( |
| 52 appContext, TESTING_WEB_APK_PACKAGE_NAME)); |
| 53 assertEquals(1, ChildProcessLauncherTestHelper.allocatedChromeSandboxedC
onnectionsCount( |
| 54 appContext)); |
| 55 |
| 56 // Verify that connections allocated for a WebAPK and the regular tab ar
e from different |
| 57 // ChildConnectionAllocators, since both ChildConnectionAllocators start
allocating |
| 58 // connections from number 0. |
| 59 assertEquals(0, webapkConnection.getServiceNumber()); |
| 60 assertEquals(0, tabConnection.getServiceNumber()); |
| 61 } |
| 62 |
| 63 /** |
| 64 * Tests that after reaching the maximum allowed connections for a WebAPK, w
e can't allocate |
| 65 * a new connection to the WebAPK, but we can still allocate a connection fo
r a regular tab. |
| 66 */ |
| 67 @MediumTest |
| 68 @Feature({"ProcessManagement", "webapk"}) |
| 69 @CommandLineFlags.Add(ChildProcessLauncher.SWITCH_NUM_SANDBOXED_SERVICES_FOR
_TESTING + "=1") |
| 70 public void testExceedMaximumConnectionNumber() throws InterruptedException,
RemoteException { |
| 71 Context appContext = getInstrumentation().getTargetContext(); |
| 72 assertEquals(0, ChildProcessLauncher.allocatedSandboxedConnectionsCountF
orTesting( |
| 73 appContext, TESTING_WEB_APK_PACKAGE_NAME)); |
| 74 |
| 75 // Setup a connection for a WebAPK to reach the maximum allowed connecti
on number. |
| 76 ChildProcessConnectionImpl webapkConnection = |
| 77 startConnection(TESTING_WEB_APK_PACKAGE_NAME, WebApkSandboxedPro
cessService.class); |
| 78 assertNotNull(webapkConnection); |
| 79 |
| 80 // Verify that there isn't any connection available for the WebAPK. |
| 81 ChildProcessConnectionImpl exceedNumberWebapkConnection = |
| 82 startConnection(TESTING_WEB_APK_PACKAGE_NAME, WebApkSandboxedPro
cessService.class); |
| 83 assertNull(exceedNumberWebapkConnection); |
| 84 |
| 85 // Verify that we can still allocate connection for a regular tab. |
| 86 ChildProcessConnectionImpl tabConnection = |
| 87 startConnection(appContext.getPackageName(), null); |
| 88 assertNotNull(tabConnection); |
| 89 } |
| 90 |
| 91 /** |
| 92 * Returns whether the connection is established and connected. Return false
if the connection |
| 93 * isn't either established nor connected. |
| 94 */ |
| 95 private ChildProcessConnectionImpl startConnection(String packageName, |
| 96 Class<? extends Service> servicecClass) throws InterruptedException
{ |
| 97 // Allocate a new connection. |
| 98 Context context = getInstrumentation().getTargetContext(); |
| 99 final ChildProcessConnectionImpl connection = |
| 100 (ChildProcessConnectionImpl) ChildProcessLauncher.allocateBoundC
onnectionForTesting( |
| 101 context, ChildProcessLauncherTestHelper.getChildProcessC
reationParams( |
| 102 packageName, servicecClass)); |
| 103 |
| 104 if (connection == null) { |
| 105 return null; |
| 106 } |
| 107 // Wait for the service to connect. |
| 108 CriteriaHelper.pollInstrumentationThread( |
| 109 new Criteria("The connection wasn't established.") { |
| 110 @Override |
| 111 public boolean isSatisfied() { |
| 112 return connection.isConnected(); |
| 113 } |
| 114 }); |
| 115 return connection; |
| 116 } |
| 117 |
| 118 @Override |
| 119 protected void setUp() throws Exception { |
| 120 super.setUp(); |
| 121 LibraryLoader.get(LibraryProcessType.PROCESS_CHILD) |
| 122 .ensureInitialized(getInstrumentation().getTargetContext()); |
| 123 } |
| 124 } |
OLD | NEW |