blob: 0297cbc230f8ae1f7c4baa8645afa28487b2b203 [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:
[email protected]131272f92009-01-22 22:55:0722 TestingCommandObserverMock() : enabled_(true) {}
23
[email protected]1fc025202009-01-20 23:03:1424 virtual void EnabledStateChangedForCommand(int id, bool enabled) {
25 enabled_ = enabled;
26 }
27
28 bool enabled() const { return enabled_; }
29
30 private:
31 bool enabled_;
32};
33
34TEST_F(CommandUpdaterTest, TestBasicAPI) {
35 TestingCommandHandlerMock handler;
36 CommandUpdater command_updater(&handler);
37
38 // Unsupported command
39 EXPECT_FALSE(command_updater.SupportsCommand(0));
40 EXPECT_FALSE(command_updater.IsCommandEnabled(0));
41 // TestingCommandHandlerMock::ExecuteCommand should not be called, since
42 // the command is not supported.
43 command_updater.ExecuteCommand(0);
44
45 // Supported, enabled command
46 command_updater.UpdateCommandEnabled(1, true);
47 EXPECT_TRUE(command_updater.SupportsCommand(1));
48 EXPECT_TRUE(command_updater.IsCommandEnabled(1));
49 command_updater.ExecuteCommand(1);
50
51 // Supported, disabled command
52 command_updater.UpdateCommandEnabled(2, false);
53 EXPECT_TRUE(command_updater.SupportsCommand(2));
54 EXPECT_FALSE(command_updater.IsCommandEnabled(2));
55 // TestingCommandHandlerMock::ExecuteCommmand should not be called, since
56 // the command_updater is disabled
57 command_updater.ExecuteCommand(2);
58}
59
[email protected]131272f92009-01-22 22:55:0760TEST_F(CommandUpdaterTest, TestObservers) {
[email protected]1fc025202009-01-20 23:03:1461 TestingCommandHandlerMock handler;
62 CommandUpdater command_updater(&handler);
63
64 // Create an observer for the command 2 and add it to the controller, then
65 // update the command.
66 TestingCommandObserverMock observer;
67 command_updater.AddCommandObserver(2, &observer);
68 command_updater.UpdateCommandEnabled(2, true);
69 EXPECT_TRUE(observer.enabled());
70 command_updater.UpdateCommandEnabled(2, false);
71 EXPECT_FALSE(observer.enabled());
72
73 // Remove the observer and update the command.
74 command_updater.RemoveCommandObserver(2, &observer);
75 command_updater.UpdateCommandEnabled(2, true);
76 EXPECT_FALSE(observer.enabled());
77}