diff options
Diffstat (limited to 'ui/views')
| -rw-r--r-- | ui/views/mus/BUILD.gn | 3 | ||||
| -rw-r--r-- | ui/views/mus/native_widget_mus.cc | 30 | ||||
| -rw-r--r-- | ui/views/mus/native_widget_mus_unittest.cc | 128 |
3 files changed, 160 insertions, 1 deletions
diff --git a/ui/views/mus/BUILD.gn b/ui/views/mus/BUILD.gn index 0c51bef..6ad0729 100644 --- a/ui/views/mus/BUILD.gn +++ b/ui/views/mus/BUILD.gn @@ -149,6 +149,7 @@ test("views_mus_unittests") { "../view_targeter_unittest.cc", "../widget/native_widget_unittest.cc", "../widget/widget_unittest.cc", + "native_widget_mus_unittest.cc", "platform_test_helper_mus.cc", "run_all_unittests_mus.cc", ] @@ -159,6 +160,8 @@ test("views_mus_unittests") { "//base:i18n", "//base/test:test_support", "//cc", + "//components/mus/public/cpp", + "//components/mus/public/interfaces", "//mojo/shell/background:lib", "//mojo/shell/background:main", "//mojo/shell/background/tests:test_support", diff --git a/ui/views/mus/native_widget_mus.cc b/ui/views/mus/native_widget_mus.cc index 10fa385..9b99d6b 100644 --- a/ui/views/mus/native_widget_mus.cc +++ b/ui/views/mus/native_widget_mus.cc @@ -196,6 +196,17 @@ int ResizeBehaviorFromDelegate(WidgetDelegate* delegate) { return behavior; } +// Returns the 1x window app icon or an empty SkBitmap if no icon is available. +// TODO(jamescook): Support other scale factors. +SkBitmap AppIconFromDelegate(WidgetDelegate* delegate) { + if (!delegate) + return SkBitmap(); + gfx::ImageSkia app_icon = delegate->GetWindowAppIcon(); + if (app_icon.isNull()) + return SkBitmap(); + return app_icon.GetRepresentation(1.f).sk_bitmap(); +} + } // namespace class NativeWidgetMus::MusWindowObserver : public mus::WindowObserver { @@ -352,6 +363,12 @@ void NativeWidgetMus::ConfigurePropertiesForNewWindow( (*properties)[mus::mojom::WindowManager::kResizeBehavior_Property] = mojo::TypeConverter<const std::vector<uint8_t>, int32_t>::Convert( ResizeBehaviorFromDelegate(init_params.delegate)); + SkBitmap app_icon = AppIconFromDelegate(init_params.delegate); + if (!app_icon.isNull()) { + (*properties)[mus::mojom::WindowManager::kWindowAppIcon_Property] = + mojo::TypeConverter<const std::vector<uint8_t>, SkBitmap>::Convert( + app_icon); + } } //////////////////////////////////////////////////////////////////////////////// @@ -542,7 +559,18 @@ bool NativeWidgetMus::SetWindowTitle(const base::string16& title) { void NativeWidgetMus::SetWindowIcons(const gfx::ImageSkia& window_icon, const gfx::ImageSkia& app_icon) { - // NOTIMPLEMENTED(); + const char* const kWindowAppIcon_Property = + mus::mojom::WindowManager::kWindowAppIcon_Property; + + if (!app_icon.isNull()) { + // Send the app icon 1x bitmap to the window manager. + // TODO(jamescook): Support other scale factors. + window_->SetSharedProperty<SkBitmap>( + kWindowAppIcon_Property, app_icon.GetRepresentation(1.f).sk_bitmap()); + } else if (window_->HasSharedProperty(kWindowAppIcon_Property)) { + // Remove the existing icon. + window_->ClearSharedProperty(kWindowAppIcon_Property); + } } void NativeWidgetMus::InitModalType(ui::ModalType modal_type) { diff --git a/ui/views/mus/native_widget_mus_unittest.cc b/ui/views/mus/native_widget_mus_unittest.cc new file mode 100644 index 0000000..a2ff4b0 --- /dev/null +++ b/ui/views/mus/native_widget_mus_unittest.cc @@ -0,0 +1,128 @@ +// Copyright 2016 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "ui/views/mus/native_widget_mus.h" + +#include "base/macros.h" +#include "components/mus/public/cpp/property_type_converters.h" +#include "components/mus/public/cpp/window.h" +#include "components/mus/public/cpp/window_property.h" +#include "components/mus/public/interfaces/window_manager.mojom.h" +#include "testing/gtest/include/gtest/gtest.h" +#include "third_party/skia/include/core/SkBitmap.h" +#include "third_party/skia/include/core/SkColor.h" +#include "ui/gfx/geometry/rect.h" +#include "ui/gfx/image/image_skia.h" +#include "ui/gfx/skia_util.h" +#include "ui/views/test/views_test_base.h" +#include "ui/views/widget/widget.h" +#include "ui/views/widget/widget_delegate.h" + +namespace views { +namespace { + +// Returns a small colored bitmap. +SkBitmap MakeBitmap(SkColor color) { + SkBitmap bitmap; + bitmap.allocN32Pixels(8, 8); + bitmap.eraseColor(color); + return bitmap; +} + +// A WidgetDelegate that supplies an app icon. +class TestWidgetDelegate : public WidgetDelegateView { + public: + explicit TestWidgetDelegate(const SkBitmap& icon) + : app_icon_(gfx::ImageSkia::CreateFrom1xBitmap(icon)) {} + + ~TestWidgetDelegate() override {} + + void SetIcon(const SkBitmap& icon) { + app_icon_ = gfx::ImageSkia::CreateFrom1xBitmap(icon); + } + + // views::WidgetDelegate: + gfx::ImageSkia GetWindowAppIcon() override { return app_icon_; } + + private: + gfx::ImageSkia app_icon_; + + DISALLOW_COPY_AND_ASSIGN(TestWidgetDelegate); +}; + +class NativeWidgetMusTest : public ViewsTestBase { + public: + NativeWidgetMusTest() {} + ~NativeWidgetMusTest() override {} + + // Creates a test widget. Takes ownership of |delegate|. + Widget* CreateWidget(TestWidgetDelegate* delegate) { + Widget* widget = new Widget(); + Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_WINDOW); + params.delegate = delegate; + params.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; + params.bounds = gfx::Rect(10, 20, 100, 200); + widget->Init(params); + return widget; + } + + private: + DISALLOW_COPY_AND_ASSIGN(NativeWidgetMusTest); +}; + +// Tests that a window with an icon sets the mus::Window icon property. +TEST_F(NativeWidgetMusTest, AppIcon) { + // Create a Widget with a bitmap as the icon. + SkBitmap source_bitmap = MakeBitmap(SK_ColorRED); + scoped_ptr<Widget> widget( + CreateWidget(new TestWidgetDelegate(source_bitmap))); + + // The mus::Window has the icon property. + mus::Window* window = + static_cast<NativeWidgetMus*>(widget->native_widget_private())->window(); + EXPECT_TRUE(window->HasSharedProperty( + mus::mojom::WindowManager::kWindowAppIcon_Property)); + + // The icon is the expected icon. + SkBitmap icon = window->GetSharedProperty<SkBitmap>( + mus::mojom::WindowManager::kWindowAppIcon_Property); + EXPECT_TRUE(gfx::BitmapsAreEqual(source_bitmap, icon)); +} + +// Tests that a window without an icon does not set the mus::Window icon +// property. +TEST_F(NativeWidgetMusTest, NoAppIcon) { + // Create a Widget without a special icon. + scoped_ptr<Widget> widget(CreateWidget(nullptr)); + + // The mus::Window does not have an icon property. + mus::Window* window = + static_cast<NativeWidgetMus*>(widget->native_widget_private())->window(); + EXPECT_FALSE(window->HasSharedProperty( + mus::mojom::WindowManager::kWindowAppIcon_Property)); +} + +// Tests that changing the icon on a Widget updates the mus::Window icon +// property. +TEST_F(NativeWidgetMusTest, ChangeAppIcon) { + // Create a Widget with an icon. + SkBitmap bitmap1 = MakeBitmap(SK_ColorRED); + TestWidgetDelegate* delegate = new TestWidgetDelegate(bitmap1); + scoped_ptr<Widget> widget(CreateWidget(delegate)); + + // Update the icon to a new image. + SkBitmap bitmap2 = MakeBitmap(SK_ColorGREEN); + delegate->SetIcon(bitmap2); + widget->UpdateWindowIcon(); + + // The window has the updated icon. + mus::Window* window = + static_cast<NativeWidgetMus*>(widget->native_widget_private())->window(); + SkBitmap icon = window->GetSharedProperty<SkBitmap>( + mus::mojom::WindowManager::kWindowAppIcon_Property); + EXPECT_TRUE(gfx::BitmapsAreEqual(bitmap2, icon)); +} + +} // namespace +} // namespace views |
