| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #import "ui/base/test/windowed_nsnotification_observer.h" | 5 #import "ui/base/test/windowed_nsnotification_observer.h" |
| 6 | 6 |
| 7 #import <Cocoa/Cocoa.h> | 7 #import <Cocoa/Cocoa.h> |
| 8 | 8 |
| 9 #include "base/run_loop.h" | 9 #include "base/run_loop.h" |
| 10 #include "base/test/test_timeouts.h" |
| 10 | 11 |
| 11 @interface WindowedNSNotificationObserver () | 12 @interface WindowedNSNotificationObserver () |
| 12 - (void)onNotification:(NSNotification*)notification; | 13 - (void)onNotification:(NSNotification*)notification; |
| 13 @end | 14 @end |
| 14 | 15 |
| 15 @implementation WindowedNSNotificationObserver | 16 @implementation WindowedNSNotificationObserver |
| 16 | 17 |
| 17 - (id)initForNotification:(NSString*)name { | 18 - (id)initForNotification:(NSString*)name object:(id)notificationSender { |
| 18 if ((self = [super init])) { | 19 if ((self = [super init])) { |
| 19 [[NSNotificationCenter defaultCenter] addObserver:self | 20 [[NSNotificationCenter defaultCenter] addObserver:self |
| 20 selector:@selector(onNotification:) | 21 selector:@selector(onNotification:) |
| 21 name:name | 22 name:name |
| 22 object:nil]; | 23 object:notificationSender]; |
| 23 } | 24 } |
| 24 return self; | 25 return self; |
| 25 } | 26 } |
| 26 | 27 |
| 28 - (id)initForNotification:(NSString*)name { |
| 29 return [self initForNotification:name object:nil]; |
| 30 } |
| 31 |
| 27 - (id)initForWorkspaceNotification:(NSString*)name | 32 - (id)initForWorkspaceNotification:(NSString*)name |
| 28 bundleId:(NSString*)bundleId { | 33 bundleId:(NSString*)bundleId { |
| 29 if ((self = [super init])) { | 34 if ((self = [super init])) { |
| 30 bundleId_.reset([bundleId copy]); | 35 bundleId_.reset([bundleId copy]); |
| 31 [[[NSWorkspace sharedWorkspace] notificationCenter] | 36 [[[NSWorkspace sharedWorkspace] notificationCenter] |
| 32 addObserver:self | 37 addObserver:self |
| 33 selector:@selector(onNotification:) | 38 selector:@selector(onNotification:) |
| 34 name:name | 39 name:name |
| 35 object:nil]; | 40 object:nil]; |
| 36 } | 41 } |
| 37 return self; | 42 return self; |
| 38 } | 43 } |
| 39 | 44 |
| 45 - (void)dealloc { |
| 46 if (bundleId_) |
| 47 [[[NSWorkspace sharedWorkspace] notificationCenter] removeObserver:self]; |
| 48 else |
| 49 [[NSNotificationCenter defaultCenter] removeObserver:self]; |
| 50 [super dealloc]; |
| 51 } |
| 52 |
| 40 - (void)onNotification:(NSNotification*)notification { | 53 - (void)onNotification:(NSNotification*)notification { |
| 41 if (bundleId_) { | 54 if (bundleId_) { |
| 42 NSRunningApplication* application = | 55 NSRunningApplication* application = |
| 43 [[notification userInfo] objectForKey:NSWorkspaceApplicationKey]; | 56 [[notification userInfo] objectForKey:NSWorkspaceApplicationKey]; |
| 44 if (![[application bundleIdentifier] isEqualToString:bundleId_]) | 57 if (![[application bundleIdentifier] isEqualToString:bundleId_]) |
| 45 return; | 58 return; |
| 46 | |
| 47 [[[NSWorkspace sharedWorkspace] notificationCenter] removeObserver:self]; | |
| 48 } else { | |
| 49 [[NSNotificationCenter defaultCenter] removeObserver:self]; | |
| 50 } | 59 } |
| 51 | 60 |
| 52 notificationReceived_ = YES; | 61 ++notificationCount_; |
| 53 if (runLoop_) | 62 if (runLoop_) |
| 54 runLoop_->Quit(); | 63 runLoop_->Quit(); |
| 55 } | 64 } |
| 56 | 65 |
| 57 - (void)wait { | 66 - (BOOL)waitForCount:(int)minimumCount { |
| 58 if (notificationReceived_) | 67 if (notificationCount_ >= minimumCount) |
| 59 return; | 68 return YES; |
| 60 | 69 |
| 61 base::RunLoop runLoop; | 70 base::RunLoop runLoop; |
| 71 base::MessageLoop::current()->task_runner()->PostDelayedTask( |
| 72 FROM_HERE, runLoop.QuitClosure(), TestTimeouts::action_timeout()); |
| 62 runLoop_ = &runLoop; | 73 runLoop_ = &runLoop; |
| 63 runLoop.Run(); | 74 runLoop.Run(); |
| 64 runLoop_ = nullptr; | 75 runLoop_ = nullptr; |
| 76 return notificationCount_ >= minimumCount; |
| 77 } |
| 78 |
| 79 - (BOOL)wait { |
| 80 return [self waitForCount:1]; |
| 65 } | 81 } |
| 66 | 82 |
| 67 @end | 83 @end |
| OLD | NEW |