Add ICU message format support
[chromium-blink-merge.git] / ui / base / test / windowed_nsnotification_observer.mm
blobf5c1e667a6f00cf9950eed7d9ce9d6cec79a5d4c
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;
14 @end
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:)
28                                                  name:name
29                                                object:sender];
30   }
31   return self;
34 - (id)initForWorkspaceNotification:(NSString*)name
35                           bundleId:(NSString*)bundleId {
36   if ((self = [super init])) {
37     bundleId_.reset([bundleId copy]);
38     [[[NSWorkspace sharedWorkspace] notificationCenter]
39         addObserver:self
40            selector:@selector(onNotification:)
41                name:name
42              object:nil];
43   }
44   return self;
47 - (void)dealloc {
48   if (bundleId_)
49     [[[NSWorkspace sharedWorkspace] notificationCenter] removeObserver:self];
50   else
51     [[NSNotificationCenter defaultCenter] removeObserver:self];
52   [super dealloc];
55 - (void)onNotification:(NSNotification*)notification {
56   if (bundleId_) {
57     NSRunningApplication* application =
58         [[notification userInfo] objectForKey:NSWorkspaceApplicationKey];
59     if (![[application bundleIdentifier] isEqualToString:bundleId_])
60       return;
61   }
63   ++notificationCount_;
64   if (runLoop_)
65     runLoop_->Quit();
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());
74     runLoop_ = &runLoop;
75     runLoop.Run();
76     runLoop_ = nullptr;
78     // If there was no new notification, it must have been a timeout.
79     if (notificationCount_ == oldCount)
80       break;
81   }
82   return notificationCount_ >= minimumCount;
85 - (BOOL)wait {
86   return [self waitForCount:1];
89 @end