Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(399)

Side by Side Diff: components/cronet/android/test/mock_url_request_job_factory.cc

Issue 2406273002: [Cronet] Test the libcronet that's shipped, not libcronet_test (Closed)
Patch Set: fixes Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 #include "mock_url_request_job_factory.h" 5 #include "mock_url_request_job_factory.h"
6 6
7 #include "base/android/jni_android.h" 7 #include "base/android/jni_android.h"
8 #include "base/android/jni_string.h" 8 #include "base/android/jni_string.h"
9 #include "base/memory/ptr_util.h"
10 #include "components/cronet/android/test/cronet_test_util.h"
9 #include "jni/MockUrlRequestJobFactory_jni.h" 11 #include "jni/MockUrlRequestJobFactory_jni.h"
10 #include "net/test/url_request/ssl_certificate_error_job.h" 12 #include "net/test/url_request/ssl_certificate_error_job.h"
11 #include "net/test/url_request/url_request_failed_job.h" 13 #include "net/test/url_request/url_request_failed_job.h"
12 #include "net/test/url_request/url_request_hanging_read_job.h" 14 #include "net/test/url_request/url_request_hanging_read_job.h"
13 #include "net/test/url_request/url_request_mock_data_job.h" 15 #include "net/test/url_request/url_request_mock_data_job.h"
16 #include "net/url_request/url_request_context.h"
17 #include "net/url_request/url_request_filter.h"
18 #include "net/url_request/url_request_intercepting_job_factory.h"
14 #include "url/gurl.h" 19 #include "url/gurl.h"
15 20
16 using base::android::JavaParamRef; 21 using base::android::JavaParamRef;
17 using base::android::ScopedJavaLocalRef; 22 using base::android::ScopedJavaLocalRef;
18 23
19 namespace cronet { 24 namespace cronet {
20 25
26 // Intercept URLRequestJob creation using URLRequestFilter from
27 // libcronet_tests.so
28 class UrlInterceptorJobFactoryHandle {
29 public:
30 // |jcontext_adapter| points to a URLRequestContextAdapater.
31 UrlInterceptorJobFactoryHandle(jlong jcontext_adapter)
32 : jcontext_adapter_(jcontext_adapter) {
33 TestUtil::getTaskRunner(jcontext_adapter_)
34 ->PostTask(
35 FROM_HERE,
36 base::Bind(
37 &TestUtil::runAfterContextInit, jcontext_adapter,
38 base::Bind(&UrlInterceptorJobFactoryHandle::initOnNetworkThread,
39 base::Unretained(this))));
40 }
41 // Should only be called on network thread; other threads should use
42 // shutdown().
43 ~UrlInterceptorJobFactoryHandle() {
44 TestUtil::getURLRequestContext(jcontext_adapter_)
45 ->set_job_factory(old_job_factory_);
46 }
47
48 void shutdown() {
49 TestUtil::getTaskRunner(jcontext_adapter_)->DeleteSoon(FROM_HERE, this);
50 }
51
52 private:
53 void initOnNetworkThread() {
xunjieli 2016/10/25 17:51:38 nit: CamelCase.
pauljensen 2016/10/26 17:55:18 Done.
54 net::URLRequestContext* request_context =
55 TestUtil::getURLRequestContext(jcontext_adapter_);
56 old_job_factory_ = request_context->job_factory();
57 new_job_factory_.reset(new net::URLRequestInterceptingJobFactory(
58 const_cast<net::URLRequestJobFactory*>(old_job_factory_),
59 net::URLRequestFilter::GetInstance()));
60 request_context->set_job_factory(new_job_factory_.get());
61 }
62
63 // The URLRequestContextAdapater this object intercepts from.
64 const jlong jcontext_adapter_;
65 // URLRequestJobFactory previously used in URLRequestContext.
66 const net::URLRequestJobFactory* old_job_factory_;
67 // URLRequestJobFactory inserted during tests to intercept URLRequests with
68 // libcronet's URLRequestFilter.
69 std::unique_ptr<net::URLRequestInterceptingJobFactory> new_job_factory_;
70 };
71
21 void AddUrlInterceptors(JNIEnv* env, const JavaParamRef<jclass>& jcaller) { 72 void AddUrlInterceptors(JNIEnv* env, const JavaParamRef<jclass>& jcaller) {
22 net::URLRequestMockDataJob::AddUrlHandler(); 73 net::URLRequestMockDataJob::AddUrlHandler();
23 net::URLRequestFailedJob::AddUrlHandler(); 74 net::URLRequestFailedJob::AddUrlHandler();
24 net::URLRequestHangingReadJob::AddUrlHandler(); 75 net::URLRequestHangingReadJob::AddUrlHandler();
25 net::SSLCertificateErrorJob::AddUrlHandler(); 76 net::SSLCertificateErrorJob::AddUrlHandler();
26 } 77 }
27 78
79 // URL interceptors are registered with the URLRequestFilter in
80 // libcronet_tests.so. However tests are run on libcronet.so. Use the
81 // URLRequestFilter in libcronet_tests.so with the URLRequestContext in
82 // libcronet.so by installing a URLRequestInterceptingJobFactory
83 // that calls into libcronet_tests.so's URLRequestFilter.
84 jlong AddUrlInterceptorJobFactory(JNIEnv* env,
85 const JavaParamRef<jclass>& jcaller,
86 jlong jcontext_adapter) {
87 return reinterpret_cast<jlong>(
88 new UrlInterceptorJobFactoryHandle(jcontext_adapter));
89 }
90
91 // Put back the old URLRequestJobFactory into the URLRequestContext.
92 void RemoveUrlInterceptorJobFactory(JNIEnv* env,
93 const JavaParamRef<jclass>& jcaller,
94 jlong jinterceptor_handle) {
95 reinterpret_cast<UrlInterceptorJobFactoryHandle*>(jinterceptor_handle)
96 ->shutdown();
97 }
98
28 ScopedJavaLocalRef<jstring> GetMockUrlWithFailure( 99 ScopedJavaLocalRef<jstring> GetMockUrlWithFailure(
29 JNIEnv* jenv, 100 JNIEnv* jenv,
30 const JavaParamRef<jclass>& jcaller, 101 const JavaParamRef<jclass>& jcaller,
31 jint jphase, 102 jint jphase,
32 jint jnet_error) { 103 jint jnet_error) {
33 GURL url(net::URLRequestFailedJob::GetMockHttpUrlWithFailurePhase( 104 GURL url(net::URLRequestFailedJob::GetMockHttpUrlWithFailurePhase(
34 static_cast<net::URLRequestFailedJob::FailurePhase>(jphase), 105 static_cast<net::URLRequestFailedJob::FailurePhase>(jphase),
35 static_cast<int>(jnet_error))); 106 static_cast<int>(jnet_error)));
36 return base::android::ConvertUTF8ToJavaString(jenv, url.spec()); 107 return base::android::ConvertUTF8ToJavaString(jenv, url.spec());
37 } 108 }
(...skipping 28 matching lines...) Expand all
66 const JavaParamRef<jclass>& jcaller) { 137 const JavaParamRef<jclass>& jcaller) {
67 GURL url(net::URLRequestHangingReadJob::GetMockHttpUrl()); 138 GURL url(net::URLRequestHangingReadJob::GetMockHttpUrl());
68 return base::android::ConvertUTF8ToJavaString(jenv, url.spec()); 139 return base::android::ConvertUTF8ToJavaString(jenv, url.spec());
69 } 140 }
70 141
71 bool RegisterMockUrlRequestJobFactory(JNIEnv* env) { 142 bool RegisterMockUrlRequestJobFactory(JNIEnv* env) {
72 return RegisterNativesImpl(env); 143 return RegisterNativesImpl(env);
73 } 144 }
74 145
75 } // namespace cronet 146 } // namespace cronet
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698