summaryrefslogtreecommitdiffstats
path: root/browser/base/content/test/webrtc/browser_macos_indicator_hiding.js
blob: 65110ae655cd4c97d35b48830e1cedd48a2c2e4a (plain)
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
/**
 * Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/
 *
 * This test only runs for MacOS 14.0 and above to test the case for
 * Bug 1857254 - MacOS 14 displays two camera in use icons in menu bar
 */

"use strict";

const TEST_ROOT = getRootDirectory(gTestPath).replace(
  "chrome://mochitests/content/",
  "https://example.com/"
);
const TEST_PAGE = TEST_ROOT + "get_user_media.html";

const { MockRegistrar } = ChromeUtils.importESModule(
  "resource://testing-common/MockRegistrar.sys.mjs"
);

var systemStatusBarService = {
  counter: 0,
  _reset() {
    this.counter = 0;
  },
  QueryInterface: ChromeUtils.generateQI(["nsISystemStatusBar"]),

  addItem(element) {
    info(
      `Add item call was fired for element ${element}, updating counter from ${
        this.counter
      } to ${this.counter + 1}`
    );
    this.counter += 1;
  },

  removeItem(element) {
    info(`remove item call was fired for element ${element}`);
  },
};

/**
 * Helper to test if the indicators are shown based on the params
 *
 * @param {Object}
 *   expectedCount (number) - expected number of indicators turned on
 *   cameraState (boolean) - if the camera indicator should be shown
 *   microphoneState (boolean) - if the microphone indicator should be shown
 *   screenShareState (string) - if the screen share indicator should be shown
 *                                  (SCREEN_SHARE or "")
 */
async function indicatorHelper({
  expectedCount,
  cameraState,
  microphoneState,
  shareScreenState,
}) {
  await BrowserTestUtils.withNewTab(TEST_PAGE, async browser => {
    let indicatorPromise = promiseIndicatorWindow();

    await shareDevices(browser, cameraState, microphoneState, shareScreenState);
    await indicatorPromise;
  });

  is(
    systemStatusBarService.counter,
    expectedCount,
    `${expectedCount} indicator(s) should be shown`
  );

  systemStatusBarService._reset();
}

add_task(async function testIconChanges() {
  SpecialPowers.pushPrefEnv({
    set: [["privacy.webrtc.showIndicatorsOnMacos14AndAbove", false]],
  });

  let fakeStatusBarService = MockRegistrar.register(
    "@mozilla.org/widget/systemstatusbar;1",
    systemStatusBarService
  );

  systemStatusBarService._reset();

  registerCleanupFunction(function () {
    MockRegistrar.unregister(fakeStatusBarService);
  });

  info("Created mock system status bar service");

  let prefs = [
    [PREF_PERMISSION_FAKE, true],
    [PREF_AUDIO_LOOPBACK, ""],
    [PREF_VIDEO_LOOPBACK, ""],
    [PREF_FAKE_STREAMS, true],
    [PREF_FOCUS_SOURCE, false],
  ];
  await SpecialPowers.pushPrefEnv({ set: prefs });

  await indicatorHelper({
    expectedCount: 0,
    cameraState: true,
    microphoneState: true,
    shareScreenState: SHARE_SCREEN,
  });
  await indicatorHelper({
    expectedCount: 0,
    cameraState: true,
    microphoneState: false,
    shareScreenState: SHARE_SCREEN,
  });

  // In case we want to be able to see the indicator
  SpecialPowers.pushPrefEnv({
    set: [["privacy.webrtc.showIndicatorsOnMacos14AndAbove", true]],
  });

  await indicatorHelper({
    expectedCount: 3,
    cameraState: true,
    microphoneState: true,
    shareScreenState: SHARE_SCREEN,
  });
  await indicatorHelper({
    expectedCount: 1,
    cameraState: false,
    microphoneState: false,
    shareScreenState: SHARE_SCREEN,
  });
  await indicatorHelper({
    expectedCount: 1,
    cameraState: true,
    microphoneState: false,
    shareScreenState: "",
  });
  await indicatorHelper({
    expectedCount: 1,
    cameraState: false,
    microphoneState: true,
    shareScreenState: "",
  });

  await SpecialPowers.popPrefEnv();
});