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