1 // Copyright 2014 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_state_changer_win.h"
7 #include "base/memory/scoped_ptr.h"
8 #include "base/win/scoped_com_initializer.h"
9 #include "base/win/windows_version.h"
10 #include "chrome/browser/status_icons/status_icon.h"
11 #include "chrome/browser/ui/views/status_icons/status_icon_win.h"
12 #include "chrome/browser/ui/views/status_icons/status_tray_win.h"
13 #include "testing/gtest/include/gtest/gtest.h"
14 #include "third_party/skia/include/core/SkBitmap.h"
15 #include "ui/gfx/image/image_skia.h"
17 class StatusTrayStateChangerWinTest : public testing::Test {
19 StatusTrayStateChangerWinTest() {}
21 void SetUp() OVERRIDE {
22 testing::Test::SetUp();
23 com_.reset(new base::win::ScopedCOMInitializer());
24 status_tray_.reset(new StatusTrayWin());
27 // Put a real bitmap into "bitmap". 2x2 bitmap of green 32 bit pixels.
28 bitmap.setConfig(SkBitmap::kARGB_8888_Config, 16, 16);
30 bitmap.eraseColor(SK_ColorGREEN);
31 status_icon_win_ = (StatusIconWin*)status_tray_->CreateStatusIcon(
32 StatusTray::OTHER_ICON,
33 gfx::ImageSkia::CreateFrom1xBitmap(bitmap),
35 tray_watcher_ = new StatusTrayStateChangerWin(status_icon_win_->icon_id(),
36 status_icon_win_->window());
39 void TearDown() OVERRIDE {
43 testing::Test::TearDown();
47 HWND icon_window() { return status_icon_win_->window(); }
48 UINT icon_id() { return status_icon_win_->icon_id(); }
50 scoped_ptr<NOTIFYITEM> SetupAndGetCurrentNotifyItem() {
51 EXPECT_TRUE(CallCreateTrayNotify());
53 EXPECT_TRUE(IsInterfaceKnown());
55 scoped_ptr<NOTIFYITEM> notify_item = GetNotifyItem();
56 EXPECT_TRUE(notify_item.get() != NULL);
57 DCHECK_EQ(notify_item->hwnd, icon_window());
58 DCHECK_EQ(notify_item->id, icon_id());
60 return notify_item.Pass();
63 bool CallCreateTrayNotify() { return tray_watcher_->CreateTrayNotify(); }
65 bool IsInterfaceKnown() {
66 return StatusTrayStateChangerWin::INTERFACE_VERSION_UNKNOWN !=
67 tray_watcher_->interface_version_;
70 void SendNotifyItemUpdate(scoped_ptr<NOTIFYITEM> notify_item) {
71 tray_watcher_->SendNotifyItemUpdate(notify_item.Pass());
74 scoped_ptr<NOTIFYITEM> GetNotifyItem() {
75 return tray_watcher_->RegisterCallback();
78 scoped_ptr<base::win::ScopedCOMInitializer> com_;
79 scoped_ptr<StatusTrayWin> status_tray_;
80 scoped_refptr<StatusTrayStateChangerWin> tray_watcher_;
82 StatusIconWin* status_icon_win_;
84 DISALLOW_COPY_AND_ASSIGN(StatusTrayStateChangerWinTest);
87 // Test is disabled due to multiple COM initialization errors. See
88 // http//crbug.com/367199 for details.
89 TEST_F(StatusTrayStateChangerWinTest, DISABLED_Setup) {
90 // This tests the code path that will read the NOTIFYITEM data structure for
91 // use in future tests.
92 scoped_ptr<NOTIFYITEM> notify_item = SetupAndGetCurrentNotifyItem();
93 EXPECT_FALSE(notify_item.get() == NULL);
96 // Test is disabled due to multiple COM initialization errors. See
97 // http//crbug.com/367199 for details.
98 TEST_F(StatusTrayStateChangerWinTest, DISABLED_ComApiTest) {
100 // Setup code to read the current preference.
101 scoped_ptr<NOTIFYITEM> notify_item = SetupAndGetCurrentNotifyItem();
102 ASSERT_TRUE(notify_item.get() != NULL);
104 // Store the current pref.
105 DWORD current_preference = notify_item->preference;
107 // Ensure that running our code will do something.
108 if (notify_item->preference != PREFERENCE_SHOW_WHEN_ACTIVE) {
109 scoped_ptr<NOTIFYITEM> notify_item_copy(new NOTIFYITEM(*notify_item));
110 notify_item_copy->preference = PREFERENCE_SHOW_WHEN_ACTIVE;
111 SendNotifyItemUpdate(notify_item_copy.Pass());
114 // Run the interesting code.
115 tray_watcher_->EnsureTrayIconVisible();
117 EXPECT_EQ(PREFERENCE_SHOW_ALWAYS, GetNotifyItem()->preference);
118 SendNotifyItemUpdate(notify_item.Pass());
119 notify_item = GetNotifyItem();
121 EXPECT_EQ(notify_item->preference, current_preference);
124 // Test is disabled due to multiple COM initialization errors. See
125 // http//crbug.com/367199 for details.
126 TEST_F(StatusTrayStateChangerWinTest, DISABLED_TraySizeApiTest) {
128 // The tray does not auto-hide icons immediately on Vista so this test does
129 // not detect a size change.
130 if (base::win::GetVersion() <= base::win::VERSION_VISTA)
133 // Used to reset operating system state afterwards.
134 scoped_ptr<NOTIFYITEM> notify_item = SetupAndGetCurrentNotifyItem();
135 // We can't actually run this test if we're already showing the icon.
136 if (notify_item->preference == PREFERENCE_SHOW_ALWAYS)
139 // This test can only run if the tray window structure conforms to what I've
140 // seen in Win7 and Win8.
141 HWND shell_tray_hwnd = ::FindWindow(L"Shell_TrayWnd", NULL);
142 if (shell_tray_hwnd == NULL)
145 HWND tray_notify_hwnd =
146 ::FindWindowEx(shell_tray_hwnd, NULL, L"TrayNotifyWnd", NULL);
147 ASSERT_TRUE(tray_notify_hwnd != NULL);
149 RECT original_tray_notify_rect;
150 ::GetWindowRect(tray_notify_hwnd, &original_tray_notify_rect);
152 LONG width = original_tray_notify_rect.right - original_tray_notify_rect.left;
155 tray_watcher_->EnsureTrayIconVisible();
157 RECT new_tray_notify_rect;
158 ::GetWindowRect(tray_notify_hwnd, &new_tray_notify_rect);
160 LONG new_width = new_tray_notify_rect.right - new_tray_notify_rect.left;
162 EXPECT_GT(new_width, width);
164 SendNotifyItemUpdate(notify_item.Pass());
165 ::GetWindowRect(tray_notify_hwnd, &new_tray_notify_rect);
166 new_width = new_tray_notify_rect.right - new_tray_notify_rect.left;
167 EXPECT_EQ(width, new_width);