Skip to content

Commit

Permalink
Add a screenshot remote command browsertest for kiosk.
Browse files Browse the repository at this point in the history
This browser test uses a fake screenshot delegate to skip a screenshot
upload job.

Test: browser_tests --gtest_filter=*ScreenshotWithRemoteCommand
Bug: b:268321006
Change-Id: Ic6735f1731e6248ad60ffa48f4d8298cb3ccae3f
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4240560
Commit-Queue: Sergii Bykov <sbykov@google.com>
Reviewed-by: Polina Bondarenko <pbond@chromium.org>
Reviewed-by: Pavol Marko <pmarko@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1106126}
  • Loading branch information
sbykov-g authored and Chromium LUCI CQ committed Feb 16, 2023
1 parent 44009f3 commit 6d1c831
Show file tree
Hide file tree
Showing 8 changed files with 176 additions and 4 deletions.
2 changes: 2 additions & 0 deletions chrome/browser/ash/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -2455,6 +2455,8 @@ source_set("ash") {
"policy/remote_commands/device_command_wipe_users_job.h",
"policy/remote_commands/device_commands_factory_ash.cc",
"policy/remote_commands/device_commands_factory_ash.h",
"policy/remote_commands/fake_screenshot_delegate.cc",
"policy/remote_commands/fake_screenshot_delegate.h",
"policy/remote_commands/screenshot_delegate.cc",
"policy/remote_commands/screenshot_delegate.h",
"policy/remote_commands/user_command_arc_job.cc",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
#include "chrome/browser/ash/login/test/device_state_mixin.h"
#include "chrome/browser/ash/login/test/login_manager_mixin.h"
#include "chrome/browser/ash/policy/core/browser_policy_connector_ash.h"
#include "chrome/browser/ash/policy/remote_commands/device_command_screenshot_job.h"
#include "chrome/browser/ash/policy/remote_commands/device_command_set_volume_job.h"
#include "chrome/browser/ash/policy/remote_commands/device_commands_factory_ash.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/browser_process_platform_part_ash.h"
#include "chromeos/ash/components/audio/cras_audio_handler.h"
Expand Down Expand Up @@ -43,6 +45,10 @@ constexpr char kKioskRemoteVolumeCommandTag[] =
constexpr char kKioskRemoteRebootCommandTag[] =
"screenplay-95efa645-3d98-4638-9e5c-c0fe6f2c150d";

// workflow: COM_KIOSK_CUJ8_TASK3_WF1
constexpr char kKioskRemoteScreenshotCommandTag[] =
"screenplay-110c74bf-7c94-4e88-8904-95b6bbc7d649";

// Test `CloudPolicyClient` that interacts with `TestingRemoteCommandsServer`.
class TestRemoteCommandsClient : public CloudPolicyClient {
public:
Expand Down Expand Up @@ -170,6 +176,25 @@ class KioskRemoteCommandTest : public KioskBaseTest {
return signed_command;
}

em::SignedData CreateScreenshotRemoteCommand() {
constexpr char kMockUploadUrl[] = "http://example.com/upload";
std::string command_payload;
{
base::Value::Dict root_dict;
root_dict.Set(policy::DeviceCommandScreenshotJob::kUploadUrlFieldName,
kMockUploadUrl);
base::JSONWriter::Write(root_dict, &command_payload);
}
em::SignedData signed_command =
policy::SignedDataBuilder()
.WithCommandId(remote_command_server_->GetNextCommandId())
.WithTargetDeviceId(kDeviceId)
.WithCommandType(em::RemoteCommand_Type_DEVICE_SCREENSHOT)
.WithCommandPayload(command_payload)
.Build();
return signed_command;
}

em::RemoteCommandResult IssueCommandAndGetResponse(em::SignedData command) {
using ServerResponseFuture =
base::test::TestFuture<const em::RemoteCommandResult&>;
Expand Down Expand Up @@ -254,4 +279,28 @@ IN_PROC_BROWSER_TEST_F(KioskRemoteCommandTest, RebootWithRemoteCommand) {
EXPECT_EQ(power_manager::REQUEST_RESTART_REMOTE_ACTION_REBOOT,
observer.Get());
}

IN_PROC_BROWSER_TEST_F(KioskRemoteCommandTest, ScreenshotWithRemoteCommand) {
base::AddFeatureIdTagToTestResult(kKioskRemoteScreenshotCommandTag);

// Launch kiosk app
StartAppLaunchFromLoginScreen(
NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_ONLINE);
WaitForAppLaunchWithOptions(/*check_launch_data=*/true,
/*terminate_app=*/false,
/*keep_app_open=*/true);

// Create a remote command, enqueue from the server, fetch from the client
em::SignedData screenshot_command = CreateScreenshotRemoteCommand();

// skips real image upload
// TODO(b/269432279): Try real upload with local url and EmbeddedTestServer
policy::DeviceCommandsFactoryAsh::set_commands_for_testing(true);

auto response = IssueCommandAndGetResponse(screenshot_command);

// Check that remote cmd passed
EXPECT_EQ(em::RemoteCommandResult_ResultType_RESULT_SUCCESS,
response.result());
}
} // namespace ash
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,6 @@ const char* const kFileTypeHeaderName = "File-Type";
// String constant signalling that the data segment contains screenshots.
const char* const kFileTypeScreenshotFile = "screenshot_file";

// String constant identifying the upload url field in the command payload.
const char* const kUploadUrlFieldName = "fileUploadUrl";

// Helper method to hide the |screen_index| and `std::make_pair` from the
// |DeviceCommandScreenshotJob::Delegate|.
void CallCollectAndUpload(
Expand All @@ -72,6 +69,10 @@ std::string CreatePayload(ResultCode result_code) {

} // namespace

// String constant identifying the upload url field in the command payload.
constexpr char DeviceCommandScreenshotJob::kUploadUrlFieldName[] =
"fileUploadUrl";

DeviceCommandScreenshotJob::DeviceCommandScreenshotJob(
std::unique_ptr<Delegate> screenshot_delegate)
: screenshot_delegate_(std::move(screenshot_delegate)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ using OnScreenshotTakenCallback =
class DeviceCommandScreenshotJob : public RemoteCommandJob,
public UploadJob::Delegate {
public:
static const char kUploadUrlFieldName[];
// When the screenshot command terminates, the result payload that gets sent
// to the server is populated with one of the following result codes. These
// are exposed publicly here since DeviceCommandScreenshotTest uses them.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

#include "chrome/browser/ash/policy/remote_commands/device_commands_factory_ash.h"

#include <memory>

#include "base/notreached.h"
#include "chrome/browser/ash/policy/core/device_cloud_policy_manager_ash.h"
#include "chrome/browser/ash/policy/remote_commands/crd_host_delegate.h"
Expand All @@ -20,6 +22,7 @@
#include "chrome/browser/ash/policy/remote_commands/device_command_set_volume_job.h"
#include "chrome/browser/ash/policy/remote_commands/device_command_start_crd_session_job.h"
#include "chrome/browser/ash/policy/remote_commands/device_command_wipe_users_job.h"
#include "chrome/browser/ash/policy/remote_commands/fake_screenshot_delegate.h"
#include "chrome/browser/ash/policy/remote_commands/screenshot_delegate.h"
#include "components/policy/core/common/remote_commands/remote_command_job.h"
#include "components/policy/proto/device_management_backend.pb.h"
Expand All @@ -28,6 +31,8 @@ namespace policy {

using enterprise_management::RemoteCommand;

bool DeviceCommandsFactoryAsh::device_commands_test_ = false;

DeviceCommandsFactoryAsh::DeviceCommandsFactoryAsh(
DeviceCloudPolicyManagerAsh* policy_manager)
: policy_manager_(policy_manager) {}
Expand All @@ -42,7 +47,7 @@ std::unique_ptr<RemoteCommandJob> DeviceCommandsFactoryAsh::BuildJobForType(
return std::make_unique<DeviceCommandRebootJob>();
case RemoteCommand::DEVICE_SCREENSHOT:
return std::make_unique<DeviceCommandScreenshotJob>(
std::make_unique<ScreenshotDelegate>());
CreateScreenshotDelegate());
case RemoteCommand::DEVICE_SET_VOLUME:
return std::make_unique<DeviceCommandSetVolumeJob>();
case RemoteCommand::DEVICE_START_CRD_SESSION:
Expand Down Expand Up @@ -79,6 +84,11 @@ std::unique_ptr<RemoteCommandJob> DeviceCommandsFactoryAsh::BuildJobForType(
}
}

void DeviceCommandsFactoryAsh::set_commands_for_testing(
bool device_commands_test) {
device_commands_test_ = device_commands_test;
}

DeviceCommandStartCrdSessionJob::Delegate*
DeviceCommandsFactoryAsh::GetCrdHostDelegate() {
if (!crd_host_delegate_) {
Expand All @@ -87,4 +97,12 @@ DeviceCommandsFactoryAsh::GetCrdHostDelegate() {
return crd_host_delegate_.get();
}

std::unique_ptr<DeviceCommandScreenshotJob::Delegate>
DeviceCommandsFactoryAsh::CreateScreenshotDelegate() {
if (device_commands_test_) {
return std::make_unique<FakeScreenshotDelegate>();
}
return std::make_unique<ScreenshotDelegate>();
}

} // namespace policy
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#include <memory>

#include "chrome/browser/ash/policy/remote_commands/device_command_screenshot_job.h"
#include "chrome/browser/ash/policy/remote_commands/device_command_start_crd_session_job.h"
#include "components/policy/core/common/remote_commands/remote_commands_factory.h"

Expand All @@ -29,11 +30,18 @@ class DeviceCommandsFactoryAsh : public RemoteCommandsFactory {
enterprise_management::RemoteCommand_Type type,
RemoteCommandsService* service) override;

static void set_commands_for_testing(bool device_commands_test);

private:
// TODO(b/269432279): Consider removing when test uses a local upload server
static bool device_commands_test_;

DeviceCloudPolicyManagerAsh* policy_manager_;
std::unique_ptr<DeviceCommandStartCrdSessionJob::Delegate> crd_host_delegate_;

DeviceCommandStartCrdSessionJob::Delegate* GetCrdHostDelegate();
std::unique_ptr<DeviceCommandScreenshotJob::Delegate>
CreateScreenshotDelegate();
};

} // namespace policy
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// Copyright 2023 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/ash/policy/remote_commands/fake_screenshot_delegate.h"

#include <string>

#include "base/check.h"
#include "chrome/browser/ash/policy/uploading/upload_job.h"

namespace policy {

namespace {
class FakeUploadJob : public policy::UploadJob {
public:
explicit FakeUploadJob(UploadJob::Delegate* delegate) : delegate_(delegate) {
DCHECK(delegate_);
}

~FakeUploadJob() override = default;
FakeUploadJob(const FakeUploadJob&) = delete;
FakeUploadJob& operator=(const FakeUploadJob&) = delete;

void AddDataSegment(const std::string& name,
const std::string& filename,
const std::map<std::string, std::string>& header_entries,
std::unique_ptr<std::string> data) override {
// ignore data segments
return;
}

void Start() override {
// just call OnSuccess to complete a remote command
delegate_->OnSuccess();
}

private:
UploadJob::Delegate* delegate_;
};
} // namespace

bool FakeScreenshotDelegate::IsScreenshotAllowed() {
return true;
}

void FakeScreenshotDelegate::TakeSnapshot(
gfx::NativeWindow window,
const gfx::Rect& source_rect,
policy::OnScreenshotTakenCallback callback) {
std::move(callback).Run(nullptr);
}

std::unique_ptr<policy::UploadJob> FakeScreenshotDelegate::CreateUploadJob(
const GURL&,
policy::UploadJob::Delegate* delegate) {
return std::make_unique<FakeUploadJob>(delegate);
}

} // namespace policy
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_BROWSER_ASH_POLICY_REMOTE_COMMANDS_FAKE_SCREENSHOT_DELEGATE_H_
#define CHROME_BROWSER_ASH_POLICY_REMOTE_COMMANDS_FAKE_SCREENSHOT_DELEGATE_H_

#include "chrome/browser/ash/policy/remote_commands/device_command_screenshot_job.h"

namespace policy {

class FakeScreenshotDelegate
: public policy::DeviceCommandScreenshotJob::Delegate {
public:
FakeScreenshotDelegate() = default;
~FakeScreenshotDelegate() override = default;
FakeScreenshotDelegate(const FakeScreenshotDelegate&) = delete;
FakeScreenshotDelegate& operator=(const FakeScreenshotDelegate&) = delete;

bool IsScreenshotAllowed() override;

void TakeSnapshot(gfx::NativeWindow window,
const gfx::Rect& source_rect,
policy::OnScreenshotTakenCallback callback) override;

std::unique_ptr<policy::UploadJob> CreateUploadJob(
const GURL&,
policy::UploadJob::Delegate*) override;
};

} // namespace policy

#endif // CHROME_BROWSER_ASH_POLICY_REMOTE_COMMANDS_FAKE_SCREENSHOT_DELEGATE_H_

0 comments on commit 6d1c831

Please sign in to comment.