OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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; | 5 package org.chromium.net; |
6 | 6 |
7 import static junit.framework.Assert.assertTrue; | 7 import static junit.framework.Assert.assertTrue; |
8 | 8 |
| 9 import android.os.ConditionVariable; |
| 10 |
9 import org.chromium.base.annotations.JNINamespace; | 11 import org.chromium.base.annotations.JNINamespace; |
| 12 import org.chromium.net.impl.CronetUrlRequestContext; |
10 import org.chromium.net.test.FailurePhase; | 13 import org.chromium.net.test.FailurePhase; |
11 | 14 |
12 /** | 15 /** |
13 * Helper class to set up url interceptors for testing purposes. | 16 * Helper class to set up url interceptors for testing purposes. |
14 */ | 17 */ |
15 @JNINamespace("cronet") | 18 @JNINamespace("cronet") |
16 public final class MockUrlRequestJobFactory { | 19 public final class MockUrlRequestJobFactory { |
| 20 private static CronetEngine sCronetEngine; |
| 21 |
17 /** | 22 /** |
18 * Sets up URL interceptors. | 23 * Sets up URL interceptors. |
19 */ | 24 */ |
20 public static void setUp() { | 25 public static void setUp(final CronetEngine cronetEngine) { |
21 nativeAddUrlInterceptors(); | 26 sCronetEngine = cronetEngine; |
| 27 CronetTestUtil.prepareNetworkThreadForTesting(cronetEngine); |
| 28 |
| 29 // Hop over to network thread and install URL interceptors at the native
level. |
| 30 final ConditionVariable done = new ConditionVariable(); |
| 31 CronetTestUtil.postToNetworkThread(cronetEngine, new Runnable() { |
| 32 @Override |
| 33 public void run() { |
| 34 long requestContext = |
| 35 ((CronetUrlRequestContext) cronetEngine).getUrlRequestCo
ntextForTesting(); |
| 36 nativeAddUrlInterceptors(); |
| 37 nativeAddUrlInterceptorJobFactory(requestContext); |
| 38 done.open(); |
| 39 } |
| 40 }); |
| 41 done.block(); |
22 } | 42 } |
23 | 43 |
24 /** | 44 /** |
| 45 * Remove URL Interceptors. |
| 46 */ |
| 47 public static void shutdown() { |
| 48 // Hop over to network thread and remove URL interceptors at the native
level. |
| 49 final long requestContext = |
| 50 ((CronetUrlRequestContext) sCronetEngine).getUrlRequestContextFo
rTesting(); |
| 51 CronetTestUtil.postToNetworkThread(sCronetEngine, new Runnable() { |
| 52 @Override |
| 53 public void run() { |
| 54 nativeRemoveUrlInterceptorJobFactory(requestContext); |
| 55 } |
| 56 }); |
| 57 |
| 58 CronetTestUtil.cleanupNetorkThreadForTesting(); |
| 59 sCronetEngine = null; |
| 60 } |
| 61 |
| 62 /** |
25 * Constructs a mock URL that hangs or fails at certain phase. | 63 * Constructs a mock URL that hangs or fails at certain phase. |
26 * | 64 * |
27 * @param phase at which request fails. It should be a value in | 65 * @param phase at which request fails. It should be a value in |
28 * org.chromium.net.test.FailurePhase. | 66 * org.chromium.net.test.FailurePhase. |
29 * @param netError reported by UrlRequestJob. Passing -1, results in hang. | 67 * @param netError reported by UrlRequestJob. Passing -1, results in hang. |
30 */ | 68 */ |
31 public static String getMockUrlWithFailure(int phase, int netError) { | 69 public static String getMockUrlWithFailure(int phase, int netError) { |
32 assertTrue(netError < 0); | 70 assertTrue(netError < 0); |
33 switch (phase) { | 71 switch (phase) { |
34 case FailurePhase.START: | 72 case FailurePhase.START: |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
70 | 108 |
71 /** | 109 /** |
72 * Constructs a mock URL that will hang when try to read response body from
the remote. | 110 * Constructs a mock URL that will hang when try to read response body from
the remote. |
73 */ | 111 */ |
74 public static String getMockUrlForHangingRead() { | 112 public static String getMockUrlForHangingRead() { |
75 return nativeGetMockUrlForHangingRead(); | 113 return nativeGetMockUrlForHangingRead(); |
76 } | 114 } |
77 | 115 |
78 private static native void nativeAddUrlInterceptors(); | 116 private static native void nativeAddUrlInterceptors(); |
79 | 117 |
| 118 private static native void nativeAddUrlInterceptorJobFactory(long requestCon
text); |
| 119 |
| 120 private static native void nativeRemoveUrlInterceptorJobFactory(long request
Context); |
| 121 |
80 private static native String nativeGetMockUrlWithFailure(int phase, int netE
rror); | 122 private static native String nativeGetMockUrlWithFailure(int phase, int netE
rror); |
81 | 123 |
82 private static native String nativeGetMockUrlForData(String data, | 124 private static native String nativeGetMockUrlForData(String data, |
83 int dataRepeatCount); | 125 int dataRepeatCount); |
84 | 126 |
85 private static native String nativeGetMockUrlForClientCertificateRequest(); | 127 private static native String nativeGetMockUrlForClientCertificateRequest(); |
86 | 128 |
87 private static native String nativeGetMockUrlForSSLCertificateError(); | 129 private static native String nativeGetMockUrlForSSLCertificateError(); |
88 | 130 |
89 private static native String nativeGetMockUrlForHangingRead(); | 131 private static native String nativeGetMockUrlForHangingRead(); |
90 } | 132 } |
OLD | NEW |