| 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/native_widget_mus.h" | 5 #include "ui/views/mus/native_widget_mus.h" |
| 6 | 6 |
| 7 #include "base/macros.h" | 7 #include "base/macros.h" |
| 8 #include "components/mus/public/cpp/property_type_converters.h" | 8 #include "components/mus/public/cpp/property_type_converters.h" |
| 9 #include "components/mus/public/cpp/window.h" | 9 #include "components/mus/public/cpp/window.h" |
| 10 #include "components/mus/public/cpp/window_property.h" | 10 #include "components/mus/public/cpp/window_property.h" |
| 11 #include "components/mus/public/interfaces/window_manager.mojom.h" | 11 #include "components/mus/public/interfaces/window_manager.mojom.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
| 13 #include "third_party/skia/include/core/SkBitmap.h" | 13 #include "third_party/skia/include/core/SkBitmap.h" |
| 14 #include "third_party/skia/include/core/SkColor.h" | 14 #include "third_party/skia/include/core/SkColor.h" |
| 15 #include "ui/aura/window.h" | 15 #include "ui/aura/window.h" |
| 16 #include "ui/gfx/geometry/rect.h" | 16 #include "ui/gfx/geometry/rect.h" |
| 17 #include "ui/gfx/image/image_skia.h" | 17 #include "ui/gfx/image/image_skia.h" |
| 18 #include "ui/gfx/skia_util.h" | 18 #include "ui/gfx/skia_util.h" |
| 19 #include "ui/views/test/focus_manager_test.h" |
| 19 #include "ui/views/test/views_test_base.h" | 20 #include "ui/views/test/views_test_base.h" |
| 20 #include "ui/views/widget/widget.h" | 21 #include "ui/views/widget/widget.h" |
| 21 #include "ui/views/widget/widget_delegate.h" | 22 #include "ui/views/widget/widget_delegate.h" |
| 23 #include "ui/views/widget/widget_observer.h" |
| 22 | 24 |
| 23 namespace views { | 25 namespace views { |
| 24 namespace { | 26 namespace { |
| 25 | 27 |
| 26 // Returns a small colored bitmap. | 28 // Returns a small colored bitmap. |
| 27 SkBitmap MakeBitmap(SkColor color) { | 29 SkBitmap MakeBitmap(SkColor color) { |
| 28 SkBitmap bitmap; | 30 SkBitmap bitmap; |
| 29 bitmap.allocN32Pixels(8, 8); | 31 bitmap.allocN32Pixels(8, 8); |
| 30 bitmap.eraseColor(color); | 32 bitmap.eraseColor(color); |
| 31 return bitmap; | 33 return bitmap; |
| 32 } | 34 } |
| 33 | 35 |
| 36 // An observer that tracks widget activation changes. |
| 37 class WidgetActivationObserver : public WidgetObserver { |
| 38 public: |
| 39 explicit WidgetActivationObserver(Widget* widget) : widget_(widget) { |
| 40 widget_->AddObserver(this); |
| 41 } |
| 42 |
| 43 ~WidgetActivationObserver() override { |
| 44 widget_->RemoveObserver(this); |
| 45 } |
| 46 |
| 47 const std::vector<bool>& changes() const { return changes_; } |
| 48 |
| 49 // WidgetObserver: |
| 50 void OnWidgetActivationChanged(Widget* widget, bool active) override { |
| 51 ASSERT_EQ(widget_, widget); |
| 52 changes_.push_back(active); |
| 53 } |
| 54 |
| 55 private: |
| 56 Widget* widget_; |
| 57 std::vector<bool> changes_; |
| 58 |
| 59 DISALLOW_COPY_AND_ASSIGN(WidgetActivationObserver); |
| 60 }; |
| 61 |
| 34 // A WidgetDelegate that supplies an app icon. | 62 // A WidgetDelegate that supplies an app icon. |
| 35 class TestWidgetDelegate : public WidgetDelegateView { | 63 class TestWidgetDelegate : public WidgetDelegateView { |
| 36 public: | 64 public: |
| 37 explicit TestWidgetDelegate(const SkBitmap& icon) | 65 explicit TestWidgetDelegate(const SkBitmap& icon) |
| 38 : app_icon_(gfx::ImageSkia::CreateFrom1xBitmap(icon)) {} | 66 : app_icon_(gfx::ImageSkia::CreateFrom1xBitmap(icon)) {} |
| 39 | 67 |
| 40 ~TestWidgetDelegate() override {} | 68 ~TestWidgetDelegate() override {} |
| 41 | 69 |
| 42 void SetIcon(const SkBitmap& icon) { | 70 void SetIcon(const SkBitmap& icon) { |
| 43 app_icon_ = gfx::ImageSkia::CreateFrom1xBitmap(icon); | 71 app_icon_ = gfx::ImageSkia::CreateFrom1xBitmap(icon); |
| 44 } | 72 } |
| 45 | 73 |
| 46 // views::WidgetDelegate: | 74 // views::WidgetDelegate: |
| 47 gfx::ImageSkia GetWindowAppIcon() override { return app_icon_; } | 75 gfx::ImageSkia GetWindowAppIcon() override { return app_icon_; } |
| 48 | 76 |
| 49 private: | 77 private: |
| 50 gfx::ImageSkia app_icon_; | 78 gfx::ImageSkia app_icon_; |
| 51 | 79 |
| 52 DISALLOW_COPY_AND_ASSIGN(TestWidgetDelegate); | 80 DISALLOW_COPY_AND_ASSIGN(TestWidgetDelegate); |
| 53 }; | 81 }; |
| 54 | 82 |
| 55 class NativeWidgetMusTest : public ViewsTestBase { | 83 class NativeWidgetMusTest : public ViewsTestBase { |
| 56 public: | 84 public: |
| 57 NativeWidgetMusTest() {} | 85 NativeWidgetMusTest() {} |
| 58 ~NativeWidgetMusTest() override {} | 86 ~NativeWidgetMusTest() override {} |
| 59 | 87 |
| 60 // Creates a test widget. Takes ownership of |delegate|. | 88 // Creates a test widget. Takes ownership of |delegate|. |
| 61 Widget* CreateWidget(TestWidgetDelegate* delegate) { | 89 scoped_ptr<Widget> CreateWidget(TestWidgetDelegate* delegate) { |
| 62 Widget* widget = new Widget(); | 90 scoped_ptr<Widget> widget(new Widget()); |
| 63 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_WINDOW); | 91 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_WINDOW); |
| 64 params.delegate = delegate; | 92 params.delegate = delegate; |
| 65 params.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; | 93 params.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; |
| 66 params.bounds = gfx::Rect(10, 20, 100, 200); | 94 params.bounds = gfx::Rect(10, 20, 100, 200); |
| 67 widget->Init(params); | 95 widget->Init(params); |
| 68 return widget; | 96 return widget; |
| 69 } | 97 } |
| 70 | 98 |
| 71 private: | 99 private: |
| 72 DISALLOW_COPY_AND_ASSIGN(NativeWidgetMusTest); | 100 DISALLOW_COPY_AND_ASSIGN(NativeWidgetMusTest); |
| 73 }; | 101 }; |
| 74 | 102 |
| 103 // Tests communication of activation and focus between Widget and |
| 104 // NativeWidgetMus. |
| 105 TEST_F(NativeWidgetMusTest, OnActivationChanged) { |
| 106 scoped_ptr<Widget> widget(CreateWidget(nullptr)); |
| 107 widget->Show(); |
| 108 |
| 109 // Track activation, focus and blur events. |
| 110 WidgetActivationObserver activation_observer(widget.get()); |
| 111 TestWidgetFocusChangeListener focus_listener; |
| 112 WidgetFocusManager::GetInstance()->AddFocusChangeListener(&focus_listener); |
| 113 |
| 114 // Deactivate the Widget, which deactivates the NativeWidgetMus. |
| 115 widget->Deactivate(); |
| 116 |
| 117 // The widget is blurred and deactivated. |
| 118 ASSERT_EQ(1u, focus_listener.focus_changes().size()); |
| 119 EXPECT_EQ(nullptr, focus_listener.focus_changes()[0]); |
| 120 ASSERT_EQ(1u, activation_observer.changes().size()); |
| 121 EXPECT_EQ(false, activation_observer.changes()[0]); |
| 122 |
| 123 // Re-activate the Widget, which actives the NativeWidgetMus. |
| 124 widget->Activate(); |
| 125 |
| 126 // The widget is focused and activated. |
| 127 ASSERT_EQ(2u, focus_listener.focus_changes().size()); |
| 128 EXPECT_EQ(widget->GetNativeView(), focus_listener.focus_changes()[1]); |
| 129 ASSERT_EQ(2u, activation_observer.changes().size()); |
| 130 EXPECT_TRUE(activation_observer.changes()[1]); |
| 131 |
| 132 WidgetFocusManager::GetInstance()->RemoveFocusChangeListener(&focus_listener); |
| 133 } |
| 134 |
| 75 // Tests that a window with an icon sets the mus::Window icon property. | 135 // Tests that a window with an icon sets the mus::Window icon property. |
| 76 TEST_F(NativeWidgetMusTest, AppIcon) { | 136 TEST_F(NativeWidgetMusTest, AppIcon) { |
| 77 // Create a Widget with a bitmap as the icon. | 137 // Create a Widget with a bitmap as the icon. |
| 78 SkBitmap source_bitmap = MakeBitmap(SK_ColorRED); | 138 SkBitmap source_bitmap = MakeBitmap(SK_ColorRED); |
| 79 scoped_ptr<Widget> widget( | 139 scoped_ptr<Widget> widget( |
| 80 CreateWidget(new TestWidgetDelegate(source_bitmap))); | 140 CreateWidget(new TestWidgetDelegate(source_bitmap))); |
| 81 | 141 |
| 82 // The mus::Window has the icon property. | 142 // The mus::Window has the icon property. |
| 83 mus::Window* window = | 143 mus::Window* window = |
| 84 static_cast<NativeWidgetMus*>(widget->native_widget_private())->window(); | 144 static_cast<NativeWidgetMus*>(widget->native_widget_private())->window(); |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 128 TEST_F(NativeWidgetMusTest, ValidLayerTree) { | 188 TEST_F(NativeWidgetMusTest, ValidLayerTree) { |
| 129 scoped_ptr<Widget> widget(CreateWidget(nullptr)); | 189 scoped_ptr<Widget> widget(CreateWidget(nullptr)); |
| 130 View* content = new View; | 190 View* content = new View; |
| 131 content->SetPaintToLayer(true); | 191 content->SetPaintToLayer(true); |
| 132 widget->GetContentsView()->AddChildView(content); | 192 widget->GetContentsView()->AddChildView(content); |
| 133 EXPECT_TRUE(widget->GetNativeWindow()->layer()->Contains(content->layer())); | 193 EXPECT_TRUE(widget->GetNativeWindow()->layer()->Contains(content->layer())); |
| 134 } | 194 } |
| 135 | 195 |
| 136 } // namespace | 196 } // namespace |
| 137 } // namespace views | 197 } // namespace views |
| OLD | NEW |