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
5 #include "chrome/browser/status_icons/status_icon.h"
6
7 #include "base/compiler_specific.h"
8 #include "chrome/browser/status_icons/status_icon_observer.h"
9 #include "testing/gmock/include/gmock/gmock.h"
10 #include "testing/gtest/include/gtest/gtest.h"
11
12 class MockStatusIconObserver : public StatusIconObserver {
13 public:
14 MOCK_METHOD0(OnStatusIconClicked, void());
15 };
16
17 // Define pure virtual functions so we can test base class functionality.
18 class TestStatusIcon : public StatusIcon {
19 public:
TestStatusIcon()20 TestStatusIcon() {}
SetImage(const gfx::ImageSkia & image)21 virtual void SetImage(const gfx::ImageSkia& image) OVERRIDE {}
SetPressedImage(const gfx::ImageSkia & image)22 virtual void SetPressedImage(const gfx::ImageSkia& image) OVERRIDE {}
SetToolTip(const base::string16 & tool_tip)23 virtual void SetToolTip(const base::string16& tool_tip) OVERRIDE {}
UpdatePlatformContextMenu(StatusIconMenuModel * menu)24 virtual void UpdatePlatformContextMenu(
25 StatusIconMenuModel* menu) OVERRIDE {}
DisplayBalloon(const gfx::ImageSkia & icon,const base::string16 & title,const base::string16 & contents)26 virtual void DisplayBalloon(const gfx::ImageSkia& icon,
27 const base::string16& title,
28 const base::string16& contents) OVERRIDE {}
29 };
30
TEST(StatusIconTest,ObserverAdd)31 TEST(StatusIconTest, ObserverAdd) {
32 // Make sure that observers are invoked when we click items.
33 TestStatusIcon icon;
34 MockStatusIconObserver observer, observer2;
35 EXPECT_CALL(observer, OnStatusIconClicked()).Times(2);
36 EXPECT_CALL(observer2, OnStatusIconClicked());
37 icon.AddObserver(&observer);
38 icon.DispatchClickEvent();
39 icon.AddObserver(&observer2);
40 icon.DispatchClickEvent();
41 icon.RemoveObserver(&observer);
42 icon.RemoveObserver(&observer2);
43 }
44
TEST(StatusIconTest,ObserverRemove)45 TEST(StatusIconTest, ObserverRemove) {
46 // Make sure that observers are no longer invoked after they are removed.
47 TestStatusIcon icon;
48 MockStatusIconObserver observer;
49 EXPECT_CALL(observer, OnStatusIconClicked());
50 icon.AddObserver(&observer);
51 icon.DispatchClickEvent();
52 icon.RemoveObserver(&observer);
53 icon.DispatchClickEvent();
54 }
55