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

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: address two remaining comments 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..b9b90b9035f07f2710dd717fd63a894206ab0b00 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,11 +23,71 @@ using base::android::ScopedJavaLocalRef;
namespace cronet {
-void AddUrlInterceptors(JNIEnv* env, const JavaParamRef<jclass>& jcaller) {
+// 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::RunAfterContextInit(
+ jcontext_adapter,
+ base::Bind(&UrlInterceptorJobFactoryHandle::InitOnNetworkThread,
+ base::Unretained(this)));
+ }
+ // Should only be called on network thread; other threads should use
mef 2016/11/16 18:45:44 Can we have DCHECK for this?
pauljensen 2016/11/18 18:12:10 Done.
+ // 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() {
+ 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_;
+};
+
+// 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 AddUrlInterceptors(JNIEnv* env,
+ const JavaParamRef<jclass>& jcaller,
+ jlong jcontext_adapter) {
net::URLRequestMockDataJob::AddUrlHandler();
net::URLRequestFailedJob::AddUrlHandler();
net::URLRequestHangingReadJob::AddUrlHandler();
net::SSLCertificateErrorJob::AddUrlHandler();
+ 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(

Powered by Google App Engine
This is Rietveld 408576698