Chromium Code Reviews| Index: ui/wm/core/shadow_unittest.cc |
| diff --git a/ui/wm/core/shadow_unittest.cc b/ui/wm/core/shadow_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..422e7c6d66d395728b29dfb7d745013c29c39ec5 |
| --- /dev/null |
| +++ b/ui/wm/core/shadow_unittest.cc |
| @@ -0,0 +1,128 @@ |
| +// Copyright 2014 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/wm/core/shadow.h" |
| + |
| +#include "base/memory/scoped_ptr.h" |
| +#include "third_party/skia/include/core/SkBitmap.h" |
| +#include "ui/aura/test/aura_test_base.h" |
| +#include "ui/aura/test/test_windows.h" |
| +#include "ui/aura/window.h" |
| +#include "ui/base/resource/resource_bundle.h" |
| +#include "ui/compositor/layer.h" |
| +#include "ui/compositor/layer_tree_owner.h" |
| +#include "ui/resources/grit/ui_resources.h" |
| + |
| +namespace wm { |
| + |
| +typedef aura::test::AuraTestBase ShadowTest; |
| + |
| +// Mock for the ResourceBundle::Delegate class. |
| +class MockResourceBundleDelegate : public ui::ResourceBundle::Delegate { |
| + public: |
| + MockResourceBundleDelegate() : last_resource_id_(0) { |
| + SkBitmap bitmap_small, bitmap_large; |
| + bitmap_small.allocPixels(SkImageInfo::MakeN32Premul(129, 129)); |
|
Daniel Erat
2014/09/19 00:28:58
nit: mind moving these numbers into constants?
hshi1
2014/09/19 00:41:13
Done.
|
| + bitmap_large.allocPixels(SkImageInfo::MakeN32Premul(269, 269)); |
| + image_small_ = gfx::Image::CreateFrom1xBitmap(bitmap_small); |
| + image_large_ = gfx::Image::CreateFrom1xBitmap(bitmap_large); |
| + } |
| + virtual ~MockResourceBundleDelegate() {} |
| + |
| + // ResourceBundle::Delegate overrides. |
| + virtual base::FilePath GetPathForResourcePack( |
| + const base::FilePath& pack_path, |
| + ui::ScaleFactor scale_factor) OVERRIDE { |
| + return base::FilePath(); |
| + } |
| + virtual base::FilePath GetPathForLocalePack( |
| + const base::FilePath& pack_path, |
| + const std::string& locale) OVERRIDE { |
| + return base::FilePath(); |
| + } |
| + virtual gfx::Image GetImageNamed(int resource_id) OVERRIDE { |
| + last_resource_id_ = resource_id; |
| + switch (resource_id) { |
| + case IDR_WINDOW_BUBBLE_SHADOW_SMALL: |
| + return image_small_; |
| + case IDR_AURA_SHADOW_ACTIVE: |
| + case IDR_AURA_SHADOW_INACTIVE: |
| + return image_large_; |
| + default: |
| + NOTREACHED(); |
| + return gfx::Image(); |
| + } |
| + } |
| + virtual gfx::Image GetNativeImageNamed( |
| + int resource_id, ui::ResourceBundle::ImageRTL rtl) OVERRIDE { |
| + return gfx::Image(); |
| + } |
| + virtual base::RefCountedStaticMemory* LoadDataResourceBytes( |
| + int resource_id, ui::ScaleFactor scale_factor) OVERRIDE { |
| + return NULL; |
| + } |
| + virtual bool GetRawDataResource( |
| + int resource_id, ui::ScaleFactor scale_factor, |
| + base::StringPiece* value) OVERRIDE { |
| + return false; |
| + } |
| + virtual bool GetLocalizedString( |
| + int message_id, base::string16* value) OVERRIDE { |
| + return false; |
| + } |
| + virtual scoped_ptr<gfx::Font> GetFont( |
| + ui::ResourceBundle::FontStyle style) OVERRIDE { |
| + return scoped_ptr<gfx::Font>(); |
| + } |
| + |
| + int last_resource_id() const { return last_resource_id_; } |
| + |
| + private: |
| + gfx::Image image_small_; |
| + gfx::Image image_large_; |
| + int last_resource_id_; |
| +}; |
| + |
| +// Test if the proper image is set for the specified style. |
| +TEST_F(ShadowTest, UpdateImagesForStyle) { |
| + scoped_ptr<MockResourceBundleDelegate> delegate( |
| + new MockResourceBundleDelegate()); |
|
Daniel Erat
2014/09/19 00:28:58
mind making a ShadowTest class and moving this cod
hshi1
2014/09/19 00:41:13
Done.
|
| + ui::ResourceBundle::InitSharedInstanceWithLocale( |
| + "en-US", delegate.get(), ui::ResourceBundle::LOAD_COMMON_RESOURCES); |
|
Daniel Erat
2014/09/19 00:28:58
nit: indent four spaces; same below
hshi1
2014/09/19 00:41:13
Done.
|
| + |
| + scoped_ptr<Shadow> shadow(new Shadow()); |
| + |
| + shadow->Init(Shadow::STYLE_SMALL); |
| + EXPECT_EQ(delegate->last_resource_id(), IDR_WINDOW_BUBBLE_SHADOW_SMALL); |
|
Daniel Erat
2014/09/19 00:28:58
i was going to suggest that it'd be better to chec
hshi1
2014/09/19 00:41:13
Acknowledged.
|
| + shadow->SetStyle(Shadow::STYLE_ACTIVE); |
| + EXPECT_EQ(delegate->last_resource_id(), IDR_AURA_SHADOW_ACTIVE); |
| + shadow->SetStyle(Shadow::STYLE_INACTIVE); |
| + EXPECT_EQ(delegate->last_resource_id(), IDR_AURA_SHADOW_INACTIVE); |
| + |
| + ui::ResourceBundle::CleanupSharedInstance(); |
| +} |
| + |
| +// Test if the proper content bounds is calculated based on the current style. |
| +TEST_F(ShadowTest, SetContentBounds) { |
| + scoped_ptr<MockResourceBundleDelegate> delegate( |
| + new MockResourceBundleDelegate()); |
| + ui::ResourceBundle::InitSharedInstanceWithLocale( |
| + "en-US", delegate.get(), ui::ResourceBundle::LOAD_COMMON_RESOURCES); |
| + |
| + scoped_ptr<Shadow> shadow(new Shadow()); |
| + |
| + // verify that layer bounds are inset from content bounds |
|
Daniel Erat
2014/09/19 00:28:58
nit: capitalize "Verify" and add a trailing period
hshi1
2014/09/19 00:41:13
Done.
|
| + shadow->Init(Shadow::STYLE_ACTIVE); |
| + gfx::Rect content_bounds(100, 100, 300, 300); |
| + shadow->SetContentBounds(content_bounds); |
| + EXPECT_EQ(shadow->content_bounds(), content_bounds); |
| + EXPECT_EQ(shadow->layer()->bounds(), gfx::Rect(36, 36, 428, 428)); |
|
Daniel Erat
2014/09/19 00:28:58
could you make these numbers be a function of the
hshi1
2014/09/19 00:41:13
actually the layer bounds are only related to the
|
| + |
| + shadow->SetStyle(Shadow::STYLE_SMALL); |
| + EXPECT_EQ(shadow->content_bounds(), content_bounds); |
| + EXPECT_EQ(shadow->layer()->bounds(), gfx::Rect(96, 96, 308, 308)); |
|
Daniel Erat
2014/09/19 00:28:58
does this exercise the change that you're making,
hshi1
2014/09/19 00:41:13
No unfortunately it doesn't exercise the current c
Daniel Erat
2014/09/19 00:44:00
got it. :-/ thanks for the tests, in any case.
danakj
2014/09/19 00:44:38
You can add an accessor for those if you want.
oshima
2014/09/19 14:24:01
I think it's worth it.
|
| + |
| + ui::ResourceBundle::CleanupSharedInstance(); |
| +} |
| +} // namespace wm |