OLD | NEW |
---|---|
(Empty) | |
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. | |
4 | |
5 #include "chrome/browser/ui/views/message_center/tray_watcher_win.h" | |
6 | |
7 #include "base/memory/scoped_ptr.h" | |
8 #include "base/win/scoped_com_initializer.h" | |
9 #include "chrome/browser/status_icons/status_icon.h" | |
10 #include "chrome/browser/ui/views/status_icons/status_icon_win.h" | |
11 #include "chrome/browser/ui/views/status_icons/status_tray_win.h" | |
12 #include "chrome/test/base/in_process_browser_test.h" | |
13 #include "third_party/skia/include/core/SkBitmap.h" | |
14 #include "ui/gfx/image/image_skia.h" | |
15 | |
16 namespace message_center { | |
17 | |
18 class TrayWatcherWinTest : public InProcessBrowserTest { | |
sky
2014/03/19 22:26:39
Is there a reason these tests need to be browser t
dewittj
2014/03/20 19:31:08
I have put it into the interactive_ui_tests target
sky
2014/03/20 20:59:38
I'm not sure if there is a way to say your test ca
| |
19 public: | |
20 void SetUpOnMainThread() OVERRIDE { | |
21 com_.reset(new base::win::ScopedCOMInitializer()); | |
22 status_tray_.reset(new StatusTrayWin()); | |
23 SkBitmap bitmap; | |
24 | |
25 // Put a real bitmap into "bitmap". 2x2 bitmap of green 32 bit pixels. | |
26 bitmap.setConfig(SkBitmap::kARGB_8888_Config, 16, 16); | |
27 bitmap.allocPixels(); | |
28 bitmap.eraseColor(SK_ColorGREEN); | |
29 StatusIcon* status_icon = status_tray_->CreateStatusIcon( | |
30 StatusTray::OTHER_ICON, | |
31 gfx::ImageSkia::CreateFrom1xBitmap(bitmap), | |
32 base::string16()); | |
33 status_icon_win_ = status_icon->AsStatusIconWin(); | |
34 tray_watcher_ = new TrayWatcherWin(status_icon_win_->icon_id(), | |
35 status_icon_win_->hwnd()); | |
36 } | |
37 | |
38 void CleanUpOnMainThread() OVERRIDE { | |
39 tray_watcher_ = NULL; | |
40 status_tray_.reset(); | |
41 com_.reset(); | |
42 } | |
43 | |
44 protected: | |
45 HWND icon_hwnd() { return status_icon_win_->hwnd(); } | |
46 UINT icon_id() { return status_icon_win_->icon_id(); } | |
47 | |
48 scoped_ptr<NOTIFYITEM> SetupAndGetCurrentNotifyItem() { | |
49 EXPECT_TRUE(CallCreateTrayNotify()); | |
50 EXPECT_TRUE(IsInterfaceKnown()); | |
51 scoped_ptr<NOTIFYITEM> notify_item = GetNotifyItem(); | |
52 EXPECT_TRUE(notify_item.get() != NULL); | |
53 DCHECK_EQ(notify_item->hWnd, icon_hwnd()); | |
54 DCHECK_EQ(notify_item->uID, icon_id()); | |
55 EXPECT_TRUE(notify_item->dwPreference == PREFERENCE_SHOW_WHEN_ACTIVE); | |
56 | |
57 return notify_item.Pass(); | |
58 } | |
59 | |
60 bool CallCreateTrayNotify() { return tray_watcher_->CreateTrayNotify(); } | |
61 | |
62 bool IsInterfaceKnown() { | |
63 return TrayWatcherWin::INTERFACE_VERSION_UNKNOWN != | |
64 tray_watcher_->interface_version_; | |
65 } | |
66 | |
67 void SendNotifyItemUpdate(scoped_ptr<NOTIFYITEM> notify_item) { | |
68 tray_watcher_->SendNotifyItemUpdate(notify_item.Pass()); | |
69 } | |
70 | |
71 scoped_ptr<NOTIFYITEM> GetNotifyItem() { | |
72 return tray_watcher_->RegisterCallback(); | |
73 } | |
74 | |
75 scoped_ptr<base::win::ScopedCOMInitializer> com_; | |
76 scoped_ptr<StatusTrayWin> status_tray_; | |
77 scoped_refptr<TrayWatcherWin> tray_watcher_; | |
78 | |
79 StatusIconWin* status_icon_win_; | |
80 }; | |
sky
2014/03/19 22:26:39
DISALLOW...
dewittj
2014/03/20 19:31:08
Done.
| |
81 | |
82 IN_PROC_BROWSER_TEST_F(TrayWatcherWinTest, Setup) { | |
83 // This tests the code path that will read the NOTIFYITEM data structure for | |
84 // use in future tests. | |
85 scoped_ptr<NOTIFYITEM> notify_item = SetupAndGetCurrentNotifyItem(); | |
86 EXPECT_FALSE(notify_item.get() == NULL); | |
87 } | |
88 | |
89 IN_PROC_BROWSER_TEST_F(TrayWatcherWinTest, ComApiTest) { | |
90 | |
91 // Setup code to read the current preference. | |
92 scoped_ptr<NOTIFYITEM> notify_item = SetupAndGetCurrentNotifyItem(); | |
93 ASSERT_TRUE(notify_item.get() != NULL); | |
94 | |
95 // Store the current pref. | |
96 DWORD current_preference = notify_item->dwPreference; | |
97 | |
98 // Ensure that running our code will do something. | |
99 if (notify_item->dwPreference != PREFERENCE_SHOW_WHEN_ACTIVE) { | |
100 scoped_ptr<NOTIFYITEM> notify_item_copy(new NOTIFYITEM(*notify_item)); | |
101 notify_item_copy->dwPreference = PREFERENCE_SHOW_WHEN_ACTIVE; | |
102 SendNotifyItemUpdate(notify_item_copy.Pass()); | |
103 } | |
104 | |
105 // Run the interesting code. | |
106 tray_watcher_->EnsureTrayIconVisible(); | |
107 | |
108 EXPECT_EQ(GetNotifyItem()->dwPreference, PREFERENCE_SHOW_ALWAYS); | |
109 SendNotifyItemUpdate(notify_item.Pass()); | |
110 notify_item = GetNotifyItem(); | |
111 | |
112 EXPECT_EQ(notify_item->dwPreference, current_preference); | |
113 }; | |
114 | |
115 IN_PROC_BROWSER_TEST_F(TrayWatcherWinTest, TraySizeApiTest) { | |
116 // Used to reset operating system state afterwards. | |
117 scoped_ptr<NOTIFYITEM> notify_item = SetupAndGetCurrentNotifyItem(); | |
118 // We can't actually run this test if we're already showing the icon. | |
119 if (notify_item->dwPreference = PREFERENCE_SHOW_ALWAYS) | |
120 return; | |
121 | |
122 // This test can only run if the tray window structure conforms to what I've | |
123 // seen in Win7 and Win8. | |
124 HWND shell_tray_hwnd = ::FindWindow(L"Shell_TrayWnd", NULL); | |
125 if (shell_tray_hwnd == NULL) | |
126 return; | |
127 | |
128 HWND tray_notify_hwnd = | |
129 ::FindWindowEx(shell_tray_hwnd, NULL, L"TrayNotifyWnd", NULL); | |
130 ASSERT_TRUE(tray_notify_hwnd != NULL); | |
131 | |
132 RECT original_tray_notify_rect; | |
133 ::GetWindowRect(tray_notify_hwnd, &original_tray_notify_rect); | |
134 | |
135 LONG width = original_tray_notify_rect.right - original_tray_notify_rect.left; | |
136 ASSERT_GT(width, 0); | |
137 | |
138 tray_watcher_->EnsureTrayIconVisible(); | |
139 | |
140 RECT new_tray_notify_rect; | |
141 ::GetWindowRect(tray_notify_hwnd, &new_tray_notify_rect); | |
142 | |
143 LONG new_width = new_tray_notify_rect.right - new_tray_notify_rect.left; | |
144 | |
145 EXPECT_GT(new_width, width); | |
146 | |
147 SendNotifyItemUpdate(notify_item.Pass()); | |
148 ::GetWindowRect(tray_notify_hwnd, &new_tray_notify_rect); | |
149 new_width = new_tray_notify_rect.right - new_tray_notify_rect.left; | |
150 EXPECT_EQ(width, new_width); | |
151 } | |
152 | |
153 } // namespace message_center | |
OLD | NEW |