blob: 02ef2dbe29291ac495f706c613e84b03efe96548 [file] [log] [blame]
James Robinson646469d2014-10-03 15:33:28 -07001// Copyright (c) 2011 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.
4
5#import "base/mac/scoped_sending_event.h"
6
James Robinson24218d72014-10-20 16:18:41 -07007#import <Foundation/Foundation.h>
8
9#include "base/mac/scoped_nsobject.h"
James Robinson646469d2014-10-03 15:33:28 -070010#include "testing/gtest/include/gtest/gtest.h"
11
James Robinson24218d72014-10-20 16:18:41 -070012@interface ScopedSendingEventTestCrApp : NSObject <CrAppControlProtocol> {
13 @private
14 BOOL handlingSendEvent_;
15}
16@property(nonatomic, assign, getter=isHandlingSendEvent) BOOL handlingSendEvent;
17@end
18
19@implementation ScopedSendingEventTestCrApp
20@synthesize handlingSendEvent = handlingSendEvent_;
21@end
22
James Robinson646469d2014-10-03 15:33:28 -070023namespace {
24
James Robinson24218d72014-10-20 16:18:41 -070025class ScopedSendingEventTest : public testing::Test {
26 public:
27 ScopedSendingEventTest() : app_([[ScopedSendingEventTestCrApp alloc] init]) {
28 NSApp = app_.get();
29 }
James Robinsond753aca2015-01-12 13:07:20 -080030 ~ScopedSendingEventTest() override { NSApp = nil; }
James Robinson24218d72014-10-20 16:18:41 -070031
32 private:
33 base::scoped_nsobject<ScopedSendingEventTestCrApp> app_;
34};
35
James Robinson646469d2014-10-03 15:33:28 -070036// Sets the flag within scope, resets when leaving scope.
James Robinson24218d72014-10-20 16:18:41 -070037TEST_F(ScopedSendingEventTest, SetHandlingSendEvent) {
James Robinson646469d2014-10-03 15:33:28 -070038 id<CrAppProtocol> app = NSApp;
39 EXPECT_FALSE([app isHandlingSendEvent]);
40 {
41 base::mac::ScopedSendingEvent is_handling_send_event;
42 EXPECT_TRUE([app isHandlingSendEvent]);
43 }
44 EXPECT_FALSE([app isHandlingSendEvent]);
45}
46
47// Nested call restores previous value rather than resetting flag.
James Robinson24218d72014-10-20 16:18:41 -070048TEST_F(ScopedSendingEventTest, NestedSetHandlingSendEvent) {
James Robinson646469d2014-10-03 15:33:28 -070049 id<CrAppProtocol> app = NSApp;
50 EXPECT_FALSE([app isHandlingSendEvent]);
51 {
52 base::mac::ScopedSendingEvent is_handling_send_event;
53 EXPECT_TRUE([app isHandlingSendEvent]);
54 {
55 base::mac::ScopedSendingEvent nested_is_handling_send_event;
56 EXPECT_TRUE([app isHandlingSendEvent]);
57 }
58 EXPECT_TRUE([app isHandlingSendEvent]);
59 }
60 EXPECT_FALSE([app isHandlingSendEvent]);
61}
62
63} // namespace