Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / ui / base / test / nswindow_fullscreen_notification_waiter.mm
blobc6bf245bf0549c1215fc3a5ab080ba51a9ecdea8
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/nswindow_fullscreen_notification_waiter.h"
7 #import "base/mac/sdk_forward_declarations.h"
9 @interface NSWindowFullscreenNotificationWaiter ()
10 // Exit the RunLoop if there is one and the counts being tracked match.
11 - (void)maybeQuitForChangedArg:(int*)changedArg;
12 - (void)onEnter:(NSNotification*)notification;
13 - (void)onExit:(NSNotification*)notification;
14 @end
16 @implementation NSWindowFullscreenNotificationWaiter
18 @synthesize enterCount = enterCount_;
19 @synthesize exitCount = exitCount_;
21 - (id)initWithWindow:(NSWindow*)window {
22   if ((self = [super init])) {
23     window_.reset([window retain]);
24     NSNotificationCenter* defaultCenter = [NSNotificationCenter defaultCenter];
25     [defaultCenter addObserver:self
26                       selector:@selector(onEnter:)
27                           name:NSWindowDidEnterFullScreenNotification
28                         object:window];
29     [defaultCenter addObserver:self
30                       selector:@selector(onExit:)
31                           name:NSWindowDidExitFullScreenNotification
32                         object:window];
33   }
34   return self;
37 - (void)dealloc {
38   DCHECK(!runLoop_);
39   [[NSNotificationCenter defaultCenter] removeObserver:self];
40   [super dealloc];
43 - (void)waitForEnterCount:(int)enterCount exitCount:(int)exitCount {
44   if (enterCount_ >= enterCount && exitCount_ >= exitCount)
45     return;
47   targetEnterCount_ = enterCount;
48   targetExitCount_ = exitCount;
49   runLoop_.reset(new base::RunLoop);
50   runLoop_->Run();
51   runLoop_.reset();
54 - (void)maybeQuitForChangedArg:(int*)changedArg {
55   ++*changedArg;
56   if (!runLoop_)
57     return;
59   if (enterCount_ >= targetEnterCount_ && exitCount_ >= targetExitCount_)
60     runLoop_->Quit();
63 - (void)onEnter:(NSNotification*)notification {
64   [self maybeQuitForChangedArg:&enterCount_];
67 - (void)onExit:(NSNotification*)notification {
68   [self maybeQuitForChangedArg:&exitCount_];
71 @end