| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 package org.chromium.net.test; | 5 package org.chromium.net.test; |
| 6 | 6 |
| 7 import android.content.Context; | 7 import android.content.Context; |
| 8 import android.os.Build; | 8 import android.os.Build; |
| 9 import android.os.Handler; | 9 import android.os.Handler; |
| 10 import android.os.HandlerThread; | 10 import android.os.HandlerThread; |
| 11 | 11 |
| 12 import org.chromium.base.ContextUtils; |
| 12 import org.chromium.base.Log; | 13 import org.chromium.base.Log; |
| 13 import org.chromium.base.annotations.CalledByNative; | 14 import org.chromium.base.annotations.CalledByNative; |
| 14 import org.chromium.base.annotations.JNINamespace; | 15 import org.chromium.base.annotations.JNINamespace; |
| 15 import org.chromium.base.library_loader.LibraryLoader; | 16 import org.chromium.base.library_loader.LibraryLoader; |
| 16 import org.chromium.base.library_loader.LibraryProcessType; | 17 import org.chromium.base.library_loader.LibraryProcessType; |
| 17 import org.chromium.base.library_loader.ProcessInitException; | 18 import org.chromium.base.library_loader.ProcessInitException; |
| 18 import org.chromium.base.test.util.UrlUtils; | 19 import org.chromium.base.test.util.UrlUtils; |
| 19 | 20 |
| 20 import java.util.concurrent.Callable; | 21 import java.util.concurrent.Callable; |
| 21 import java.util.concurrent.ExecutionException; | 22 import java.util.concurrent.ExecutionException; |
| (...skipping 15 matching lines...) Expand all Loading... |
| 37 private Handler mHandler; | 38 private Handler mHandler; |
| 38 private HandlerThread mHandlerThread; | 39 private HandlerThread mHandlerThread; |
| 39 private long mNativeEmbeddedTestServer; | 40 private long mNativeEmbeddedTestServer; |
| 40 | 41 |
| 41 /** Create an uninitialized EmbeddedTestServer. */ | 42 /** Create an uninitialized EmbeddedTestServer. */ |
| 42 public EmbeddedTestServerImpl(Context context) { | 43 public EmbeddedTestServerImpl(Context context) { |
| 43 mContext = context; | 44 mContext = context; |
| 44 } | 45 } |
| 45 | 46 |
| 46 private <V> V runOnHandlerThread(Callable<V> c) { | 47 private <V> V runOnHandlerThread(Callable<V> c) { |
| 47 FutureTask<V> t = new FutureTask<V>(c); | 48 FutureTask<V> t = new FutureTask<>(c); |
| 48 mHandler.post(t); | 49 mHandler.post(t); |
| 49 try { | 50 try { |
| 50 return t.get(); | 51 return t.get(); |
| 51 } catch (ExecutionException e) { | 52 } catch (ExecutionException e) { |
| 52 Log.e(TAG, "Exception raised from native EmbeddedTestServer", e); | 53 Log.e(TAG, "Exception raised from native EmbeddedTestServer", e); |
| 53 } catch (InterruptedException e) { | 54 } catch (InterruptedException e) { |
| 54 Log.e(TAG, "Interrupted while waiting for native EmbeddedTestServer"
, e); | 55 Log.e(TAG, "Interrupted while waiting for native EmbeddedTestServer"
, e); |
| 55 } | 56 } |
| 56 return null; | 57 return null; |
| 57 } | 58 } |
| 58 | 59 |
| 59 /** Initialize the native EmbeddedTestServer object. | 60 /** Initialize the native EmbeddedTestServer object. |
| 60 * | 61 * |
| 61 * @return Whether the native object was successfully initialized. | 62 * @return Whether the native object was successfully initialized. |
| 62 */ | 63 */ |
| 63 @Override | 64 @Override |
| 64 public boolean initializeNative() { | 65 public boolean initializeNative() { |
| 66 // This is necessary as EmbeddedTestServerImpl is in a different process
than the tests |
| 67 // using it, so it needs to initialize its own application context. |
| 68 ContextUtils.initApplicationContext(mContext.getApplicationContext()); |
| 65 try { | 69 try { |
| 66 LibraryLoader libraryLoader = LibraryLoader.get(LibraryProcessType.P
ROCESS_BROWSER); | 70 LibraryLoader.get(LibraryProcessType.PROCESS_BROWSER).ensureInitiali
zed(); |
| 67 libraryLoader.ensureInitialized(mContext); | |
| 68 } catch (ProcessInitException e) { | 71 } catch (ProcessInitException e) { |
| 69 Log.e(TAG, "Failed to load native libraries.", e); | 72 Log.e(TAG, "Failed to load native libraries.", e); |
| 70 return false; | 73 return false; |
| 71 } | 74 } |
| 72 | 75 |
| 73 mHandlerThread = new HandlerThread("EmbeddedTestServer" + sCount.getAndI
ncrement()); | 76 mHandlerThread = new HandlerThread("EmbeddedTestServer" + sCount.getAndI
ncrement()); |
| 74 mHandlerThread.start(); | 77 mHandlerThread.start(); |
| 75 mHandler = new Handler(mHandlerThread.getLooper()); | 78 mHandler = new Handler(mHandlerThread.getLooper()); |
| 76 | 79 |
| 77 runOnHandlerThread(new Callable<Void>() { | 80 runOnHandlerThread(new Callable<Void>() { |
| (...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 209 private native void nativeInit(String testDataDir); | 212 private native void nativeInit(String testDataDir); |
| 210 private native void nativeDestroy(long nativeEmbeddedTestServerAndroid); | 213 private native void nativeDestroy(long nativeEmbeddedTestServerAndroid); |
| 211 private native boolean nativeStart(long nativeEmbeddedTestServerAndroid); | 214 private native boolean nativeStart(long nativeEmbeddedTestServerAndroid); |
| 212 private native boolean nativeShutdownAndWaitUntilComplete(long nativeEmbedde
dTestServerAndroid); | 215 private native boolean nativeShutdownAndWaitUntilComplete(long nativeEmbedde
dTestServerAndroid); |
| 213 private native String nativeGetURL(long nativeEmbeddedTestServerAndroid, Str
ing relativeUrl); | 216 private native String nativeGetURL(long nativeEmbeddedTestServerAndroid, Str
ing relativeUrl); |
| 214 private native void nativeAddDefaultHandlers( | 217 private native void nativeAddDefaultHandlers( |
| 215 long nativeEmbeddedTestServerAndroid, String directoryPath); | 218 long nativeEmbeddedTestServerAndroid, String directoryPath); |
| 216 private native void nativeServeFilesFromDirectory( | 219 private native void nativeServeFilesFromDirectory( |
| 217 long nativeEmbeddedTestServerAndroid, String directoryPath); | 220 long nativeEmbeddedTestServerAndroid, String directoryPath); |
| 218 } | 221 } |
| OLD | NEW |