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
3 // found in the LICENSE file.
5 #include "base/compiler_specific.h"
6 #include "chrome/browser/command_observer.h"
7 #include "chrome/browser/command_updater.h"
8 #include "testing/gtest/include/gtest/gtest.h"
10 class TestingCommandHandlerMock
11 : public CommandUpdater::CommandUpdaterDelegate
{
13 virtual void ExecuteCommandWithDisposition(int id
,
14 WindowOpenDisposition
) OVERRIDE
{
19 class CommandUpdaterTest
: public testing::Test
{
22 class TestingCommandObserverMock
: public CommandObserver
{
24 TestingCommandObserverMock() : enabled_(true) {}
26 virtual void EnabledStateChangedForCommand(int id
, bool enabled
) {
30 bool enabled() const { return enabled_
; }
36 TEST_F(CommandUpdaterTest
, TestBasicAPI
) {
37 TestingCommandHandlerMock handler
;
38 CommandUpdater
command_updater(&handler
);
40 // Unsupported command
41 EXPECT_FALSE(command_updater
.SupportsCommand(0));
42 EXPECT_FALSE(command_updater
.IsCommandEnabled(0));
43 // TestingCommandHandlerMock::ExecuteCommand should not be called, since
44 // the command is not supported.
45 command_updater
.ExecuteCommand(0);
47 // Supported, enabled command
48 command_updater
.UpdateCommandEnabled(1, true);
49 EXPECT_TRUE(command_updater
.SupportsCommand(1));
50 EXPECT_TRUE(command_updater
.IsCommandEnabled(1));
51 command_updater
.ExecuteCommand(1);
53 // Supported, disabled command
54 command_updater
.UpdateCommandEnabled(2, false);
55 EXPECT_TRUE(command_updater
.SupportsCommand(2));
56 EXPECT_FALSE(command_updater
.IsCommandEnabled(2));
57 // TestingCommandHandlerMock::ExecuteCommmand should not be called, since
58 // the command_updater is disabled
59 command_updater
.ExecuteCommand(2);
62 TEST_F(CommandUpdaterTest
, TestObservers
) {
63 TestingCommandHandlerMock handler
;
64 CommandUpdater
command_updater(&handler
);
66 // Create an observer for the command 2 and add it to the controller, then
67 // update the command.
68 TestingCommandObserverMock observer
;
69 command_updater
.AddCommandObserver(2, &observer
);
70 command_updater
.UpdateCommandEnabled(2, true);
71 EXPECT_TRUE(observer
.enabled());
72 command_updater
.UpdateCommandEnabled(2, false);
73 EXPECT_FALSE(observer
.enabled());
75 // Remove the observer and update the command.
76 command_updater
.RemoveCommandObserver(2, &observer
);
77 command_updater
.UpdateCommandEnabled(2, true);
78 EXPECT_FALSE(observer
.enabled());
81 TEST_F(CommandUpdaterTest
, TestObserverRemovingAllCommands
) {
82 TestingCommandHandlerMock handler
;
83 CommandUpdater
command_updater(&handler
);
85 // 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
87 // removed observer still thinks all commands are true and the one left
88 // observing picked up the change.
90 TestingCommandObserverMock observer_remove
, observer_keep
;
91 command_updater
.AddCommandObserver(1, &observer_remove
);
92 command_updater
.AddCommandObserver(2, &observer_remove
);
93 command_updater
.AddCommandObserver(3, &observer_remove
);
94 command_updater
.AddCommandObserver(1, &observer_keep
);
95 command_updater
.AddCommandObserver(2, &observer_keep
);
96 command_updater
.AddCommandObserver(3, &observer_keep
);
97 command_updater
.UpdateCommandEnabled(1, true);
98 command_updater
.UpdateCommandEnabled(2, true);
99 command_updater
.UpdateCommandEnabled(3, true);
100 EXPECT_TRUE(observer_remove
.enabled());
102 // Remove one observer and update the command. Check the states, which
103 // should be different.
104 command_updater
.RemoveCommandObserver(&observer_remove
);
105 command_updater
.UpdateCommandEnabled(1, false);
106 command_updater
.UpdateCommandEnabled(2, false);
107 command_updater
.UpdateCommandEnabled(3, false);
108 EXPECT_TRUE(observer_remove
.enabled());
109 EXPECT_FALSE(observer_keep
.enabled());