Skip to content

Commit

Permalink
GH Actions: Try to fix mic authorization dialog not closing.
Browse files Browse the repository at this point in the history
  • Loading branch information
kyleneideck committed May 15, 2022
1 parent cc2516a commit 30ec1c2
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 34 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/build-test-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,13 @@ jobs:
run: |
echo '::group::BGMDriver Tests'
xcodebuild \
-quiet \
-workspace BGM.xcworkspace \
-scheme 'Background Music Device' \
test
echo '::group::BGMXPCHelper Tests'
xcodebuild \
-quiet \
-workspace BGM.xcworkspace \
-scheme 'BGMXPCHelper' \
test
Expand All @@ -89,6 +91,7 @@ jobs:
composedMessage contains[cd] "Background Music" or
composedMessage contains "BGM"' > app.log &
xcodebuild \
-quiet \
-workspace BGM.xcworkspace \
-scheme 'Background Music' \
test
Expand Down
73 changes: 45 additions & 28 deletions BGMApp/BGMApp/BGMOutputVolumeMenuItem.mm
Original file line number Diff line number Diff line change
Expand Up @@ -211,36 +211,53 @@ - (void) outputDeviceDidChange {
// datasource, the device's name is set as this menu item's tooltip. Falls back to a generic name if
// the device returns an error when queried.
- (void) updateLabelAndToolTip {
BOOL didSetLabel = NO;

try {
if (outputDevice.HasDataSourceControl(kScope, kMasterChannel)) {
// The device has datasources, so use the current datasource's name like macOS does.
UInt32 dataSourceID = outputDevice.GetCurrentDataSourceID(kScope, kMasterChannel);

deviceLabel.stringValue =
(__bridge_transfer NSString*)outputDevice.CopyDataSourceNameForID(kScope,
kMasterChannel,
dataSourceID);
didSetLabel = YES; // So we know not to change the text if setting the tooltip fails.

// Set the tooltip of the menu item (the container) rather than the label because menu
// items' tooltips will still appear when a different app is focused and, as far as I
// know, BGMApp should never be the foreground app.
self.toolTip = (__bridge_transfer NSString*)outputDevice.CopyName();
} else {
deviceLabel.stringValue = (__bridge_transfer NSString*)outputDevice.CopyName();
self.toolTip = nil;
}
} catch (const CAException& e) {
BGMLogException(e);

// The device returned an error, so set the label to a generic device name, since we don't
// want to leave it set to the previous device's name.
if (outputDevice.GetObjectID() == kAudioObjectUnknown) {
DebugMsg("BGMOutputVolumeMenuItem::updateLabelAndToolTip: Output device unknown. Using the "
"generic label.");
self.toolTip = nil;
deviceLabel.stringValue = kGenericOutputDeviceName;
} else {
BOOL didSetLabel = NO;

DebugMsg("BGMOutputVolumeMenuItem::updateLabelAndToolTip: Output device: %u",
outputDevice.GetObjectID());

try {
if (outputDevice.HasDataSourceControl(kScope, kMasterChannel)) {
DebugMsg("BGMOutputVolumeMenuItem::updateLabelAndToolTip: Getting data source ID");
// The device has datasources, so use the current datasource's name like macOS does.
UInt32 dataSourceID = outputDevice.GetCurrentDataSourceID(kScope, kMasterChannel);

DebugMsg("BGMOutputVolumeMenuItem::updateLabelAndToolTip: "
"Getting name for data source %u",
dataSourceID);
deviceLabel.stringValue =
(__bridge_transfer NSString*)outputDevice.CopyDataSourceNameForID(
kScope, kMasterChannel, dataSourceID);

// So we know not to change the text if setting the tooltip fails.
didSetLabel = YES;

DebugMsg("BGMOutputVolumeMenuItem::updateLabelAndToolTip: Getting device name");
// Set the tooltip of the menu item (the container) rather than the label because
// menu items' tooltips will still appear when a different app is focused and, as
// far as I know, BGMApp should never be the foreground app.
self.toolTip = (__bridge_transfer NSString*)outputDevice.CopyName();
} else {
DebugMsg("BGMOutputVolumeMenuItem::updateLabelAndToolTip: Getting device name");
deviceLabel.stringValue = (__bridge_transfer NSString*)outputDevice.CopyName();
self.toolTip = nil;
}
} catch (const CAException& e) {
BGMLogException(e);

// The device returned an error, so set the label to a generic device name, since we
// don't want to leave it set to the previous device's name.
self.toolTip = nil;

if (!didSetLabel) {
deviceLabel.stringValue = kGenericOutputDeviceName;
if (!didSetLabel) {
deviceLabel.stringValue = kGenericOutputDeviceName;
}
}
}

Expand Down
19 changes: 13 additions & 6 deletions BGMApp/BGMAppTests/UITests/BGMAppUITests.mm
Original file line number Diff line number Diff line change
Expand Up @@ -85,20 +85,20 @@ - (void) setUp {

[self acceptMicrophoneAuthorizationDialog];

if (![icon waitForExistenceWithTimeout:1.0]) {
if (![icon waitForExistenceWithTimeout:20.0]) {
// The status bar icon/button has this type when using older versions of XCTest, so try
// both. (Actually, it might depend on the macOS or Xcode version. I'm not sure.)
XCUIElement* iconOldType =
[app.menuBars childrenMatchingType:XCUIElementTypeMenuBarItem].element;
if ([iconOldType waitForExistenceWithTimeout:5.0]) {
if ([iconOldType waitForExistenceWithTimeout:20.0]) {
NSLog(@"icon = iconOldType");
icon = iconOldType;
}
}

// Wait for the initial elements.
XCTAssert([app waitForExistenceWithTimeout:10.0]);
XCTAssert([icon waitForExistenceWithTimeout:10.0]);
XCTAssert([app waitForExistenceWithTimeout:20.0]);
XCTAssert([icon waitForExistenceWithTimeout:20.0]);
}

// Clicks the OK button in the "Background Music wants to use the microphone" dialog.
Expand All @@ -108,11 +108,18 @@ - (void) acceptMicrophoneAuthorizationDialog {
NSLog(@"UserNotificationCenter: %@", unc);
XCUIElement* okButton = unc.dialogs.buttons[@"OK"];

XCTAssert([okButton waitForExistenceWithTimeout:10.0]);
XCTAssert([okButton waitForExistenceWithTimeout:20.0]);

// This click is failing on GH Actions. No idea why, so try a sleep.
(void)[XCTWaiter waitForExpectations:@[[XCTestExpectation new]] timeout:2.0];
(void)[XCTWaiter waitForExpectations:@[[XCTestExpectation new]] timeout:5.0];
[okButton click];

int retries = 10;
while (retries > 0 && [okButton waitForExistenceWithTimeout:3.0]) {
NSLog(@"Microphone authorization dialog is still open. Trying to click OK again.");
[okButton click];
retries--;
}
}

- (void) tearDown {
Expand Down

0 comments on commit 30ec1c2

Please sign in to comment.