File: permission_dashboard_unittest.cc

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,811; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (303 lines) | stat: -rw-r--r-- 11,905 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/test/scoped_feature_list.h"
#include "base/time/time.h"
#include "base/timer/timer.h"
#include "build/build_config.h"
#include "chrome/browser/ui/views/frame/browser_view.h"
#include "chrome/browser/ui/views/frame/test_with_browser_view.h"
#include "chrome/browser/ui/views/location_bar/location_bar_view.h"
#include "chrome/browser/ui/views/permissions/chip/chip_controller.h"
#include "chrome/browser/ui/views/permissions/chip/permission_chip_view.h"
#include "chrome/browser/ui/views/permissions/chip/permission_dashboard_controller.h"
#include "chrome/browser/ui/views/permissions/chip/permission_dashboard_view.h"
#include "chrome/browser/ui/views/permissions/permission_prompt_chip.h"
#include "chrome/test/base/testing_browser_process.h"
#include "components/content_settings/browser/page_specific_content_settings.h"
#include "components/content_settings/core/common/content_settings_types.h"
#include "components/content_settings/core/common/features.h"
#include "content/public/browser/web_contents.h"
#include "ui/gfx/animation/animation_test_api.h"
#include "ui/views/test/ax_event_counter.h"
#include "ui/views/test/button_test_api.h"

class AnimationObserver : public PermissionChipView::Observer {
 public:
  explicit AnimationObserver(base::OnceClosure quit_closure)
      : animation_complete_callback_(std::move(quit_closure)) {}

  void OnAnimationEnded() { std::move(animation_complete_callback_).Run(); }

  // PermissionChipView::Observer
  void OnChipVisibilityChanged(bool is_visible) override {}
  void OnExpandAnimationEnded() override { OnAnimationEnded(); }
  void OnCollapseAnimationEnded() override { OnAnimationEnded(); }

 private:
  base::OnceClosure animation_complete_callback_;
};

class PermissionDashboardUnitTest : public TestWithBrowserView {
 public:
  PermissionDashboardUnitTest()
      : TestWithBrowserView(base::test::TaskEnvironment::TimeSource::MOCK_TIME,
                            base::test::TaskEnvironment::MainThreadType::UI),
        animation_mode_reset_(gfx::AnimationTestApi::SetRichAnimationRenderMode(
            gfx::Animation::RichAnimationRenderMode::FORCE_ENABLED)) {
    feature_list_.InitAndEnableFeature(
        content_settings::features::kLeftHandSideActivityIndicators);
  }

  PermissionDashboardUnitTest(const PermissionDashboardUnitTest&) = delete;
  PermissionDashboardUnitTest& operator=(const PermissionDashboardUnitTest&) =
      delete;

  void SetUp() override {
    TestingBrowserProcess::GetGlobal()->CreateGlobalFeaturesForTesting();
    TestWithBrowserView::SetUp();

    AddTab(browser(), GURL("http://a.com"));
  }

  void WaitForAnimationCompletion() {
    PermissionDashboardView* dashboard_view =
        location_bar_view()
            ->permission_dashboard_controller()
            ->permission_dashboard_view();
    base::RunLoop run_loop;
    std::unique_ptr<AnimationObserver> observer =
        std::make_unique<AnimationObserver>(run_loop.QuitWhenIdleClosure());

    dashboard_view->GetIndicatorChip()->AddObserver(observer.get());

    run_loop.Run();

    dashboard_view->GetIndicatorChip()->RemoveObserver(observer.get());
  }

  content::WebContents* web_contents() {
    return browser()->tab_strip_model()->GetWebContentsAt(0);
  }

  LocationBarView* location_bar_view() {
    BrowserView* browser_view =
        BrowserView::GetBrowserViewForBrowser(browser());
    return browser_view->GetLocationBarView();
  }

  PermissionDashboardController* dashboard_controller() {
    return location_bar_view()->permission_dashboard_controller();
  }

 private:
  base::test::ScopedFeatureList feature_list_;
  // Some of these tests rely on animation being enabled. This forces
  // animation on even if it's turned off in the OS.
  gfx::AnimationTestApi::RenderModeResetter animation_mode_reset_;
};

