1 // 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 "ash/system/unified/camera_mic_tray_item_view.h"
6 
7 #include <memory>
8 #include <utility>
9 
10 #include "ash/public/cpp/media_controller.h"
11 #include "ash/test/ash_test_base.h"
12 #include "base/test/scoped_feature_list.h"
13 #include "chromeos/constants/chromeos_features.h"
14 #include "testing/gtest/include/gtest/gtest.h"
15 
16 namespace ash {
17 
18 namespace {
19 using Type = CameraMicTrayItemView::Type;
20 
GetRelevantCaptureState(Type type)21 MediaCaptureState GetRelevantCaptureState(Type type) {
22   switch (type) {
23     case Type::kCamera:
24       return MediaCaptureState::kVideo;
25     case Type::kMic:
26       return MediaCaptureState::kAudio;
27   }
28 }
29 
GetIrrelevantCaptureState(Type type)30 MediaCaptureState GetIrrelevantCaptureState(Type type) {
31   switch (type) {
32     case Type::kCamera:
33       return MediaCaptureState::kAudio;
34     case Type::kMic:
35       return MediaCaptureState::kVideo;
36   }
37 }
38 
39 }  // namespace
40 
41 class CameraMicTrayItemViewTest : public AshTestBase,
42                                   public testing::WithParamInterface<Type> {
43  public:
44   // AshTestBase:
SetUp()45   void SetUp() override {
46     scoped_feature_list_.InitAndEnableFeature(
47         chromeos::features::kVmCameraMicIndicatorsAndNotifications);
48     AshTestBase::SetUp();
49 
50     camera_mic_tray_item_view_ =
51         std::make_unique<CameraMicTrayItemView>(GetPrimaryShelf(), GetParam());
52 
53     // Relogin to make sure `OnActiveUserSessionChanged` is triggered.
54     ClearLogin();
55     SimulateUserLogin("user@test.com");
56   }
57 
TearDown()58   void TearDown() override {
59     camera_mic_tray_item_view_.reset();
60     AshTestBase::TearDown();
61   }
62 
63  protected:
64   base::test::ScopedFeatureList scoped_feature_list_;
65   std::unique_ptr<CameraMicTrayItemView> camera_mic_tray_item_view_;
66 };
67 
TEST_P(CameraMicTrayItemViewTest,OnVmMediaCaptureChanged)68 TEST_P(CameraMicTrayItemViewTest, OnVmMediaCaptureChanged) {
69   Type type = GetParam();
70   MediaCaptureState relevant = GetRelevantCaptureState(type);
71   MediaCaptureState irrelevant = GetIrrelevantCaptureState(type);
72 
73   EXPECT_FALSE(camera_mic_tray_item_view_->GetVisible());
74 
75   camera_mic_tray_item_view_->OnVmMediaCaptureChanged(relevant);
76   EXPECT_TRUE(camera_mic_tray_item_view_->GetVisible());
77 
78   camera_mic_tray_item_view_->OnVmMediaCaptureChanged(irrelevant);
79   EXPECT_FALSE(camera_mic_tray_item_view_->GetVisible());
80 
81   camera_mic_tray_item_view_->OnVmMediaCaptureChanged(
82       static_cast<MediaCaptureState>(static_cast<int>(relevant) |
83                                      static_cast<int>(irrelevant)));
84   EXPECT_TRUE(camera_mic_tray_item_view_->GetVisible());
85 
86   camera_mic_tray_item_view_->OnVmMediaCaptureChanged(MediaCaptureState::kNone);
87   EXPECT_FALSE(camera_mic_tray_item_view_->GetVisible());
88 }
89 
TEST_P(CameraMicTrayItemViewTest,HideForNonPrimaryUser)90 TEST_P(CameraMicTrayItemViewTest, HideForNonPrimaryUser) {
91   camera_mic_tray_item_view_->OnVmMediaCaptureChanged(
92       GetRelevantCaptureState(GetParam()));
93   EXPECT_TRUE(camera_mic_tray_item_view_->GetVisible());
94 
95   SimulateUserLogin("user2@test.com");
96   EXPECT_FALSE(camera_mic_tray_item_view_->GetVisible());
97 }
98 
99 INSTANTIATE_TEST_SUITE_P(All,
100                          CameraMicTrayItemViewTest,
101                          testing::Values(Type::kCamera, Type::kMic));
102 
103 }  // namespace ash
104