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