This source file includes following definitions.
- ExecuteCommandWithDisposition
- EnabledStateChangedForCommand
- enabled
- TEST
- TEST
- TEST
#include "chrome/browser/command_updater.h"
#include "base/compiler_specific.h"
#include "chrome/browser/command_observer.h"
#include "chrome/browser/command_updater_delegate.h"
#include "testing/gtest/include/gtest/gtest.h"
class FakeCommandUpdaterDelegate : public CommandUpdaterDelegate {
public:
virtual void ExecuteCommandWithDisposition(int id,
WindowOpenDisposition) OVERRIDE {
EXPECT_EQ(1, id);
}
};
class FakeCommandObserver : public CommandObserver {
public:
FakeCommandObserver() : enabled_(true) {}
virtual void EnabledStateChangedForCommand(int id, bool enabled) OVERRIDE {
enabled_ = enabled;
}
bool enabled() const { return enabled_; }
private:
bool enabled_;
};
TEST(CommandUpdaterTest, TestBasicAPI) {
FakeCommandUpdaterDelegate delegate;
CommandUpdater command_updater(&delegate);
EXPECT_FALSE(command_updater.SupportsCommand(0));
EXPECT_FALSE(command_updater.IsCommandEnabled(0));
command_updater.ExecuteCommand(0);
command_updater.UpdateCommandEnabled(1, true);
EXPECT_TRUE(command_updater.SupportsCommand(1));
EXPECT_TRUE(command_updater.IsCommandEnabled(1));
command_updater.ExecuteCommand(1);
command_updater.UpdateCommandEnabled(2, false);
EXPECT_TRUE(command_updater.SupportsCommand(2));
EXPECT_FALSE(command_updater.IsCommandEnabled(2));
command_updater.ExecuteCommand(2);
}
TEST(CommandUpdaterTest, TestObservers) {
FakeCommandUpdaterDelegate delegate;
CommandUpdater command_updater(&delegate);
FakeCommandObserver observer;
command_updater.AddCommandObserver(2, &observer);
command_updater.UpdateCommandEnabled(2, true);
EXPECT_TRUE(observer.enabled());
command_updater.UpdateCommandEnabled(2, false);
EXPECT_FALSE(observer.enabled());
command_updater.RemoveCommandObserver(2, &observer);
command_updater.UpdateCommandEnabled(2, true);
EXPECT_FALSE(observer.enabled());
}
TEST(CommandUpdaterTest, TestObserverRemovingAllCommands) {
FakeCommandUpdaterDelegate delegate;
CommandUpdater command_updater(&delegate);
FakeCommandObserver observer_remove, observer_keep;
command_updater.AddCommandObserver(1, &observer_remove);
command_updater.AddCommandObserver(2, &observer_remove);
command_updater.AddCommandObserver(3, &observer_remove);
command_updater.AddCommandObserver(1, &observer_keep);
command_updater.AddCommandObserver(2, &observer_keep);
command_updater.AddCommandObserver(3, &observer_keep);
command_updater.UpdateCommandEnabled(1, true);
command_updater.UpdateCommandEnabled(2, true);
command_updater.UpdateCommandEnabled(3, true);
EXPECT_TRUE(observer_remove.enabled());
command_updater.RemoveCommandObserver(&observer_remove);
command_updater.UpdateCommandEnabled(1, false);
command_updater.UpdateCommandEnabled(2, false);
command_updater.UpdateCommandEnabled(3, false);
EXPECT_TRUE(observer_remove.enabled());
EXPECT_FALSE(observer_keep.enabled());
}