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

Unified 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, 2 months 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 side-by-side diff with in-line comments
Download patch
Index: components/cronet/android/test/mock_url_request_job_factory.cc
diff --git a/components/cronet/android/test/mock_url_request_job_factory.cc b/components/cronet/android/test/mock_url_request_job_factory.cc
index 092df0ae1fd64c8633cf355bab57a6f7ed81d1c0..8bff74f172258440cb3a9d1c5f93670fb3c7aa1a 100644
--- a/components/cronet/android/test/mock_url_request_job_factory.cc
+++ b/components/cronet/android/test/mock_url_request_job_factory.cc
@@ -6,11 +6,16 @@
#include "base/android/jni_android.h"
#include "base/android/jni_string.h"
+#include "base/memory/ptr_util.h"
+#include "components/cronet/android/test/cronet_test_util.h"
#include "jni/MockUrlRequestJobFactory_jni.h"
#include "net/test/url_request/ssl_certificate_error_job.h"
#include "net/test/url_request/url_request_failed_job.h"
#include "net/test/url_request/url_request_hanging_read_job.h"
#include "net/test/url_request/url_request_mock_data_job.h"
+#include "net/url_request/url_request_context.h"
+#include "net/url_request/url_request_filter.h"
+#include "net/url_request/url_request_intercepting_job_factory.h"
#include "url/gurl.h"
using base::android::JavaParamRef;
@@ -18,6 +23,52 @@ using base::android::ScopedJavaLocalRef;
namespace cronet {
+// Intercept URLRequestJob creation using URLRequestFilter from
+// libcronet_tests.so
+class UrlInterceptorJobFactoryHandle {
+ public:
+ // |jcontext_adapter| points to a URLRequestContextAdapater.
+ UrlInterceptorJobFactoryHandle(jlong jcontext_adapter)
+ : jcontext_adapter_(jcontext_adapter) {
+ TestUtil::getTaskRunner(jcontext_adapter_)
+ ->PostTask(
+ FROM_HERE,
+ base::Bind(
+ &TestUtil::runAfterContextInit, jcontext_adapter,
+ base::Bind(&UrlInterceptorJobFactoryHandle::initOnNetworkThread,
+ base::Unretained(this))));
+ }
+ // Should only be called on network thread; other threads should use
+ // shutdown().
+ ~UrlInterceptorJobFactoryHandle() {
+ TestUtil::getURLRequestContext(jcontext_adapter_)
+ ->set_job_factory(old_job_factory_);
+ }
+
+ void shutdown() {
+ TestUtil::getTaskRunner(jcontext_adapter_)->DeleteSoon(FROM_HERE, this);
+ }
+
+ private:
+ void initOnNetworkThread() {
xunjieli 2016/10/25 17:51:38 nit: CamelCase.
pauljensen 2016/10/26 17:55:18 Done.
+ net::URLRequestContext* request_context =
+ TestUtil::getURLRequestContext(jcontext_adapter_);
+ old_job_factory_ = request_context->job_factory();
+ new_job_factory_.reset(new net::URLRequestInterceptingJobFactory(
+ const_cast<net::URLRequestJobFactory*>(old_job_factory_),
+ net::URLRequestFilter::GetInstance()));
+ request_context->set_job_factory(new_job_factory_.get());
+ }
+
+ // The URLRequestContextAdapater this object intercepts from.
+ const jlong jcontext_adapter_;
+ // URLRequestJobFactory previously used in URLRequestContext.
+ const net::URLRequestJobFactory* old_job_factory_;
+ // URLRequestJobFactory inserted during tests to intercept URLRequests with
+ // libcronet's URLRequestFilter.
+ std::unique_ptr<net::URLRequestInterceptingJobFactory> new_job_factory_;
+};
+
void AddUrlInterceptors(JNIEnv* env, const JavaParamRef<jclass>& jcaller) {
net::URLRequestMockDataJob::AddUrlHandler();
net::URLRequestFailedJob::AddUrlHandler();
@@ -25,6 +76,26 @@ void AddUrlInterceptors(JNIEnv* env, const JavaParamRef<jclass>& jcaller) {
net::SSLCertificateErrorJob::AddUrlHandler();
}
+// URL interceptors are registered with the URLRequestFilter in
+// libcronet_tests.so. However tests are run on libcronet.so. Use the
+// URLRequestFilter in libcronet_tests.so with the URLRequestContext in
+// libcronet.so by installing a URLRequestInterceptingJobFactory
+// that calls into libcronet_tests.so's URLRequestFilter.
+jlong AddUrlInterceptorJobFactory(JNIEnv* env,
+ const JavaParamRef<jclass>& jcaller,
+ jlong jcontext_adapter) {
+ return reinterpret_cast<jlong>(
+ new UrlInterceptorJobFactoryHandle(jcontext_adapter));
+}
+
+// Put back the old URLRequestJobFactory into the URLRequestContext.
+void RemoveUrlInterceptorJobFactory(JNIEnv* env,
+ const JavaParamRef<jclass>& jcaller,
+ jlong jinterceptor_handle) {
+ reinterpret_cast<UrlInterceptorJobFactoryHandle*>(jinterceptor_handle)
+ ->shutdown();
+}
+
ScopedJavaLocalRef<jstring> GetMockUrlWithFailure(
JNIEnv* jenv,
const JavaParamRef<jclass>& jcaller,

Powered by Google App Engine
This is Rietveld 408576698