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 void EnqueueChange(UINT icon_id
, HWND window
) override
{
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 : status_icon_click_count_(0), balloon_clicked_(false) {}
46 void OnStatusIconClicked() override
{ ++status_icon_click_count_
; }
47 void OnBalloonClicked() override
{ balloon_clicked_
= true; }
48 bool balloon_clicked() const { return balloon_clicked_
; }
49 size_t status_icon_click_count() const {
50 return status_icon_click_count_
;
54 size_t status_icon_click_count_
;
55 bool balloon_clicked_
;
58 TEST(StatusTrayWinTest
, CreateTray
) {
59 // Just tests creation/destruction.
63 TEST(StatusTrayWinTest
, CreateIconAndMenu
) {
64 // Create an icon, set the images, tooltip, and context menu, then shut it
67 ui::ResourceBundle
& rb
= ui::ResourceBundle::GetSharedInstance();
68 gfx::ImageSkia
* image
= rb
.GetImageSkiaNamed(IDR_STATUS_TRAY_ICON
);
69 StatusIcon
* icon
= tray
.CreateStatusIcon(
70 StatusTray::OTHER_ICON
, *image
, base::ASCIIToUTF16("tool tip"));
71 scoped_ptr
<StatusIconMenuModel
> menu(new StatusIconMenuModel(NULL
));
72 menu
->AddItem(0, L
"foo");
73 icon
->SetContextMenu(menu
.Pass());
76 #if !defined(USE_AURA) // http://crbug.com/156370
77 TEST(StatusTrayWinTest
, ClickOnIcon
) {
78 // Create an icon, send a fake click event, make sure observer is called.
80 ui::ResourceBundle
& rb
= ui::ResourceBundle::GetSharedInstance();
81 gfx::ImageSkia
* image
= rb
.GetImageSkiaNamed(IDR_STATUS_TRAY_ICON
);
83 StatusIconWin
* icon
= static_cast<StatusIconWin
*>(tray
.CreateStatusIcon(
84 StatusTray::OTHER_ICON
, *image
, base::ASCIIToUTF16("tool tip")));
85 FakeStatusIconObserver observer
;
86 icon
->AddObserver(&observer
);
88 tray
.WndProc(NULL
, icon
->message_id(), icon
->icon_id(), WM_LBUTTONDOWN
);
89 // Mimic a right-click - observer should not be called.
90 tray
.WndProc(NULL
, icon
->message_id(), icon
->icon_id(), WM_RBUTTONDOWN
);
91 EXPECT_EQ(1, observer
.status_icon_click_count());
92 icon
->RemoveObserver(&observer
);
95 TEST(StatusTrayWinTest
, ClickOnBalloon
) {
96 // Create an icon, send a fake click event, make sure observer is called.
98 ui::ResourceBundle
& rb
= ui::ResourceBundle::GetSharedInstance();
99 gfx::ImageSkia
* image
= rb
.GetImageSkiaNamed(IDR_STATUS_TRAY_ICON
);
101 StatusIconWin
* icon
= static_cast<StatusIconWin
*>(tray
.CreateStatusIcon(
102 StatusTray::OTHER_ICON
, *image
, base::ASCIIToUTF16("tool tip")));
103 FakeStatusIconObserver observer
;
104 icon
->AddObserver(&observer
);
107 NULL
, icon
->message_id(), icon
->icon_id(), TB_INDETERMINATE
);
108 EXPECT_TRUE(observer
.balloon_clicked());
109 icon
->RemoveObserver(&observer
);
112 TEST(StatusTrayWinTest
, HandleOldIconId
) {
114 ui::ResourceBundle
& rb
= ui::ResourceBundle::GetSharedInstance();
115 gfx::ImageSkia
* image
= rb
.GetImageSkiaNamed(IDR_STATUS_TRAY_ICON
);
117 StatusIconWin
* icon
= static_cast<StatusIconWin
*>(tray
.CreateStatusIcon(
118 StatusTray::OTHER_ICON
, *image
, base::ASCIIToUTF16("tool tip")));
119 UINT message_id
= icon
->message_id();
120 UINT icon_id
= icon
->icon_id();
122 tray
.RemoveStatusIcon(icon
);
123 tray
.WndProc(NULL
, message_id
, icon_id
, WM_LBUTTONDOWN
);
125 #endif // !defined(USE_AURA)
127 TEST(StatusTrayWinTest
, EnsureVisibleTest
) {
129 ui::ResourceBundle
& rb
= ui::ResourceBundle::GetSharedInstance();
130 gfx::ImageSkia
* image
= rb
.GetImageSkiaNamed(IDR_STATUS_TRAY_ICON
);
132 FakeStatusTrayStateChangerProxy
* proxy
=
133 new FakeStatusTrayStateChangerProxy();
134 tray
.SetStatusTrayStateChangerProxyForTest(
135 scoped_ptr
<StatusTrayStateChangerProxy
>(proxy
));
137 StatusIconWin
* icon
= static_cast<StatusIconWin
*>(tray
.CreateStatusIcon(
138 StatusTray::OTHER_ICON
, *image
, base::ASCIIToUTF16("tool tip")));
140 icon
->ForceVisible();
141 // |proxy| is owned by |tray|, and |tray| lives to the end of the scope,
142 // so calling methods on |proxy| is safe.
143 EXPECT_TRUE(proxy
->enqueue_called());
144 EXPECT_EQ(proxy
->window(), icon
->window());
145 EXPECT_EQ(proxy
->icon_id(), icon
->icon_id());