addUIInterruptionMonitor is not getting called on macOS - swift

I want to test my macOS application. It uses your Macbook's camera, and want to handle this in my UITest. However I cannot get it working. Here is my NOT working code. This code triggers to notification, and I'm presented an alert to allow access to my camera, but the closure is not getting called. Thanks fo any help.
There are many solutions for iOS, but I need it on macOS.
let alertHandler = addUIInterruptionMonitor(withDescription: "Camera Permission Alert") { (alert) -> Bool in
if alert.buttons.matching(identifier: "OK").count > 0 {
alert.buttons["OK"].click()
self.app.click()
return true
} else {
return false
}
}
XCTAssertTrue(startButton.waitForExistence(timeout: 1.0))
startButton.click()
XCTAssertTrue(recordButton.waitForExistence(timeout: 20.0))
recordButton.click()
wait(for: 8)
recordButton.click()
removeUIInterruptionMonitor(alertHandler)
}

I managed to make interruption monitor work on macOS by adding an extra interaction after the interaction that triggers the system dialog (be it camera access or else). So in your example I would add an action after startButton.click() (if that is what triggers the camera access dialog).
Example:
func testCamera() {
let alertHandler = addUIInterruptionMonitor(withDescription: "Camera Permission Alert") { (alert) -> Bool in
if alert.buttons.matching(identifier: "OK").count > 0 {
alert.buttons["OK"].click()
self.app.click()
return true
} else {
return false
}
}
useCameraButton.click()
// try to interact with the app by clicking on the app's window
app.windows.first().click()
// at this point the handler should intercept the system interruption
// and blocks further execution until handler does return
// try to use the camera again
useCameraButton.click()
removeUIInterruptionMonitor(alertHandler)
}
Hint about this behaviour in Apple's documentation:
When an alert or other modal UI is an expected part of the
test workflow, don't write a UI interruption monitor. The test won’t
use the monitor because the modal UI isn’t blocking the test. A UI
test only tries its UI interruption monitors if the elements it needs
to interact with to complete the test are blocked by an interruption
from an unrelated UI.
https://developer.apple.com/documentation/xctest/xctestcase/handling_ui_interruptions

Related

How can I get permanent access to files in macOS?

I am working in app that tells the count of folders and files exist on Desktop in macOS, for making things easier to my issue get solved I made a simple and small project that re create the problem. At first time of lunching the app I am asking the user to select the Desktop, after I am using the selected path for later use case when the app get killed, I do not want ask user each time to select the Desktop each time, it would be very bothering! currently sometimes macOS allows my app to get access to Desktop without showing the permission dialog but sometimes it goes ask each single time, I am trying to make my app authorized for such permission via user and use that permission for later use case in app. For example as you can see Terminal has such permission in the picture:
So here is my code with disabling sandbox:
struct ContentView: View {
#State private var contents: [String]? = nil
var body: some View {
VStack(spacing: 10.0) {
Text("Count of contents of Desktop is:")
Text(contents != nil ? String(contents!.count) : "Unknown").bold()
}
.frame(width: 200.0, height: 200.0)
.padding()
.onAppear {
contents = directoryContentsReader(folderLocation: "/Users/myNameHere/Desktop")
}
}
}
func directoryContentsReader(folderLocation: String) -> [String]? {
do {
return try FileManager.default.contentsOfDirectory(atPath: folderLocation)
}
catch(let error) {
print(error.localizedDescription)
return nil
}
}
And as said before most of the time and randomly I get this below dialog from macOS, I want make this kind permission done one time and for ever when the user use my app, and not bothering user with this dialog any more. How can I get this permission for my app?

How to display Unity WebGL template in full screen after loaded [duplicate]

Task at hand is to add support for fullscreen mode to an WebGL application written in Dart.
canvas.requestFullscreen() works for simple test cases, but fails on the full app.
Please point out the way to tell what is preventing the browser from switching to fullscreen.
The code is:
void trapFullscreenError() {
document.onFullscreenError.listen((e) {
log("fullscreenerror: $e");
});
}
void toggleFullscreen(CanvasElement c) {
log(
"fullscreenSupport=${document.fullscreenEnabled} fullscreenElement=${document.fullscreenElement}"
);
if (document.fullscreenElement != null) {
log("exiting fullscreen");
document.exitFullscreen();
} else {
log("requesting fullscreen");
c.requestFullscreen();
}
}
In Chrome that code results in:
fullscreenSupport=true fullscreenElement=null
requesting fullscreen
fullscreenerror: Instance of 'Event'
Dartium debugger shows these fields:
Event [id=4]
_selector null
bubbles true
cancelable false
clipboardData null
currentTarget #document [id=5]
defaultPrevented false
eventPhase 3
hashCode 234642739
path NodeList[6] [id=6]
target canvas#main_canvas [id=7]
timeStamp 1398779450832
type "webkitfullscreenerror"
For security reasons requestFullscreen can only be called in an event handler of a keyboard or click event.
see also Javascript request fullscreen is unreliable

