1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #import "ui/base/test/windowed_nsnotification_observer.h"
7 #import <Cocoa/Cocoa.h>
9 #include "base/run_loop.h"
10 #include "base/test/test_timeouts.h"
12 @interface WindowedNSNotificationObserver ()
13 - (void)onNotification:(NSNotification*)notification;
16 @implementation WindowedNSNotificationObserver
18 @synthesize notificationCount = notificationCount_;
20 - (id)initForNotification:(NSString*)name {
21 return [self initForNotification:name object:nil];
24 - (id)initForNotification:(NSString*)name object:(id)sender {
25 if ((self = [super init])) {
26 [[NSNotificationCenter defaultCenter] addObserver:self
27 selector:@selector(onNotification:)
34 - (id)initForWorkspaceNotification:(NSString*)name
35 bundleId:(NSString*)bundleId {
36 if ((self = [super init])) {
37 bundleId_.reset([bundleId copy]);
38 [[[NSWorkspace sharedWorkspace] notificationCenter]
40 selector:@selector(onNotification:)
49 [[[NSWorkspace sharedWorkspace] notificationCenter] removeObserver:self];
51 [[NSNotificationCenter defaultCenter] removeObserver:self];
55 - (void)onNotification:(NSNotification*)notification {
57 NSRunningApplication* application =
58 [[notification userInfo] objectForKey:NSWorkspaceApplicationKey];
59 if (![[application bundleIdentifier] isEqualToString:bundleId_])
68 - (BOOL)waitForCount:(int)minimumCount {
69 while (notificationCount_ < minimumCount) {
70 const int oldCount = notificationCount_;
71 base::RunLoop runLoop;
72 base::MessageLoop::current()->task_runner()->PostDelayedTask(
73 FROM_HERE, runLoop.QuitClosure(), TestTimeouts::action_timeout());
78 // If there was no new notification, it must have been a timeout.
79 if (notificationCount_ == oldCount)
82 return notificationCount_ >= minimumCount;
86 return [self waitForCount:1];