File: alert_indicator_button_browsertest.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 (99 lines) | stat: -rw-r--r-- 3,787 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

// 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 "chrome/browser/ui/views/tabs/alert_indicator_button.h"

#include "base/command_line.h"
#include "chrome/browser/content_settings/host_content_settings_map_factory.h"
#include "chrome/browser/media/webrtc/webrtc_browsertest_base.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/views/frame/browser_view.h"
#include "chrome/browser/ui/views/tabs/tab.h"
#include "chrome/browser/ui/views/tabs/tab_strip.h"
#include "components/content_settings/core/browser/host_content_settings_map.h"
#include "components/content_settings/core/common/content_settings.h"
#include "components/content_settings/core/common/content_settings_types.h"
#include "content/public/browser/web_contents.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_utils.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace {

class AlertIndicatorButtonBrowserTest
    : public WebRtcTestBase,
      public testing::WithParamInterface<std::string> {
 public:
  void SetUpOnMainThread() override {
    WebRtcTestBase::SetUpOnMainThread();

    ASSERT_TRUE(embedded_test_server()->Start());
    web_contents_ = OpenTestPageInNewTab("/video_conference_demo.html");

    // Set permissions for camera and microphone.
    SetPermission(web_contents_, ContentSettingsType::MEDIASTREAM_CAMERA,
                  CONTENT_SETTING_ALLOW);
    SetPermission(web_contents_, ContentSettingsType::MEDIASTREAM_MIC,
                  CONTENT_SETTING_ALLOW);

    // Assign the alert_indicator_button_.
    auto* tabstrip = static_cast<BrowserView*>(browser()->window())->tabstrip();
    alert_indicator_button_ =
        tabstrip->tab_at(tabstrip->GetActiveIndex().value())
            ->alert_indicator_button_for_testing();
  }

  content::WebContents* web_contents() { return web_contents_; }

  AlertIndicatorButton* alert_indicator_button() {
    return alert_indicator_button_;
  }

 private:
  // Allows or disallow permissions for `web_contents`.
  void SetPermission(content::WebContents* web_contents,
                     ContentSettingsType type,
                     ContentSetting result) {
    HostContentSettingsMapFactory::GetForProfile(browser()->profile())
        ->SetContentSettingDefaultScope(web_contents->GetURL(), GURL(), type,
                                        result);
  }

  raw_ptr<content::WebContents, DanglingUntriaged> web_contents_ = nullptr;
  raw_ptr<AlertIndicatorButton, DanglingUntriaged> alert_indicator_button_ =
      nullptr;
};

// ToDo(b/323446918): this test fails for startScreenSharing on win10.
INSTANTIATE_TEST_SUITE_P(,  // Empty to simplify gtest output
                         AlertIndicatorButtonBrowserTest,
                         testing::ValuesIn(std::vector<std::string>{
                             "startVideo();", "startAudio();"}));

IN_PROC_BROWSER_TEST_P(AlertIndicatorButtonBrowserTest,
                       CapturingShouldShowAlertIndicatorButton) {
  const std::string action = GetParam();

  // alert_indicator_button_ should be invisible in the beginning.
  EXPECT_FALSE(alert_indicator_button()->GetVisible());

  // Prepare for the visibility change notification.
  base::RunLoop run_loop;
  auto callback_subscription =
      alert_indicator_button()->AddVisibleChangedCallback(
          run_loop.QuitClosure());

  // Start capturing.
  EXPECT_TRUE(content::ExecJs(web_contents(), action));

  // Wait for the visibility change to trigger.
  run_loop.Run();

  // alert_indicator_button_ should be visible now.
  EXPECT_TRUE(alert_indicator_button()->GetVisible());
}

}  // namespace