web_engine_integration_logging_test.cc 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. // Copyright 2020 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include <fuchsia/logger/cpp/fidl.h>
  5. #include <cstring>
  6. #include "base/containers/contains.h"
  7. #include "base/fuchsia/fuchsia_logging.h"
  8. #include "base/fuchsia/process_context.h"
  9. #include "base/fuchsia/test_log_listener_safe.h"
  10. #include "base/strings/string_piece.h"
  11. #include "base/strings/stringprintf.h"
  12. #include "base/test/bind.h"
  13. #include "fuchsia_web/common/test/frame_test_util.h"
  14. #include "fuchsia_web/webengine/test/context_provider_test_connector.h"
  15. #include "fuchsia_web/webengine/web_engine_integration_test_base.h"
  16. namespace {
  17. // Name of the console logging test page.
  18. constexpr char kLogTestPageFileName[] = "console_logging.html";
  19. constexpr char kLogTestPageDebugMessage[] = "This is a debug() message.";
  20. // Debug name to create Frames with, to use as their logging tag.
  21. constexpr char kFrameLogTag[] = "Test🖼🪵";
  22. constexpr char kNormalizedPortNumber[] = "678";
  23. // Replaces the line number in frame_impl.cc with kNormalizedLineNumber and
  24. // the port with kNormalizedPortNumber to enable reliable comparison of
  25. // console log messages.
  26. std::string NormalizeConsoleLogMessage(base::StringPiece original) {
  27. const char kSchemePortColon[] = "http://127.0.0.1:";
  28. size_t port_begin =
  29. original.find(kSchemePortColon) + strlen(kSchemePortColon);
  30. size_t path_begin = original.find("/", port_begin);
  31. return std::string(original).replace(port_begin, path_begin - port_begin,
  32. kNormalizedPortNumber);
  33. }
  34. } // namespace
  35. class WebEngineIntegrationLoggingTest : public WebEngineIntegrationTestBase {
  36. protected:
  37. WebEngineIntegrationLoggingTest()
  38. : isolated_archivist_service_dir_(
  39. StartIsolatedArchivist(archivist_controller_.NewRequest())) {
  40. // Redirect the LogSink service to an isolated archivist instance.
  41. zx_status_t status = filtered_service_directory()
  42. .outgoing_directory()
  43. ->RemovePublicService<fuchsia::logger::LogSink>();
  44. ZX_CHECK(status == ZX_OK, status) << "RemovePublicService";
  45. status =
  46. filtered_service_directory().outgoing_directory()->AddPublicService(
  47. fidl::InterfaceRequestHandler<fuchsia::logger::LogSink>(
  48. [this](auto request) {
  49. isolated_archivist_service_dir_.Connect(std::move(request));
  50. }));
  51. ZX_CHECK(status == ZX_OK, status) << "AddPublicService";
  52. }
  53. void SetUp() override {
  54. WebEngineIntegrationTestBase::SetUp();
  55. StartWebEngineForLoggingTest(
  56. base::CommandLine(base::CommandLine::NO_PROGRAM));
  57. log_ = isolated_archivist_service_dir_.Connect<fuchsia::logger::Log>();
  58. }
  59. fuchsia::logger::Log* log() { return log_.get(); }
  60. private:
  61. // Starts WebEngine without redirecting its logs.
  62. void StartWebEngineForLoggingTest(base::CommandLine command_line) {
  63. web_context_provider_ = ConnectContextProviderForLoggingTest(
  64. web_engine_controller_.NewRequest(), std::move(command_line));
  65. web_context_provider_.set_error_handler(
  66. [](zx_status_t status) { ADD_FAILURE(); });
  67. }
  68. // Starts an isolated instance of Archivist to receive and dump log statements
  69. // via the fuchsia.logger.Log* APIs.
  70. fidl::InterfaceHandle<fuchsia::io::Directory> StartIsolatedArchivist(
  71. fidl::InterfaceRequest<fuchsia::sys::ComponentController>
  72. component_controller_request) {
  73. const char kArchivistUrl[] =
  74. "fuchsia-pkg://fuchsia.com/archivist-for-embedding#meta/"
  75. "archivist-for-embedding.cmx";
  76. fuchsia::sys::LaunchInfo launch_info;
  77. launch_info.url = kArchivistUrl;
  78. fidl::InterfaceHandle<fuchsia::io::Directory> archivist_services_dir;
  79. launch_info.directory_request =
  80. archivist_services_dir.NewRequest().TakeChannel();
  81. auto launcher = base::ComponentContextForProcess()
  82. ->svc()
  83. ->Connect<fuchsia::sys::Launcher>();
  84. launcher->CreateComponent(std::move(launch_info),
  85. std::move(component_controller_request));
  86. return archivist_services_dir;
  87. }
  88. fuchsia::sys::ComponentControllerPtr archivist_controller_;
  89. sys::ServiceDirectory isolated_archivist_service_dir_;
  90. fuchsia::logger::LogPtr log_;
  91. };
  92. // Verifies that calling messages from console.debug() calls go to the Fuchsia
  93. // system log when the script log level is set to DEBUG.
  94. TEST_F(WebEngineIntegrationLoggingTest, SetJavaScriptLogLevel_DEBUG) {
  95. base::SimpleTestLogListener log_listener;
  96. log_listener.ListenToLog(log(), nullptr);
  97. // Create the Context & Frame with all log severities enabled.
  98. CreateContext(TestContextParams());
  99. fuchsia::web::CreateFrameParams frame_params;
  100. frame_params.set_debug_name(kFrameLogTag);
  101. CreateFrameWithParams(std::move(frame_params));
  102. frame_->SetJavaScriptLogLevel(fuchsia::web::ConsoleLogLevel::DEBUG);
  103. // Navigate to the test page, which will emit console logging.
  104. ASSERT_NO_FATAL_FAILURE(LoadUrlAndExpectResponse(
  105. embedded_test_server_.GetURL(std::string("/") + kLogTestPageFileName)
  106. .spec()));
  107. navigation_listener()->RunUntilTitleEquals("ended");
  108. // Run until the message passed to console.debug() is received.
  109. absl::optional<fuchsia::logger::LogMessage> logged_message =
  110. log_listener.RunUntilMessageReceived(kLogTestPageDebugMessage);
  111. ASSERT_TRUE(logged_message.has_value());
  112. // console.debug() should map to Fuchsia's DEBUG log severity.
  113. EXPECT_EQ(logged_message->severity,
  114. static_cast<int32_t>(fuchsia::logger::LogLevelFilter::DEBUG));
  115. // Verify that the Frame's |debug_name| is amongst the log message tags.
  116. EXPECT_FALSE(logged_message->tags.empty());
  117. EXPECT_TRUE(base::Contains(logged_message->tags, kFrameLogTag));
  118. // Verify that the message is formatted as expected.
  119. EXPECT_EQ(NormalizeConsoleLogMessage(logged_message->msg),
  120. base::StringPrintf("[http://127.0.0.1:%s/console_logging.html(8)] "
  121. "This is a debug() message.",
  122. kNormalizedPortNumber));
  123. }