File: command_updater_unittest.cc

package info (click to toggle)
chromium-browser 57.0.2987.98-1~deb8u1
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 2,637,852 kB
  • ctags: 2,544,394
  • sloc: cpp: 12,815,961; ansic: 3,676,222; python: 1,147,112; asm: 526,608; java: 523,212; xml: 286,794; perl: 92,654; sh: 86,408; objc: 73,271; makefile: 27,698; cs: 18,487; yacc: 13,031; tcl: 12,957; pascal: 4,875; ml: 4,716; lex: 3,904; sql: 3,862; ruby: 1,982; lisp: 1,508; php: 1,368; exp: 404; awk: 325; csh: 117; jsp: 39; sed: 37
file content (107 lines) | stat: -rw-r--r-- 3,902 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// Copyright (c) 2012 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 "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:
  void ExecuteCommandWithDisposition(int id, WindowOpenDisposition) override {
    EXPECT_EQ(1, id);
  }
};

class FakeCommandObserver : public CommandObserver {
 public:
  FakeCommandObserver() : enabled_(true) {}

  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);

  // Unsupported command
  EXPECT_FALSE(command_updater.SupportsCommand(0));
  EXPECT_FALSE(command_updater.IsCommandEnabled(0));
  // FakeCommandUpdaterDelegate::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));
  // FakeCommandUpdaterDelegate::ExecuteCommmand should not be called, since
  // the command_updater is disabled
  command_updater.ExecuteCommand(2);
}

TEST(CommandUpdaterTest, TestObservers) {
  FakeCommandUpdaterDelegate delegate;
  CommandUpdater command_updater(&delegate);

  // Create an observer for the command 2 and add it to the controller, then
  // update the command.
  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());

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

TEST(CommandUpdaterTest, TestObserverRemovingAllCommands) {
  FakeCommandUpdaterDelegate delegate;
  CommandUpdater command_updater(&delegate);

  // 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.

  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());

  // 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());
}