tree: 8e62855a763a62efae02c2887b92d8b3d3ff6283 [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. blink_test_browser_support.h
  13. blink_test_environment.cc
  14. blink_test_environment.h
  15. bluetooth_test_utils.cc
  16. bluetooth_test_utils.h
  17. browser_task_environment.cc
  18. browser_task_environment.h
  19. browser_task_environment_unittest.cc
  20. browser_test.h
  21. browser_test_base.cc
  22. browser_test_base.h
  23. browser_test_utils.cc
  24. browser_test_utils.h
  25. browsing_data_remover_test_util.cc
  26. browsing_data_remover_test_util.h
  27. content_browser_test.cc
  28. content_browser_test.h
  29. content_browser_test_shell_main_delegate.cc
  30. content_browser_test_shell_main_delegate.h
  31. content_browser_test_utils.cc
  32. content_browser_test_utils.h
  33. content_browser_test_utils_mac.mm
  34. content_cert_verifier_browser_test.cc
  35. content_cert_verifier_browser_test.h
  36. content_mock_cert_verifier.cc
  37. content_mock_cert_verifier.h
  38. content_test_suite_base.cc
  39. content_test_suite_base.h
  40. DEPS
  41. devtools_permission_overrides_unittest.cc
  42. download_test_observer.cc
  43. download_test_observer.h
  44. dump_accessibility_test_helper.cc
  45. dump_accessibility_test_helper.h
  46. dump_accessibility_test_helper_unittest.cc
  47. fake_download_item.cc
  48. fake_download_item.h
  49. fake_frame_widget.cc
  50. fake_frame_widget.h
  51. fake_local_frame.cc
  52. fake_local_frame.h
  53. fake_pepper_plugin_instance.cc
  54. fake_pepper_plugin_instance.h
  55. fake_remote_frame.cc
  56. fake_remote_frame.h
  57. fake_render_widget_host.cc
  58. fake_render_widget_host.h
  59. fake_service_worker_context.cc
  60. fake_service_worker_context.h
  61. fake_speech_recognition_manager.cc
  62. fake_speech_recognition_manager.h
  63. find_test_utils.cc
  64. find_test_utils.h
  65. frame_load_waiter.cc
  66. frame_load_waiter.h
  67. hit_test_region_observer.cc
  68. hit_test_region_observer.h
  69. idle_test_utils.cc
  70. idle_test_utils.h
  71. javascript_test_observer.cc
  72. javascript_test_observer.h
  73. local_frame_host_interceptor.cc
  74. local_frame_host_interceptor.h
  75. media_start_stop_observer.cc
  76. media_start_stop_observer.h
  77. mock_browsing_data_remover_delegate.cc
  78. mock_browsing_data_remover_delegate.h
  79. mock_download_manager.cc
  80. mock_download_manager.h
  81. mock_navigation_handle.cc
  82. mock_navigation_handle.h
  83. mock_notification_observer.cc
  84. mock_notification_observer.h
  85. mock_permission_manager.cc
  86. mock_permission_manager.h
  87. mock_policy_container_host.cc
  88. mock_policy_container_host.h
  89. mock_render_process_host.cc
  90. mock_render_process_host.h
  91. mock_render_thread.cc
  92. mock_render_thread.h
  93. mock_resource_context.cc
  94. mock_resource_context.h
  95. navigation_handle_observer.cc
  96. navigation_handle_observer.h
  97. navigation_simulator.h
  98. nested_message_pump_android.cc
  99. nested_message_pump_android.h
  100. network_connection_change_simulator.cc
  101. network_connection_change_simulator.h
  102. network_service_test_helper.cc
  103. network_service_test_helper.h
  104. no_renderer_crashes_assertion.cc
  105. no_renderer_crashes_assertion.h
  106. no_renderer_crashes_assertion_unittest.cc
  107. OWNERS
  108. permission_type_unittest.cc
  109. policy_container_utils.cc
  110. policy_container_utils.h
  111. ppapi_test_utils.cc
  112. ppapi_test_utils.h
  113. README.md
  114. referrer_unittest.cc
  115. render_frame_host_test_support.cc
  116. render_frame_host_test_support.h
  117. render_view_test.cc
  118. render_view_test.h
  119. scoped_overscroll_modes.cc
  120. scoped_overscroll_modes.h
  121. service_worker_host_interceptor.cc
  122. service_worker_host_interceptor.h
  123. service_worker_test_helpers.cc
  124. service_worker_test_helpers.h
  125. signed_exchange_browser_test_helper.cc
  126. signed_exchange_browser_test_helper.h
  127. simple_url_loader_test_helper.cc
  128. simple_url_loader_test_helper.h
  129. slow_download_http_response.cc
  130. slow_download_http_response.h
  131. slow_http_response.cc
  132. slow_http_response.h
  133. test_browser_context.cc
  134. test_browser_context.h
  135. test_content_client_initializer.cc
  136. test_content_client_initializer.h
  137. test_download_http_response.cc
  138. test_download_http_response.h
  139. test_file_error_injector.cc
  140. test_file_error_injector.h
  141. test_fileapi_operation_waiter.cc
  142. test_fileapi_operation_waiter.h
  143. test_frame_navigation_observer.cc
  144. test_frame_navigation_observer.h
  145. test_host_resolver.cc
  146. test_host_resolver.h
  147. test_launcher.cc
  148. test_launcher.h
  149. test_navigation_observer.cc
  150. test_navigation_observer.h
  151. test_navigation_throttle.cc
  152. test_navigation_throttle.h
  153. test_navigation_throttle_inserter.cc
  154. test_navigation_throttle_inserter.h
  155. test_navigation_ui_data.cc
  156. test_navigation_ui_data.h
  157. test_notification_tracker.cc
  158. test_notification_tracker.h
  159. test_renderer_host.cc
  160. test_renderer_host.h
  161. test_service.mojom
  162. test_storage_partition.cc
  163. test_storage_partition.h
  164. test_synchronous_compositor_android.cc
  165. test_synchronous_compositor_android.h
  166. test_utils.cc
  167. test_utils.h
  168. test_utils_unittest.cc
  169. test_web_contents_factory.h
  170. test_web_ui.cc
  171. test_web_ui.h
  172. test_web_ui_data_source.cc
  173. test_web_ui_data_source.h
  174. test_web_ui_listener_observer.cc
  175. test_web_ui_listener_observer.h
  176. text_input_test_utils.cc
  177. text_input_test_utils.h
  178. text_input_test_utils_mac.mm
  179. theme_change_waiter.cc
  180. theme_change_waiter.h
  181. unittest_test_suite.cc
  182. unittest_test_suite.h
  183. url_loader_interceptor.cc
  184. url_loader_interceptor.h
  185. url_loader_monitor.cc
  186. url_loader_monitor.h
  187. web_contents_receiver_set_test_binder.h
  188. web_contents_tester.cc
  189. web_contents_tester.h
  190. web_ui_browsertest_util.cc
  191. web_ui_browsertest_util.h
  192. xr_test_utils.cc
  193. 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()->GetWebContents(), "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()->GetWebContents()->GetMainFrame(), 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")));

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()->GetWebContents(), url_1));

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

  // Navigates the first child frame.
  GURL url_3(embedded_test_server()->GetURL("a.com", "/empty.html"));
  EXPECT_TRUE(
      NavigateToURLFromRenderer(
          ChildFrameAt(shell()->GetWebContents()->GetMainFrame(), 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.