Blink roll 25b6bd3a7a131ffe68d809546ad1a20707915cdc:3a503f41ae42e5b79cfcd2ff10e65afde...
[chromium-blink-merge.git] / base / mac / scoped_sending_event_unittest.mm
blob95f6eba16ab4304bd5a23a81062971c371094d1a
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> {
13  @private
14   BOOL handlingSendEvent_;
16 @property(nonatomic, assign, getter=isHandlingSendEvent) BOOL handlingSendEvent;
17 @end
19 @implementation ScopedSendingEventTestCrApp
20 @synthesize handlingSendEvent = handlingSendEvent_;
21 @end
23 namespace {
25 class ScopedSendingEventTest : public testing::Test {
26  public:
27   ScopedSendingEventTest() : app_([[ScopedSendingEventTestCrApp alloc] init]) {
28     NSApp = app_.get();
29   }
30   virtual ~ScopedSendingEventTest() {
31     NSApp = nil;
32   }
34  private:
35   base::scoped_nsobject<ScopedSendingEventTestCrApp> app_;
38 // Sets the flag within scope, resets when leaving scope.
39 TEST_F(ScopedSendingEventTest, SetHandlingSendEvent) {
40   id<CrAppProtocol> app = NSApp;
41   EXPECT_FALSE([app isHandlingSendEvent]);
42   {
43     base::mac::ScopedSendingEvent is_handling_send_event;
44     EXPECT_TRUE([app isHandlingSendEvent]);
45   }
46   EXPECT_FALSE([app isHandlingSendEvent]);
49 // Nested call restores previous value rather than resetting flag.
50 TEST_F(ScopedSendingEventTest, NestedSetHandlingSendEvent) {
51   id<CrAppProtocol> app = NSApp;
52   EXPECT_FALSE([app isHandlingSendEvent]);
53   {
54     base::mac::ScopedSendingEvent is_handling_send_event;
55     EXPECT_TRUE([app isHandlingSendEvent]);
56     {
57       base::mac::ScopedSendingEvent nested_is_handling_send_event;
58       EXPECT_TRUE([app isHandlingSendEvent]);
59     }
60     EXPECT_TRUE([app isHandlingSendEvent]);
61   }
62   EXPECT_FALSE([app isHandlingSendEvent]);
65 }  // namespace