| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "ui/views/mus/desktop_window_tree_host_mus.h" | 5 #include "ui/views/mus/desktop_window_tree_host_mus.h" |
| 6 | 6 |
| 7 #include "base/debug/stack_trace.h" |
| 8 |
| 7 #include "base/memory/ptr_util.h" | 9 #include "base/memory/ptr_util.h" |
| 10 #include "base/run_loop.h" |
| 8 #include "ui/aura/client/cursor_client.h" | 11 #include "ui/aura/client/cursor_client.h" |
| 9 #include "ui/aura/window.h" | 12 #include "ui/aura/window.h" |
| 10 #include "ui/views/test/views_test_base.h" | 13 #include "ui/views/test/views_test_base.h" |
| 11 #include "ui/views/widget/widget.h" | 14 #include "ui/views/widget/widget.h" |
| 12 #include "ui/views/widget/widget_delegate.h" | 15 #include "ui/views/widget/widget_delegate.h" |
| 16 #include "ui/views/widget/widget_observer.h" |
| 13 | 17 |
| 14 namespace views { | 18 namespace views { |
| 15 | 19 |
| 16 class DesktopWindowTreeHostMusTest : public ViewsTestBase { | 20 class DesktopWindowTreeHostMusTest : public ViewsTestBase, |
| 21 public WidgetObserver { |
| 17 public: | 22 public: |
| 18 DesktopWindowTreeHostMusTest() {} | 23 DesktopWindowTreeHostMusTest() |
| 24 : widget_activated_(nullptr), |
| 25 widget_deactivated_(nullptr), |
| 26 waiting_for_deactivate_(nullptr) {} |
| 19 ~DesktopWindowTreeHostMusTest() override {} | 27 ~DesktopWindowTreeHostMusTest() override {} |
| 20 | 28 |
| 21 // Creates a test widget. Takes ownership of |delegate|. | 29 // Creates a test widget. Takes ownership of |delegate|. |
| 22 std::unique_ptr<Widget> CreateWidget(WidgetDelegate* delegate) { | 30 std::unique_ptr<Widget> CreateWidget(WidgetDelegate* delegate) { |
| 23 std::unique_ptr<Widget> widget = base::MakeUnique<Widget>(); | 31 std::unique_ptr<Widget> widget = base::MakeUnique<Widget>(); |
| 24 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_WINDOW); | 32 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_WINDOW); |
| 25 params.delegate = delegate; | 33 params.delegate = delegate; |
| 26 params.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; | 34 params.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; |
| 27 params.bounds = gfx::Rect(0, 1, 111, 123); | 35 params.bounds = gfx::Rect(0, 1, 111, 123); |
| 28 widget->Init(params); | 36 widget->Init(params); |
| 37 widget->AddObserver(this); |
| 29 return widget; | 38 return widget; |
| 30 } | 39 } |
| 31 | 40 |
| 41 const Widget* widget_activated() const { return widget_activated_; } |
| 42 const Widget* widget_deactivated() const { return widget_deactivated_; } |
| 43 |
| 44 void DeactivateAndWait(Widget* to_deactivate) { |
| 45 waiting_for_deactivate_ = to_deactivate; |
| 46 |
| 47 base::RunLoop loop; |
| 48 on_deactivate_ = loop.QuitClosure(); |
| 49 |
| 50 to_deactivate->Deactivate(); |
| 51 loop.Run(); |
| 52 |
| 53 waiting_for_deactivate_ = nullptr; |
| 54 } |
| 55 |
| 32 private: | 56 private: |
| 57 void OnWidgetActivationChanged(Widget* widget, bool active) override { |
| 58 if (active) { |
| 59 widget_activated_ = widget; |
| 60 } else { |
| 61 if (widget_activated_ == widget) |
| 62 widget_activated_ = nullptr; |
| 63 widget_deactivated_ = widget; |
| 64 |
| 65 if (waiting_for_deactivate_ && waiting_for_deactivate_ == widget) |
| 66 on_deactivate_.Run(); |
| 67 } |
| 68 } |
| 69 |
| 70 Widget* widget_activated_; |
| 71 Widget* widget_deactivated_; |
| 72 |
| 73 Widget* waiting_for_deactivate_; |
| 74 base::Closure on_deactivate_; |
| 75 |
| 33 DISALLOW_COPY_AND_ASSIGN(DesktopWindowTreeHostMusTest); | 76 DISALLOW_COPY_AND_ASSIGN(DesktopWindowTreeHostMusTest); |
| 34 }; | 77 }; |
| 35 | 78 |
| 36 class ExpectsNullCursorClientDuringTearDown : public aura::WindowObserver { | 79 class ExpectsNullCursorClientDuringTearDown : public aura::WindowObserver { |
| 37 public: | 80 public: |
| 38 explicit ExpectsNullCursorClientDuringTearDown(aura::Window* window) | 81 explicit ExpectsNullCursorClientDuringTearDown(aura::Window* window) |
| 39 : window_(window) { | 82 : window_(window) { |
| 40 window_->AddObserver(this); | 83 window_->AddObserver(this); |
| 41 } | 84 } |
| 42 | 85 |
| (...skipping 26 matching lines...) Expand all Loading... |
| 69 widget->Show(); | 112 widget->Show(); |
| 70 EXPECT_TRUE(widget->IsVisible()); | 113 EXPECT_TRUE(widget->IsVisible()); |
| 71 EXPECT_TRUE(widget->GetNativeView()->IsVisible()); | 114 EXPECT_TRUE(widget->GetNativeView()->IsVisible()); |
| 72 EXPECT_TRUE(widget->GetNativeView()->parent()->IsVisible()); | 115 EXPECT_TRUE(widget->GetNativeView()->parent()->IsVisible()); |
| 73 widget->Hide(); | 116 widget->Hide(); |
| 74 EXPECT_FALSE(widget->IsVisible()); | 117 EXPECT_FALSE(widget->IsVisible()); |
| 75 EXPECT_FALSE(widget->GetNativeView()->IsVisible()); | 118 EXPECT_FALSE(widget->GetNativeView()->IsVisible()); |
| 76 EXPECT_FALSE(widget->GetNativeView()->parent()->IsVisible()); | 119 EXPECT_FALSE(widget->GetNativeView()->parent()->IsVisible()); |
| 77 } | 120 } |
| 78 | 121 |
| 122 TEST_F(DesktopWindowTreeHostMusTest, Deactivate) { |
| 123 std::unique_ptr<Widget> widget1(CreateWidget(nullptr)); |
| 124 widget1->Show(); |
| 125 |
| 126 std::unique_ptr<Widget> widget2(CreateWidget(nullptr)); |
| 127 widget2->Show(); |
| 128 |
| 129 widget1->Activate(); |
| 130 RunPendingMessages(); |
| 131 EXPECT_TRUE(widget1->IsActive()); |
| 132 EXPECT_EQ(widget_activated(), widget1.get()); |
| 133 |
| 134 DeactivateAndWait(widget1.get()); |
| 135 EXPECT_FALSE(widget1->IsActive()); |
| 136 } |
| 137 |
| 79 TEST_F(DesktopWindowTreeHostMusTest, CursorClientDuringTearDown) { | 138 TEST_F(DesktopWindowTreeHostMusTest, CursorClientDuringTearDown) { |
| 80 std::unique_ptr<Widget> widget(CreateWidget(nullptr)); | 139 std::unique_ptr<Widget> widget(CreateWidget(nullptr)); |
| 81 widget->Show(); | 140 widget->Show(); |
| 82 | 141 |
| 83 std::unique_ptr<aura::Window> window(new aura::Window(nullptr)); | 142 std::unique_ptr<aura::Window> window(new aura::Window(nullptr)); |
| 84 window->Init(ui::LAYER_SOLID_COLOR); | 143 window->Init(ui::LAYER_SOLID_COLOR); |
| 85 ExpectsNullCursorClientDuringTearDown observer(window.get()); | 144 ExpectsNullCursorClientDuringTearDown observer(window.get()); |
| 86 | 145 |
| 87 widget->GetNativeWindow()->AddChild(window.release()); | 146 widget->GetNativeWindow()->AddChild(window.release()); |
| 88 widget.reset(); | 147 widget.reset(); |
| 89 } | 148 } |
| 90 | 149 |
| 91 } // namespace views | 150 } // namespace views |
| OLD | NEW |