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 "ui/views/controls/native/native_view_host_test_base.h"
6
7#include "ui/views/controls/native/native_view_host.h"
8#include "ui/views/widget/widget.h"
9
10namespace views {
11namespace test {
12
13// Testing wrapper of the NativeViewHost.
14class NativeViewHostTestBase::NativeViewHostTesting : public NativeViewHost {
15 public:
16  explicit NativeViewHostTesting(NativeViewHostTestBase* owner)
17      : owner_(owner) {}
18  virtual ~NativeViewHostTesting() { owner_->host_destroyed_count_++; }
19
20 private:
21  NativeViewHostTestBase* owner_;
22
23  DISALLOW_COPY_AND_ASSIGN(NativeViewHostTesting);
24};
25
26NativeViewHostTestBase::NativeViewHostTestBase() : host_destroyed_count_(0) {
27}
28
29NativeViewHostTestBase::~NativeViewHostTestBase() {
30}
31
32void NativeViewHostTestBase::CreateTopLevel() {
33  toplevel_.reset(new Widget);
34  Widget::InitParams toplevel_params =
35      CreateParams(Widget::InitParams::TYPE_WINDOW);
36  toplevel_params.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
37  toplevel_->Init(toplevel_params);
38}
39
40void NativeViewHostTestBase::CreateTestingHost() {
41  host_.reset(new NativeViewHostTesting(this));
42}
43
44Widget* NativeViewHostTestBase::CreateChildForHost(
45    gfx::NativeView native_parent_view,
46    View* parent_view,
47    View* contents_view,
48    NativeViewHost* host) {
49  Widget* child = new Widget;
50  Widget::InitParams child_params(Widget::InitParams::TYPE_CONTROL);
51  child_params.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
52  child_params.parent = native_parent_view;
53  child->Init(child_params);
54  child->SetContentsView(contents_view);
55
56  // Owned by |parent_view|.
57  parent_view->AddChildView(host);
58  host->Attach(child->GetNativeView());
59
60  return child;
61}
62
63void NativeViewHostTestBase::DestroyTopLevel() {
64  toplevel_.reset();
65}
66
67void NativeViewHostTestBase::DestroyHost() {
68  host_.reset();
69}
70
71NativeViewHost* NativeViewHostTestBase::ReleaseHost() {
72  return host_.release();
73}
74
75NativeViewHostWrapper* NativeViewHostTestBase::GetNativeWrapper() {
76  return host_->native_wrapper_.get();
77}
78
79}  // namespace test
80}  // namespace views
81