[go: nahoru, domu]

tree: 2090d276aa7c18f915cae3d123f3c088682ef38d [path history] [tgz]
  1. android/
  2. accessibility_notification_waiter.cc
  3. accessibility_notification_waiter.h
  4. audio_service_test_helper.cc
  5. audio_service_test_helper.h
  6. back_forward_cache_util.cc
  7. back_forward_cache_util.h
  8. background_color_change_waiter.cc
  9. background_color_change_waiter.h
  10. background_sync_test_util.cc
  11. background_sync_test_util.h
  12. background_tracing_test_support.cc
  13. background_tracing_test_support.h
  14. blink_test_browser_support.h
  15. blink_test_environment.cc
  16. blink_test_environment.h
  17. bluetooth_test_utils.cc
  18. bluetooth_test_utils.h
  19. browser_task_environment.cc
  20. browser_task_environment.h
  21. browser_task_environment_unittest.cc
  22. browser_test.h
  23. browser_test_base.cc
  24. browser_test_base.h
  25. browser_test_utils.cc
  26. browser_test_utils.h
  27. browser_test_utils_mac.mm
  28. browsing_data_remover_test_util.cc
  29. browsing_data_remover_test_util.h
  30. browsing_topics_test_util.cc
  31. browsing_topics_test_util.h
  32. captured_surface_test_utils.cc
  33. captured_surface_test_utils.h
  34. commit_message_delayer.cc
  35. commit_message_delayer.h
  36. content_browser_test.cc
  37. content_browser_test.h
  38. content_browser_test_content_browser_client.cc
  39. content_browser_test_content_browser_client.h
  40. content_browser_test_shell_main_delegate.cc
  41. content_browser_test_shell_main_delegate.h
  42. content_browser_test_utils.cc
  43. content_browser_test_utils.h
  44. content_browser_test_utils_mac.mm
  45. content_cert_verifier_browser_test.cc
  46. content_cert_verifier_browser_test.h
  47. content_mock_cert_verifier.cc
  48. content_mock_cert_verifier.h
  49. content_test_suite_base.cc
  50. content_test_suite_base.h
  51. context_menu_interceptor.cc
  52. context_menu_interceptor.h
  53. cursor_utils.cc
  54. cursor_utils.h
  55. DEPS
  56. download_test_observer.cc
  57. download_test_observer.h
  58. embedded_worker_instance_test_harness.cc
  59. embedded_worker_instance_test_harness.h
  60. fake_download_item.cc
  61. fake_download_item.h
  62. fake_file_system_access_permission_context.cc
  63. fake_file_system_access_permission_context.h
  64. fake_frame_widget.cc
  65. fake_frame_widget.h
  66. fake_local_frame.cc
  67. fake_local_frame.h
  68. fake_remote_frame.cc
  69. fake_remote_frame.h
  70. fake_render_widget_host.cc
  71. fake_render_widget_host.h
  72. fake_service_worker_context.cc
  73. fake_service_worker_context.h
  74. fake_speech_recognition_manager.cc
  75. fake_speech_recognition_manager.h
  76. fenced_frame_reporter_observer.cc
  77. fenced_frame_reporter_observer.h
  78. fenced_frame_test_util.cc
  79. fenced_frame_test_util.h
  80. file_system_chooser_test_helpers.cc
  81. file_system_chooser_test_helpers.h
  82. find_test_utils.cc
  83. find_test_utils.h
  84. focus_changed_observer.cc
  85. focus_changed_observer.h
  86. frame_load_waiter.cc
  87. frame_load_waiter.h
  88. frame_test_utils.cc
  89. frame_test_utils.h
  90. hit_test_region_observer.cc
  91. hit_test_region_observer.h
  92. ipc_interfaces_dumper.h
  93. javascript_test_observer.cc
  94. javascript_test_observer.h
  95. keep_alive_url_loader_utils.cc
  96. keep_alive_url_loader_utils.h
  97. local_frame_host_interceptor.cc
  98. local_frame_host_interceptor.h
  99. media_start_stop_observer.cc
  100. media_start_stop_observer.h
  101. mock_browsing_data_remover_delegate.cc
  102. mock_browsing_data_remover_delegate.h
  103. mock_captured_surface_controller.cc
  104. mock_captured_surface_controller.h
  105. mock_client_hints_controller_delegate.h
  106. mock_devtools_agent_host.cc
  107. mock_devtools_agent_host.h
  108. mock_download_manager.cc
  109. mock_download_manager.h
  110. mock_media_session.cc
  111. mock_media_session.h
  112. mock_navigation_handle.cc
  113. mock_navigation_handle.h
  114. mock_permission_controller.cc
  115. mock_permission_controller.h
  116. mock_permission_manager.cc
  117. mock_permission_manager.h
  118. mock_policy_container_host.cc
  119. mock_policy_container_host.h
  120. mock_render_process_host.cc
  121. mock_render_process_host.h
  122. mock_render_thread.cc
  123. mock_render_thread.h
  124. mock_video_picture_in_picture_window_controller_impl.cc
  125. mock_video_picture_in_picture_window_controller_impl.h
  126. mock_web_contents_observer.cc
  127. mock_web_contents_observer.h
  128. mojo_capability_control_test_interfaces.mojom
  129. mojo_capability_control_test_util.cc
  130. mojo_capability_control_test_util.h
  131. navigation_handle_observer.cc
  132. navigation_handle_observer.h
  133. navigation_simulator.h
  134. navigation_transition_test_utils.cc
  135. navigation_transition_test_utils.h
  136. nested_message_pump_android.cc
  137. nested_message_pump_android.h
  138. network_connection_change_simulator.cc
  139. network_connection_change_simulator.h
  140. network_service_test_helper.cc
  141. network_service_test_helper.h
  142. no_renderer_crashes_assertion.cc
  143. no_renderer_crashes_assertion.h
  144. no_renderer_crashes_assertion_unittest.cc
  145. OWNERS
  146. permission_overrides_unittest.cc
  147. permission_type_unittest.cc
  148. permissions_test_utils.cc
  149. permissions_test_utils.h
  150. policy_container_utils.cc
  151. policy_container_utils.h
  152. ppapi_test_utils.cc
  153. ppapi_test_utils.h
  154. prefetch_test_util.cc
  155. prefetch_test_util.h
  156. preloading_test_util.cc
  157. preloading_test_util.h
  158. prerender_test_util.cc
  159. prerender_test_util.h
  160. private_network_access_util.cc
  161. private_network_access_util.h
  162. README.md
  163. referrer_unittest.cc
  164. render_frame_host_test_support.cc
  165. render_frame_host_test_support.h
  166. render_view_test.cc
  167. render_view_test.h
  168. resource_load_observer.cc
  169. resource_load_observer.h
  170. scoped_accessibility_mode_override.cc
  171. scoped_accessibility_mode_override.h
  172. scoped_overscroll_modes.cc
  173. scoped_overscroll_modes.h
  174. scoped_page_focus_override.cc
  175. scoped_page_focus_override.h
  176. scoped_time_zone.cc
  177. scoped_time_zone.h
  178. scoped_web_ui_controller_factory_registration.cc
  179. scoped_web_ui_controller_factory_registration.h
  180. service_worker_host_interceptor.cc
  181. service_worker_host_interceptor.h
  182. service_worker_test_helpers.cc
  183. service_worker_test_helpers.h
  184. shared_storage_test_utils.cc
  185. shared_storage_test_utils.h
  186. signed_exchange_browser_test_helper.cc
  187. signed_exchange_browser_test_helper.h
  188. simple_url_loader_test_helper.cc
  189. simple_url_loader_test_helper.h
  190. slow_download_http_response.cc
  191. slow_download_http_response.h
  192. slow_http_response.cc
  193. slow_http_response.h
  194. synchronize_visual_properties_interceptor.cc
  195. synchronize_visual_properties_interceptor.h
  196. test_aggregation_service.cc
  197. test_aggregation_service.h
  198. test_browser_context.cc
  199. test_browser_context.h
  200. test_content_client_initializer.cc
  201. test_content_client_initializer.h
  202. test_devtools_protocol_client.cc
  203. test_devtools_protocol_client.h
  204. test_download_http_response.cc
  205. test_download_http_response.h
  206. test_file_error_injector.cc
  207. test_file_error_injector.h
  208. test_fileapi_operation_waiter.cc
  209. test_fileapi_operation_waiter.h
  210. test_frame_navigation_observer.cc
  211. test_frame_navigation_observer.h
  212. test_host_resolver.cc
  213. test_host_resolver.h
  214. test_image_transport_factory.cc
  215. test_image_transport_factory.h
  216. test_launcher.cc
  217. test_launcher.h
  218. test_media_session_client.cc
  219. test_media_session_client.h
  220. test_navigation_observer.cc
  221. test_navigation_observer.h
  222. test_navigation_throttle.cc
  223. test_navigation_throttle.h
  224. test_navigation_throttle_inserter.cc
  225. test_navigation_throttle_inserter.h
  226. test_navigation_ui_data.cc
  227. test_navigation_ui_data.h
  228. test_renderer_host.cc
  229. test_renderer_host.h
  230. test_select_url_fenced_frame_config_observer.h
  231. test_service.mojom
  232. test_shared_storage_header_observer.cc
  233. test_shared_storage_header_observer.h
  234. test_storage_partition.cc
  235. test_storage_partition.h
  236. test_synchronous_compositor_android.cc
  237. test_synchronous_compositor_android.h
  238. test_utils.cc
  239. test_utils.h
  240. test_utils_unittest.cc
  241. test_web_contents_factory.h
  242. test_web_ui.cc
  243. test_web_ui.h
  244. test_web_ui_data_source.cc
  245. test_web_ui_data_source.h
  246. test_web_ui_listener_observer.cc
  247. test_web_ui_listener_observer.h
  248. text_input_test_utils.cc
  249. text_input_test_utils.h
  250. text_input_test_utils_mac.mm
  251. theme_change_waiter.cc
  252. theme_change_waiter.h
  253. unittest_test_suite.cc
  254. unittest_test_suite.h
  255. update_user_activation_state_interceptor.cc
  256. update_user_activation_state_interceptor.h
  257. url_loader_interceptor.cc
  258. url_loader_interceptor.h
  259. url_loader_monitor.cc
  260. url_loader_monitor.h
  261. web_contents_observer_test_utils.cc
  262. web_contents_observer_test_utils.h
  263. web_contents_tester.cc
  264. web_contents_tester.h
  265. web_transport_simple_test_server.cc
  266. web_transport_simple_test_server.h
  267. web_ui_browsertest_util.cc
  268. web_ui_browsertest_util.h
  269. xr_test_utils.cc
  270. xr_test_utils.h
