1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64
   65
   66
   67
   68
   69
   70
   71
   72
   73
   74
   75
   76
   77
   78
   79
   80
   81
   82
   83
   84
   85
   86
   87
   88
   89
   90
   91
   92
   93
   94
   95
   96
   97
   98
   99
  100
  101
  102
  103
  104
  105
  106
  107
  108

fuchsia_web / webengine / test / web_engine_browser_test.cc [blame]

// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "fuchsia_web/webengine/test/web_engine_browser_test.h"

#include <fuchsia/web/cpp/fidl.h>
#include <lib/sys/cpp/component_context.h>
#include <lib/sys/cpp/service_directory.h>
#include <lib/vfs/cpp/pseudo_dir.h>

#include <vector>

#include "base/command_line.h"
#include "base/fuchsia/fuchsia_logging.h"
#include "base/fuchsia/process_context.h"
#include "base/fuchsia/test_component_context_for_process.h"
#include "base/run_loop.h"
#include "fuchsia_web/webengine/browser/web_engine_browser_context.h"
#include "fuchsia_web/webengine/browser/web_engine_browser_main_parts.h"
#include "fuchsia_web/webengine/browser/web_engine_content_browser_client.h"
#include "fuchsia_web/webengine/switches.h"
#include "fuchsia_web/webengine/web_engine_main_delegate.h"
#include "net/test/embedded_test_server/default_handlers.h"
#include "ui/gfx/switches.h"
#include "ui/ozone/public/ozone_switches.h"

WebEngineBrowserTest::WebEngineBrowserTest() = default;

WebEngineBrowserTest::~WebEngineBrowserTest() = default;

void WebEngineBrowserTest::SetUp() {
  base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
  SetUpCommandLine(command_line);
  BrowserTestBase::SetUp();
}

void WebEngineBrowserTest::PreRunTestOnMainThread() {
  zx_status_t status = published_services().Connect(context_.NewRequest());
  ZX_CHECK(status == ZX_OK, status) << "Connect fuchsia.web.Context";

  net::test_server::RegisterDefaultHandlers(embedded_test_server());
  if (!test_server_root_.empty()) {
    embedded_test_server()->ServeFilesFromSourceDirectory(test_server_root_);
  }
}

void WebEngineBrowserTest::PostRunTestOnMainThread() {
  // Unbind the Context while the message loops are still alive.
  context_.Unbind();

  // Shutting down the context needs to run connection error handlers
  // etc which normally are what causes the main loop to exit. Since in
  // tests we are not running a main loop indefinitely, we want to let those
  // things run, just as they would in production, before shutting down. This
  // makes the main loop run until breaking the connection completes.
  base::RunLoop().RunUntilIdle();
}

sys::ServiceDirectory& WebEngineBrowserTest::published_services() {
  if (!published_services_) {
    zx::channel svc_request;
    published_services_ =
        sys::ServiceDirectory::CreateWithRequest(&svc_request);
    base::ComponentContextForProcess()
        ->outgoing()
        ->GetOrCreateDirectory("svc")
        ->Serve(fuchsia_io::wire::kPermReadable,
                fidl::ServerEnd<fuchsia_io::Directory>(std::move(svc_request)));
  }
  return *published_services_;
}

void WebEngineBrowserTest::SetHeadlessInCommandLine(
    base::CommandLine* command_line) {
  command_line->AppendSwitchNative(switches::kOzonePlatform,
                                   switches::kHeadless);
  command_line->AppendSwitch(switches::kHeadless);
}

ContextImpl* WebEngineBrowserTest::context_impl() const {
  // The ContentMainDelegate and ContentBrowserClient must already exist,
  // since those are created early on, before test setup or execution.
  auto* browser_client =
      WebEngineMainDelegate::GetInstanceForTest()->browser_client();
  CHECK(browser_client);

  auto* main_parts = browser_client->main_parts_for_test();
  CHECK(main_parts) << "context_impl() called too early in browser startup.";

  auto* context = main_parts->context_for_test();
  CHECK(context) << "context_impl() called before Context connected.";

  return context;
}

std::vector<FrameHostImpl*> WebEngineBrowserTest::frame_host_impls() const {
  // The ContentMainDelegate and ContentBrowserClient must already exist,
  // since those are created early on, before test setup or execution.
  auto* browser_client =
      WebEngineMainDelegate::GetInstanceForTest()->browser_client();
  CHECK(browser_client);

  auto* main_parts = browser_client->main_parts_for_test();
  CHECK(main_parts) << "frame_host_impl() called too early in browser startup.";

  return main_parts->frame_hosts_for_test();
}