1 // Copyright (c) 2012 The Chromium Embedded Framework Authors. All rights
2 // reserved. Use of this source code is governed by a BSD-style license that
3 // can be found in the LICENSE file.
4
5 #ifndef CEF_TESTS_CEFTESTS_TEST_UTIL_H_
6 #define CEF_TESTS_CEFTESTS_TEST_UTIL_H_
7 #pragma once
8
9 #include "include/cef_process_message.h"
10 #include "include/cef_request.h"
11 #include "include/cef_request_context.h"
12 #include "include/cef_response.h"
13 #include "include/cef_values.h"
14 #include "tests/ceftests/test_suite.h"
15
16 // Test that CefRequest::HeaderMap objects are equal. Multiple values with the
17 // same key are allowed, but not duplicate entries with the same key/value. If
18 // |allowExtras| is true then additional header fields will be allowed in
19 // |map2|.
20 void TestMapEqual(const CefRequest::HeaderMap& map1,
21 const CefRequest::HeaderMap& map2,
22 bool allowExtras);
23
24 // Test that the CefRequest::HeaderMap object contains no duplicate entries.
25 void TestMapNoDuplicates(const CefRequest::HeaderMap& map);
26
27 // Test that CefPostDataElement objects are equal
28 void TestPostDataElementEqual(CefRefPtr<CefPostDataElement> elem1,
29 CefRefPtr<CefPostDataElement> elem2);
30
31 // Test that CefPostData objects are equal
32 void TestPostDataEqual(CefRefPtr<CefPostData> postData1,
33 CefRefPtr<CefPostData> postData2);
34
35 // Test that CefRequest objects are equal
36 // If |allowExtras| is true then additional header fields will be allowed in
37 // |request2|.
38 void TestRequestEqual(CefRefPtr<CefRequest> request1,
39 CefRefPtr<CefRequest> request2,
40 bool allowExtras);
41
42 // Test that CefResponse objects are equal
43 // If |allowExtras| is true then additional header fields will be allowed in
44 // |response2|.
45 void TestResponseEqual(CefRefPtr<CefResponse> response1,
46 CefRefPtr<CefResponse> response2,
47 bool allowExtras);
48
49 // Test if two binary values are equal.
50 void TestBinaryEqual(CefRefPtr<CefBinaryValue> val1,
51 CefRefPtr<CefBinaryValue> val2);
52
53 // Test if two list values are equal.
54 void TestListEqual(CefRefPtr<CefListValue> val1, CefRefPtr<CefListValue> val2);
55
56 // Test if two dictionary values are equal.
57 void TestDictionaryEqual(CefRefPtr<CefDictionaryValue> val1,
58 CefRefPtr<CefDictionaryValue> val2);
59
60 // Test if two process message values are equal.
61 void TestProcessMessageEqual(CefRefPtr<CefProcessMessage> val1,
62 CefRefPtr<CefProcessMessage> val2);
63
64 // Test if two CefString vectors are equal.
65 void TestStringVectorEqual(const std::vector<CefString>& val1,
66 const std::vector<CefString>& val2);
67
68 enum TestRequestContextMode {
69 TEST_RC_MODE_NONE,
70 TEST_RC_MODE_GLOBAL,
71 TEST_RC_MODE_GLOBAL_WITH_HANDLER,
72 TEST_RC_MODE_CUSTOM,
73 TEST_RC_MODE_CUSTOM_WITH_HANDLER,
74 };
75
IsTestRequestContextModeCustom(TestRequestContextMode mode)76 inline bool IsTestRequestContextModeCustom(TestRequestContextMode mode) {
77 return mode == TEST_RC_MODE_CUSTOM ||
78 mode == TEST_RC_MODE_CUSTOM_WITH_HANDLER;
79 }
80
81 // Returns true if the old CefResourceHandler API should be tested.
82 bool TestOldResourceAPI();
83
84 // Returns true if the Chrome runtime is enabled.
85 bool IsChromeRuntimeEnabled();
86
87 // Returns true if requests for |url| should be ignored by tests.
88 bool IgnoreURL(const std::string& url);
89
90 // Return a RequestContext object matching the specified |mode|.
91 // |cache_path| may be specified for CUSTOM modes.
92 // Use the RC_TEST_GROUP_BASE macro to test all valid combinations.
93 CefRefPtr<CefRequestContext> CreateTestRequestContext(
94 TestRequestContextMode mode,
95 const std::string& cache_path);
96
97 // Helper macro for testing a single RequestContextMode value.
98 // See RC_TEST_GROUP_ALL documentation for example usage.
99 #define RC_TEST_BASE(test_case_name, test_name, test_class, test_mode, \
100 rc_mode, with_cache_path) \
101 TEST(test_case_name, test_name) { \
102 CefScopedTempDir scoped_temp_dir; \
103 std::string cache_path; \
104 if (with_cache_path) { \
105 EXPECT_TRUE(scoped_temp_dir.CreateUniqueTempDirUnderPath( \
106 CefTestSuite::GetInstance()->root_cache_path())); \
107 cache_path = scoped_temp_dir.GetPath(); \
108 } \
109 CefRefPtr<test_class> handler = \
110 new test_class(test_class::test_mode, rc_mode, cache_path); \
111 handler->ExecuteTest(); \
112 ReleaseAndWaitForDestructor(handler); \
113 if (!scoped_temp_dir.IsEmpty()) { \
114 scoped_temp_dir.Take(); \
115 } \
116 }
117
118 // RequestContextModes that operate in memory.
119 #define RC_TEST_GROUP_IN_MEMORY(test_case_name, test_name, test_class, \
120 test_mode) \
121 RC_TEST_BASE(test_case_name, test_name##RCNone, test_class, test_mode, \
122 TEST_RC_MODE_NONE, false) \
123 RC_TEST_BASE(test_case_name, test_name##RCGlobal, test_class, test_mode, \
124 TEST_RC_MODE_GLOBAL, false) \
125 RC_TEST_BASE(test_case_name, test_name##RCGlobalWithHandler, test_class, \
126 test_mode, TEST_RC_MODE_GLOBAL_WITH_HANDLER, false) \
127 RC_TEST_BASE(test_case_name, test_name##RCCustomInMemory, test_class, \
128 test_mode, TEST_RC_MODE_CUSTOM, false) \
129 RC_TEST_BASE(test_case_name, test_name##RCCustomInMemoryWithHandler, \
130 test_class, test_mode, TEST_RC_MODE_CUSTOM_WITH_HANDLER, false)
131
132 // RequestContextModes that operate on disk.
133 #define RC_TEST_GROUP_ON_DISK(test_case_name, test_name, test_class, \
134 test_mode) \
135 RC_TEST_BASE(test_case_name, test_name##RCCustomOnDisk, test_class, \
136 test_mode, TEST_RC_MODE_CUSTOM, true) \
137 RC_TEST_BASE(test_case_name, test_name##RCCustomOnDiskWithHandler, \
138 test_class, test_mode, TEST_RC_MODE_CUSTOM_WITH_HANDLER, true)
139
140 // Helper macro for testing all valid combinations of RequestContextMode values.
141 // For example:
142 //
143 // // Test handler implementation.
144 // class MyTestHandler : public TestHandler {
145 // public:
146 // // Test modes supported by MyTestHandler.
147 // enum TestMode {
148 // FIRST,
149 // SECOND,
150 // };
151 //
152 // // Constructor always accepts three arguments.
153 // MyTestHandler(TestMode test_mode,
154 // TestRequestContextMode rc_mode,
155 // const std::string& rc_cache_path)
156 // : test_mode_(test_mode), rc_mode_(rc_mode),
157 // rc_cache_path_(rc_cache_path) {}
158 //
159 // void RunTest() override {
160 // // Create a RequestContext with the specified attributes.
161 // CefRefPtr<CefRequestContext> request_context =
162 // CreateTestRequestContext(rc_mode_, rc_cache_path_);
163 //
164 // // Do something with |test_mode_| and |request_context|...
165 // }
166 //
167 // private:
168 // const TestMode test_mode_;
169 // const TestRequestContextMode rc_mode_;
170 // const std::string rc_cache_path_;
171 //
172 // IMPLEMENT_REFCOUNTING(MyTestHandler);
173 // };
174 //
175 // // Helper macro for defining tests using MyTestHandler.
176 // #define MY_TEST_GROUP(test_name, test_mode) \
177 // RC_TEST_GROUP_ALL(MyTest, test_name, MyTestHandler, test_mode)
178 //
179 // // Implementation for MyTest.First* tests.
180 // MY_TEST_GROUP(First, FIRST);
181 // // Implementation for MyTest.Second* tests.
182 // MY_TEST_GROUP(Second, SECOND);
183 //
184 #define RC_TEST_GROUP_ALL(test_case_name, test_name, test_class, test_mode) \
185 RC_TEST_GROUP_IN_MEMORY(test_case_name, test_name, test_class, test_mode) \
186 RC_TEST_GROUP_ON_DISK(test_case_name, test_name, test_class, test_mode)
187
188 #endif // CEF_TESTS_CEFTESTS_TEST_UTIL_H_
189