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 class MockCrasDelegate : public DeviceCommandSetVolumeJob::CrasDelegate { | |
46 public: | |
47 ~MockCrasDelegate() override {} | |
48 | |
49 // DeviceCommandSetVolumeJob::CrasDelegate: | |
50 void SetOutputVolumePercent(int volume) override { g_volume = volume; } | |
51 }; | |
52 | |
53 std::unique_ptr<RemoteCommandJob> CreateSetVolumeJob( | |
54 base::TimeTicks issued_time, | |
55 int volume) { | |
56 std::unique_ptr<RemoteCommandJob> job( | |
57 new DeviceCommandSetVolumeJob(base::MakeUnique<MockCrasDelegate>())); | |
58 auto set_volume_command_proto = GenerateSetVolumeCommandProto( | |
59 base::TimeTicks::Now() - issued_time, volume); | |
60 EXPECT_TRUE(job->Init(base::TimeTicks::Now(), set_volume_command_proto)); | |
61 EXPECT_EQ(kUniqueID, job->unique_id()); | |
62 EXPECT_EQ(RemoteCommandJob::NOT_STARTED, job->status()); | |
63 return job; | |
64 } | |
65 | |
66 } // namespace | |
67 | |
68 class DeviceCommandSetVolumeTest : public ash::test::AshTestBase { | |
69 public: | |
70 void VerifyResults(RemoteCommandJob* job, | |
71 RemoteCommandJob::Status expected_status, | |
72 int expected_volume); | |
73 | |
74 protected: | |
75 DeviceCommandSetVolumeTest(); | |
76 | |
77 // testing::Test | |
78 void SetUp() override; | |
79 | |
80 base::RunLoop run_loop_; | |
81 base::TimeTicks test_start_time_; | |
82 | |
83 private: | |
84 scoped_refptr<base::TestMockTimeTaskRunner> task_runner_; | |
85 | |
86 DISALLOW_COPY_AND_ASSIGN(DeviceCommandSetVolumeTest); | |
87 }; | |
88 | |
89 DeviceCommandSetVolumeTest::DeviceCommandSetVolumeTest() | |
90 : task_runner_(new base::TestMockTimeTaskRunner()) {} | |
91 | |
92 void DeviceCommandSetVolumeTest::SetUp() { | |
93 ash::test::AshTestBase::SetUp(); | |
94 test_start_time_ = base::TimeTicks::Now(); | |
95 } | |
96 | |
97 void DeviceCommandSetVolumeTest::VerifyResults( | |
98 RemoteCommandJob* job, | |
99 RemoteCommandJob::Status expected_status, | |
100 int expected_volume) { | |
101 EXPECT_EQ(expected_status, job->status()); | |
102 if (job->status() == RemoteCommandJob::SUCCEEDED) { | |
103 EXPECT_EQ(expected_volume, g_volume); | |
104 } | |
105 run_loop_.Quit(); | |
106 } | |
107 | |
108 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.
| |
109 const int kVolume = 45; | |
110 auto job = CreateSetVolumeJob(test_start_time_, kVolume); | |
111 bool success = | |
112 job->Run(base::TimeTicks::Now(), | |
113 base::Bind(&DeviceCommandSetVolumeTest::VerifyResults, | |
114 base::Unretained(this), base::Unretained(job.get()), | |
115 RemoteCommandJob::SUCCEEDED, kVolume)); | |
116 EXPECT_TRUE(success); | |
117 run_loop_.Run(); | |
118 } | |
119 | |
120 } // namespace policy | |
OLD | NEW |