1 // Copyright (c) 2013 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/test/base/scoped_testing_local_state.h"
6 #include "chrome/test/base/testing_browser_process.h"
7 #include "chrome/test/base/view_event_test_base.h"
8 #include "ui/views/controls/native/native_view_host.h"
9 #include "ui/views/widget/widget.h"
11 class NativeWidgetWinTest
: public ViewEventTestBase
{
14 : ViewEventTestBase(),
15 contents_view_(new views::View
) {
18 virtual void SetUp() OVERRIDE
{
19 local_state_
.reset(new ScopedTestingLocalState(
20 TestingBrowserProcess::GetGlobal()));
21 ViewEventTestBase::SetUp();
25 virtual views::View
* CreateContentsView() OVERRIDE
{
26 return contents_view_
;
29 virtual gfx::Size
GetPreferredSize() OVERRIDE
{
30 return gfx::Size(500, 500);
33 views::View
* contents_view_
;
35 scoped_ptr
<views::Widget
> child_
;
38 scoped_ptr
<ScopedTestingLocalState
> local_state_
;
40 DISALLOW_COPY_AND_ASSIGN(NativeWidgetWinTest
);
43 // This test creates a child Widget that is attached by way of a NativeViewHost.
44 // The child HWND is focused (through NativeViewHost) the top level Widget is
45 // minimized then restored and the test asserts focus is still on the child
46 // widget and corresponding NativeViewHost.
48 // This scenario replicates what happens with a browser and a corresponding
49 // RenderWidgetHostViewWin.
51 // This test really belongs in views, but as it exercises focus it needs to
53 class NativeWidgetWinTest1
: public NativeWidgetWinTest
{
55 NativeWidgetWinTest1()
56 : NativeWidgetWinTest(),
58 native_view_host_(NULL
) {
61 virtual void DoTestOnMessageLoop() OVERRIDE
{
63 // Focus the native widget.
64 native_view_host_
->RequestFocus();
65 EXPECT_TRUE(native_view_host_
->HasFocus());
66 EXPECT_EQ(child_
->GetNativeView(), ::GetFocus());
68 // Minimize then restore the window, focus should still be on the
69 // NativeViewHost and its corresponding widget.
71 DLOG(WARNING
) << "Restoring widget=" << window_
->GetNativeView();
73 EXPECT_TRUE(native_view_host_
->HasFocus());
74 EXPECT_EQ(child_
->GetNativeView(), ::GetFocus());
80 void AttachChildWidget() {
81 child_
= new views::Widget();
82 views::Widget::InitParams
child_params(
83 views::Widget::InitParams::TYPE_CONTROL
);
84 child_params
.parent
= window_
->GetNativeView();
85 child_
->Init(child_params
);
87 native_view_host_
= new views::NativeViewHost
;
88 native_view_host_
->SetFocusable(true);
89 contents_view_
->AddChildView(native_view_host_
);
90 native_view_host_
->SetBoundsRect(gfx::Rect(0, 0, 200, 200));
91 native_view_host_
->Attach(child_
->GetNativeView());
94 // Owned by the parent widget.
95 views::Widget
* child_
;
97 // Owned by the parent View.
98 views::NativeViewHost
* native_view_host_
;
100 DISALLOW_COPY_AND_ASSIGN(NativeWidgetWinTest1
);
103 VIEW_TEST(NativeWidgetWinTest1
, FocusRestoredToChildAfterMiniminize
)