blob: 9100753fc47afa527bdc66fd1d1258bea05deb0f [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
5#include "base/logging.h"
6#include "chrome/browser/command_updater.h"
7#include "testing/gtest/include/gtest/gtest.h"
8
9class TestingCommandHandlerMock
10 : public CommandUpdater::CommandUpdaterDelegate {
11 public:
12 virtual void ExecuteCommand(int id) {
13 EXPECT_EQ(1, id);
14 }
15};
16
17class CommandUpdaterTest : public testing::Test {
18};
19
20class TestingCommandObserverMock : public CommandUpdater::CommandObserver {
21 public:
22 virtual void EnabledStateChangedForCommand(int id, bool enabled) {
23 enabled_ = enabled;
24 }
25
26 bool enabled() const { return enabled_; }
27
28 private:
29 bool enabled_;
30};
31
32TEST_F(CommandUpdaterTest, TestBasicAPI) {
33 TestingCommandHandlerMock handler;
34 CommandUpdater command_updater(&handler);
35
36 // Unsupported command
37 EXPECT_FALSE(command_updater.SupportsCommand(0));
38 EXPECT_FALSE(command_updater.IsCommandEnabled(0));
39 // TestingCommandHandlerMock::ExecuteCommand should not be called, since
40 // the command is not supported.
41 command_updater.ExecuteCommand(0);
42
43 // Supported, enabled command
44 command_updater.UpdateCommandEnabled(1, true);
45 EXPECT_TRUE(command_updater.SupportsCommand(1));
46 EXPECT_TRUE(command_updater.IsCommandEnabled(1));
47 command_updater.ExecuteCommand(1);
48
49 // Supported, disabled command
50 command_updater.UpdateCommandEnabled(2, false);
51 EXPECT_TRUE(command_updater.SupportsCommand(2));
52 EXPECT_FALSE(command_updater.IsCommandEnabled(2));
53 // TestingCommandHandlerMock::ExecuteCommmand should not be called, since
54 // the command_updater is disabled
55 command_updater.ExecuteCommand(2);
56}
57
58TEST_F(CommandUpdaterTest, TestObservers) {
59 TestingCommandHandlerMock handler;
60 CommandUpdater command_updater(&handler);
61
62 // Create an observer for the command 2 and add it to the controller, then
63 // update the command.
64 TestingCommandObserverMock observer;
65 command_updater.AddCommandObserver(2, &observer);
66 command_updater.UpdateCommandEnabled(2, true);
67 EXPECT_TRUE(observer.enabled());
68 command_updater.UpdateCommandEnabled(2, false);
69 EXPECT_FALSE(observer.enabled());
70
71 // Remove the observer and update the command.
72 command_updater.RemoveCommandObserver(2, &observer);
73 command_updater.UpdateCommandEnabled(2, true);
74 EXPECT_FALSE(observer.enabled());
75}
76
77TEST_F(CommandUpdaterTest, TestRemoveObserverForUnsupportedCommand) {
78 TestingCommandHandlerMock handler;
79 CommandUpdater command_updater(&handler);
80
81 // Test removing observers for commands that are unsupported
82 TestingCommandObserverMock observer;
83 command_updater.RemoveCommandObserver(3, &observer);
84}
85
86TEST_F(CommandUpdaterTest, TestAddingNullObserver) {
87 TestingCommandHandlerMock handler;
88 CommandUpdater command_updater(&handler);
89
90 // Test adding/removing NULL observers
91 command_updater.AddCommandObserver(4, NULL);
92}
93
94TEST_F(CommandUpdaterTest, TestRemovingNullObserver) {
95 TestingCommandHandlerMock handler;
96 CommandUpdater command_updater(&handler);
97
98 command_updater.RemoveCommandObserver(4, NULL);
99}