blob: 659e3c6bb868ea94530393dd3f6d9ececb31bded [file] [log] [blame]
[email protected]1fc025202009-01-20 23:03:141// 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
[email protected]1fc025202009-01-20 23:03:145#include "chrome/browser/command_updater.h"
6#include "testing/gtest/include/gtest/gtest.h"
7
8class TestingCommandHandlerMock
9 : public CommandUpdater::CommandUpdaterDelegate {
10 public:
11 virtual void ExecuteCommand(int id) {
12 EXPECT_EQ(1, id);
13 }
14};
15
16class CommandUpdaterTest : public testing::Test {
17};
18
19class TestingCommandObserverMock : public CommandUpdater::CommandObserver {
20 public:
[email protected]131272f92009-01-22 22:55:0721 TestingCommandObserverMock() : enabled_(true) {}
22
[email protected]1fc025202009-01-20 23:03:1423 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
33TEST_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
[email protected]131272f92009-01-22 22:55:0759TEST_F(CommandUpdaterTest, TestObservers) {
[email protected]1fc025202009-01-20 23:03:1460 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}
[email protected]f7d2e272009-02-09 19:40:3677
78TEST_F(CommandUpdaterTest, TestObserverRemovingAllCommands) {
79 TestingCommandHandlerMock handler;
80 CommandUpdater command_updater(&handler);
[email protected]f0a51fb52009-03-05 12:46:3881
[email protected]f7d2e272009-02-09 19:40:3682 // 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.
[email protected]f0a51fb52009-03-05 12:46:3886
[email protected]f7d2e272009-02-09 19:40:3687 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}