Chromium Code Reviews| 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" |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 24 namespace { | 24 namespace { |
| 25 | 25 |
| 26 // Returns a small colored bitmap. | 26 // Returns a small colored bitmap. |
| 27 SkBitmap MakeBitmap(SkColor color) { | 27 SkBitmap MakeBitmap(SkColor color) { |
| 28 SkBitmap bitmap; | 28 SkBitmap bitmap; |
| 29 bitmap.allocN32Pixels(8, 8); | 29 bitmap.allocN32Pixels(8, 8); |
| 30 bitmap.eraseColor(color); | 30 bitmap.eraseColor(color); |
| 31 return bitmap; | 31 return bitmap; |
| 32 } | 32 } |
| 33 | 33 |
| 34 // A Widget that tracks activation changes. | |
|
sky
2016/04/07 17:55:10
Rather than creating a widget subclass can you use
James Cook
2016/04/07 20:43:43
Switched to using TestWidgetFocusChangeListener an
| |
| 35 class TestWidget : public Widget { | |
| 36 public: | |
| 37 TestWidget() {} | |
| 38 ~TestWidget() override {} | |
| 39 | |
| 40 bool last_activation_change() const { return last_activation_change_; } | |
| 41 int focus_count() const { return focus_count_; } | |
| 42 int blur_count() const { return blur_count_; } | |
| 43 | |
| 44 void ResetCounts() { | |
| 45 focus_count_ = 0; | |
| 46 blur_count_ = 0; | |
| 47 } | |
| 48 | |
| 49 // views::internal::NativeWidgetDelegate: | |
| 50 void OnNativeWidgetActivationChanged(bool active) override { | |
| 51 last_activation_change_ = active; | |
| 52 Widget::OnNativeWidgetActivationChanged(active); | |
| 53 } | |
| 54 void OnNativeFocus() override { | |
| 55 focus_count_++; | |
| 56 Widget::OnNativeFocus(); | |
| 57 } | |
| 58 void OnNativeBlur() override { | |
| 59 blur_count_++; | |
| 60 Widget::OnNativeBlur(); | |
| 61 } | |
| 62 | |
| 63 private: | |
| 64 bool last_activation_change_ = false; | |
| 65 int focus_count_ = 0; | |
| 66 int blur_count_ = 0; | |
| 67 | |
| 68 DISALLOW_COPY_AND_ASSIGN(TestWidget); | |
| 69 }; | |
| 70 | |
| 34 // A WidgetDelegate that supplies an app icon. | 71 // A WidgetDelegate that supplies an app icon. |
| 35 class TestWidgetDelegate : public WidgetDelegateView { | 72 class TestWidgetDelegate : public WidgetDelegateView { |
| 36 public: | 73 public: |
| 37 explicit TestWidgetDelegate(const SkBitmap& icon) | 74 explicit TestWidgetDelegate(const SkBitmap& icon) |
| 38 : app_icon_(gfx::ImageSkia::CreateFrom1xBitmap(icon)) {} | 75 : app_icon_(gfx::ImageSkia::CreateFrom1xBitmap(icon)) {} |
| 39 | 76 |
| 40 ~TestWidgetDelegate() override {} | 77 ~TestWidgetDelegate() override {} |
| 41 | 78 |
| 42 void SetIcon(const SkBitmap& icon) { | 79 void SetIcon(const SkBitmap& icon) { |
| 43 app_icon_ = gfx::ImageSkia::CreateFrom1xBitmap(icon); | 80 app_icon_ = gfx::ImageSkia::CreateFrom1xBitmap(icon); |
| 44 } | 81 } |
| 45 | 82 |
| 46 // views::WidgetDelegate: | 83 // views::WidgetDelegate: |
| 47 gfx::ImageSkia GetWindowAppIcon() override { return app_icon_; } | 84 gfx::ImageSkia GetWindowAppIcon() override { return app_icon_; } |
| 48 | 85 |
| 49 private: | 86 private: |
| 50 gfx::ImageSkia app_icon_; | 87 gfx::ImageSkia app_icon_; |
| 51 | 88 |
| 52 DISALLOW_COPY_AND_ASSIGN(TestWidgetDelegate); | 89 DISALLOW_COPY_AND_ASSIGN(TestWidgetDelegate); |
| 53 }; | 90 }; |
| 54 | 91 |
| 55 class NativeWidgetMusTest : public ViewsTestBase { | 92 class NativeWidgetMusTest : public ViewsTestBase { |
| 56 public: | 93 public: |
| 57 NativeWidgetMusTest() {} | 94 NativeWidgetMusTest() {} |
| 58 ~NativeWidgetMusTest() override {} | 95 ~NativeWidgetMusTest() override {} |
| 59 | 96 |
| 60 // Creates a test widget. Takes ownership of |delegate|. | 97 // Creates a test widget. Takes ownership of |delegate|. |
| 61 Widget* CreateWidget(TestWidgetDelegate* delegate) { | 98 TestWidget* CreateWidget(TestWidgetDelegate* delegate) { |
|
sky
2016/04/07 17:55:10
Make this return a scoped_ptr
James Cook
2016/04/07 20:43:43
Done.
| |
| 62 Widget* widget = new Widget(); | 99 TestWidget* widget = new TestWidget(); |
| 63 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_WINDOW); | 100 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_WINDOW); |
| 64 params.delegate = delegate; | 101 params.delegate = delegate; |
| 65 params.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; | 102 params.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; |
| 66 params.bounds = gfx::Rect(10, 20, 100, 200); | 103 params.bounds = gfx::Rect(10, 20, 100, 200); |
| 67 widget->Init(params); | 104 widget->Init(params); |
| 68 return widget; | 105 return widget; |
| 69 } | 106 } |
| 70 | 107 |
| 71 private: | 108 private: |
| 72 DISALLOW_COPY_AND_ASSIGN(NativeWidgetMusTest); | 109 DISALLOW_COPY_AND_ASSIGN(NativeWidgetMusTest); |
| 73 }; | 110 }; |
| 74 | 111 |
| 112 // Tests that native window activation and focus is passed to the Widget. | |
| 113 TEST_F(NativeWidgetMusTest, OnActivationChanged) { | |
| 114 scoped_ptr<TestWidget> widget(CreateWidget(nullptr)); | |
| 115 NativeWidgetMus* native_widget = static_cast<NativeWidgetMus*>(widget | |
| 116 ->native_widget_private()); | |
| 117 | |
| 118 // Simulate native widget being activated. | |
| 119 native_widget->OnActivationChanged(true); | |
|
sky
2016/04/07 17:55:10
If you call widget->Activate() does it call this?
James Cook
2016/04/07 20:43:43
Yes. Changed test to start the widget in the shown
| |
| 120 EXPECT_EQ(1, widget->focus_count()); | |
| 121 EXPECT_EQ(0, widget->blur_count()); | |
| 122 EXPECT_TRUE(widget->last_activation_change()); | |
| 123 | |
| 124 widget->ResetCounts(); | |
| 125 | |
| 126 // Simulate native widget being deactivated. | |
| 127 native_widget->OnActivationChanged(false); | |
| 128 EXPECT_EQ(0, widget->focus_count()); | |
| 129 EXPECT_EQ(1, widget->blur_count()); | |
| 130 EXPECT_FALSE(widget->last_activation_change()); | |
| 131 } | |
| 132 | |
| 75 // Tests that a window with an icon sets the mus::Window icon property. | 133 // Tests that a window with an icon sets the mus::Window icon property. |
| 76 TEST_F(NativeWidgetMusTest, AppIcon) { | 134 TEST_F(NativeWidgetMusTest, AppIcon) { |
| 77 // Create a Widget with a bitmap as the icon. | 135 // Create a Widget with a bitmap as the icon. |
| 78 SkBitmap source_bitmap = MakeBitmap(SK_ColorRED); | 136 SkBitmap source_bitmap = MakeBitmap(SK_ColorRED); |
| 79 scoped_ptr<Widget> widget( | 137 scoped_ptr<Widget> widget( |
| 80 CreateWidget(new TestWidgetDelegate(source_bitmap))); | 138 CreateWidget(new TestWidgetDelegate(source_bitmap))); |
| 81 | 139 |
| 82 // The mus::Window has the icon property. | 140 // The mus::Window has the icon property. |
| 83 mus::Window* window = | 141 mus::Window* window = |
| 84 static_cast<NativeWidgetMus*>(widget->native_widget_private())->window(); | 142 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) { | 186 TEST_F(NativeWidgetMusTest, ValidLayerTree) { |
| 129 scoped_ptr<Widget> widget(CreateWidget(nullptr)); | 187 scoped_ptr<Widget> widget(CreateWidget(nullptr)); |
| 130 View* content = new View; | 188 View* content = new View; |
| 131 content->SetPaintToLayer(true); | 189 content->SetPaintToLayer(true); |
| 132 widget->GetContentsView()->AddChildView(content); | 190 widget->GetContentsView()->AddChildView(content); |
| 133 EXPECT_TRUE(widget->GetNativeWindow()->layer()->Contains(content->layer())); | 191 EXPECT_TRUE(widget->GetNativeWindow()->layer()->Contains(content->layer())); |
| 134 } | 192 } |
| 135 | 193 |
| 136 } // namespace | 194 } // namespace |
| 137 } // namespace views | 195 } // namespace views |
| OLD | NEW |