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.
5 package org
.chromium
.net
;
7 import android
.content
.Context
;
8 import android
.os
.ConditionVariable
;
10 import org
.chromium
.base
.annotations
.CalledByNative
;
11 import org
.chromium
.base
.annotations
.JNINamespace
;
14 * Wrapper class to start an in-process native test server, and get URLs
15 * needed to talk to it.
17 @JNINamespace("cronet")
18 public final class NativeTestServer
{
19 private static final ConditionVariable sHostResolverBlock
= new ConditionVariable();
21 // This variable contains the response body of a request to getSuccessURL().
22 public static final String SUCCESS_BODY
= "this is a text file\n";
24 public static boolean startNativeTestServer(Context context
) {
25 TestFilesInstaller
.installIfNeeded(context
);
26 return nativeStartNativeTestServer(
27 TestFilesInstaller
.getInstalledPath(context
));
30 public static void shutdownNativeTestServer() {
31 nativeShutdownNativeTestServer();
35 * Registers customized DNS mapping for {@link NativeTestServer}.
36 * @param contextAdapter native context adapter object that this
37 * mapping should apply to.
38 * @param isLegacyAPI {@code true} if this context adapter is a part of the
41 public static void registerHostResolverProc(long contextAdapter
, boolean isLegacyAPI
) {
42 nativeRegisterHostResolverProc(contextAdapter
, isLegacyAPI
);
43 sHostResolverBlock
.block();
44 sHostResolverBlock
.close();
47 public static String
getEchoBodyURL() {
48 return nativeGetEchoBodyURL();
51 public static String
getEchoHeaderURL(String header
) {
52 return nativeGetEchoHeaderURL(header
);
55 public static String
getEchoAllHeadersURL() {
56 return nativeGetEchoAllHeadersURL();
59 public static String
getEchoMethodURL() {
60 return nativeGetEchoMethodURL();
63 public static String
getRedirectToEchoBody() {
64 return nativeGetRedirectToEchoBody();
67 public static String
getFileURL(String filePath
) {
68 return nativeGetFileURL(filePath
);
71 public static String
getSdchURL() {
72 return nativeGetSdchURL();
75 // The following URLs will make NativeTestServer serve a response based on
76 // the contents of the corresponding file and its mock-http-headers file.
78 public static String
getSuccessURL() {
79 return nativeGetFileURL("/success.txt");
82 public static String
getRedirectURL() {
83 return nativeGetFileURL("/redirect.html");
86 public static String
getMultiRedirectURL() {
87 return nativeGetFileURL("/multiredirect.html");
90 public static String
getNotFoundURL() {
91 return nativeGetFileURL("/notfound.html");
94 public static String
getHostPort() {
95 return nativeGetHostPort();
98 public static boolean isDataReductionProxySupported() {
99 return nativeIsDataReductionProxySupported();
103 private static void onHostResolverProcRegistered() {
104 sHostResolverBlock
.open();
107 private static native boolean nativeStartNativeTestServer(String filePath
);
108 private static native void nativeShutdownNativeTestServer();
109 private static native void nativeRegisterHostResolverProc(
110 long contextAdapter
, boolean isLegacyAPI
);
111 private static native String
nativeGetEchoBodyURL();
112 private static native String
nativeGetEchoHeaderURL(String header
);
113 private static native String
nativeGetEchoAllHeadersURL();
114 private static native String
nativeGetEchoMethodURL();
115 private static native String
nativeGetRedirectToEchoBody();
116 private static native String
nativeGetFileURL(String filePath
);
117 private static native String
nativeGetSdchURL();
118 private static native String
nativeGetHostPort();
119 private static native boolean nativeIsDataReductionProxySupported();