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
  109
  110
  111
  112
  113
  114
  115
  116
  117
  118
  119
  120
  121
  122
  123
  124
  125
  126
  127
  128
  129
  130
  131
  132
  133
  134
  135
  136
  137
  138
  139

content / web_test / browser / web_test_shell_platform_delegate_views.cc [blame]

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

#include "content/web_test/browser/web_test_shell_platform_delegate.h"

#include "base/containers/contains.h"
#include "content/public/browser/render_widget_host_view.h"
#include "content/public/browser/web_contents.h"
#include "content/shell/browser/shell.h"
#include "content/shell/browser/shell_platform_data_aura.h"

namespace content {

struct WebTestShellPlatformDelegate::WebTestShellData {
  gfx::Size content_size;
};

struct WebTestShellPlatformDelegate::WebTestPlatformData {
  // Only used in headless mode. Uses `wm::WMState` from the
  // ShellPlatformDelegate, which must outlive this.
  std::unique_ptr<ShellPlatformDataAura> aura;
};

WebTestShellPlatformDelegate::WebTestShellPlatformDelegate() = default;
WebTestShellPlatformDelegate::~WebTestShellPlatformDelegate() = default;

void WebTestShellPlatformDelegate::Initialize(
    const gfx::Size& default_window_size) {
  ShellPlatformDelegate::Initialize(default_window_size);

  if (IsHeadless()) {
    web_test_platform_ = std::make_unique<WebTestPlatformData>();
    web_test_platform_->aura =
        std::make_unique<ShellPlatformDataAura>(default_window_size);
  }
}

void WebTestShellPlatformDelegate::CreatePlatformWindow(
    Shell* shell,
    const gfx::Size& initial_size) {
  if (!IsHeadless()) {
    ShellPlatformDelegate::CreatePlatformWindow(shell, initial_size);
    return;
  }

  DCHECK(!base::Contains(web_test_shell_data_map_, shell));
  WebTestShellData& shell_data = web_test_shell_data_map_[shell];

  shell_data.content_size = initial_size;

  web_test_platform_->aura->ResizeWindow(initial_size);
}

gfx::NativeWindow WebTestShellPlatformDelegate::GetNativeWindow(Shell* shell) {
  if (!IsHeadless())
    return ShellPlatformDelegate::GetNativeWindow(shell);

  NOTREACHED_IN_MIGRATION();
  return {};
}

void WebTestShellPlatformDelegate::CleanUp(Shell* shell) {
  if (!IsHeadless()) {
    ShellPlatformDelegate::CleanUp(shell);
    return;
  }

  DCHECK(base::Contains(web_test_shell_data_map_, shell));
  web_test_shell_data_map_.erase(shell);
}

void WebTestShellPlatformDelegate::SetContents(Shell* shell) {
  if (!IsHeadless()) {
    ShellPlatformDelegate::SetContents(shell);
    return;
  }

  DCHECK(base::Contains(web_test_shell_data_map_, shell));
  WebTestShellData& shell_data = web_test_shell_data_map_[shell];

  aura::Window* content = shell->web_contents()->GetNativeView();
  aura::Window* parent = web_test_platform_->aura->host()->window();
  if (!parent->Contains(content)) {
    parent->AddChild(content);
    // Move the cursor to a fixed position before tests run to avoid getting
    // an unpredictable result from mouse events.
    content->MoveCursorTo(gfx::Point());
    content->Show();
  }
  content->SetBounds(gfx::Rect(shell_data.content_size));
  RenderWidgetHostView* host_view =
      shell->web_contents()->GetRenderWidgetHostView();
  if (host_view)
    host_view->SetSize(shell_data.content_size);
}

void WebTestShellPlatformDelegate::EnableUIControl(Shell* shell,
                                                   UIControl control,
                                                   bool is_enabled) {
  if (!IsHeadless())
    ShellPlatformDelegate::EnableUIControl(shell, control, is_enabled);
  // Nothing in headless mode.
}

void WebTestShellPlatformDelegate::SetAddressBarURL(Shell* shell,
                                                    const GURL& url) {
  if (!IsHeadless())
    ShellPlatformDelegate::SetAddressBarURL(shell, url);
  // Nothing in headless mode.
}

void WebTestShellPlatformDelegate::SetTitle(Shell* shell,
                                            const std::u16string& title) {
  if (!IsHeadless())
    ShellPlatformDelegate::SetTitle(shell, title);
  // Nothing in headless mode.
}

void WebTestShellPlatformDelegate::MainFrameCreated(Shell* shell) {
  // No difference in headless mode.
  ShellPlatformDelegate::MainFrameCreated(shell);
}

bool WebTestShellPlatformDelegate::DestroyShell(Shell* shell) {
  if (!IsHeadless())
    return ShellPlatformDelegate::DestroyShell(shell);

  return false;  // Shell destroys itself.
}

void WebTestShellPlatformDelegate::ResizeWebContent(
    Shell* shell,
    const gfx::Size& content_size) {
  // No difference in headless mode.
  ShellPlatformDelegate::ResizeWebContent(shell, content_size);
}

}  // namespace content