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 #include "mock_url_request_job_factory.h"
7 #include "base/android/jni_android.h"
8 #include "base/android/jni_string.h"
9 #include "base/files/file_path.h"
10 #include "base/path_service.h"
11 #include "base/threading/sequenced_worker_pool.h"
12 #include "jni/MockUrlRequestJobFactory_jni.h"
13 #include "net/test/url_request/url_request_failed_job.h"
14 #include "net/test/url_request/url_request_mock_data_job.h"
15 #include "net/test/url_request/url_request_mock_http_job.h"
19 void AddUrlInterceptors(JNIEnv
* env
, jclass jcaller
, jstring jtest_files_root
) {
20 base::FilePath
test_files_root(
21 base::android::ConvertJavaStringToUTF8(env
, jtest_files_root
));
22 net::URLRequestMockHTTPJob::AddUrlHandlers(
23 test_files_root
, new base::SequencedWorkerPool(1, "Worker"));
24 net::URLRequestMockDataJob::AddUrlHandler();
25 net::URLRequestFailedJob::AddUrlHandler();
28 jstring
GetMockUrl(JNIEnv
* jenv
, jclass jcaller
, jstring jpath
) {
29 base::FilePath
path(base::android::ConvertJavaStringToUTF8(jenv
, jpath
));
30 GURL
url(net::URLRequestMockHTTPJob::GetMockUrl(path
));
31 return base::android::ConvertUTF8ToJavaString(jenv
, url
.spec()).Release();
34 jstring
GetMockUrlWithFailure(JNIEnv
* jenv
,
39 base::FilePath
path(base::android::ConvertJavaStringToUTF8(jenv
, jpath
));
40 GURL
url(net::URLRequestMockHTTPJob::GetMockUrlWithFailure(
42 static_cast<net::URLRequestMockHTTPJob::FailurePhase
>(jphase
),
43 static_cast<int>(jnet_error
)));
44 return base::android::ConvertUTF8ToJavaString(jenv
, url
.spec()).Release();
47 jstring
GetMockUrlForData(JNIEnv
* jenv
,
50 jint jdata_repeat_count
) {
51 std::string
data(base::android::ConvertJavaStringToUTF8(jenv
, jdata
));
52 GURL
url(net::URLRequestMockDataJob::GetMockHttpUrl(data
,
54 return base::android::ConvertUTF8ToJavaString(jenv
, url
.spec()).Release();
57 bool RegisterMockUrlRequestJobFactory(JNIEnv
* env
) {
58 return RegisterNativesImpl(env
);