Commit 8de851ba authored by Erik Chen's avatar Erik Chen Committed by Commit Bot

Add tests for ash-chrome's implementation of screen manager crosapi.

The screen manager crosapi is used to take snapshots of the screen and
window. This CL adds an ash-chrome only browser-test which confirms that
the implementation of the mojo interface calls returns appropriately
sized snapshots.

Bug: 1094460
Change-Id: I6f65d0ba01b60b82c35a5dd896cf3e2f132e3b56
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2310890
Commit-Queue: Erik Chen <erikchen@chromium.org>
Reviewed-by: default avatarJames Cook <jamescook@chromium.org>
Auto-Submit: Erik Chen <erikchen@chromium.org>
Cr-Commit-Position: refs/heads/master@{#790656}
parent af1b0f93
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <memory>
#include "base/memory/scoped_refptr.h"
#include "base/task/task_traits.h"
#include "base/task/thread_pool.h"
#include "chrome/browser/chromeos/crosapi/screen_manager_crosapi.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_window.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chromeos/crosapi/cpp/window_snapshot.h"
#include "chromeos/crosapi/mojom/screen_manager.mojom.h"
#include "content/public/test/browser_test.h"
#include "mojo/public/cpp/bindings/remote.h"
#include "mojo/public/cpp/bindings/sync_call_restrictions.h"
#include "ui/aura/window.h"
namespace {
// This class tests that the ash-chrome implementation of the screen manager
// crosapi works properly.
class ScreenManagerCrosapiBrowserTest : public InProcessBrowserTest {
protected:
using SMRemote = mojo::Remote<crosapi::mojom::ScreenManager>;
using SMPendingRemote = mojo::PendingRemote<crosapi::mojom::ScreenManager>;
using SMPendingReceiver =
mojo::PendingReceiver<crosapi::mojom::ScreenManager>;
ScreenManagerCrosapiBrowserTest() = default;
ScreenManagerCrosapiBrowserTest(const ScreenManagerCrosapiBrowserTest&) =
delete;
ScreenManagerCrosapiBrowserTest& operator=(
const ScreenManagerCrosapiBrowserTest&) = delete;
~ScreenManagerCrosapiBrowserTest() override {
background_sequence_->DeleteSoon(FROM_HERE,
std::move(screen_manager_remote_));
}
void SetUpOnMainThread() override {
// The implementation of screen manager is affine to this sequence.
screen_manager_ = std::make_unique<ScreenManagerCrosapi>();
SMPendingRemote pending_remote;
SMPendingReceiver pending_receiver =
pending_remote.InitWithNewPipeAndPassReceiver();
// Bind the implementation of ScreenManager to this sequence.
screen_manager_->BindReceiver(std::move(pending_receiver));
// Bind the remote to a background sequence. This is necessary because the
// screen manager API is synchronous and blocks the calling sequence.
background_sequence_ = base::ThreadPool::CreateSequencedTaskRunner(
{base::TaskPriority::USER_BLOCKING, base::MayBlock(),
base::TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN});
// We construct the remote on this sequence for simplicity. All subsequent
// invocations, including destruction, are from the background sequence.
screen_manager_remote_ = std::make_unique<SMRemote>();
auto bind_background = base::BindOnce(
[](SMRemote* remote, SMPendingRemote pending_remote) {
remote->Bind(std::move(pending_remote));
},
screen_manager_remote_.get(), std::move(pending_remote));
background_sequence_->PostTask(FROM_HERE, std::move(bind_background));
}
// Affine to main sequence.
std::unique_ptr<ScreenManagerCrosapi> screen_manager_;
// A sequence that is allowed to block.
scoped_refptr<base::SequencedTaskRunner> background_sequence_;
// Affine to background sequence.
std::unique_ptr<SMRemote> screen_manager_remote_;
};
// Tests that taking a screen snapshot works.
IN_PROC_BROWSER_TEST_F(ScreenManagerCrosapiBrowserTest, TakeScreenSnapshot) {
base::RunLoop run_loop;
crosapi::WindowSnapshot snapshot;
// Take a snapshot on a background sequence. The call is blocking, so when it
// finishes, we can also unblock the main thread.
auto take_snapshot_background = base::BindOnce(
[](SMRemote* remote, crosapi::WindowSnapshot* snapshot) {
mojo::ScopedAllowSyncCallForTesting allow_sync;
(*remote)->TakeScreenSnapshot(snapshot);
},
screen_manager_remote_.get(), &snapshot);
background_sequence_->PostTaskAndReply(
FROM_HERE, std::move(take_snapshot_background), run_loop.QuitClosure());
run_loop.Run();
// Check that the screenshot has the right dimensions.
aura::Window* primary_window =
browser()->window()->GetNativeWindow()->GetRootWindow();
EXPECT_EQ(int{snapshot.width}, primary_window->bounds().width());
EXPECT_EQ(int{snapshot.height}, primary_window->bounds().height());
}
IN_PROC_BROWSER_TEST_F(ScreenManagerCrosapiBrowserTest, TakeWindowSnapshot) {
base::RunLoop run_loop;
bool success;
crosapi::WindowSnapshot snapshot;
// Take a snapshot on a background sequence. The call is blocking, so when it
// finishes, we can also unblock the main thread.
auto take_snapshot_background = base::BindOnce(
[](SMRemote* remote, bool* success, crosapi::WindowSnapshot* snapshot) {
mojo::ScopedAllowSyncCallForTesting allow_sync;
std::vector<crosapi::mojom::WindowDetailsPtr> windows;
(*remote)->ListWindows(&windows);
// There should be exactly 1 window.
ASSERT_EQ(1u, windows.size());
(*remote)->TakeWindowSnapshot(windows[0]->id, success, snapshot);
},
screen_manager_remote_.get(), &success, &snapshot);
background_sequence_->PostTaskAndReply(
FROM_HERE, std::move(take_snapshot_background), run_loop.QuitClosure());
run_loop.Run();
// Check that the IPC succeeded.
ASSERT_TRUE(success);
// Check that the screenshot has the right dimensions.
aura::Window* window = browser()->window()->GetNativeWindow();
EXPECT_EQ(int{snapshot.width}, window->bounds().width());
EXPECT_EQ(int{snapshot.height}, window->bounds().height());
}
} // namespace
...@@ -2128,6 +2128,14 @@ if (!is_android) { ...@@ -2128,6 +2128,14 @@ if (!is_android) {
[ "//chrome/browser/internal/resources/signin/test_accounts.json" ] [ "//chrome/browser/internal/resources/signin/test_accounts.json" ]
} }
# Browser tests for functionality that is only intended to be present in
# ash-chrome, not lacros-chrome.
if (is_chromeos && !chromeos_is_browser_only) {
sources += [
"../browser/chromeos/crosapi/screen_manager_crosapi_browsertest.cc",
]
}
if (is_chromeos) { if (is_chromeos) {
sources += [ sources += [
"../browser/apps/platform_apps/app_window_interactive_uitest_base.cc", "../browser/apps/platform_apps/app_window_interactive_uitest_base.cc",
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment