diff options
author | pinkerton@chromium.org <pinkerton@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-04-15 17:26:08 +0000 |
---|---|---|
committer | pinkerton@chromium.org <pinkerton@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-04-15 17:26:08 +0000 |
commit | 4825062601489323b02c6cbb9c57ecc8c953ec53 (patch) | |
tree | 4583edcba17e72f0ad81af3789f96585a80a1ae1 | |
parent | 83c77a4c71b6d338bcb27d2cc7e58ceb18f191d7 (diff) | |
download | chromium_src-4825062601489323b02c6cbb9c57ecc8c953ec53.zip chromium_src-4825062601489323b02c6cbb9c57ecc8c953ec53.tar.gz chromium_src-4825062601489323b02c6cbb9c57ecc8c953ec53.tar.bz2 |
Unit test for command observer bridge class. Add ifdef guards that were missing in the original header file.
Review URL: http://codereview.chromium.org/73082
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@13755 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r-- | chrome/browser/cocoa/command_observer_bridge.h | 5 | ||||
-rw-r--r-- | chrome/browser/cocoa/command_observer_bridge_unittest.mm | 88 | ||||
-rw-r--r-- | chrome/chrome.gyp | 1 |
3 files changed, 94 insertions, 0 deletions
diff --git a/chrome/browser/cocoa/command_observer_bridge.h b/chrome/browser/cocoa/command_observer_bridge.h index 042c0d1..e4487c8 100644 --- a/chrome/browser/cocoa/command_observer_bridge.h +++ b/chrome/browser/cocoa/command_observer_bridge.h @@ -2,6 +2,9 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. +#ifndef CHROME_BROWSER_COCOA_COMMAND_OBSERVER_BRIDGE +#define CHROME_BROWSER_COCOA_COMMAND_OBSERVER_BRIDGE + #import <Cocoa/Cocoa.h> #include "chrome/browser/command_updater.h" @@ -39,3 +42,5 @@ class CommandObserverBridge : public CommandUpdater::CommandObserver { @protocol CommandObserverProtocol - (void)enabledStateChangedForCommand:(NSInteger)command enabled:(BOOL)enabled; @end + +#endif // CHROME_BROWSER_COCOA_COMMAND_OBSERVER_BRIDGE diff --git a/chrome/browser/cocoa/command_observer_bridge_unittest.mm b/chrome/browser/cocoa/command_observer_bridge_unittest.mm new file mode 100644 index 0000000..b510b3f --- /dev/null +++ b/chrome/browser/cocoa/command_observer_bridge_unittest.mm @@ -0,0 +1,88 @@ +// Copyright (c) 2009 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#import <Cocoa/Cocoa.h> + +#include "base/scoped_ptr.h" +#include "base/scoped_nsobject.h" +#import "chrome/browser/cocoa/command_observer_bridge.h" +#include "testing/gtest/include/gtest/gtest.h" + +// Implements the callback interface. Records the last command id and +// enabled state it has received so it can be queried by the tests to see +// if we got a notification or not. +@interface CommandTestObserver : NSObject<CommandObserverProtocol> { + @private + int lastCommand_; // id of last received state change + bool lastState_; // state of last received state change +} +- (int)lastCommand; +- (bool)lastState; +@end + +@implementation CommandTestObserver +- (void)enabledStateChangedForCommand:(NSInteger)command enabled:(BOOL)enabled { + lastCommand_ = command; + lastState_ = enabled; +} +- (int)lastCommand { + return lastCommand_; +} +- (bool)lastState { + return lastState_; +} +@end + +namespace { + +class CommandObserverBridgeTest : public testing::Test { + public: + CommandObserverBridgeTest() + : updater_(new CommandUpdater(NULL)), + observer_([[CommandTestObserver alloc] init]) { + } + scoped_ptr<CommandUpdater> updater_; + scoped_nsobject<CommandTestObserver> observer_; +}; + +// Tests creation and deletion. NULL arguments aren't allowed. +TEST_F(CommandObserverBridgeTest, Create) { + CommandObserverBridge bridge(observer_.get(), updater_.get()); +} + +// Observes state changes on command ids 1 and 2. Ensure we don't get +// a notification of a state change on a command we're not observing (3). +// Commands start off enabled in CommandUpdater. +TEST_F(CommandObserverBridgeTest, Observe) { + CommandObserverBridge bridge(observer_.get(), updater_.get()); + bridge.ObserveCommand(1); + bridge.ObserveCommand(2); + + // Validate initial state assumptions. + EXPECT_EQ([observer_ lastCommand], 0); + EXPECT_EQ([observer_ lastState], false); + EXPECT_EQ(updater_->IsCommandEnabled(1), true); + EXPECT_EQ(updater_->IsCommandEnabled(2), true); + + updater_->UpdateCommandEnabled(1, false); + EXPECT_EQ([observer_ lastCommand], 1); + EXPECT_EQ([observer_ lastState], false); + + updater_->UpdateCommandEnabled(2, false); + EXPECT_EQ([observer_ lastCommand], 2); + EXPECT_EQ([observer_ lastState], false); + + updater_->UpdateCommandEnabled(1, true); + EXPECT_EQ([observer_ lastCommand], 1); + EXPECT_EQ([observer_ lastState], true); + + // Change something we're not watching and make sure the last state hasn't + // changed. + updater_->UpdateCommandEnabled(3, false); + EXPECT_EQ([observer_ lastCommand], 1); + EXPECT_NE([observer_ lastCommand], 3); + EXPECT_EQ([observer_ lastState], true); +} + +} // namespace diff --git a/chrome/chrome.gyp b/chrome/chrome.gyp index f3b36b1..24d9112 100644 --- a/chrome/chrome.gyp +++ b/chrome/chrome.gyp @@ -2117,6 +2117,7 @@ 'browser/cocoa/bookmark_bar_state_controller_unittest.mm', 'browser/cocoa/bookmark_menu_bridge_unittest.mm', 'browser/cocoa/bookmark_menu_cocoa_controller_unittest.mm', + 'browser/cocoa/command_observer_bridge_unittest.mm', 'browser/cocoa/location_bar_view_mac_unittest.mm', 'browser/cocoa/status_bubble_mac_unittest.mm', 'browser/command_updater_unittest.cc', |