Home | History | Annotate | Download | only in widget
      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.
      4 
      5 #include "ui/views/widget/native_widget_win.h"
      6 
      7 #include "base/basictypes.h"
      8 #include "base/memory/scoped_ptr.h"
      9 #include "base/message_loop/message_loop.h"
     10 #include "testing/gtest/include/gtest/gtest.h"
     11 #include "ui/base/win/scoped_ole_initializer.h"
     12 
     13 namespace views {
     14 namespace {
     15 
     16 class NativeWidgetWinTest : public testing::Test {
     17  public:
     18   NativeWidgetWinTest() {}
     19   ~NativeWidgetWinTest() {}
     20 
     21   virtual void TearDown() {
     22     // Flush the message loop because we have pending release tasks
     23     // and these tasks if un-executed would upset Valgrind.
     24     RunPendingMessages();
     25   }
     26 
     27   // Create a simple widget win. The caller is responsible for taking ownership
     28   // of the returned value.
     29   NativeWidgetWin* CreateNativeWidgetWin();
     30 
     31   void RunPendingMessages() {
     32     message_loop_.RunUntilIdle();
     33   }
     34 
     35  private:
     36   base::MessageLoopForUI message_loop_;
     37   ui::ScopedOleInitializer ole_initializer_;
     38 
     39   DISALLOW_COPY_AND_ASSIGN(NativeWidgetWinTest);
     40 };
     41 
     42 NativeWidgetWin* NativeWidgetWinTest::CreateNativeWidgetWin() {
     43   scoped_ptr<Widget> widget(new Widget);
     44   Widget::InitParams params(Widget::InitParams::TYPE_POPUP);
     45   params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
     46   params.bounds = gfx::Rect(50, 50, 650, 650);
     47   widget->Init(params);
     48   return static_cast<NativeWidgetWin*>(widget.release()->native_widget());
     49 }
     50 
     51 TEST_F(NativeWidgetWinTest, ZoomWindow) {
     52   scoped_ptr<NativeWidgetWin> window(CreateNativeWidgetWin());
     53   ShowWindow(window->GetNativeView(), SW_HIDE);
     54   EXPECT_FALSE(window->IsActive());
     55   ShowWindow(window->GetNativeView(), SW_MAXIMIZE);
     56   EXPECT_TRUE(IsZoomed(window->GetNativeView()));
     57   window->CloseNow();
     58 }
     59 
     60 TEST_F(NativeWidgetWinTest, SetBoundsForZoomedWindow) {
     61   scoped_ptr<NativeWidgetWin> window(CreateNativeWidgetWin());
     62   ShowWindow(window->GetNativeView(), SW_MAXIMIZE);
     63   EXPECT_TRUE(IsZoomed(window->GetNativeView()));
     64 
     65   // Create another window, so that it will be active.
     66   scoped_ptr<NativeWidgetWin> window2(CreateNativeWidgetWin());
     67   ShowWindow(window2->GetNativeView(), SW_MAXIMIZE);
     68   EXPECT_TRUE(window2->IsActive());
     69   EXPECT_FALSE(window->IsActive());
     70 
     71   // Verify that setting the bounds of a zoomed window will unzoom it and not
     72   // cause it to be activated.
     73   window->SetBounds(gfx::Rect(50, 50, 650, 650));
     74   EXPECT_FALSE(IsZoomed(window->GetNativeView()));
     75   EXPECT_FALSE(window->IsActive());
     76 
     77   // Cleanup.
     78   window->CloseNow();
     79   window2->CloseNow();
     80 }
     81 
     82 }  // namespace
     83 }  // namespace views
     84