| 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 { |
| 18 return [self initForNotification:name object:nil]; | 19 return [self initForNotification:name object:nil]; |
| 19 } | 20 } |
| (...skipping 14 matching lines...) Expand all Loading... |
| 34 bundleId_.reset([bundleId copy]); | 35 bundleId_.reset([bundleId copy]); |
| 35 [[[NSWorkspace sharedWorkspace] notificationCenter] | 36 [[[NSWorkspace sharedWorkspace] notificationCenter] |
| 36 addObserver:self | 37 addObserver:self |
| 37 selector:@selector(onNotification:) | 38 selector:@selector(onNotification:) |
| 38 name:name | 39 name:name |
| 39 object:nil]; | 40 object:nil]; |
| 40 } | 41 } |
| 41 return self; | 42 return self; |
| 42 } | 43 } |
| 43 | 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 |
| 44 - (void)onNotification:(NSNotification*)notification { | 53 - (void)onNotification:(NSNotification*)notification { |
| 45 if (bundleId_) { | 54 if (bundleId_) { |
| 46 NSRunningApplication* application = | 55 NSRunningApplication* application = |
| 47 [[notification userInfo] objectForKey:NSWorkspaceApplicationKey]; | 56 [[notification userInfo] objectForKey:NSWorkspaceApplicationKey]; |
| 48 if (![[application bundleIdentifier] isEqualToString:bundleId_]) | 57 if (![[application bundleIdentifier] isEqualToString:bundleId_]) |
| 49 return; | 58 return; |
| 50 | |
| 51 [[[NSWorkspace sharedWorkspace] notificationCenter] removeObserver:self]; | |
| 52 } else { | |
| 53 [[NSNotificationCenter defaultCenter] removeObserver:self]; | |
| 54 } | 59 } |
| 55 | 60 |
| 56 notificationReceived_ = YES; | 61 ++notificationCount_; |
| 57 if (runLoop_) | 62 if (runLoop_) |
| 58 runLoop_->Quit(); | 63 runLoop_->Quit(); |
| 59 } | 64 } |
| 60 | 65 |
| 61 - (void)wait { | 66 - (BOOL)waitForCount:(int)minimumCount { |
| 62 if (notificationReceived_) | 67 while (notificationCount_ < minimumCount) { |
| 63 return; | 68 const int oldCount = notificationCount_; |
| 69 base::RunLoop runLoop; |
| 70 base::MessageLoop::current()->task_runner()->PostDelayedTask( |
| 71 FROM_HERE, runLoop.QuitClosure(), TestTimeouts::action_timeout()); |
| 72 runLoop_ = &runLoop; |
| 73 runLoop.Run(); |
| 74 runLoop_ = nullptr; |
| 64 | 75 |
| 65 base::RunLoop runLoop; | 76 // If there was no new notification, it must have been a timeout. |
| 66 runLoop_ = &runLoop; | 77 if (notificationCount_ == oldCount) |
| 67 runLoop.Run(); | 78 break; |
| 68 runLoop_ = nullptr; | 79 } |
| 80 return notificationCount_ >= minimumCount; |
| 81 } |
| 82 |
| 83 - (BOOL)wait { |
| 84 return [self waitForCount:1]; |
| 69 } | 85 } |
| 70 | 86 |
| 71 @end | 87 @end |
| OLD | NEW |