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.
5 #include "chrome/browser/ui/views/status_icons/status_tray_win.h"
9 #include "base/strings/string_util.h"
10 #include "base/strings/utf_string_conversions.h"
11 #include "chrome/browser/status_icons/status_icon_menu_model.h"
12 #include "chrome/browser/status_icons/status_icon_observer.h"
13 #include "chrome/browser/ui/views/status_icons/status_icon_win.h"
14 #include "grit/chrome_unscaled_resources.h"
15 #include "testing/gtest/include/gtest/gtest.h"
16 #include "ui/base/resource/resource_bundle.h"
17 #include "ui/gfx/image/image_skia.h"
19 class FakeStatusTrayStateChangerProxy
: public StatusTrayStateChangerProxy
{
21 FakeStatusTrayStateChangerProxy()
22 : enqueue_called_(false), icon_id_(0), window_(NULL
) {}
24 virtual void EnqueueChange(UINT icon_id
, HWND window
) {
25 enqueue_called_
= true;
30 bool enqueue_called() { return enqueue_called_
; }
31 UINT
icon_id() { return icon_id_
; }
32 HWND
window() { return window_
; }
39 DISALLOW_COPY_AND_ASSIGN(FakeStatusTrayStateChangerProxy
);
42 class FakeStatusIconObserver
: public StatusIconObserver
{
44 FakeStatusIconObserver()
45 : balloon_clicked_(false),
46 status_icon_click_count_(0) {}
47 virtual void OnStatusIconClicked() {
48 ++status_icon_click_count_
;
50 virtual void OnBalloonClicked() { balloon_clicked_
= true; }
51 bool balloon_clicked() const { return balloon_clicked_
; }
52 size_t status_icon_click_count() const {
53 return status_icon_click_count_
;
57 size_t status_icon_click_count_
;
58 bool balloon_clicked_
;
61 TEST(StatusTrayWinTest
, CreateTray
) {
62 // Just tests creation/destruction.
66 TEST(StatusTrayWinTest
, CreateIconAndMenu
) {
67 // Create an icon, set the images, tooltip, and context menu, then shut it
70 ui::ResourceBundle
& rb
= ui::ResourceBundle::GetSharedInstance();
71 gfx::ImageSkia
* image
= rb
.GetImageSkiaNamed(IDR_STATUS_TRAY_ICON
);
72 StatusIcon
* icon
= tray
.CreateStatusIcon(
73 StatusTray::OTHER_ICON
, *image
, base::ASCIIToUTF16("tool tip"));
74 scoped_ptr
<StatusIconMenuModel
> menu(new StatusIconMenuModel(NULL
));
75 menu
->AddItem(0, L
"foo");
76 icon
->SetContextMenu(menu
.Pass());
79 #if !defined(USE_AURA) // http://crbug.com/156370
80 TEST(StatusTrayWinTest
, ClickOnIcon
) {
81 // Create an icon, send a fake click event, make sure observer is called.
83 ui::ResourceBundle
& rb
= ui::ResourceBundle::GetSharedInstance();
84 gfx::ImageSkia
* image
= rb
.GetImageSkiaNamed(IDR_STATUS_TRAY_ICON
);
86 StatusIconWin
* icon
= static_cast<StatusIconWin
*>(tray
.CreateStatusIcon(
87 StatusTray::OTHER_ICON
, *image
, base::ASCIIToUTF16("tool tip")));
88 FakeStatusIconObserver observer
;
89 icon
->AddObserver(&observer
);
91 tray
.WndProc(NULL
, icon
->message_id(), icon
->icon_id(), WM_LBUTTONDOWN
);
92 // Mimic a right-click - observer should not be called.
93 tray
.WndProc(NULL
, icon
->message_id(), icon
->icon_id(), WM_RBUTTONDOWN
);
94 EXPECT_EQ(1, observer
.status_icon_click_count());
95 icon
->RemoveObserver(&observer
);
98 TEST(StatusTrayWinTest
, ClickOnBalloon
) {
99 // Create an icon, send a fake click event, make sure observer is called.
101 ui::ResourceBundle
& rb
= ui::ResourceBundle::GetSharedInstance();
102 gfx::ImageSkia
* image
= rb
.GetImageSkiaNamed(IDR_STATUS_TRAY_ICON
);
104 StatusIconWin
* icon
= static_cast<StatusIconWin
*>(tray
.CreateStatusIcon(
105 StatusTray::OTHER_ICON
, *image
, base::ASCIIToUTF16("tool tip")));
106 FakeStatusIconObserver observer
;
107 icon
->AddObserver(&observer
);
110 NULL
, icon
->message_id(), icon
->icon_id(), TB_INDETERMINATE
);
111 EXPECT_TRUE(observer
.balloon_clicked());
112 icon
->RemoveObserver(&observer
);
115 TEST(StatusTrayWinTest
, HandleOldIconId
) {
117 ui::ResourceBundle
& rb
= ui::ResourceBundle::GetSharedInstance();
118 gfx::ImageSkia
* image
= rb
.GetImageSkiaNamed(IDR_STATUS_TRAY_ICON
);
120 StatusIconWin
* icon
= static_cast<StatusIconWin
*>(tray
.CreateStatusIcon(
121 StatusTray::OTHER_ICON
, *image
, base::ASCIIToUTF16("tool tip")));
122 UINT message_id
= icon
->message_id();
123 UINT icon_id
= icon
->icon_id();
125 tray
.RemoveStatusIcon(icon
);
126 tray
.WndProc(NULL
, message_id
, icon_id
, WM_LBUTTONDOWN
);
128 #endif // !defined(USE_AURA)
130 TEST(StatusTrayWinTest
, EnsureVisibleTest
) {
132 ui::ResourceBundle
& rb
= ui::ResourceBundle::GetSharedInstance();
133 gfx::ImageSkia
* image
= rb
.GetImageSkiaNamed(IDR_STATUS_TRAY_ICON
);
135 FakeStatusTrayStateChangerProxy
* proxy
=
136 new FakeStatusTrayStateChangerProxy();
137 tray
.SetStatusTrayStateChangerProxyForTest(
138 scoped_ptr
<StatusTrayStateChangerProxy
>(proxy
));
140 StatusIconWin
* icon
= static_cast<StatusIconWin
*>(tray
.CreateStatusIcon(
141 StatusTray::OTHER_ICON
, *image
, base::ASCIIToUTF16("tool tip")));
143 icon
->ForceVisible();
144 // |proxy| is owned by |tray|, and |tray| lives to the end of the scope,
145 // so calling methods on |proxy| is safe.
146 EXPECT_TRUE(proxy
->enqueue_called());
147 EXPECT_EQ(proxy
->window(), icon
->window());
148 EXPECT_EQ(proxy
->icon_id(), icon
->icon_id());