- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / cocoa / command_observer_bridge_unittest.mm
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.
4
5 #import <Cocoa/Cocoa.h>
6
7 #include "base/mac/scoped_nsobject.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "chrome/browser/command_updater.h"
10 #import "chrome/browser/ui/cocoa/command_observer_bridge.h"
11 #include "testing/gtest/include/gtest/gtest.h"
12 #include "testing/platform_test.h"
13
14 // Implements the callback interface. Records the last command id and
15 // enabled state it has received so it can be queried by the tests to see
16 // if we got a notification or not.
17 @interface CommandTestObserver : NSObject<CommandObserverProtocol> {
18  @private
19   int lastCommand_;  // id of last received state change
20   bool lastState_;  // state of last received state change
21 }
22 - (int)lastCommand;
23 - (bool)lastState;
24 @end
25
26 @implementation CommandTestObserver
27 - (void)enabledStateChangedForCommand:(NSInteger)command enabled:(BOOL)enabled {
28   lastCommand_ = command;
29   lastState_ = enabled;
30 }
31 - (int)lastCommand {
32   return lastCommand_;
33 }
34 - (bool)lastState {
35   return lastState_;
36 }
37 @end
38
39 namespace {
40
41 class CommandObserverBridgeTest : public PlatformTest {
42  public:
43   CommandObserverBridgeTest()
44       : updater_(new CommandUpdater(NULL)),
45         observer_([[CommandTestObserver alloc] init]) {
46   }
47   scoped_ptr<CommandUpdater> updater_;
48   base::scoped_nsobject<CommandTestObserver> observer_;
49 };
50
51 // Tests creation and deletion. NULL arguments aren't allowed.
52 TEST_F(CommandObserverBridgeTest, Create) {
53   CommandObserverBridge bridge(observer_.get(), updater_.get());
54 }
55
56 // Observes state changes on command ids 1 and 2. Ensure we don't get
57 // a notification of a state change on a command we're not observing (3).
58 // Commands start off enabled in CommandUpdater.
59 TEST_F(CommandObserverBridgeTest, Observe) {
60   CommandObserverBridge bridge(observer_.get(), updater_.get());
61   bridge.ObserveCommand(1);
62   bridge.ObserveCommand(2);
63
64   // Validate initial state assumptions.
65   EXPECT_EQ([observer_ lastCommand], 0);
66   EXPECT_EQ([observer_ lastState], false);
67   EXPECT_EQ(updater_->IsCommandEnabled(1), true);
68   EXPECT_EQ(updater_->IsCommandEnabled(2), true);
69
70   updater_->UpdateCommandEnabled(1, false);
71   EXPECT_EQ([observer_ lastCommand], 1);
72   EXPECT_EQ([observer_ lastState], false);
73
74   updater_->UpdateCommandEnabled(2, false);
75   EXPECT_EQ([observer_ lastCommand], 2);
76   EXPECT_EQ([observer_ lastState], false);
77
78   updater_->UpdateCommandEnabled(1, true);
79   EXPECT_EQ([observer_ lastCommand], 1);
80   EXPECT_EQ([observer_ lastState], true);
81
82   // Change something we're not watching and make sure the last state hasn't
83   // changed.
84   updater_->UpdateCommandEnabled(3, false);
85   EXPECT_EQ([observer_ lastCommand], 1);
86   EXPECT_NE([observer_ lastCommand], 3);
87   EXPECT_EQ([observer_ lastState], true);
88 }
89
90 }  // namespace