Home | History | Annotate | Download | only in native
      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.
      4 
      5 #include "ui/views/controls/native/native_view_host_aura.h"
      6 
      7 #include "base/basictypes.h"
      8 #include "base/memory/scoped_ptr.h"
      9 #include "ui/aura/window.h"
     10 #include "ui/views/controls/native/native_view_host.h"
     11 #include "ui/views/test/views_test_base.h"
     12 #include "ui/views/view.h"
     13 #include "ui/views/view_constants_aura.h"
     14 #include "ui/views/widget/widget.h"
     15 
     16 namespace views {
     17 
     18 class NativeViewHostAuraTest : public ViewsTestBase {
     19  public:
     20   NativeViewHostAuraTest() {
     21   }
     22 
     23   NativeViewHostAura* native_host() {
     24     return static_cast<NativeViewHostAura*>(host_->native_wrapper_.get());
     25   }
     26 
     27   NativeViewHost* host() {
     28     return host_.get();
     29   }
     30 
     31   Widget* child() {
     32     return child_.get();
     33   }
     34 
     35   void CreateHost() {
     36     // Create the top level widget.
     37     toplevel_.reset(new Widget);
     38     Widget::InitParams toplevel_params =
     39         CreateParams(Widget::InitParams::TYPE_WINDOW);
     40     toplevel_params.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
     41     toplevel_->Init(toplevel_params);
     42 
     43     // And the child widget.
     44     View* test_view = new View;
     45     child_.reset(new Widget);
     46     Widget::InitParams child_params(Widget::InitParams::TYPE_CONTROL);
     47     child_params.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
     48     child_params.parent = toplevel_->GetNativeView();
     49     child_->Init(child_params);
     50     child_->SetContentsView(test_view);
     51 
     52     // Owned by |toplevel|.
     53     host_.reset(new NativeViewHost);
     54     toplevel_->GetRootView()->AddChildView(host_.get());
     55     host_->Attach(child_->GetNativeView());
     56   }
     57 
     58   void DestroyHost() {
     59     host_.reset();
     60   }
     61 
     62  private:
     63   scoped_ptr<Widget> toplevel_;
     64   scoped_ptr<NativeViewHost> host_;
     65   scoped_ptr<Widget> child_;
     66 
     67   DISALLOW_COPY_AND_ASSIGN(NativeViewHostAuraTest);
     68 };
     69 
     70 // Verifies NativeViewHostAura stops observing native view on destruction.
     71 TEST_F(NativeViewHostAuraTest, StopObservingNativeViewOnDestruct) {
     72   CreateHost();
     73   aura::Window* child_win = child()->GetNativeView();
     74   NativeViewHostAura* aura_host = native_host();
     75 
     76   EXPECT_TRUE(child_win->HasObserver(aura_host));
     77   DestroyHost();
     78   EXPECT_FALSE(child_win->HasObserver(aura_host));
     79 }
     80 
     81 // Tests that the kHostViewKey is correctly set and cleared.
     82 TEST_F(NativeViewHostAuraTest, HostViewPropertyKey) {
     83   // Create the NativeViewHost and attach a NativeView.
     84   CreateHost();
     85   aura::Window* child_win = child()->GetNativeView();
     86   EXPECT_EQ(host(), child_win->GetProperty(views::kHostViewKey));
     87 
     88   host()->Detach();
     89   EXPECT_FALSE(child_win->GetProperty(views::kHostViewKey));
     90 
     91   host()->Attach(child_win);
     92   EXPECT_EQ(host(), child_win->GetProperty(views::kHostViewKey));
     93 
     94   DestroyHost();
     95   EXPECT_FALSE(child_win->GetProperty(views::kHostViewKey));
     96 }
     97 
     98 }  // namespace views
     99