content/public/test/README.md

Content Browser Test Tips

A random collection of useful things to know when writing browser tests.

Executing JavaScript

If the test needs to use the return value of the script, use EvalJs():

  // Works with numerical types...
  EXPECT_EQ(0, EvalJs(shell(), "1 * 0");

  // ... string types ...
  EXPECT_EQ("BODY", EvalJs(shell(), "document.body.tagName");

  // and booleans too. Note the explicit use of EXPECT_EQ() instead of
  // EXPECT_TRUE() or EXPECT_FALSE(); this is intentional, and the latter
  // will not compile.
  EXPECT_EQ(false, EvalJs(shell(), "2 + 2 == 5"));

Like many other test helpers (e.g. the navigation helpers), the first argument accepts RenderFrameHost, WebContents, and other types.

  // Executes in the main frame.
  EXPECT_EQ(true, EvalJs(shell()->web_contents(), "window.top == window"));

  // Also executes in the main frame.
  EXPECT_EQ(true, EvalJs(shell(), "window.top == window"));

  // Executes in the first child frame of the main frame.
  EXPECT_EQ(
      false,
      EvalJs(ChildFrameAt(shell()->web_contents()->GetPrimaryMainFrame(), 0),
             "window.top == window"));

Otherwise, simply use ExecJs():

  EXPECT_TRUE(ExecJs("console.log('Hello world!')"));

Note that these helpers block until the script completes. For async execution, use ExecuteScriptAsync().

Finally, JsReplace() provides a convenient way to build strings for script execution:

  EXPECT_EQ("00", EvalJs(JsReplace("$1 + $2", 0, "0")));

Simulating Input

A wide range of methods are provided to simulate input such as clicks, touch, mouse moves and so on. Many reside in https://source.chromium.org/chromium/chromium/src/+/main:content/public/test/browser_test_utils.h.

When using input in tests, be aware that the renderer drops all input received when the main frame is not being updated or rendered immediately after load. There are three ways, in order of preference, to ensure that the input will be processed. Use these when your test input seems to be having no effect:

  • Use the ‘WaitForHitTestData method’ from content/public/test/hit_test_region_observer.h

  • Include visible text in the web contents you are interacting with.

  • Add ‘blink::switches::kAllowPreCommitInput’ as a command line flag.

Cross-origin navigations

For cross-origin navigations, it is to simplest to configure all hostnames to resolve to 127.0.0.1 in tests, using a snippet like this:

  void SetUpOnMainThread() override {
    host_resolver()->AddRule("*", "127.0.0.1");
    ASSERT_TRUE(embedded_test_server()->Start());
  }

After that, EmbeddedTestServer::GetURL() can be used to generate navigable URLs with the specific origin:

  const GURL& url_a = embedded_test_server()->GetURL("a.com", "/title1.html");
  const GURL& url_b = embedded_test_server()->GetURL("b.com", "/empty.html");

Most test resources are located in //content/test/data, e.g. navigating to GetURL("a.com", "/title1.html") will serve //content/test/data/title1.html as the content.

Browser-initiated navigation to a specific origin

Note: using arbitrary hostnames requires the host resolver to be correctly configured.

NavigateToURL() begins and waits for the navigation to complete, as if the navigation was browser-initiated, e.g. from the omnibox:

  GURL url(embedded_test_server()->GetURL("a.com", "/title1.html"));
  EXPECT_TRUE(NavigateToURL(shell(), url));

Note: NavigateToURL() allows subframes to be targetted, but outside of history navigations, subframe navigations are generally renderer-initiated.

Renderer-initiated navigation to a specific origin

Note: using arbitrary hostnames requires the host resolver to be correctly configured.

NavigateToURLFromRenderer() begins and waits for the navigation to complete, as if the navigation was renderer-initiated, e.g. by setting window.location:

  // Navigates the main frame.
  GURL url_1(embedded_test_server()->GetURL("a.com", "/title1.html"));
  EXPECT_TRUE(NavigateToURLFromRenderer(shell()->web_contents(), url_1));

  // Navigates the main frame too.
  GURL url_2(embedded_test_server()->GetURL("b.com", "/page_with_iframe.html"));
  EXPECT_TRUE(NavigateToURLFromRenderer(shell()->web_contents(), url_2));

  // Navigates the first child frame.
  GURL url_3(embedded_test_server()->GetURL("a.com", "/empty.html"));
  EXPECT_TRUE(
      NavigateToURLFromRenderer(
          ChildFrameAt(shell()->web_contents()->GetPrimaryMainFrame(), 0),
          url_3));

Dynamically generating a page with iframes

Note: using arbitrary hostnames requires the host resolver to be correctly configured.

cross_site_iframe_factory.html is a helper that makes it easy to generate a page with an arbitrary frame tree by navigating to a URL. The query string to the URL allows configuration of the frame tree, the origin of each frame, and a number of other options:

  GURL url(embedded_test_server()->GetURL(
      "a.com", "/cross_site_iframe_factory.html?a(b(a),c,example.com)"));
  EXPECT_TRUE(NavigateToURL(shell(), url));

Will generate a page with:

Main frame with origin `a.com`
  ├─ Child frame #1 with origin `b.com`
  │    └─ Grandchild frame with origin `a.com`
  ├─ Child frame #2 with origin `c.com`
  └─ Child frame #3 with origin `example.com`

Embedding an <iframe> with a specific origin

Note: using arbitrary hostnames requires the host resolver to be correctly configured.

Sometimes, a test page may need to embed a cross-origin URL. This is problematic for pages that contain only static HTML, as the embedded test server runs on a randomly selected port. Instead, static HTML can use the cross-site redirector to generate a cross-origin frame:

<!-- static_iframe.html -->
<html>
  <body>
    <iframe src="/cross-site/b.com/title1.html">
  </body>
</iframe>

Important: the cross-site redirector is not enabled by default. Override SetUpOnMainThread() to configure it like this:

  void SetUpOnMainThread() override {
    ...
    SetupCrossSiteRedirector(embedded_test_server());
    ...
  }

Simulating a slow load

Navigates to a page that takes 60 seconds to load.

  GURL url(embedded_test_server()->GetURL("/slow?60");
  EXPECT_TRUE(NavigateToURL(shell(), url));

The embedded test server also registers other default handlers that may be useful.

Simulating a failed navigation

Note that this is distinct from a navigation that results in an HTTP error, since those navigations still load arbitrary HTML from the server-supplied error page; a failed navigation is one that results in committing a Chrome-supplied error page, i.e. RenderFrameHost::IsErrorDocument() returns true.

  GURL url = embedded_test_server()->GetURL("/title1.html");
  std::unique_ptr<URLLoaderInterceptor> url_interceptor =
      URLLoaderInterceptor::SetupRequestFailForURL(url, net::ERR_DNS_TIMED_OUT);
  EXPECT_FALSE(NavigateToURLFromRenderer(shell()->web_contents(), url));