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 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333
|
// Copyright 2017 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/media_router/presentation_receiver_window_controller.h"
#include <string>
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/functional/bind.h"
#include "base/path_service.h"
#include "base/run_loop.h"
#include "base/test/run_until.h"
#include "base/threading/thread_restrictions.h"
#include "base/timer/elapsed_timer.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_window.h"
#include "chrome/common/chrome_paths.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "components/media_router/browser/presentation/local_presentation_manager.h"
#include "components/media_router/browser/presentation/local_presentation_manager_factory.h"
#include "content/public/browser/navigation_controller.h"
#include "content/public/browser/navigation_entry.h"
#include "content/public/browser/navigation_handle.h"
#include "content/public/browser/web_contents_observer.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_utils.h"
#include "content/public/test/test_utils.h"
#include "extensions/browser/script_executor.h"
#include "mojo/public/cpp/bindings/pending_receiver.h"
#include "mojo/public/cpp/bindings/pending_remote.h"
#include "mojo/public/cpp/bindings/receiver.h"
#include "mojo/public/cpp/bindings/remote.h"
#include "net/base/filename_util.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "third_party/blink/public/mojom/presentation/presentation.mojom.h"
#include "ui/display/display.h"
#include "ui/display/screen.h"
#include "ui/views/widget/widget.h"
#include "url/gurl.h"
using testing::_;
using testing::Invoke;
namespace {
constexpr char kPresentationId[] = "test_id";
const base::FilePath::StringViewType kResourcePath =
FILE_PATH_LITERAL("media/router/");
base::RepeatingCallback<void(const std::string&)> GetNoopTitleChangeCallback() {
return base::BindRepeating([](const std::string& title) {});
}
base::FilePath GetResourceFile(base::FilePath::StringViewType relative_path) {
base::FilePath base_dir;
if (!base::PathService::Get(chrome::DIR_TEST_DATA, &base_dir)) {
return base::FilePath();
}
base::FilePath full_path =
base_dir.Append(kResourcePath).Append(relative_path);
{
base::ScopedAllowBlockingForTesting scoped_allow_blocking;
if (!PathExists(full_path)) {
return base::FilePath();
}
}
return full_path;
}
// This class imitates a presentation controller page from a messaging
// standpoint. It is registered as a controller connection for the appropriate
// presentation ID with the LocalPresentationManager to facilitate a
// presentation API communication test with the receiver window.
class FakeControllerConnection final
: public blink::mojom::PresentationConnection {
public:
FakeControllerConnection() = default;
FakeControllerConnection(const FakeControllerConnection&) = delete;
FakeControllerConnection& operator=(const FakeControllerConnection&) = delete;
void SendTextMessage(const std::string& message) {
ASSERT_TRUE(receiver_connection_remote_.is_bound());
receiver_connection_remote_->OnMessage(
blink::mojom::PresentationConnectionMessage::NewMessage(message));
}
// blink::mojom::PresentationConnection implementation
MOCK_METHOD(void,
OnMessage,
(blink::mojom::PresentationConnectionMessagePtr message));
void DidChangeState(
blink::mojom::PresentationConnectionState state) override {}
void DidClose(
blink::mojom::PresentationConnectionCloseReason reason) override {}
mojo::PendingReceiver<blink::mojom::PresentationConnection>
MakeConnectionRequest() {
return receiver_connection_remote_.BindNewPipeAndPassReceiver();
}
mojo::PendingRemote<blink::mojom::PresentationConnection> Bind() {
mojo::PendingRemote<blink::mojom::PresentationConnection> connection;
receiver_connection_receiver_.Bind(
connection.InitWithNewPipeAndPassReceiver());
return connection;
}
private:
mojo::Receiver<blink::mojom::PresentationConnection>
receiver_connection_receiver_{this};
mojo::Remote<blink::mojom::PresentationConnection>
receiver_connection_remote_;
};
// This class is used to wait for Terminate to finish before destroying a
// PresentationReceiverWindowController. It destroys it as soon as its
// termination callback is called (OnTerminate). This is in contrast to just
// using a RunLoop with a QuitClosure callback in each test. The latter allows
// extra events in the RunLoop to be handled before actually destroying the
// PresentationReceiverWindowController, which doesn't test whether it's
// actually safe to destroy it in the termination callback itself.
class ReceiverWindowDestroyer {
public:
ReceiverWindowDestroyer() = default;
~ReceiverWindowDestroyer() = default;
void AwaitTerminate(
std::unique_ptr<PresentationReceiverWindowController> receiver_window) {
receiver_window_ = std::move(receiver_window);
receiver_window_->Terminate();
run_loop_.Run();
}
void OnTerminate() {
receiver_window_.reset();
run_loop_.Quit();
}
private:
base::RunLoop run_loop_;
std::unique_ptr<PresentationReceiverWindowController> receiver_window_;
};
} // namespace
class PresentationReceiverWindowControllerBrowserTest
: public InProcessBrowserTest {
protected:
void CloseWindow(PresentationReceiverWindowController* receiver_window) {
receiver_window->CloseWindowForTest();
}
bool IsWindowFullscreen(
const PresentationReceiverWindowController& receiver_window) {
return receiver_window.IsWindowFullscreenForTest();
}
bool IsWindowActive(
const PresentationReceiverWindowController& receiver_window) {
return receiver_window.IsWindowActiveForTest();
}
gfx::Rect GetWindowBounds(
const PresentationReceiverWindowController& receiver_window) {
return receiver_window.GetWindowBoundsForTest();
}
};
IN_PROC_BROWSER_TEST_F(PresentationReceiverWindowControllerBrowserTest,
CreatesWindow) {
ReceiverWindowDestroyer destroyer;
auto receiver_window =
PresentationReceiverWindowController::CreateFromOriginalProfile(
browser()->profile(), gfx::Rect(100, 100),
base::BindOnce(&ReceiverWindowDestroyer::OnTerminate,
base::Unretained(&destroyer)),
GetNoopTitleChangeCallback());
receiver_window->Start(kPresentationId, GURL("about:blank"));
EXPECT_TRUE(base::test::RunUntil(
[&]() { return IsWindowFullscreen(*receiver_window); }));
destroyer.AwaitTerminate(std::move(receiver_window));
}
IN_PROC_BROWSER_TEST_F(PresentationReceiverWindowControllerBrowserTest,
MANUAL_CreatesWindowOnGivenDisplay) {
// Pick specific display.
auto* screen = display::Screen::GetScreen();
const auto& displays = screen->GetAllDisplays();
for (const auto& display : displays) {
DVLOG(0) << display.ToString();
}
// Choose a non-default display to which to move the receiver window.
ASSERT_LE(2ul, displays.size());
const auto default_display =
screen->GetDisplayNearestWindow(browser()->window()->GetNativeWindow());
display::Display target_display;
ASSERT_FALSE(target_display.is_valid());
for (const auto& display : displays) {
if (display.id() != default_display.id()) {
target_display = display;
break;
}
}
ASSERT_TRUE(target_display.is_valid());
ReceiverWindowDestroyer destroyer;
auto receiver_window =
PresentationReceiverWindowController::CreateFromOriginalProfile(
browser()->profile(), target_display.bounds(),
base::BindOnce(&ReceiverWindowDestroyer::OnTerminate,
base::Unretained(&destroyer)),
GetNoopTitleChangeCallback());
receiver_window->Start(kPresentationId, GURL("about:blank"));
ASSERT_TRUE(content::WaitForLoadStop(receiver_window->web_contents()));
// Check the window is on the correct display.
const auto display =
screen->GetDisplayMatching(GetWindowBounds(*receiver_window));
EXPECT_EQ(display.id(), target_display.id());
// The inactive test won't work on single-display systems because fullscreen
// forces it to have focus, so it must be part of the manual test with 2+
// displays.
EXPECT_FALSE(IsWindowActive(*receiver_window));
destroyer.AwaitTerminate(std::move(receiver_window));
}
// Flaky. See https://crbug.com/880045.
IN_PROC_BROWSER_TEST_F(PresentationReceiverWindowControllerBrowserTest,
DISABLED_NavigationClosesWindow) {
// Start receiver window.
auto file_path =
GetResourceFile(FILE_PATH_LITERAL("presentation_receiver.html"));
ASSERT_FALSE(file_path.empty());
const GURL presentation_url = net::FilePathToFileURL(file_path);
ReceiverWindowDestroyer destroyer;
auto receiver_window =
PresentationReceiverWindowController::CreateFromOriginalProfile(
browser()->profile(), gfx::Rect(100, 100),
base::BindOnce(&ReceiverWindowDestroyer::OnTerminate,
base::Unretained(&destroyer)),
GetNoopTitleChangeCallback());
receiver_window->Start(kPresentationId, presentation_url);
ASSERT_TRUE(content::WaitForLoadStop(receiver_window->web_contents()));
content::WebContentsDestroyedWatcher destroyed_watcher(
receiver_window->web_contents());
ASSERT_TRUE(content::ExecJs(receiver_window->web_contents(),
"window.location = 'about:blank'"));
destroyed_watcher.Wait();
destroyer.AwaitTerminate(std::move(receiver_window));
}
// Flaky. See https://crbug.com/840136.
IN_PROC_BROWSER_TEST_F(PresentationReceiverWindowControllerBrowserTest,
DISABLED_PresentationApiCommunication) {
// Start receiver window.
auto file_path =
GetResourceFile(FILE_PATH_LITERAL("presentation_receiver.html"));
ASSERT_FALSE(file_path.empty());
const GURL presentation_url = net::FilePathToFileURL(file_path);
ReceiverWindowDestroyer destroyer;
auto receiver_window =
PresentationReceiverWindowController::CreateFromOriginalProfile(
browser()->profile(), gfx::Rect(100, 100),
base::BindOnce(&ReceiverWindowDestroyer::OnTerminate,
base::Unretained(&destroyer)),
GetNoopTitleChangeCallback());
receiver_window->Start(kPresentationId, presentation_url);
// Register controller with LocalPresentationManager using test-local
// implementation of blink::mojom::PresentationConnection.
FakeControllerConnection controller_connection;
auto controller_ptr = controller_connection.Bind();
media_router::LocalPresentationManagerFactory::GetOrCreateForBrowserContext(
browser()->profile())
->RegisterLocalPresentationController(
blink::mojom::PresentationInfo(presentation_url, kPresentationId),
content::GlobalRenderFrameHostId(0, 0), std::move(controller_ptr),
controller_connection.MakeConnectionRequest(),
media_router::MediaRoute("route",
media_router::MediaSource(presentation_url),
"sink", "desc", true));
base::RunLoop connection_loop;
EXPECT_CALL(controller_connection, OnMessage(_)).WillOnce([&](auto response) {
ASSERT_TRUE(response->is_message());
EXPECT_EQ("ready", response->get_message());
connection_loop.Quit();
});
connection_loop.Run();
// Test ping-pong message.
const std::string message("turtles");
base::RunLoop run_loop;
EXPECT_CALL(controller_connection, OnMessage(_)).WillOnce([&](auto response) {
ASSERT_TRUE(response->is_message());
EXPECT_EQ("Pong: " + message, response->get_message());
run_loop.Quit();
});
controller_connection.SendTextMessage(message);
run_loop.Run();
destroyer.AwaitTerminate(std::move(receiver_window));
}
IN_PROC_BROWSER_TEST_F(PresentationReceiverWindowControllerBrowserTest,
WindowClosingTerminatesPresentation) {
// Start receiver window.
ReceiverWindowDestroyer destroyer;
auto receiver_window =
PresentationReceiverWindowController::CreateFromOriginalProfile(
browser()->profile(), gfx::Rect(100, 100),
base::BindOnce(&ReceiverWindowDestroyer::OnTerminate,
base::Unretained(&destroyer)),
GetNoopTitleChangeCallback());
receiver_window->Start(kPresentationId, GURL("about:blank"));
ASSERT_TRUE(content::WaitForLoadStop(receiver_window->web_contents()));
content::WebContentsDestroyedWatcher destroyed_watcher(
receiver_window->web_contents());
CloseWindow(receiver_window.get());
destroyed_watcher.Wait();
destroyer.AwaitTerminate(std::move(receiver_window));
}
|