blob: 21c8fe74db4c682356fe95aeff4111428744b8ff [file] [log] [blame]
Saurabh Nijhara8015be522020-11-18 16:08:391// Copyright 2020 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 "base/values.h"
6#include "chrome/browser/policy/policy_test_utils.h"
7#include "chromeos/audio/cras_audio_handler.h"
8#include "components/policy/core/common/policy_map.h"
9#include "components/policy/core/common/policy_types.h"
10#include "components/policy/policy_constants.h"
11#include "content/public/test/browser_test.h"
12
13namespace policy {
14
15namespace {
16
17class TestAudioObserver : public chromeos::CrasAudioHandler::AudioObserver {
18 public:
19 TestAudioObserver() : output_mute_changed_count_(0) {}
20 ~TestAudioObserver() override {}
21
22 TestAudioObserver(const TestAudioObserver&) = delete;
23 TestAudioObserver& operator=(const TestAudioObserver&) = delete;
24
25 int output_mute_changed_count() const { return output_mute_changed_count_; }
26
27 protected:
28 // chromeos::CrasAudioHandler::AudioObserver overrides.
29 void OnOutputMuteChanged(bool /* mute_on */) override {
30 ++output_mute_changed_count_;
31 }
32
33 private:
34 int output_mute_changed_count_;
35};
36
37} // namespace
38
39IN_PROC_BROWSER_TEST_F(PolicyTest, DisableAudioOutput) {
40 // Set up the mock observer.
41 chromeos::CrasAudioHandler* audio_handler = chromeos::CrasAudioHandler::Get();
42 std::unique_ptr<TestAudioObserver> test_observer(new TestAudioObserver);
43 audio_handler->AddAudioObserver(test_observer.get());
44
45 bool prior_state = audio_handler->IsOutputMuted();
46 // Make sure the audio is not muted and then toggle the policy and observe
47 // if the output mute changed event is fired.
48 audio_handler->SetOutputMute(false);
49 EXPECT_FALSE(audio_handler->IsOutputMuted());
50 EXPECT_EQ(1, test_observer->output_mute_changed_count());
51 PolicyMap policies;
52 policies.Set(key::kAudioOutputAllowed, POLICY_LEVEL_MANDATORY,
53 POLICY_SCOPE_USER, POLICY_SOURCE_CLOUD, base::Value(false),
54 nullptr);
55 UpdateProviderPolicy(policies);
56 EXPECT_TRUE(audio_handler->IsOutputMuted());
57 // This should not change the state now and should not trigger output mute
58 // changed event.
59 audio_handler->SetOutputMute(false);
60 EXPECT_TRUE(audio_handler->IsOutputMuted());
61 EXPECT_EQ(1, test_observer->output_mute_changed_count());
62
63 // Toggle back and observe if the output mute changed event is fired.
64 policies.Set(key::kAudioOutputAllowed, POLICY_LEVEL_MANDATORY,
65 POLICY_SCOPE_USER, POLICY_SOURCE_CLOUD, base::Value(true),
66 nullptr);
67 UpdateProviderPolicy(policies);
68 EXPECT_FALSE(audio_handler->IsOutputMuted());
69 EXPECT_EQ(1, test_observer->output_mute_changed_count());
70 audio_handler->SetOutputMute(true);
71 EXPECT_TRUE(audio_handler->IsOutputMuted());
72 EXPECT_EQ(2, test_observer->output_mute_changed_count());
73 // Revert the prior state.
74 audio_handler->SetOutputMute(prior_state);
75 audio_handler->RemoveAudioObserver(test_observer.get());
76}
77
78} // namespace policy