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