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 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
|
// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/ui/webui/top_chrome/webui_contents_wrapper.h"
#include <memory>
#include <utility>
#include "base/memory/weak_ptr.h"
#include "chrome/test/base/testing_profile.h"
#include "chrome/test/views/chrome_views_test_base.h"
#include "components/input/native_web_keyboard_event.h"
#include "content/public/browser/render_process_host.h"
#include "content/public/browser/render_widget_host.h"
#include "content/public/browser/render_widget_host_view.h"
#include "content/public/browser/site_instance.h"
#include "content/public/common/content_client.h"
#include "content/public/test/browser_task_environment.h"
#include "content/public/test/test_renderer_host.h"
#include "content/public/test/web_contents_tester.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/public/mojom/page/draggable_region.mojom.h"
#include "third_party/blink/public/mojom/window_features/window_features.mojom.h"
namespace views {
namespace {
class MockHost : public WebUIContentsWrapper::Host {
public:
// WebUIContentsWrapper::Host:
void ShowUI() override { ++show_ui_called_; }
void CloseUI() override { ++close_ui_called_; }
void ShowCustomContextMenu(
gfx::Point point,
std::unique_ptr<ui::MenuModel> menu_model) override {
++show_custom_context_menu_called_;
}
void ResizeDueToAutoResize(content::WebContents* source,
const gfx::Size& new_size) override {
++resize_due_to_auto_resize_called_;
}
void DraggableRegionsChanged(
const std::vector<blink::mojom::DraggableRegionPtr>& regions,
content::WebContents* contents) override {
++draggable_regions_changed_called_;
}
content::WebContents* AddNewContents(
content::WebContents* source,
std::unique_ptr<content::WebContents> new_contents,
const GURL& target_url,
WindowOpenDisposition disposition,
const blink::mojom::WindowFeatures& window_features,
bool user_gesture,
bool* was_blocked) override {
++add_new_contents_called_;
return nullptr;
}
base::WeakPtr<MockHost> GetWeakPtr() {
return weak_ptr_factory_.GetWeakPtr();
}
int show_ui_called() const { return show_ui_called_; }
int close_ui_called() const { return close_ui_called_; }
int show_custom_context_menu_called() const {
return show_custom_context_menu_called_;
}
int resize_due_to_auto_resize_called() const {
return resize_due_to_auto_resize_called_;
}
int draggable_regions_changed_called() const {
return draggable_regions_changed_called_;
}
int add_new_contents_called() const { return add_new_contents_called_; }
private:
int show_ui_called_ = 0;
int close_ui_called_ = 0;
int show_custom_context_menu_called_ = 0;
int resize_due_to_auto_resize_called_ = 0;
int draggable_regions_changed_called_ = 0;
int add_new_contents_called_ = 0;
base::WeakPtrFactory<MockHost> weak_ptr_factory_{this};
};
class TestWebUIContentsWrapper : public WebUIContentsWrapper {
public:
explicit TestWebUIContentsWrapper(Profile* profile)
: WebUIContentsWrapper(GURL(""), profile, 0, true, true, true, "Test") {}
~TestWebUIContentsWrapper() override = default;
// WebUIContentsWrapper:
void ReloadWebContents() override {}
base::WeakPtr<WebUIContentsWrapper> GetWeakPtr() override {
return weak_ptr_factory_.GetWeakPtr();
}
private:
base::WeakPtrFactory<TestWebUIContentsWrapper> weak_ptr_factory_{this};
};
} // namespace
namespace test {
class WebUIContentsWrapperTest : public ChromeViewsTestBase {
public:
WebUIContentsWrapperTest() = default;
WebUIContentsWrapperTest(const WebUIContentsWrapperTest&) = delete;
WebUIContentsWrapperTest& operator=(const WebUIContentsWrapperTest&) = delete;
~WebUIContentsWrapperTest() override = default;
// ViewsTestBase:
void SetUp() override {
ChromeViewsTestBase::SetUp();
profile_ = std::make_unique<TestingProfile>();
scoped_refptr<content::SiteInstance> instance =
content::SiteInstance::Create(profile_.get());
instance->GetOrCreateProcess()->Init();
auto test_contents = content::WebContentsTester::CreateTestWebContents(
profile_.get(), std::move(instance));
contents_wrapper_ =
std::make_unique<TestWebUIContentsWrapper>(profile_.get());
contents_wrapper_->SetWebContentsForTesting(std::move(test_contents));
}
WebUIContentsWrapper* contents_wrapper() { return contents_wrapper_.get(); }
private:
content::RenderViewHostTestEnabler test_render_host_factories_;
std::unique_ptr<TestingProfile> profile_;
std::unique_ptr<WebUIContentsWrapper> contents_wrapper_;
};
TEST_F(WebUIContentsWrapperTest, CallsHostForShowUIAndCloseUIWhenPresent) {
MockHost host;
EXPECT_EQ(0, host.show_ui_called());
EXPECT_EQ(0, host.close_ui_called());
contents_wrapper()->SetHost(host.GetWeakPtr());
contents_wrapper()->ShowUI();
contents_wrapper()->CloseUI();
EXPECT_EQ(1, host.show_ui_called());
EXPECT_EQ(1, host.close_ui_called());
contents_wrapper()->SetHost(nullptr);
contents_wrapper()->ShowUI();
contents_wrapper()->CloseUI();
EXPECT_EQ(1, host.show_ui_called());
EXPECT_EQ(1, host.close_ui_called());
}
TEST_F(WebUIContentsWrapperTest, CallsShowContextMenu) {
MockHost host;
EXPECT_EQ(0, host.show_custom_context_menu_called());
contents_wrapper()->SetHost(host.GetWeakPtr());
contents_wrapper()->ShowContextMenu(gfx::Point(0, 0), nullptr);
EXPECT_EQ(1, host.show_custom_context_menu_called());
contents_wrapper()->SetHost(nullptr);
contents_wrapper()->ShowContextMenu(gfx::Point(0, 0), nullptr);
EXPECT_EQ(1, host.show_custom_context_menu_called());
}
TEST_F(WebUIContentsWrapperTest, NotifiesHostWhenResized) {
MockHost host;
EXPECT_EQ(0, host.resize_due_to_auto_resize_called());
contents_wrapper()->SetHost(host.GetWeakPtr());
contents_wrapper()->ResizeDueToAutoResize(contents_wrapper()->web_contents(),
gfx::Size());
EXPECT_EQ(1, host.resize_due_to_auto_resize_called());
contents_wrapper()->SetHost(nullptr);
contents_wrapper()->ResizeDueToAutoResize(contents_wrapper()->web_contents(),
gfx::Size());
EXPECT_EQ(1, host.resize_due_to_auto_resize_called());
}
TEST_F(WebUIContentsWrapperTest, EscapeKeyClosesHost) {
MockHost host;
contents_wrapper()->SetHost(host.GetWeakPtr());
input::NativeWebKeyboardEvent event(
blink::WebInputEvent::Type::kRawKeyDown,
blink::WebInputEvent::kNoModifiers,
blink::WebInputEvent::GetStaticTimeStampForTests());
event.windows_key_code = ui::VKEY_ESCAPE;
EXPECT_EQ(0, host.close_ui_called());
contents_wrapper()
->web_contents()
->GetRenderWidgetHostView()
->GetRenderWidgetHost()
->ForwardKeyboardEvent(event);
EXPECT_EQ(1, host.close_ui_called());
}
TEST_F(WebUIContentsWrapperTest, ClosesHostOnWebContentsCrash) {
MockHost host;
contents_wrapper()->SetHost(host.GetWeakPtr());
EXPECT_EQ(0, host.close_ui_called());
contents_wrapper()->PrimaryMainFrameRenderProcessGone(
base::TerminationStatus::TERMINATION_STATUS_PROCESS_CRASHED);
EXPECT_EQ(1, host.close_ui_called());
}
TEST_F(WebUIContentsWrapperTest, NotifiesHostWhenDraggableRegionsUpdated) {
MockHost host;
EXPECT_EQ(0, host.draggable_regions_changed_called());
// Ensure updates are propagated to the host.
std::vector<blink::mojom::DraggableRegionPtr> regions;
auto region_rect = blink::mojom::DraggableRegion::New();
region_rect->bounds = {10, 10, 100, 100};
region_rect->draggable = true;
regions.push_back(std::move(region_rect));
contents_wrapper()->SetHost(host.GetWeakPtr());
contents_wrapper()->DraggableRegionsChanged(
regions, contents_wrapper()->web_contents());
EXPECT_EQ(1, host.draggable_regions_changed_called());
// After the host has been unset it should no longer receive updates.
contents_wrapper()->SetHost(nullptr);
contents_wrapper()->DraggableRegionsChanged(
regions, contents_wrapper()->web_contents());
EXPECT_EQ(1, host.draggable_regions_changed_called());
// When a draggable region has been received and a host is set it should be
// notified of the most recently set draggable region.
contents_wrapper()->SetHost(host.GetWeakPtr());
EXPECT_EQ(2, host.draggable_regions_changed_called());
}
// Tests that when auto-resize is enabled (this is configured in
// TestWebUIContentsWrapper), the host is resize when SetHost() is called if the
// frame size is available.
TEST_F(WebUIContentsWrapperTest, HostIsResizedOnSetHost) {
MockHost host;
contents_wrapper()->SetHost(host.GetWeakPtr());
// The render frame size is unset so the host is not resized.
EXPECT_EQ(0, host.resize_due_to_auto_resize_called());
contents_wrapper()->SetHost(nullptr);
// The frame size is updated and therefore the host should be resized.
content::WebContentsTester::For(contents_wrapper()->web_contents())
->SetMainFrameSize(gfx::Size(100, 100));
contents_wrapper()->SetHost(host.GetWeakPtr());
EXPECT_EQ(1, host.resize_due_to_auto_resize_called());
}
TEST_F(WebUIContentsWrapperTest, HostNotifiedOnAddNewContents) {
MockHost host;
EXPECT_EQ(0, host.add_new_contents_called());
contents_wrapper()->SetHost(host.GetWeakPtr());
bool blocked;
contents_wrapper()->AddNewContents(
nullptr /* source */,
std::unique_ptr<content::WebContents>() /* new_contents */,
GURL() /* target_url */,
WindowOpenDisposition::CURRENT_TAB /* disposition */,
blink::mojom::WindowFeatures() /* window_features */,
false /* user_gesture */, &blocked /* was_blocked */);
EXPECT_EQ(1, host.add_new_contents_called());
contents_wrapper()->SetHost(nullptr);
contents_wrapper()->AddNewContents(
nullptr /* source */,
std::unique_ptr<content::WebContents>() /* new_contents */,
GURL() /* target_url */,
WindowOpenDisposition::CURRENT_TAB /* disposition */,
blink::mojom::WindowFeatures() /* window_features */,
false /* user_gesture */, &blocked /* was_blocked */);
EXPECT_EQ(1, host.add_new_contents_called());
}
} // namespace test
} // namespace views
|