Home | History | Annotate | Download | only in status_icons
      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/status_icons/status_tray_state_changer_win.h"
      6 
      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"
     16 
     17 class StatusTrayStateChangerWinTest : public testing::Test {
     18  public:
     19   StatusTrayStateChangerWinTest() {}
     20 
     21   void SetUp() OVERRIDE {
     22     testing::Test::SetUp();
     23     com_.reset(new base::win::ScopedCOMInitializer());
     24     status_tray_.reset(new StatusTrayWin());
     25     SkBitmap bitmap;
     26 
     27     // Put a real bitmap into "bitmap".  2x2 bitmap of green 32 bit pixels.
     28     bitmap.setConfig(SkBitmap::kARGB_8888_Config, 16, 16);
     29     bitmap.allocPixels();
     30     bitmap.eraseColor(SK_ColorGREEN);
     31     status_icon_win_ = (StatusIconWin*)status_tray_->CreateStatusIcon(
     32         StatusTray::OTHER_ICON,
     33         gfx::ImageSkia::CreateFrom1xBitmap(bitmap),
     34         base::string16());
     35     tray_watcher_ = new StatusTrayStateChangerWin(status_icon_win_->icon_id(),
     36                                                   status_icon_win_->window());
     37   }
     38 
     39   void TearDown() OVERRIDE {
     40     tray_watcher_ = NULL;
     41     status_tray_.reset();
     42     com_.reset();
     43     testing::Test::TearDown();
     44   }
     45 
     46  protected:
     47   HWND icon_window() { return status_icon_win_->window(); }
     48   UINT icon_id() { return status_icon_win_->icon_id(); }
     49 
     50   scoped_ptr<NOTIFYITEM> SetupAndGetCurrentNotifyItem() {
     51     EXPECT_TRUE(CallCreateTrayNotify());
     52 
     53     EXPECT_TRUE(IsInterfaceKnown());
     54 
     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());
     59 
     60     return notify_item.Pass();
     61   }
     62 
     63   bool CallCreateTrayNotify() { return tray_watcher_->CreateTrayNotify(); }
     64 
     65   bool IsInterfaceKnown() {
     66     return StatusTrayStateChangerWin::INTERFACE_VERSION_UNKNOWN !=
     67            tray_watcher_->interface_version_;
     68   }
     69 
     70   void SendNotifyItemUpdate(scoped_ptr<NOTIFYITEM> notify_item) {
     71     tray_watcher_->SendNotifyItemUpdate(notify_item.Pass());
     72   }
     73 
     74   scoped_ptr<NOTIFYITEM> GetNotifyItem() {
     75     return tray_watcher_->RegisterCallback();
     76   }
     77 
     78   scoped_ptr<base::win::ScopedCOMInitializer> com_;
     79   scoped_ptr<StatusTrayWin> status_tray_;
     80   scoped_refptr<StatusTrayStateChangerWin> tray_watcher_;
     81 
     82   StatusIconWin* status_icon_win_;
     83 
     84   DISALLOW_COPY_AND_ASSIGN(StatusTrayStateChangerWinTest);
     85 };
     86 
     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);
     94 }
     95 
     96 // Test is disabled due to multiple COM initialization errors.  See
     97 // http//crbug.com/367199 for details.
     98 TEST_F(StatusTrayStateChangerWinTest, DISABLED_ComApiTest) {
     99 
    100   // Setup code to read the current preference.
    101   scoped_ptr<NOTIFYITEM> notify_item = SetupAndGetCurrentNotifyItem();
    102   ASSERT_TRUE(notify_item.get() != NULL);
    103 
    104   // Store the current pref.
    105   DWORD current_preference = notify_item->preference;
    106 
    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());
    112   }
    113 
    114   // Run the interesting code.
    115   tray_watcher_->EnsureTrayIconVisible();
    116 
    117   EXPECT_EQ(PREFERENCE_SHOW_ALWAYS, GetNotifyItem()->preference);
    118   SendNotifyItemUpdate(notify_item.Pass());
    119   notify_item = GetNotifyItem();
    120 
    121   EXPECT_EQ(notify_item->preference, current_preference);
    122 };
    123 
    124 // Test is disabled due to multiple COM initialization errors.  See
    125 // http//crbug.com/367199 for details.
    126 TEST_F(StatusTrayStateChangerWinTest, DISABLED_TraySizeApiTest) {
    127 
    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)
    131     return;
    132 
    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)
    137     return;
    138 
    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)
    143     return;
    144 
    145   HWND tray_notify_hwnd =
    146       ::FindWindowEx(shell_tray_hwnd, NULL, L"TrayNotifyWnd", NULL);
    147   ASSERT_TRUE(tray_notify_hwnd != NULL);
    148 
    149   RECT original_tray_notify_rect;
    150   ::GetWindowRect(tray_notify_hwnd, &original_tray_notify_rect);
    151 
    152   LONG width = original_tray_notify_rect.right - original_tray_notify_rect.left;
    153   ASSERT_GT(width, 0);
    154 
    155   tray_watcher_->EnsureTrayIconVisible();
    156 
    157   RECT new_tray_notify_rect;
    158   ::GetWindowRect(tray_notify_hwnd, &new_tray_notify_rect);
    159 
    160   LONG new_width = new_tray_notify_rect.right - new_tray_notify_rect.left;
    161 
    162   EXPECT_GT(new_width, width);
    163 
    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);
    168 }
    169