// TODO(crbug.com/41492809): Test LHS indicators animation on macOS as well.
#if !BUILDFLAG(IS_MAC)
// This test verifies:
// 1. Camera activity indicator chip is shown in verbose form after
// `PageSpecificContentSettings` updates camera usage.
// 2. The chip's verbose state collapses after 4 seconds.
// 3. The chip disappears after `PageSpecificContentSettings` resets camera
// usage.
TEST_F(PermissionDashboardUnitTest, DisplayLHSIndicatorForCamera) {
  PermissionDashboardController* dashboard_controller =
      location_bar_view()->permission_dashboard_controller();

  PermissionChipView* indicator_chip =
      dashboard_controller->permission_dashboard_view()->GetIndicatorChip();

  content_settings::PageSpecificContentSettings* pscs =
      content_settings::PageSpecificContentSettings::GetForFrame(
          web_contents()->GetPrimaryMainFrame());
  ASSERT_TRUE(pscs);

  pscs->OnMediaStreamPermissionSet(
      GURL("http://a.com"),
      {content_settings::PageSpecificContentSettings::kCameraAccessed});

  // Wait for the expand animation to finish.
  WaitForAnimationCompletion();

  EXPECT_TRUE(indicator_chip->GetVisible());
  EXPECT_TRUE(dashboard_controller->is_verbose());
  EXPECT_TRUE(
      pscs->IsIndicatorVisible(ContentSettingsType::MEDIASTREAM_CAMERA));

  EXPECT_TRUE(
      dashboard_controller->get_collapse_timer_for_testing().IsRunning());
  EXPECT_FALSE(indicator_chip->is_animating());
  // Wait longer than 4 seconds for collapse timer to fire and the collapse
  // animation to finish.
  task_environment()->AdvanceClock(base::Milliseconds(4100));
  base::RunLoop().RunUntilIdle();
  EXPECT_TRUE(indicator_chip->is_animating());

  WaitForAnimationCompletion();

  EXPECT_FALSE(indicator_chip->is_animating());

  EXPECT_TRUE(indicator_chip->GetVisible());
  EXPECT_FALSE(
      dashboard_controller->get_collapse_timer_for_testing().IsRunning());

  EXPECT_FALSE(dashboard_controller->is_verbose());

  pscs->OnCapturingStateChanged(ContentSettingsType::MEDIASTREAM_CAMERA, false);
  base::RunLoop().RunUntilIdle();

  ASSERT_TRUE(pscs->get_indicators_hiding_delay_timer_for_testing().contains(
      ContentSettingsType::MEDIASTREAM_CAMERA));
  EXPECT_TRUE(pscs->get_indicators_hiding_delay_timer_for_testing()
                  [ContentSettingsType::MEDIASTREAM_CAMERA]
                      .IsRunning());

  pscs->get_indicators_hiding_delay_timer_for_testing()
      [ContentSettingsType::MEDIASTREAM_CAMERA]
          .FireNow();
  EXPECT_FALSE(indicator_chip->GetVisible());
  EXPECT_FALSE(
      pscs->IsIndicatorVisible(ContentSettingsType::MEDIASTREAM_CAMERA));
}

// This test verifies:
// 1. Camera & Mic activity indicator chip is shown.
// 2. The chip disappears after `PageSpecificContentSettings` resets camera &
// microphone usage.
TEST_F(PermissionDashboardUnitTest, DisplayLHSIndicatorForCameraMic) {
  PermissionDashboardController* dashboard_controller =
      location_bar_view()->permission_dashboard_controller();

  PermissionChipView* indicator_chip =
      dashboard_controller->permission_dashboard_view()->GetIndicatorChip();

  content_settings::PageSpecificContentSettings* pscs =
      content_settings::PageSpecificContentSettings::GetForFrame(
          web_contents()->GetPrimaryMainFrame());
  ASSERT_TRUE(pscs);

  pscs->OnMediaStreamPermissionSet(
      GURL("http://a.com"),
      {content_settings::PageSpecificContentSettings::kCameraAccessed,
       content_settings::PageSpecificContentSettings::kMicrophoneAccessed});

  // Wait for the expand animation to finish.
  WaitForAnimationCompletion();

  EXPECT_TRUE(indicator_chip->GetVisible());
  EXPECT_TRUE(
      dashboard_controller->get_collapse_timer_for_testing().IsRunning());

  EXPECT_TRUE(dashboard_controller->is_verbose());
  EXPECT_TRUE(
      pscs->IsIndicatorVisible(ContentSettingsType::MEDIASTREAM_CAMERA));
  EXPECT_TRUE(pscs->IsIndicatorVisible(ContentSettingsType::MEDIASTREAM_MIC));

  pscs->OnCapturingStateChanged(ContentSettingsType::MEDIASTREAM_CAMERA, false);

  // Because indicator is displayed for both camera and mic, disabling only one
  // does not trigger a delay timer.
  EXPECT_FALSE(pscs->get_indicators_hiding_delay_timer_for_testing().contains(
      ContentSettingsType::MEDIASTREAM_CAMERA));

  pscs->OnCapturingStateChanged(ContentSettingsType::MEDIASTREAM_MIC, false);
  ASSERT_TRUE(pscs->get_indicators_hiding_delay_timer_for_testing().contains(
      ContentSettingsType::MEDIASTREAM_MIC));

  pscs->get_indicators_hiding_delay_timer_for_testing()
      [ContentSettingsType::MEDIASTREAM_MIC]
          .FireNow();

  // Wait for the collapse animation to finish.
  WaitForAnimationCompletion();
  EXPECT_FALSE(indicator_chip->GetVisible());
}