Perform action after coming from a closed state - SwiftUI

I currently have this:
.onReceive(NotificationCenter.default.publisher(for: UIApplication.willEnterForegroundNotification)) { _ in
...
}
and that works when I don't quit the app, it simply is in the background, but when I quit it, the code inside there that is suppoused to check for a time, doesn't work.
I also tried putting it in
.onAppear() {
but that doesn't workout either :/
How can I once the app has been closed, then opened, as soon as it opens run an action?
you could try adding the following code to perform an action when your app starts:
.onReceive(NotificationCenter.default.publisher(for: UIApplication.didBecomeActiveNotification)) { _ in
// do your action here
....
}

XCUITest verify that ui interruption handler occurred

I'm very new to swift and xcuitest. I recently came across addUIInterruptionMonitor for handling alerts that can pop up. What I would like to know is how I can verify that the alert happened and that the handler was dealt with. Take the following for example
addUIInterruptionMonitorWithDescription("Location Services") {
(alert) -> Bool in
alert.buttons["Allow"].tap()
return true
}
app.buttons["Request Location"].tap()
app.tap() // need to interact with the app again for the handler to fire
// after this if the handler never gets called I want the test to fail
I would like to test that the alert actually happens, but as far as I understand, after my last tap() if the alert never gets triggered my handler wont be called. I need to test that the alert actually happened and then maybe add some assertions to the contents of the handler
I seem to have answered my own question on further investigation. For asynchronous testing with xcuitest I can use a XCTestExpectation which will when created, cause the test to wait until the expectation is fulfilled, or fail after a certain timeout. With that my above code becomes:
let expectation = XCTestExpectation(description: "Location Service Alert")
let locationMonitorToken = addUIInterruptionMonitorWithDescription("Location Services") {
(alert) -> Bool in
alert.buttons["Allow"].tap()
expectation.fulfill() // test waits for this before passing
return true
}
app.buttons["Request Location"].tap()
app.tap() // alert triggered here
wait(for: [expectation], timeout: 3.0)
removeUIInterruptionMonitor(locationMonitorToken)
Update: forgot to put in the wait(for: [expectation], timeout: 3.0) after alert triggered to ensure handler is called.

Chartboost with Swift - Interstitial: prefetch if NOT complete. wait... but no ad?

I am trying to integrate Chartboost ads in Swift into my project and have followed the tutorial verbatim:
https://answers.chartboost.com/hc/en-us/articles/205853276
Everything is imported and in App delegate I have:
Chartboost.startWithAppId(//stuff)
Chartboost.setShouldRequestInterstitialsInFirstSession(false)
Chartboost.cacheMoreApps(CBLocationHomeScreen)
//Chartboost
class func showChartboostAds()
{
Chartboost.showInterstitial(CBLocationHomeScreen);
}
func didFailToLoadInterstitial(location :String!, withError error: CBLoadError)
{
}
func didDismissInterstitial(location :String! )
{
if(location == CBLocationHomeScreen)
{
Chartboost.cacheInterstitial(CBLocationMainMenu)
}
else if(location == CBLocationMainMenu)
{
Chartboost.cacheInterstitial(CBLocationGameOver)
}
else if(location == CBLocationGameOver)
{
Chartboost.cacheInterstitial(CBLocationLevelComplete)
}
else if(location == CBLocationLevelComplete)
{
Chartboost.cacheInterstitial(CBLocationHomeScreen)
}
}
Then in my main view controlleR:
AppDelegate.showChartboostAds()
I have my app in test mode in the dashboard and am running on a real device but I just get Interstitial: prefetch if NOT complete. wait... in the console.
Why won't it show any ads?
Just an aside, the reason this solution works is because of the prefetch method. It seems when you call the ad the Chartboost prefetch isn't completing on time, which is causing the show call to fail.
Chartboost does have a delegate method that will give you the all-clear when prefetch completes, that may be worth a look: - (void)didPrefetchVideos;
Here's a link to the rest of the page.
Hope this helps.
I think you're trying to show the AD to quickly. Try delaying the AD, that solved it for me.