1 // 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.
5 #import "base/mac/scoped_sending_event.h"
7 #import <Foundation/Foundation.h>
9 #include "base/mac/scoped_nsobject.h"
10 #include "testing/gtest/include/gtest/gtest.h"
12 @interface ScopedSendingEventTestCrApp : NSObject <CrAppControlProtocol> {
14 BOOL handlingSendEvent_;
16 @property(nonatomic, assign, getter=isHandlingSendEvent) BOOL handlingSendEvent;
19 @implementation ScopedSendingEventTestCrApp
20 @synthesize handlingSendEvent = handlingSendEvent_;
25 class ScopedSendingEventTest : public testing::Test {
27 ScopedSendingEventTest() : app_([[ScopedSendingEventTestCrApp alloc] init]) {
30 ~ScopedSendingEventTest() override { NSApp = nil; }
33 base::scoped_nsobject<ScopedSendingEventTestCrApp> app_;
36 // Sets the flag within scope, resets when leaving scope.
37 TEST_F(ScopedSendingEventTest, SetHandlingSendEvent) {
38 id<CrAppProtocol> app = NSApp;
39 EXPECT_FALSE([app isHandlingSendEvent]);
41 base::mac::ScopedSendingEvent is_handling_send_event;
42 EXPECT_TRUE([app isHandlingSendEvent]);
44 EXPECT_FALSE([app isHandlingSendEvent]);
47 // Nested call restores previous value rather than resetting flag.
48 TEST_F(ScopedSendingEventTest, NestedSetHandlingSendEvent) {
49 id<CrAppProtocol> app = NSApp;
50 EXPECT_FALSE([app isHandlingSendEvent]);
52 base::mac::ScopedSendingEvent is_handling_send_event;
53 EXPECT_TRUE([app isHandlingSendEvent]);
55 base::mac::ScopedSendingEvent nested_is_handling_send_event;
56 EXPECT_TRUE([app isHandlingSendEvent]);
58 EXPECT_TRUE([app isHandlingSendEvent]);
60 EXPECT_FALSE([app isHandlingSendEvent]);