command_updater_impl_unittest.cc 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. // Copyright (c) 2012 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. #include "chrome/browser/command_updater_impl.h"
  5. #include "base/compiler_specific.h"
  6. #include "chrome/browser/command_observer.h"
  7. #include "chrome/browser/command_updater_delegate.h"
  8. #include "testing/gtest/include/gtest/gtest.h"
  9. class FakeCommandUpdaterDelegate : public CommandUpdaterDelegate {
  10. public:
  11. void ExecuteCommandWithDisposition(int id, WindowOpenDisposition) override {
  12. EXPECT_EQ(1, id);
  13. }
  14. };
  15. class FakeCommandObserver : public CommandObserver {
  16. public:
  17. FakeCommandObserver() : enabled_(true) {}
  18. void EnabledStateChangedForCommand(int id, bool enabled) override {
  19. enabled_ = enabled;
  20. }
  21. bool enabled() const { return enabled_; }
  22. private:
  23. bool enabled_;
  24. };
  25. TEST(CommandUpdaterImplTest, TestBasicAPI) {
  26. FakeCommandUpdaterDelegate delegate;
  27. CommandUpdaterImpl command_updater(&delegate);
  28. // Unsupported command
  29. EXPECT_FALSE(command_updater.SupportsCommand(0));
  30. EXPECT_FALSE(command_updater.IsCommandEnabled(0));
  31. // FakeCommandUpdaterDelegate::ExecuteCommand should not be called, since
  32. // the command is not supported.
  33. command_updater.ExecuteCommand(0);
  34. // Supported, enabled command
  35. command_updater.UpdateCommandEnabled(1, true);
  36. EXPECT_TRUE(command_updater.SupportsCommand(1));
  37. EXPECT_TRUE(command_updater.IsCommandEnabled(1));
  38. command_updater.ExecuteCommand(1);
  39. // Supported, disabled command
  40. command_updater.UpdateCommandEnabled(2, false);
  41. EXPECT_TRUE(command_updater.SupportsCommand(2));
  42. EXPECT_FALSE(command_updater.IsCommandEnabled(2));
  43. // FakeCommandUpdaterDelegate::ExecuteCommmand should not be called, since
  44. // the command_updater is disabled
  45. command_updater.ExecuteCommand(2);
  46. }
  47. TEST(CommandUpdaterImplTest, TestObservers) {
  48. FakeCommandUpdaterDelegate delegate;
  49. CommandUpdaterImpl command_updater(&delegate);
  50. // Create an observer for the command 2 and add it to the controller, then
  51. // update the command.
  52. FakeCommandObserver observer;
  53. command_updater.AddCommandObserver(2, &observer);
  54. command_updater.UpdateCommandEnabled(2, true);
  55. EXPECT_TRUE(observer.enabled());
  56. command_updater.UpdateCommandEnabled(2, false);
  57. EXPECT_FALSE(observer.enabled());
  58. // Remove the observer and update the command.
  59. command_updater.RemoveCommandObserver(2, &observer);
  60. command_updater.UpdateCommandEnabled(2, true);
  61. EXPECT_FALSE(observer.enabled());
  62. }
  63. TEST(CommandUpdaterImplTest, TestObserverRemovingAllCommands) {
  64. FakeCommandUpdaterDelegate delegate;
  65. CommandUpdaterImpl command_updater(&delegate);
  66. // Create two observers for the commands 1-3 as true, remove one using the
  67. // single remove command, then set the command to false. Ensure that the
  68. // removed observer still thinks all commands are true and the one left
  69. // observing picked up the change.
  70. FakeCommandObserver observer_remove, observer_keep;
  71. command_updater.AddCommandObserver(1, &observer_remove);
  72. command_updater.AddCommandObserver(2, &observer_remove);
  73. command_updater.AddCommandObserver(3, &observer_remove);
  74. command_updater.AddCommandObserver(1, &observer_keep);
  75. command_updater.AddCommandObserver(2, &observer_keep);
  76. command_updater.AddCommandObserver(3, &observer_keep);
  77. command_updater.UpdateCommandEnabled(1, true);
  78. command_updater.UpdateCommandEnabled(2, true);
  79. command_updater.UpdateCommandEnabled(3, true);
  80. EXPECT_TRUE(observer_remove.enabled());
  81. // Remove one observer and update the command. Check the states, which
  82. // should be different.
  83. command_updater.RemoveCommandObserver(&observer_remove);
  84. command_updater.UpdateCommandEnabled(1, false);
  85. command_updater.UpdateCommandEnabled(2, false);
  86. command_updater.UpdateCommandEnabled(3, false);
  87. EXPECT_TRUE(observer_remove.enabled());
  88. EXPECT_FALSE(observer_keep.enabled());
  89. }