Index: chrome/browser/ui/views/message_center/tray_watcher_win_browsertest.cc |
diff --git a/chrome/browser/ui/views/message_center/tray_watcher_win_browsertest.cc b/chrome/browser/ui/views/message_center/tray_watcher_win_browsertest.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..881e5f407236b2d417f2d86afc14e6cfb8fefa16 |
--- /dev/null |
+++ b/chrome/browser/ui/views/message_center/tray_watcher_win_browsertest.cc |
@@ -0,0 +1,153 @@ |
+// Copyright 2014 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. |
+ |
+#include "chrome/browser/ui/views/message_center/tray_watcher_win.h" |
+ |
+#include "base/memory/scoped_ptr.h" |
+#include "base/win/scoped_com_initializer.h" |
+#include "chrome/browser/status_icons/status_icon.h" |
+#include "chrome/browser/ui/views/status_icons/status_icon_win.h" |
+#include "chrome/browser/ui/views/status_icons/status_tray_win.h" |
+#include "chrome/test/base/in_process_browser_test.h" |
+#include "third_party/skia/include/core/SkBitmap.h" |
+#include "ui/gfx/image/image_skia.h" |
+ |
+namespace message_center { |
+ |
+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
|
+ public: |
+ void SetUpOnMainThread() OVERRIDE { |
+ com_.reset(new base::win::ScopedCOMInitializer()); |
+ status_tray_.reset(new StatusTrayWin()); |
+ SkBitmap bitmap; |
+ |
+ // Put a real bitmap into "bitmap". 2x2 bitmap of green 32 bit pixels. |
+ bitmap.setConfig(SkBitmap::kARGB_8888_Config, 16, 16); |
+ bitmap.allocPixels(); |
+ bitmap.eraseColor(SK_ColorGREEN); |
+ StatusIcon* status_icon = status_tray_->CreateStatusIcon( |
+ StatusTray::OTHER_ICON, |
+ gfx::ImageSkia::CreateFrom1xBitmap(bitmap), |
+ base::string16()); |
+ status_icon_win_ = status_icon->AsStatusIconWin(); |
+ tray_watcher_ = new TrayWatcherWin(status_icon_win_->icon_id(), |
+ status_icon_win_->hwnd()); |
+ } |
+ |
+ void CleanUpOnMainThread() OVERRIDE { |
+ tray_watcher_ = NULL; |
+ status_tray_.reset(); |
+ com_.reset(); |
+ } |
+ |
+ protected: |
+ HWND icon_hwnd() { return status_icon_win_->hwnd(); } |
+ UINT icon_id() { return status_icon_win_->icon_id(); } |
+ |
+ scoped_ptr<NOTIFYITEM> SetupAndGetCurrentNotifyItem() { |
+ EXPECT_TRUE(CallCreateTrayNotify()); |
+ EXPECT_TRUE(IsInterfaceKnown()); |
+ scoped_ptr<NOTIFYITEM> notify_item = GetNotifyItem(); |
+ EXPECT_TRUE(notify_item.get() != NULL); |
+ DCHECK_EQ(notify_item->hWnd, icon_hwnd()); |
+ DCHECK_EQ(notify_item->uID, icon_id()); |
+ EXPECT_TRUE(notify_item->dwPreference == PREFERENCE_SHOW_WHEN_ACTIVE); |
+ |
+ return notify_item.Pass(); |
+ } |
+ |
+ bool CallCreateTrayNotify() { return tray_watcher_->CreateTrayNotify(); } |
+ |
+ bool IsInterfaceKnown() { |
+ return TrayWatcherWin::INTERFACE_VERSION_UNKNOWN != |
+ tray_watcher_->interface_version_; |
+ } |
+ |
+ void SendNotifyItemUpdate(scoped_ptr<NOTIFYITEM> notify_item) { |
+ tray_watcher_->SendNotifyItemUpdate(notify_item.Pass()); |
+ } |
+ |
+ scoped_ptr<NOTIFYITEM> GetNotifyItem() { |
+ return tray_watcher_->RegisterCallback(); |
+ } |
+ |
+ scoped_ptr<base::win::ScopedCOMInitializer> com_; |
+ scoped_ptr<StatusTrayWin> status_tray_; |
+ scoped_refptr<TrayWatcherWin> tray_watcher_; |
+ |
+ StatusIconWin* status_icon_win_; |
+}; |
sky
2014/03/19 22:26:39
DISALLOW...
dewittj
2014/03/20 19:31:08
Done.
|
+ |
+IN_PROC_BROWSER_TEST_F(TrayWatcherWinTest, Setup) { |
+ // This tests the code path that will read the NOTIFYITEM data structure for |
+ // use in future tests. |
+ scoped_ptr<NOTIFYITEM> notify_item = SetupAndGetCurrentNotifyItem(); |
+ EXPECT_FALSE(notify_item.get() == NULL); |
+} |
+ |
+IN_PROC_BROWSER_TEST_F(TrayWatcherWinTest, ComApiTest) { |
+ |
+ // Setup code to read the current preference. |
+ scoped_ptr<NOTIFYITEM> notify_item = SetupAndGetCurrentNotifyItem(); |
+ ASSERT_TRUE(notify_item.get() != NULL); |
+ |
+ // Store the current pref. |
+ DWORD current_preference = notify_item->dwPreference; |
+ |
+ // Ensure that running our code will do something. |
+ if (notify_item->dwPreference != PREFERENCE_SHOW_WHEN_ACTIVE) { |
+ scoped_ptr<NOTIFYITEM> notify_item_copy(new NOTIFYITEM(*notify_item)); |
+ notify_item_copy->dwPreference = PREFERENCE_SHOW_WHEN_ACTIVE; |
+ SendNotifyItemUpdate(notify_item_copy.Pass()); |
+ } |
+ |
+ // Run the interesting code. |
+ tray_watcher_->EnsureTrayIconVisible(); |
+ |
+ EXPECT_EQ(GetNotifyItem()->dwPreference, PREFERENCE_SHOW_ALWAYS); |
+ SendNotifyItemUpdate(notify_item.Pass()); |
+ notify_item = GetNotifyItem(); |
+ |
+ EXPECT_EQ(notify_item->dwPreference, current_preference); |
+}; |
+ |
+IN_PROC_BROWSER_TEST_F(TrayWatcherWinTest, TraySizeApiTest) { |
+ // Used to reset operating system state afterwards. |
+ scoped_ptr<NOTIFYITEM> notify_item = SetupAndGetCurrentNotifyItem(); |
+ // We can't actually run this test if we're already showing the icon. |
+ if (notify_item->dwPreference = PREFERENCE_SHOW_ALWAYS) |
+ return; |
+ |
+ // This test can only run if the tray window structure conforms to what I've |
+ // seen in Win7 and Win8. |
+ HWND shell_tray_hwnd = ::FindWindow(L"Shell_TrayWnd", NULL); |
+ if (shell_tray_hwnd == NULL) |
+ return; |
+ |
+ HWND tray_notify_hwnd = |
+ ::FindWindowEx(shell_tray_hwnd, NULL, L"TrayNotifyWnd", NULL); |
+ ASSERT_TRUE(tray_notify_hwnd != NULL); |
+ |
+ RECT original_tray_notify_rect; |
+ ::GetWindowRect(tray_notify_hwnd, &original_tray_notify_rect); |
+ |
+ LONG width = original_tray_notify_rect.right - original_tray_notify_rect.left; |
+ ASSERT_GT(width, 0); |
+ |
+ tray_watcher_->EnsureTrayIconVisible(); |
+ |
+ RECT new_tray_notify_rect; |
+ ::GetWindowRect(tray_notify_hwnd, &new_tray_notify_rect); |
+ |
+ LONG new_width = new_tray_notify_rect.right - new_tray_notify_rect.left; |
+ |
+ EXPECT_GT(new_width, width); |
+ |
+ SendNotifyItemUpdate(notify_item.Pass()); |
+ ::GetWindowRect(tray_notify_hwnd, &new_tray_notify_rect); |
+ new_width = new_tray_notify_rect.right - new_tray_notify_rect.left; |
+ EXPECT_EQ(width, new_width); |
+} |
+ |
+} // namespace message_center |