<commit_before>// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/logging.h"
#include "chrome/browser/command_updater.h"
#include "testing/gtest/include/gtest/gtest.h"

class TestingCommandHandlerMock
    : public CommandUpdater::CommandUpdaterDelegate {
 public:
  virtual void ExecuteCommand(int id) {
    EXPECT_EQ(1, id);
  }
};

class CommandUpdaterTest : public testing::Test {
};

class TestingCommandObserverMock : public CommandUpdater::CommandObserver {
 public:
  TestingCommandObserverMock() : enabled_(true) {}

  virtual void EnabledStateChangedForCommand(int id, bool enabled) {
    enabled_ = enabled;
  }

  bool enabled() const { return enabled_; }

 private:
  bool enabled_;
};

TEST_F(CommandUpdaterTest, TestBasicAPI) {
  TestingCommandHandlerMock handler;
  CommandUpdater command_updater(&handler);

  // Unsupported command
  EXPECT_FALSE(command_updater.SupportsCommand(0));
  EXPECT_FALSE(command_updater.IsCommandEnabled(0));
  // TestingCommandHandlerMock::ExecuteCommand should not be called, since
  // the command is not supported.
  command_updater.ExecuteCommand(0);

  // Supported, enabled command
  command_updater.UpdateCommandEnabled(1, true);
  EXPECT_TRUE(command_updater.SupportsCommand(1));
  EXPECT_TRUE(command_updater.IsCommandEnabled(1));
  command_updater.ExecuteCommand(1);

  // Supported, disabled command
  command_updater.UpdateCommandEnabled(2, false);
  EXPECT_TRUE(command_updater.SupportsCommand(2));
  EXPECT_FALSE(command_updater.IsCommandEnabled(2));
  // TestingCommandHandlerMock::ExecuteCommmand should not be called, since
  // the command_updater is disabled
  command_updater.ExecuteCommand(2);
}

TEST_F(CommandUpdaterTest, TestObservers) {
  TestingCommandHandlerMock handler;
  CommandUpdater command_updater(&handler);

  // Create an observer for the command 2 and add it to the controller, then
  // update the command.
  TestingCommandObserverMock observer;
  command_updater.AddCommandObserver(2, &observer);
  command_updater.UpdateCommandEnabled(2, true);
  EXPECT_TRUE(observer.enabled());
  command_updater.UpdateCommandEnabled(2, false);
  EXPECT_FALSE(observer.enabled());

  // Remove the observer and update the command.
  command_updater.RemoveCommandObserver(2, &observer);
  command_updater.UpdateCommandEnabled(2, true);
  EXPECT_FALSE(observer.enabled());
}<commit_msg>Test the RemoveCommandObserver() routine that removes an observer from all its registered commands. Review URL: http://codereview.chromium.org/20180<commit_after>// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/logging.h"
#include "chrome/browser/command_updater.h"
#include "testing/gtest/include/gtest/gtest.h"

class TestingCommandHandlerMock
    : public CommandUpdater::CommandUpdaterDelegate {
 public:
  virtual void ExecuteCommand(int id) {
    EXPECT_EQ(1, id);
  }
};

class CommandUpdaterTest : public testing::Test {
};

class TestingCommandObserverMock : public CommandUpdater::CommandObserver {
 public:
  TestingCommandObserverMock() : enabled_(true) {}

  virtual void EnabledStateChangedForCommand(int id, bool enabled) {
    enabled_ = enabled;
  }

  bool enabled() const { return enabled_; }

 private:
  bool enabled_;
};

TEST_F(CommandUpdaterTest, TestBasicAPI) {
  TestingCommandHandlerMock handler;
  CommandUpdater command_updater(&handler);

  // Unsupported command
  EXPECT_FALSE(command_updater.SupportsCommand(0));
  EXPECT_FALSE(command_updater.IsCommandEnabled(0));
  // TestingCommandHandlerMock::ExecuteCommand should not be called, since
  // the command is not supported.
  command_updater.ExecuteCommand(0);

  // Supported, enabled command
  command_updater.UpdateCommandEnabled(1, true);
  EXPECT_TRUE(command_updater.SupportsCommand(1));
  EXPECT_TRUE(command_updater.IsCommandEnabled(1));
  command_updater.ExecuteCommand(1);

  // Supported, disabled command
  command_updater.UpdateCommandEnabled(2, false);
  EXPECT_TRUE(command_updater.SupportsCommand(2));
  EXPECT_FALSE(command_updater.IsCommandEnabled(2));
  // TestingCommandHandlerMock::ExecuteCommmand should not be called, since
  // the command_updater is disabled
  command_updater.ExecuteCommand(2);
}

TEST_F(CommandUpdaterTest, TestObservers) {
  TestingCommandHandlerMock handler;
  CommandUpdater command_updater(&handler);

  // Create an observer for the command 2 and add it to the controller, then
  // update the command.
  TestingCommandObserverMock observer;
  command_updater.AddCommandObserver(2, &observer);
  command_updater.UpdateCommandEnabled(2, true);
  EXPECT_TRUE(observer.enabled());
  command_updater.UpdateCommandEnabled(2, false);
  EXPECT_FALSE(observer.enabled());

  // Remove the observer and update the command.
  command_updater.RemoveCommandObserver(2, &observer);
  command_updater.UpdateCommandEnabled(2, true);
  EXPECT_FALSE(observer.enabled());
}

TEST_F(CommandUpdaterTest, TestObserverRemovingAllCommands) {
  TestingCommandHandlerMock handler;
  CommandUpdater command_updater(&handler);
  
  // Create two observers for the commands 1-3 as true, remove one using the
  // single remove command, then set the command to false. Ensure that the
  // removed observer still thinks all commands are true and the one left
  // observing picked up the change.
  
  TestingCommandObserverMock 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());

  // Remove one observer and update the command. Check the states, which
  // should be different.
  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());
}
{'fraction_non_alphanumeric': 0.09356814701378255, 'fraction_numerical': 0.010872894333843797, 'mean_word_length': 5.803125, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4543254', 'n_tokens_mistral': 1929, 'n_tokens_neox': 1944, 'n_words': 440}