Commit 8a7923fa authored by tfarina@chromium.org's avatar tfarina@chromium.org

status_icons: Factor out status icon observer into its own header file.

R=atwilson@chromium.org
TBR=pkasting@chromium.org

Review URL: https://chromiumcodereview.appspot.com/10408039

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@138473 0039d316-1c4b-4281-b951-d872f2087c98
parent 0b229356
// Copyright (c) 2011 The Chromium Authors. All rights reserved. // Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
#include "chrome/browser/status_icons/status_icon.h" #include "chrome/browser/status_icons/status_icon.h"
#include "chrome/browser/status_icons/status_icon_observer.h"
#include "ui/base/models/menu_model.h" #include "ui/base/models/menu_model.h"
StatusIcon::StatusIcon() { StatusIcon::StatusIcon() {
...@@ -12,20 +13,20 @@ StatusIcon::StatusIcon() { ...@@ -12,20 +13,20 @@ StatusIcon::StatusIcon() {
StatusIcon::~StatusIcon() { StatusIcon::~StatusIcon() {
} }
void StatusIcon::AddObserver(Observer* observer) { void StatusIcon::AddObserver(StatusIconObserver* observer) {
observers_.AddObserver(observer); observers_.AddObserver(observer);
} }
void StatusIcon::RemoveObserver(Observer* observer) { void StatusIcon::RemoveObserver(StatusIconObserver* observer) {
observers_.RemoveObserver(observer); observers_.RemoveObserver(observer);
} }
bool StatusIcon::HasObservers() { bool StatusIcon::HasObservers() const {
return observers_.size() > 0; return observers_.size() > 0;
} }
void StatusIcon::DispatchClickEvent() { void StatusIcon::DispatchClickEvent() {
FOR_EACH_OBSERVER(Observer, observers_, OnClicked()); FOR_EACH_OBSERVER(StatusIconObserver, observers_, OnStatusIconClicked());
} }
void StatusIcon::SetContextMenu(ui::MenuModel* menu) { void StatusIcon::SetContextMenu(ui::MenuModel* menu) {
......
// Copyright (c) 2011 The Chromium Authors. All rights reserved. // Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
#define CHROME_BROWSER_STATUS_ICONS_STATUS_ICON_H_ #define CHROME_BROWSER_STATUS_ICONS_STATUS_ICON_H_
#pragma once #pragma once
#include "base/basictypes.h"
#include "base/memory/scoped_ptr.h" #include "base/memory/scoped_ptr.h"
#include "base/observer_list.h" #include "base/observer_list.h"
#include "base/string16.h" #include "base/string16.h"
...@@ -16,6 +17,8 @@ namespace ui { ...@@ -16,6 +17,8 @@ namespace ui {
class MenuModel; class MenuModel;
} }
class StatusIconObserver;
class StatusIcon { class StatusIcon {
public: public:
StatusIcon(); StatusIcon();
...@@ -40,29 +43,15 @@ class StatusIcon { ...@@ -40,29 +43,15 @@ class StatusIcon {
// context menu. Passing NULL results in no menu at all. // context menu. Passing NULL results in no menu at all.
void SetContextMenu(ui::MenuModel* menu); void SetContextMenu(ui::MenuModel* menu);
class Observer {
public:
virtual ~Observer() {}
// Called when the user clicks on the system tray icon. Clicks that result
// in the context menu being displayed will not be passed to this observer
// (i.e. if there's a context menu set on this status icon, and the user
// right clicks on the icon to display the context menu, OnClicked will not
// be called).
// Note: Chrome OS displays the context menu on left button clicks.
// This will only be fired for this platform if no context menu is present.
virtual void OnClicked() = 0;
};
// Adds/Removes an observer for clicks on the status icon. If an observer is // Adds/Removes an observer for clicks on the status icon. If an observer is
// registered, then left clicks on the status icon will result in the observer // registered, then left clicks on the status icon will result in the observer
// being called, otherwise, both left and right clicks will display the // being called, otherwise, both left and right clicks will display the
// context menu (if any). // context menu (if any).
void AddObserver(Observer* observer); void AddObserver(StatusIconObserver* observer);
void RemoveObserver(Observer* observer); void RemoveObserver(StatusIconObserver* observer);
// Returns true if there are registered click observers. // Returns true if there are registered click observers.
bool HasObservers(); bool HasObservers() const;
// Dispatches a click event to the observers. // Dispatches a click event to the observers.
void DispatchClickEvent(); void DispatchClickEvent();
...@@ -74,9 +63,11 @@ class StatusIcon { ...@@ -74,9 +63,11 @@ class StatusIcon {
virtual void UpdatePlatformContextMenu(ui::MenuModel* model) = 0; virtual void UpdatePlatformContextMenu(ui::MenuModel* model) = 0;
private: private:
ObserverList<Observer> observers_; ObserverList<StatusIconObserver> observers_;
// Context menu, if any. // Context menu, if any.
scoped_ptr<ui::MenuModel> context_menu_contents_; scoped_ptr<ui::MenuModel> context_menu_contents_;
DISALLOW_COPY_AND_ASSIGN(StatusIcon); DISALLOW_COPY_AND_ASSIGN(StatusIcon);
}; };
......
// 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.
#ifndef CHROME_BROWSER_STATUS_ICONS_STATUS_ICON_OBSERVER_H_
#define CHROME_BROWSER_STATUS_ICONS_STATUS_ICON_OBSERVER_H_
#pragma once
class StatusIconObserver {
public:
// Called when the user clicks on the system tray icon. Clicks that result
// in the context menu being displayed will not be passed to this observer
// (i.e. if there's a context menu set on this status icon, and the user
// right clicks on the icon to display the context menu, OnStatusIconClicked()
// will not be called).
// Note: Chrome OS displays the context menu on left button clicks.
// This will only be fired for this platform if no context menu is present.
virtual void OnStatusIconClicked() = 0;
protected:
virtual ~StatusIconObserver() {}
};
#endif // CHROME_BROWSER_STATUS_ICONS_STATUS_ICON_OBSERVER_H_
// Copyright (c) 2011 The Chromium Authors. All rights reserved. // Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
#include "base/compiler_specific.h"
#include "chrome/browser/status_icons/status_icon.h" #include "chrome/browser/status_icons/status_icon.h"
#include "base/compiler_specific.h"
#include "chrome/browser/status_icons/status_icon_observer.h"
#include "testing/gmock/include/gmock/gmock.h" #include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h" #include "testing/gtest/include/gtest/gtest.h"
class MockStatusIconObserver : public StatusIcon::Observer { class MockStatusIconObserver : public StatusIconObserver {
public: public:
MOCK_METHOD0(OnClicked, void()); MOCK_METHOD0(OnStatusIconClicked, void());
}; };
// Define pure virtual functions so we can test base class functionality. // Define pure virtual functions so we can test base class functionality.
...@@ -29,8 +31,8 @@ TEST(StatusIconTest, ObserverAdd) { ...@@ -29,8 +31,8 @@ TEST(StatusIconTest, ObserverAdd) {
// Make sure that observers are invoked when we click items. // Make sure that observers are invoked when we click items.
TestStatusIcon icon; TestStatusIcon icon;
MockStatusIconObserver observer, observer2; MockStatusIconObserver observer, observer2;
EXPECT_CALL(observer, OnClicked()).Times(2); EXPECT_CALL(observer, OnStatusIconClicked()).Times(2);
EXPECT_CALL(observer2, OnClicked()); EXPECT_CALL(observer2, OnStatusIconClicked());
icon.AddObserver(&observer); icon.AddObserver(&observer);
icon.DispatchClickEvent(); icon.DispatchClickEvent();
icon.AddObserver(&observer2); icon.AddObserver(&observer2);
...@@ -43,7 +45,7 @@ TEST(StatusIconTest, ObserverRemove) { ...@@ -43,7 +45,7 @@ TEST(StatusIconTest, ObserverRemove) {
// Make sure that observers are no longer invoked after they are removed. // Make sure that observers are no longer invoked after they are removed.
TestStatusIcon icon; TestStatusIcon icon;
MockStatusIconObserver observer; MockStatusIconObserver observer;
EXPECT_CALL(observer, OnClicked()); EXPECT_CALL(observer, OnStatusIconClicked());
icon.AddObserver(&observer); icon.AddObserver(&observer);
icon.DispatchClickEvent(); icon.DispatchClickEvent();
icon.RemoveObserver(&observer); icon.RemoveObserver(&observer);
......
// Copyright (c) 2011 The Chromium Authors. All rights reserved. // Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
#include "chrome/browser/ui/gtk/status_icons/status_tray_gtk.h"
#include "base/string_util.h" #include "base/string_util.h"
#include "base/utf_string_conversions.h" #include "base/utf_string_conversions.h"
#include "chrome/browser/status_icons/status_icon_observer.h"
#include "chrome/browser/ui/gtk/status_icons/status_icon_gtk.h" #include "chrome/browser/ui/gtk/status_icons/status_icon_gtk.h"
#include "chrome/browser/ui/gtk/status_icons/status_tray_gtk.h"
#include "grit/theme_resources.h" #include "grit/theme_resources.h"
#include "testing/gmock/include/gmock/gmock.h" #include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h" #include "testing/gtest/include/gtest/gtest.h"
...@@ -14,9 +16,9 @@ ...@@ -14,9 +16,9 @@
namespace { namespace {
class MockStatusIconObserver : public StatusIcon::Observer { class MockStatusIconObserver : public StatusIconObserver {
public: public:
MOCK_METHOD0(OnClicked, void()); MOCK_METHOD0(OnStatusIconClicked, void());
}; };
TEST(StatusTrayGtkTest, CreateTray) { TEST(StatusTrayGtkTest, CreateTray) {
...@@ -44,7 +46,7 @@ TEST(StatusTrayGtkTest, ClickOnIcon) { ...@@ -44,7 +46,7 @@ TEST(StatusTrayGtkTest, ClickOnIcon) {
StatusIconGtk* icon = static_cast<StatusIconGtk*>(tray.CreateStatusIcon()); StatusIconGtk* icon = static_cast<StatusIconGtk*>(tray.CreateStatusIcon());
MockStatusIconObserver observer; MockStatusIconObserver observer;
icon->AddObserver(&observer); icon->AddObserver(&observer);
EXPECT_CALL(observer, OnClicked()); EXPECT_CALL(observer, OnStatusIconClicked());
// Mimic a click. // Mimic a click.
icon->OnClick(NULL); icon->OnClick(NULL);
icon->RemoveObserver(&observer); icon->RemoveObserver(&observer);
......
...@@ -2,10 +2,12 @@ ...@@ -2,10 +2,12 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
#include "chrome/browser/ui/views/status_icons/status_tray_win.h"
#include "base/string_util.h" #include "base/string_util.h"
#include "base/utf_string_conversions.h" #include "base/utf_string_conversions.h"
#include "chrome/browser/status_icons/status_icon_observer.h"
#include "chrome/browser/ui/views/status_icons/status_icon_win.h" #include "chrome/browser/ui/views/status_icons/status_icon_win.h"
#include "chrome/browser/ui/views/status_icons/status_tray_win.h"
#include "grit/theme_resources.h" #include "grit/theme_resources.h"
#include "testing/gmock/include/gmock/gmock.h" #include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h" #include "testing/gtest/include/gtest/gtest.h"
...@@ -14,9 +16,9 @@ ...@@ -14,9 +16,9 @@
class SkBitmap; class SkBitmap;
class MockStatusIconObserver : public StatusIcon::Observer { class MockStatusIconObserver : public StatusIconObserver {
public: public:
MOCK_METHOD0(OnClicked, void()); MOCK_METHOD0(OnStatusIconClicked, void());
}; };
TEST(StatusTrayWinTest, CreateTray) { TEST(StatusTrayWinTest, CreateTray) {
...@@ -45,7 +47,7 @@ TEST(StatusTrayWinTest, ClickOnIcon) { ...@@ -45,7 +47,7 @@ TEST(StatusTrayWinTest, ClickOnIcon) {
StatusIconWin* icon = static_cast<StatusIconWin*>(tray.CreateStatusIcon()); StatusIconWin* icon = static_cast<StatusIconWin*>(tray.CreateStatusIcon());
MockStatusIconObserver observer; MockStatusIconObserver observer;
icon->AddObserver(&observer); icon->AddObserver(&observer);
EXPECT_CALL(observer, OnClicked()); EXPECT_CALL(observer, OnStatusIconClicked());
// Mimic a click. // Mimic a click.
tray.WndProc(NULL, icon->message_id(), icon->icon_id(), WM_LBUTTONDOWN); tray.WndProc(NULL, icon->message_id(), icon->icon_id(), WM_LBUTTONDOWN);
// Mimic a right-click - observer should not be called. // Mimic a right-click - observer should not be called.
......
...@@ -2056,6 +2056,7 @@ ...@@ -2056,6 +2056,7 @@
'browser/status_icons/desktop_notification_balloon.h', 'browser/status_icons/desktop_notification_balloon.h',
'browser/status_icons/status_icon.cc', 'browser/status_icons/status_icon.cc',
'browser/status_icons/status_icon.h', 'browser/status_icons/status_icon.h',
'browser/status_icons/status_icon_observer.h',
'browser/status_icons/status_tray.cc', 'browser/status_icons/status_tray.cc',
'browser/status_icons/status_tray.h', 'browser/status_icons/status_tray.h',
'browser/sync/glue/app_notification_data_type_controller.cc', 'browser/sync/glue/app_notification_data_type_controller.cc',
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment