1 // Copyright 2012 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/command_updater_impl.h"
7 #include "base/compiler_specific.h"
8 #include "chrome/browser/command_observer.h"
9 #include "chrome/browser/command_updater_delegate.h"
10 #include "testing/gtest/include/gtest/gtest.h"
12 class FakeCommandUpdaterDelegate : public CommandUpdaterDelegate {
14 void ExecuteCommandWithDisposition(int id, WindowOpenDisposition) override {
19 class FakeCommandObserver : public CommandObserver {
21 FakeCommandObserver() : enabled_(true) {}
23 void EnabledStateChangedForCommand(int id, bool enabled) override {
27 bool enabled() const { return enabled_; }
33 TEST(CommandUpdaterImplTest, TestBasicAPI) {
34 FakeCommandUpdaterDelegate delegate;
35 CommandUpdaterImpl command_updater(&delegate);
37 // Unsupported command
38 EXPECT_FALSE(command_updater.SupportsCommand(0));
39 EXPECT_FALSE(command_updater.IsCommandEnabled(0));
40 // FakeCommandUpdaterDelegate::ExecuteCommand should not be called, since
41 // the command is not supported.
42 command_updater.ExecuteCommand(0);
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);
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 // FakeCommandUpdaterDelegate::ExecuteCommmand should not be called, since
55 // the command_updater is disabled
56 command_updater.ExecuteCommand(2);
59 TEST(CommandUpdaterImplTest, TestObservers) {
60 FakeCommandUpdaterDelegate delegate;
61 CommandUpdaterImpl command_updater(&delegate);
63 // Create an observer for the command 2 and add it to the controller, then
64 // update the command.
65 FakeCommandObserver 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());
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());
78 TEST(CommandUpdaterImplTest, TestObserverRemovingAllCommands) {
79 FakeCommandUpdaterDelegate delegate;
80 CommandUpdaterImpl command_updater(&delegate);
82 // 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.
87 FakeCommandObserver 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());
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());