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