Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / components / cronet / android / test / src / org / chromium / net / MockUrlRequestJobFactory.java
blob505db12d415976566576c716e37f46644f81d496
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 static junit.framework.Assert.assertTrue;
9 import org.chromium.base.annotations.JNINamespace;
10 import org.chromium.net.test.FailurePhase;
12 /**
13 * Helper class to set up url interceptors for testing purposes.
15 @JNINamespace("cronet")
16 public final class MockUrlRequestJobFactory {
17 /**
18 * Sets up URL interceptors.
20 public static void setUp() {
21 nativeAddUrlInterceptors();
24 /**
25 * Constructs a mock URL that hangs or fails at certain phase.
27 * @param phase at which request fails. It should be a value in
28 * org.chromium.net.test.FailurePhase.
29 * @param netError reported by UrlRequestJob. Passing -1, results in hang.
31 public static String getMockUrlWithFailure(int phase, int netError) {
32 assertTrue(netError < 0);
33 switch (phase) {
34 case FailurePhase.START:
35 case FailurePhase.READ_SYNC:
36 case FailurePhase.READ_ASYNC:
37 break;
38 default:
39 throw new IllegalArgumentException(
40 "phase not in org.chromium.net.test.FailurePhase");
42 return nativeGetMockUrlWithFailure(phase, netError);
45 /**
46 * Constructs a mock URL that synchronously responds with data repeated many
47 * times.
49 * @param data to return in response.
50 * @param dataRepeatCount number of times to repeat the data.
52 public static String getMockUrlForData(String data, int dataRepeatCount) {
53 return nativeGetMockUrlForData(data, dataRepeatCount);
56 /**
57 * Constructs a mock URL that will fail with an SSL certificate error.
59 public static String getMockUrlForSSLCertificateError() {
60 return nativeGetMockUrlForSSLCertificateError();
63 private static native void nativeAddUrlInterceptors();
65 private static native String nativeGetMockUrlWithFailure(int phase, int netError);
67 private static native String nativeGetMockUrlForData(String data,
68 int dataRepeatCount);
70 private static native String nativeGetMockUrlForSSLCertificateError();