xref: /aosp_15_r20/external/cronet/components/cronet/android/test/cronet_test_util.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2016 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef COMPONENTS_CRONET_ANDROID_TEST_CRONET_TEST_UTIL_H_
6 #define COMPONENTS_CRONET_ANDROID_TEST_CRONET_TEST_UTIL_H_
7 
8 #include <jni.h>
9 
10 #include "base/android/jni_android.h"
11 #include "base/containers/flat_map.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/task/single_thread_task_runner.h"
14 #include "net/base/network_handle.h"
15 
16 namespace net {
17 class URLRequest;
18 class URLRequestContext;
19 }  // namespace net
20 
21 namespace cronet {
22 
23 // Various test utility functions for testing Cronet.
24 // NOTE(pauljensen): This class is friended by Cronet internal implementation
25 // classes to provide access to internals.
26 class TestUtil {
27  public:
28   TestUtil() = delete;
29   TestUtil(const TestUtil&) = delete;
30   TestUtil& operator=(const TestUtil&) = delete;
31 
32   // CronetURLRequestContextAdapter manipulation:
33 
34   // Returns SingleThreadTaskRunner for the network thread of the context
35   // adapter.
36   static scoped_refptr<base::SingleThreadTaskRunner> GetTaskRunner(
37       jlong jcontext_adapter);
38   // Returns underlying default URLRequestContext.
39   static net::URLRequestContext* GetURLRequestContext(jlong jcontext_adapter);
40   // Run |task| after URLRequestContext is initialized.
41   static void RunAfterContextInit(jlong jcontext_adapter,
42                                   base::OnceClosure task);
43 
44   // CronetURLRequestAdapter manipulation:
45 
46   // Returns underlying URLRequest.
47   static net::URLRequest* GetURLRequest(jlong jrequest_adapter);
48 
49   // Returns underlying network to URLRequestContext map.
50   static base::flat_map<net::handles::NetworkHandle,
51                         std::unique_ptr<net::URLRequestContext>>*
52   GetURLRequestContexts(jlong jcontext_adapter);
53 
54  private:
55   static void RunAfterContextInitOnNetworkThread(jlong jcontext_adapter,
56                                                  base::OnceClosure task);
57 };
58 
59 }  // namespace cronet
60 
61 #endif  // COMPONENTS_CRONET_ANDROID_TEST_CRONET_TEST_UTIL_H_
62