// This test verifies:
// 1. Camera activity indicator chip is shown.
// 2. After the Camera indicator collapsed, Microphone usage will not trigger
// expand animation because there is only one indicator for both camera and mic.
// 3. The chip does not disappears after `PageSpecificContentSettings` resets
// camera.
// 4. The chip disappears after `PageSpecificContentSettings` resets microphone
// usage.
TEST_F(PermissionDashboardUnitTest, DisplayLHSIndicatorForCameraAndThenMic) {
  PermissionDashboardController* dashboard_controller =
      location_bar_view()->permission_dashboard_controller();

  PermissionChipView* indicator_chip =
      dashboard_controller->permission_dashboard_view()->GetIndicatorChip();

  content_settings::PageSpecificContentSettings* pscs =
      content_settings::PageSpecificContentSettings::GetForFrame(
          web_contents()->GetPrimaryMainFrame());
  ASSERT_TRUE(pscs);

  pscs->OnMediaStreamPermissionSet(
      GURL("http://a.com"),
      {content_settings::PageSpecificContentSettings::kCameraAccessed});

  base::RunLoop().RunUntilIdle();
  EXPECT_TRUE(indicator_chip->GetVisible());
  EXPECT_FALSE(
      dashboard_controller->get_collapse_timer_for_testing().IsRunning());

  WaitForAnimationCompletion();

  EXPECT_TRUE(dashboard_controller->is_verbose());
  EXPECT_TRUE(
      pscs->IsIndicatorVisible(ContentSettingsType::MEDIASTREAM_CAMERA));
  EXPECT_FALSE(pscs->IsIndicatorVisible(ContentSettingsType::MEDIASTREAM_MIC));

  // Wait longer than 4 seconds for collapse timer to fire and the collapse
  // animation to finish.
  task_environment()->AdvanceClock(base::Milliseconds(4100));
  base::RunLoop().RunUntilIdle();

  WaitForAnimationCompletion();

  EXPECT_FALSE(dashboard_controller->is_verbose());

  pscs->OnCapturingStateChanged(ContentSettingsType::MEDIASTREAM_MIC, true);

  EXPECT_TRUE(indicator_chip->GetVisible());
  EXPECT_FALSE(indicator_chip->is_animating());
  // The indicator stays collapsed.
  EXPECT_FALSE(dashboard_controller->is_verbose());

  EXPECT_TRUE(pscs->GetMicrophoneCameraState().HasAll(
      {content_settings::PageSpecificContentSettings::kCameraAccessed,
       content_settings::PageSpecificContentSettings::kMicrophoneAccessed}));

  EXPECT_TRUE(
      pscs->IsIndicatorVisible(ContentSettingsType::MEDIASTREAM_CAMERA));
  EXPECT_TRUE(pscs->IsIndicatorVisible(ContentSettingsType::MEDIASTREAM_MIC));

  pscs->OnCapturingStateChanged(ContentSettingsType::MEDIASTREAM_CAMERA, false);

  EXPECT_TRUE(indicator_chip->GetVisible());
  EXPECT_FALSE(indicator_chip->is_animating());
  // The indicator stays collapsed.
  EXPECT_FALSE(dashboard_controller->is_verbose());

  EXPECT_FALSE(
      pscs->IsIndicatorVisible(ContentSettingsType::MEDIASTREAM_CAMERA));
  EXPECT_TRUE(pscs->IsIndicatorVisible(ContentSettingsType::MEDIASTREAM_MIC));

  pscs->OnCapturingStateChanged(ContentSettingsType::MEDIASTREAM_MIC, false);
  ASSERT_TRUE(pscs->get_indicators_hiding_delay_timer_for_testing().contains(
      ContentSettingsType::MEDIASTREAM_MIC));

  pscs->get_indicators_hiding_delay_timer_for_testing()
      [ContentSettingsType::MEDIASTREAM_MIC]
          .FireNow();

  EXPECT_FALSE(indicator_chip->GetVisible());
}
#endif