OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/chromeos/policy/remote_commands/device_command_set_volu
me_job.h" |
| 6 |
| 7 #include "ash/test/ash_test_base.h" |
| 8 #include "base/bind.h" |
| 9 #include "base/bind_helpers.h" |
| 10 #include "base/json/json_writer.h" |
| 11 #include "base/memory/ptr_util.h" |
| 12 #include "base/run_loop.h" |
| 13 #include "base/test/test_mock_time_task_runner.h" |
| 14 #include "base/values.h" |
| 15 #include "testing/gtest/include/gtest/gtest.h" |
| 16 |
| 17 namespace policy { |
| 18 |
| 19 namespace em = enterprise_management; |
| 20 |
| 21 namespace { |
| 22 |
| 23 const RemoteCommandJob::UniqueIDType kUniqueID = 123456789; |
| 24 |
| 25 // Name of the field in the command payload containing the volume. |
| 26 const char kVolumeFieldName[] = "volume"; |
| 27 |
| 28 int g_volume; |
| 29 |
| 30 em::RemoteCommand GenerateSetVolumeCommandProto(base::TimeDelta age_of_command, |
| 31 int volume) { |
| 32 em::RemoteCommand command_proto; |
| 33 command_proto.set_type( |
| 34 enterprise_management::RemoteCommand_Type_DEVICE_SET_VOLUME); |
| 35 command_proto.set_command_id(kUniqueID); |
| 36 command_proto.set_age_of_command(age_of_command.InMilliseconds()); |
| 37 std::string payload; |
| 38 base::DictionaryValue root_dict; |
| 39 root_dict.SetInteger(kVolumeFieldName, volume); |
| 40 base::JSONWriter::Write(root_dict, &payload); |
| 41 command_proto.set_payload(payload); |
| 42 return command_proto; |
| 43 } |
| 44 |
| 45 void SetVolumeCallback(int volume) { |
| 46 g_volume = volume; |
| 47 } |
| 48 |
| 49 std::unique_ptr<RemoteCommandJob> CreateSetVolumeJob( |
| 50 base::TimeTicks issued_time, |
| 51 int volume) { |
| 52 auto* job_ptr = new DeviceCommandSetVolumeJob(); |
| 53 auto job = base::WrapUnique<RemoteCommandJob>(job_ptr); |
| 54 job_ptr->SetVolumeCallbackForTesting(base::Bind(&SetVolumeCallback)); |
| 55 auto set_volume_command_proto = GenerateSetVolumeCommandProto( |
| 56 base::TimeTicks::Now() - issued_time, volume); |
| 57 EXPECT_TRUE(job->Init(base::TimeTicks::Now(), set_volume_command_proto)); |
| 58 EXPECT_EQ(kUniqueID, job->unique_id()); |
| 59 EXPECT_EQ(RemoteCommandJob::NOT_STARTED, job->status()); |
| 60 return job; |
| 61 } |
| 62 |
| 63 } // namespace |
| 64 |
| 65 class DeviceCommandSetVolumeTest : public ash::test::AshTestBase { |
| 66 public: |
| 67 void VerifyResults(RemoteCommandJob* job, |
| 68 RemoteCommandJob::Status expected_status, |
| 69 int expected_volume); |
| 70 |
| 71 protected: |
| 72 DeviceCommandSetVolumeTest(); |
| 73 |
| 74 // testing::Test |
| 75 void SetUp() override; |
| 76 |
| 77 base::RunLoop run_loop_; |
| 78 base::TimeTicks test_start_time_; |
| 79 |
| 80 private: |
| 81 scoped_refptr<base::TestMockTimeTaskRunner> task_runner_; |
| 82 |
| 83 DISALLOW_COPY_AND_ASSIGN(DeviceCommandSetVolumeTest); |
| 84 }; |
| 85 |
| 86 DeviceCommandSetVolumeTest::DeviceCommandSetVolumeTest() |
| 87 : task_runner_(new base::TestMockTimeTaskRunner()) {} |
| 88 |
| 89 void DeviceCommandSetVolumeTest::SetUp() { |
| 90 ash::test::AshTestBase::SetUp(); |
| 91 test_start_time_ = base::TimeTicks::Now(); |
| 92 } |
| 93 |
| 94 void DeviceCommandSetVolumeTest::VerifyResults( |
| 95 RemoteCommandJob* job, |
| 96 RemoteCommandJob::Status expected_status, |
| 97 int expected_volume) { |
| 98 EXPECT_EQ(expected_status, job->status()); |
| 99 if (job->status() == RemoteCommandJob::SUCCEEDED) { |
| 100 EXPECT_EQ(expected_volume, g_volume); |
| 101 } |
| 102 run_loop_.Quit(); |
| 103 } |
| 104 |
| 105 TEST_F(DeviceCommandSetVolumeTest, Success) { |
| 106 const int kVolume = 45; |
| 107 auto job = CreateSetVolumeJob(test_start_time_, kVolume); |
| 108 bool success = |
| 109 job->Run(base::TimeTicks::Now(), |
| 110 base::Bind(&DeviceCommandSetVolumeTest::VerifyResults, |
| 111 base::Unretained(this), base::Unretained(job.get()), |
| 112 RemoteCommandJob::SUCCEEDED, kVolume)); |
| 113 EXPECT_TRUE(success); |
| 114 run_loop_.Run(); |
| 115 } |
| 116 |
| 117 TEST_F(DeviceCommandSetVolumeTest, VolumeOutOfRange) { |
| 118 const int kVolume = 110; |
| 119 std::unique_ptr<RemoteCommandJob> job(new DeviceCommandSetVolumeJob()); |
| 120 auto set_volume_command_proto = GenerateSetVolumeCommandProto( |
| 121 base::TimeTicks::Now() - test_start_time_, kVolume); |
| 122 EXPECT_FALSE(job->Init(base::TimeTicks::Now(), set_volume_command_proto)); |
| 123 EXPECT_EQ(RemoteCommandJob::INVALID, job->status()); |
| 124 } |
| 125 |
| 126 TEST_F(DeviceCommandSetVolumeTest, CommandTimeout) { |
| 127 const int kVolume = 45; |
| 128 auto delta = base::TimeDelta::FromMinutes(10); |
| 129 auto job = CreateSetVolumeJob(test_start_time_ - delta, kVolume); |
| 130 bool success = |
| 131 job->Run(base::TimeTicks::Now(), |
| 132 base::Bind(&DeviceCommandSetVolumeTest::VerifyResults, |
| 133 base::Unretained(this), base::Unretained(job.get()), |
| 134 RemoteCommandJob::SUCCEEDED, kVolume)); |
| 135 EXPECT_FALSE(success); |
| 136 } |
| 137 |
| 138 } // namespace policy |
OLD | NEW |