| OLD | NEW |
| (Empty) |
| 1 // Copyright 2014 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.content.browser; | |
| 6 | |
| 7 import android.test.suitebuilder.annotation.SmallTest; | |
| 8 | |
| 9 import org.chromium.base.library_loader.LibraryLoader; | |
| 10 import org.chromium.base.library_loader.LibraryProcessType; | |
| 11 import org.chromium.content.browser.ServiceRegistry.ImplementationFactory; | |
| 12 import org.chromium.content_shell.ShellMojoTestUtils; | |
| 13 import org.chromium.content_shell_apk.ContentShellTestBase; | |
| 14 import org.chromium.mojo.bindings.ConnectionErrorHandler; | |
| 15 import org.chromium.mojo.bindings.InterfaceRequest; | |
| 16 import org.chromium.mojo.bindings.test.mojom.math.Calculator; | |
| 17 import org.chromium.mojo.system.MojoException; | |
| 18 import org.chromium.mojo.system.Pair; | |
| 19 import org.chromium.mojo.system.impl.CoreImpl; | |
| 20 | |
| 21 import java.io.Closeable; | |
| 22 import java.util.ArrayList; | |
| 23 import java.util.List; | |
| 24 | |
| 25 /** | |
| 26 * Instrumentation tests for ServiceRegistry. | |
| 27 */ | |
| 28 public class ServiceRegistryTest extends ContentShellTestBase { | |
| 29 | |
| 30 private static final long RUN_LOOP_TIMEOUT_MS = 25; | |
| 31 | |
| 32 private final List<Closeable> mCloseablesToClose = new ArrayList<Closeable>(
); | |
| 33 private long mNativeTestEnvironment; | |
| 34 | |
| 35 static class CalcConnectionErrorHandler implements ConnectionErrorHandler { | |
| 36 | |
| 37 MojoException mLastMojoException; | |
| 38 | |
| 39 @Override | |
| 40 public void onConnectionError(MojoException e) { | |
| 41 mLastMojoException = e; | |
| 42 } | |
| 43 } | |
| 44 | |
| 45 static class CalcCallback implements Calculator.AddResponse, Calculator.Mult
iplyResponse { | |
| 46 | |
| 47 double mResult = 0.0; | |
| 48 | |
| 49 @Override | |
| 50 public void call(Double result) { | |
| 51 mResult = result; | |
| 52 } | |
| 53 | |
| 54 public double getResult() { | |
| 55 return mResult; | |
| 56 } | |
| 57 } | |
| 58 | |
| 59 static class CalculatorImpl implements Calculator { | |
| 60 | |
| 61 double mResult = 0.0; | |
| 62 | |
| 63 @Override | |
| 64 public void close() {} | |
| 65 | |
| 66 @Override | |
| 67 public void onConnectionError(MojoException e) {} | |
| 68 | |
| 69 @Override | |
| 70 public void clear(ClearResponse callback) { | |
| 71 mResult = 0.0; | |
| 72 callback.call(mResult); | |
| 73 } | |
| 74 | |
| 75 @Override | |
| 76 public void add(double value, AddResponse callback) { | |
| 77 mResult += value; | |
| 78 callback.call(mResult); | |
| 79 } | |
| 80 | |
| 81 @Override | |
| 82 public void multiply(double value, MultiplyResponse callback) { | |
| 83 mResult *= value; | |
| 84 callback.call(mResult); | |
| 85 } | |
| 86 } | |
| 87 | |
| 88 static class CalculatorFactory implements ImplementationFactory<Calculator>
{ | |
| 89 | |
| 90 @Override | |
| 91 public Calculator createImpl() { | |
| 92 return new CalculatorImpl(); | |
| 93 } | |
| 94 } | |
| 95 | |
| 96 | |
| 97 @Override | |
| 98 protected void setUp() throws Exception { | |
| 99 super.setUp(); | |
| 100 LibraryLoader.get(LibraryProcessType.PROCESS_BROWSER).ensureInitialized( | |
| 101 getInstrumentation().getTargetContext()); | |
| 102 launchContentShellWithUrl("about://blank"); | |
| 103 mNativeTestEnvironment = ShellMojoTestUtils.setupTestEnvironment(); | |
| 104 } | |
| 105 | |
| 106 @Override | |
| 107 protected void tearDown() throws Exception { | |
| 108 for (Closeable c : mCloseablesToClose) { | |
| 109 c.close(); | |
| 110 } | |
| 111 ShellMojoTestUtils.tearDownTestEnvironment(mNativeTestEnvironment); | |
| 112 mNativeTestEnvironment = 0; | |
| 113 super.tearDown(); | |
| 114 } | |
| 115 | |
| 116 /** | |
| 117 * Verifies that remote service can be requested and works. | |
| 118 */ | |
| 119 @SmallTest | |
| 120 public void testConnectToService() { | |
| 121 Pair<ServiceRegistry, ServiceRegistry> registryPair = | |
| 122 ShellMojoTestUtils.createServiceRegistryPair(mNativeTestEnvironm
ent); | |
| 123 ServiceRegistry serviceRegistryA = registryPair.first; | |
| 124 ServiceRegistry serviceRegistryB = registryPair.second; | |
| 125 | |
| 126 // Add the Calculator service. | |
| 127 serviceRegistryA.addService(Calculator.MANAGER, new CalculatorFactory())
; | |
| 128 | |
| 129 Pair<Calculator.Proxy, InterfaceRequest<Calculator>> requestPair = | |
| 130 Calculator.MANAGER.getInterfaceRequest(CoreImpl.getInstance()); | |
| 131 | |
| 132 mCloseablesToClose.add(requestPair.first); | |
| 133 serviceRegistryB.connectToRemoteService(Calculator.MANAGER, requestPair.
second); | |
| 134 | |
| 135 // Perform a few operations on the Calculator. | |
| 136 Calculator.Proxy calculator = requestPair.first; | |
| 137 CalcConnectionErrorHandler errorHandler = new CalcConnectionErrorHandler
(); | |
| 138 calculator.getProxyHandler().setErrorHandler(errorHandler); | |
| 139 CalcCallback callback = new CalcCallback(); | |
| 140 | |
| 141 calculator.add(21, callback); | |
| 142 ShellMojoTestUtils.runLoop(RUN_LOOP_TIMEOUT_MS); | |
| 143 assertEquals(21.0, callback.getResult()); | |
| 144 | |
| 145 calculator.multiply(2, callback); | |
| 146 ShellMojoTestUtils.runLoop(RUN_LOOP_TIMEOUT_MS); | |
| 147 assertEquals(42.0, callback.getResult()); | |
| 148 } | |
| 149 | |
| 150 /** | |
| 151 * Verifies that a service can be requested only after it is added and not a
fter it is removed. | |
| 152 */ | |
| 153 @SmallTest | |
| 154 public void testAddRemoveService() { | |
| 155 Pair<ServiceRegistry, ServiceRegistry> registryPair = | |
| 156 ShellMojoTestUtils.createServiceRegistryPair(mNativeTestEnvironm
ent); | |
| 157 ServiceRegistry serviceRegistryA = registryPair.first; | |
| 158 ServiceRegistry serviceRegistryB = registryPair.second; | |
| 159 | |
| 160 // Request the Calculator service before it is added. | |
| 161 Pair<Calculator.Proxy, InterfaceRequest<Calculator>> requestPair = | |
| 162 Calculator.MANAGER.getInterfaceRequest(CoreImpl.getInstance()); | |
| 163 Calculator.Proxy calculator = requestPair.first; | |
| 164 CalcConnectionErrorHandler errorHandler = new CalcConnectionErrorHandler
(); | |
| 165 calculator.getProxyHandler().setErrorHandler(errorHandler); | |
| 166 mCloseablesToClose.add(calculator); | |
| 167 serviceRegistryB.connectToRemoteService(Calculator.MANAGER, requestPair.
second); | |
| 168 | |
| 169 // Spin the message loop and verify that an error occured. | |
| 170 assertNull(errorHandler.mLastMojoException); | |
| 171 ShellMojoTestUtils.runLoop(RUN_LOOP_TIMEOUT_MS); | |
| 172 assertNotNull(errorHandler.mLastMojoException); | |
| 173 | |
| 174 // Add the Calculator service and request it again. | |
| 175 errorHandler.mLastMojoException = null; | |
| 176 serviceRegistryA.addService(Calculator.MANAGER, new CalculatorFactory())
; | |
| 177 requestPair = Calculator.MANAGER.getInterfaceRequest(CoreImpl.getInstanc
e()); | |
| 178 calculator = requestPair.first; | |
| 179 errorHandler = new CalcConnectionErrorHandler(); | |
| 180 mCloseablesToClose.add(calculator); | |
| 181 serviceRegistryB.connectToRemoteService(Calculator.MANAGER, requestPair.
second); | |
| 182 | |
| 183 // Spin the message loop and verify that no error occured. | |
| 184 assertNull(errorHandler.mLastMojoException); | |
| 185 ShellMojoTestUtils.runLoop(RUN_LOOP_TIMEOUT_MS); | |
| 186 assertNull(errorHandler.mLastMojoException); | |
| 187 | |
| 188 // Remove the Calculator service and request it again. | |
| 189 errorHandler.mLastMojoException = null; | |
| 190 serviceRegistryA.removeService(Calculator.MANAGER); | |
| 191 requestPair = Calculator.MANAGER.getInterfaceRequest(CoreImpl.getInstanc
e()); | |
| 192 calculator = requestPair.first; | |
| 193 errorHandler = new CalcConnectionErrorHandler(); | |
| 194 calculator.getProxyHandler().setErrorHandler(errorHandler); | |
| 195 mCloseablesToClose.add(calculator); | |
| 196 serviceRegistryB.connectToRemoteService(Calculator.MANAGER, requestPair.
second); | |
| 197 | |
| 198 // Spin the message loop and verify that an error occured. | |
| 199 assertNull(errorHandler.mLastMojoException); | |
| 200 ShellMojoTestUtils.runLoop(RUN_LOOP_TIMEOUT_MS); | |
| 201 assertNotNull(errorHandler.mLastMojoException); | |
| 202 } | |
| 203 } | |
| OLD | NEW |