1 // Copyright (c) 2006-2008 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 #include "chrome/browser/command_updater.h" 6 #include "testing/gtest/include/gtest/gtest.h" 7 8 class TestingCommandHandlerMock 9 : public CommandUpdater::CommandUpdaterDelegate { 10 public: 11 virtual void ExecuteCommand(int id) { 12 EXPECT_EQ(1, id); 13 } 14 }; 15 16 class CommandUpdaterTest : public testing::Test { 17 }; 18 19 class TestingCommandObserverMock : public CommandUpdater::CommandObserver { 20 public: 21 TestingCommandObserverMock() : enabled_(true) {} 22 23 virtual void EnabledStateChangedForCommand(int id, bool enabled) { 24 enabled_ = enabled; 25 } 26 27 bool enabled() const { return enabled_; } 28 29 private: 30 bool enabled_; 31 }; 32 33 TEST_F(CommandUpdaterTest, TestBasicAPI) { 34 TestingCommandHandlerMock handler; 35 CommandUpdater command_updater(&handler); 36 37 // Unsupported command 38 EXPECT_FALSE(command_updater.SupportsCommand(0)); 39 EXPECT_FALSE(command_updater.IsCommandEnabled(0)); 40 // TestingCommandHandlerMock::ExecuteCommand should not be called, since 41 // the command is not supported. 42 command_updater.ExecuteCommand(0); 43 44 // Supported, enabled command 45 command_updater.UpdateCommandEnabled(1, true); 46 EXPECT_TRUE(command_updater.SupportsCommand(1)); 47 EXPECT_TRUE(command_updater.IsCommandEnabled(1)); 48 command_updater.ExecuteCommand(1); 49 50 // Supported, disabled command 51 command_updater.UpdateCommandEnabled(2, false); 52 EXPECT_TRUE(command_updater.SupportsCommand(2)); 53 EXPECT_FALSE(command_updater.IsCommandEnabled(2)); 54 // TestingCommandHandlerMock::ExecuteCommmand should not be called, since 55 // the command_updater is disabled 56 command_updater.ExecuteCommand(2); 57 } 58 59 TEST_F(CommandUpdaterTest, TestObservers) { 60 TestingCommandHandlerMock handler; 61 CommandUpdater command_updater(&handler); 62 63 // Create an observer for the command 2 and add it to the controller, then 64 // update the command. 65 TestingCommandObserverMock observer; 66 command_updater.AddCommandObserver(2, &observer); 67 command_updater.UpdateCommandEnabled(2, true); 68 EXPECT_TRUE(observer.enabled()); 69 command_updater.UpdateCommandEnabled(2, false); 70 EXPECT_FALSE(observer.enabled()); 71 72 // Remove the observer and update the command. 73 command_updater.RemoveCommandObserver(2, &observer); 74 command_updater.UpdateCommandEnabled(2, true); 75 EXPECT_FALSE(observer.enabled()); 76 } 77 78 TEST_F(CommandUpdaterTest, TestObserverRemovingAllCommands) { 79 TestingCommandHandlerMock handler; 80 CommandUpdater command_updater(&handler); 81 82 // Create two observers for the commands 1-3 as true, remove one using the 83 // single remove command, then set the command to false. Ensure that the 84 // removed observer still thinks all commands are true and the one left 85 // observing picked up the change. 86 87 TestingCommandObserverMock observer_remove, observer_keep; 88 command_updater.AddCommandObserver(1, &observer_remove); 89 command_updater.AddCommandObserver(2, &observer_remove); 90 command_updater.AddCommandObserver(3, &observer_remove); 91 command_updater.AddCommandObserver(1, &observer_keep); 92 command_updater.AddCommandObserver(2, &observer_keep); 93 command_updater.AddCommandObserver(3, &observer_keep); 94 command_updater.UpdateCommandEnabled(1, true); 95 command_updater.UpdateCommandEnabled(2, true); 96 command_updater.UpdateCommandEnabled(3, true); 97 EXPECT_TRUE(observer_remove.enabled()); 98 99 // Remove one observer and update the command. Check the states, which 100 // should be different. 101 command_updater.RemoveCommandObserver(&observer_remove); 102 command_updater.UpdateCommandEnabled(1, false); 103 command_updater.UpdateCommandEnabled(2, false); 104 command_updater.UpdateCommandEnabled(3, false); 105 EXPECT_TRUE(observer_remove.enabled()); 106 EXPECT_FALSE(observer_keep.enabled()); 107 } 108