summaryrefslogtreecommitdiffstats
path: root/views/widget/native_widget_unittest.cc
blob: 91c29a3d28344f74e319a7873ed17d9a7704ef12 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// Copyright (c) 2011 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 "testing/gtest/include/gtest/gtest.h"
#include "views/test/views_test_base.h"
#include "views/view.h"
#include "views/controls/native/native_view_host.h"
#include "views/widget/native_widget.h"
#include "views/widget/widget.h"
#include "views/widget/native_widget_test_utils.h"

#if defined(TOOLKIT_USES_GTK)
#include "views/widget/widget_gtk.h"
#endif

namespace views {

class ScopedTestWidget {
 public:
  ScopedTestWidget(NativeWidget* native_widget)
      : native_widget_(native_widget) {
  }
  ~ScopedTestWidget() {
    native_widget_->GetWidget()->CloseNow();
  }

  NativeWidget* operator->() const  {
    return native_widget_.get();
  }
  NativeWidget* get() const { return native_widget_.get(); }

 private:
  scoped_ptr<NativeWidget> native_widget_;
  DISALLOW_COPY_AND_ASSIGN(ScopedTestWidget);
};

class NativeWidgetTest : public ViewsTestBase {
 public:
  NativeWidgetTest() {}
  virtual ~NativeWidgetTest() {}

 private:
  DISALLOW_COPY_AND_ASSIGN(NativeWidgetTest);
};

TEST_F(NativeWidgetTest, CreateNativeWidget) {
  ScopedTestWidget widget(internal::CreateNativeWidget());
  EXPECT_TRUE(widget->GetWidget()->GetNativeView() != NULL);
}

TEST_F(NativeWidgetTest, GetNativeWidgetForNativeView) {
  ScopedTestWidget widget(internal::CreateNativeWidget());
  EXPECT_EQ(widget.get(),
            NativeWidget::GetNativeWidgetForNativeView(
                widget->GetWidget()->GetNativeView()));
}

// |widget| has the toplevel NativeWidget.
TEST_F(NativeWidgetTest, GetTopLevelNativeWidget1) {
  ScopedTestWidget widget(internal::CreateNativeWidget());
  EXPECT_EQ(widget.get(),
            NativeWidget::GetTopLevelNativeWidget(
                widget->GetWidget()->GetNativeView()));
}

// |toplevel_widget| has the toplevel NativeWidget.
TEST_F(NativeWidgetTest, GetTopLevelNativeWidget2) {
  ScopedTestWidget child_widget(internal::CreateNativeWidgetWithParent(NULL));
  ScopedTestWidget toplevel_widget(internal::CreateNativeWidget());

  NativeViewHost* child_host = new NativeViewHost;
  toplevel_widget->GetWidget()->SetContentsView(child_host);
  child_host->Attach(child_widget->GetWidget()->GetNativeView());

  EXPECT_EQ(toplevel_widget.get(),
            NativeWidget::GetTopLevelNativeWidget(
                child_widget->GetWidget()->GetNativeView()));
}

}  // namespace views