Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(719)

Unified Diff: chrome/browser/chromeos/policy/remote_commands/device_command_set_volume_job_unittest.cc

Issue 2733903002: Remote set volume command (Closed)
Patch Set: Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/browser/chromeos/policy/remote_commands/device_command_set_volume_job_unittest.cc
diff --git a/chrome/browser/chromeos/policy/remote_commands/device_command_set_volume_job_unittest.cc b/chrome/browser/chromeos/policy/remote_commands/device_command_set_volume_job_unittest.cc
new file mode 100644
index 0000000000000000000000000000000000000000..7bff0cfbf5b4a4f5a41ad1501a48eee98c559085
--- /dev/null
+++ b/chrome/browser/chromeos/policy/remote_commands/device_command_set_volume_job_unittest.cc
@@ -0,0 +1,120 @@
+// Copyright 2017 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 "chrome/browser/chromeos/policy/remote_commands/device_command_set_volume_job.h"
+
+#include "ash/test/ash_test_base.h"
+#include "base/bind.h"
+#include "base/bind_helpers.h"
+#include "base/json/json_writer.h"
+#include "base/memory/ptr_util.h"
+#include "base/run_loop.h"
+#include "base/test/test_mock_time_task_runner.h"
+#include "base/values.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+namespace policy {
+
+namespace em = enterprise_management;
+
+namespace {
+
+const RemoteCommandJob::UniqueIDType kUniqueID = 123456789;
+
+// Name of the field in the command payload containing the volume.
+const char kVolumeFieldName[] = "volume";
+
+int g_volume;
+
+em::RemoteCommand GenerateSetVolumeCommandProto(base::TimeDelta age_of_command,
+ int volume) {
+ em::RemoteCommand command_proto;
+ command_proto.set_type(
+ enterprise_management::RemoteCommand_Type_DEVICE_SET_VOLUME);
+ command_proto.set_command_id(kUniqueID);
+ command_proto.set_age_of_command(age_of_command.InMilliseconds());
+ std::string payload;
+ base::DictionaryValue root_dict;
+ root_dict.SetInteger(kVolumeFieldName, volume);
+ base::JSONWriter::Write(root_dict, &payload);
+ command_proto.set_payload(payload);
+ return command_proto;
+}
+
+class MockCrasDelegate : public DeviceCommandSetVolumeJob::CrasDelegate {
+ public:
+ ~MockCrasDelegate() override {}
+
+ // DeviceCommandSetVolumeJob::CrasDelegate:
+ void SetOutputVolumePercent(int volume) override { g_volume = volume; }
+};
+
+std::unique_ptr<RemoteCommandJob> CreateSetVolumeJob(
+ base::TimeTicks issued_time,
+ int volume) {
+ std::unique_ptr<RemoteCommandJob> job(
+ new DeviceCommandSetVolumeJob(base::MakeUnique<MockCrasDelegate>()));
+ auto set_volume_command_proto = GenerateSetVolumeCommandProto(
+ base::TimeTicks::Now() - issued_time, volume);
+ EXPECT_TRUE(job->Init(base::TimeTicks::Now(), set_volume_command_proto));
+ EXPECT_EQ(kUniqueID, job->unique_id());
+ EXPECT_EQ(RemoteCommandJob::NOT_STARTED, job->status());
+ return job;
+}
+
+} // namespace
+
+class DeviceCommandSetVolumeTest : public ash::test::AshTestBase {
+ public:
+ void VerifyResults(RemoteCommandJob* job,
+ RemoteCommandJob::Status expected_status,
+ int expected_volume);
+
+ protected:
+ DeviceCommandSetVolumeTest();
+
+ // testing::Test
+ void SetUp() override;
+
+ base::RunLoop run_loop_;
+ base::TimeTicks test_start_time_;
+
+ private:
+ scoped_refptr<base::TestMockTimeTaskRunner> task_runner_;
+
+ DISALLOW_COPY_AND_ASSIGN(DeviceCommandSetVolumeTest);
+};
+
+DeviceCommandSetVolumeTest::DeviceCommandSetVolumeTest()
+ : task_runner_(new base::TestMockTimeTaskRunner()) {}
+
+void DeviceCommandSetVolumeTest::SetUp() {
+ ash::test::AshTestBase::SetUp();
+ test_start_time_ = base::TimeTicks::Now();
+}
+
+void DeviceCommandSetVolumeTest::VerifyResults(
+ RemoteCommandJob* job,
+ RemoteCommandJob::Status expected_status,
+ int expected_volume) {
+ EXPECT_EQ(expected_status, job->status());
+ if (job->status() == RemoteCommandJob::SUCCEEDED) {
+ EXPECT_EQ(expected_volume, g_volume);
+ }
+ run_loop_.Quit();
+}
+
+TEST_F(DeviceCommandSetVolumeTest, Success) {
Andrew T Wilson (Slow) 2017/03/07 11:45:14 Are there other cases we should test for - malform
Ivan Šandrk 2017/03/07 16:15:31 Added a couple more.
+ const int kVolume = 45;
+ auto job = CreateSetVolumeJob(test_start_time_, kVolume);
+ bool success =
+ job->Run(base::TimeTicks::Now(),
+ base::Bind(&DeviceCommandSetVolumeTest::VerifyResults,
+ base::Unretained(this), base::Unretained(job.get()),
+ RemoteCommandJob::SUCCEEDED, kVolume));
+ EXPECT_TRUE(success);
+ run_loop_.Run();
+}
+
+} // namespace policy

Powered by Google App Engine
This is Rietveld 408576698