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 <Cocoa/Cocoa.h>
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"
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> {
19 int lastCommand_; // id of last received state change
20 bool lastState_; // state of last received state change
26 @implementation CommandTestObserver
27 - (void)enabledStateChangedForCommand:(NSInteger)command enabled:(BOOL)enabled {
28 lastCommand_ = command;
41 class CommandObserverBridgeTest : public PlatformTest {
43 CommandObserverBridgeTest()
44 : updater_(new CommandUpdater(NULL)),
45 observer_([[CommandTestObserver alloc] init]) {
47 scoped_ptr<CommandUpdater> updater_;
48 base::scoped_nsobject<CommandTestObserver> observer_;
51 // Tests creation and deletion. NULL arguments aren't allowed.
52 TEST_F(CommandObserverBridgeTest, Create) {
53 CommandObserverBridge bridge(observer_.get(), updater_.get());
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);
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);
70 updater_->UpdateCommandEnabled(1, false);
71 EXPECT_EQ([observer_ lastCommand], 1);
72 EXPECT_EQ([observer_ lastState], false);
74 updater_->UpdateCommandEnabled(2, false);
75 EXPECT_EQ([observer_ lastCommand], 2);
76 EXPECT_EQ([observer_ lastState], false);
78 updater_->UpdateCommandEnabled(1, true);
79 EXPECT_EQ([observer_ lastCommand], 1);
80 EXPECT_EQ([observer_ lastState], true);
82 // Change something we're not watching and make sure the last state hasn't
84 updater_->UpdateCommandEnabled(3, false);
85 EXPECT_EQ([observer_ lastCommand], 1);
86 EXPECT_NE([observer_ lastCommand], 3);
87 EXPECT_EQ([observer_ lastState], true);