Chromium Code Reviews| Index: services/ui/display/platform_screen_ozone_unittests.cc |
| diff --git a/services/ui/display/platform_screen_ozone_unittests.cc b/services/ui/display/platform_screen_ozone_unittests.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..8dc3d1969e4a95e15f22c092131432287cd2baf1 |
| --- /dev/null |
| +++ b/services/ui/display/platform_screen_ozone_unittests.cc |
| @@ -0,0 +1,298 @@ |
| +// 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 <memory> |
| +#include <vector> |
| + |
| +#include "base/logging.h" |
| +#include "base/memory/ptr_util.h" |
| +#include "services/ui/display/platform_screen.h" |
| +#include "services/ui/display/platform_screen_impl_ozone.h" |
| +#include "testing/gtest/include/gtest/gtest.h" |
| +#include "ui/display/chromeos/display_configurator.h" |
| +#include "ui/display/chromeos/display_snapshot_virtual.h" |
| +#include "ui/display/types/display_constants.h" |
| +#include "ui/display/types/display_mode.h" |
| +#include "ui/display/types/display_snapshot.h" |
| +#include "ui/ozone/public/ozone_platform.h" |
| + |
| +namespace display { |
| + |
| +using ui::DisplayConfigurator; |
| +using ui::DisplayMode; |
| +using ui::DisplaySnapshot; |
| +using ui::DisplaySnapshotVirtual; |
| + |
| +namespace { |
| + |
| +const int64_t kDefaultDisplayId = 36028797018963969; |
|
rjkroege
2016/08/30 18:41:59
reasoning for number?
kylechar
2016/08/31 15:58:01
Added comment.
|
| + |
| +// Make a DisplaySnapshot with specified id and size. |
| +std::unique_ptr<DisplaySnapshot> MakeSnapshot(int64_t id, |
| + const gfx::Size& size) { |
| + auto snapshot = base::MakeUnique<DisplaySnapshotVirtual>(id, size); |
| + snapshot->set_current_mode(snapshot->modes()[0].get()); |
| + return snapshot; |
| +} |
| + |
| +// Functor to find DisplaySnapshot with specified id. |
| +class DisplayIdPredicate { |
|
rjkroege
2016/08/30 18:41:59
can't you just have a "FindForID()" method instead
kylechar
2016/08/31 15:58:01
I added that where it's clearer. I've also just us
|
| + public: |
| + explicit DisplayIdPredicate(int64_t id) : id_(id) {} |
| + bool operator()(const std::unique_ptr<DisplaySnapshot>& snapshot) const { |
| + return snapshot->display_id() == id_; |
| + } |
| + |
| + private: |
| + int64_t id_; |
| +}; |
| + |
| +// Test delegate to track what functions calls the delegate receives. |
| +class TestPlatformScreenDelegate : public PlatformScreenDelegate { |
|
rjkroege
2016/08/30 18:41:59
seems like mocks would be handy? Is mus allowed to
kylechar
2016/08/31 15:58:01
I'm not sure I understand the mocks bit since Plat
|
| + public: |
| + struct DisplayState { |
| + int64_t id; |
| + gfx::Rect bounds; |
| + }; |
| + |
| + TestPlatformScreenDelegate() {} |
| + ~TestPlatformScreenDelegate() override {} |
| + |
| + std::vector<DisplayState> added() { return added_; } |
| + std::vector<int64_t> removed() { return removed_; } |
| + std::vector<DisplayState> modified() { return modified_; } |
| + |
| + void Reset() { |
| + added_.clear(); |
| + removed_.clear(); |
| + modified_.clear(); |
| + } |
| + |
| + private: |
| + void OnDisplayAdded(PlatformScreen* platform_screen, |
| + int64_t id, |
| + const gfx::Rect& bounds) override { |
| + added_.push_back({id, bounds}); |
| + } |
| + |
| + void OnDisplayRemoved(int64_t id) override { removed_.push_back(id); } |
| + |
| + void OnDisplayModified(int64_t id, const gfx::Rect& bounds) override { |
| + modified_.push_back({id, bounds}); |
| + } |
| + |
| + std::vector<DisplayState> added_; |
| + std::vector<int64_t> removed_; |
| + std::vector<DisplayState> modified_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(TestPlatformScreenDelegate); |
| +}; |
| + |
| +// Test case that updates PlatformOzoneImplOzone with helpers to trigger |
| +// OnDisplay |
| +class PlatformScreenOzoneTest : public testing::Test { |
| + public: |
| + PlatformScreenOzoneTest() {} |
| + ~PlatformScreenOzoneTest() override {} |
| + |
| + PlatformScreen* platform_screen() { return platform_screen_.get(); } |
| + TestPlatformScreenDelegate* delegate() { return &delegate_; } |
| + |
| + // Adds a display snapshot with specified ID and default size. |
| + void AddDisplay(int64_t id) { return AddDisplay(id, gfx::Size(1024, 768)); } |
| + |
| + // Adds a display snapshot with specified ID and size to list of snapshots. |
| + void AddDisplay(int64_t id, const gfx::Size& size) { |
| + ASSERT_EQ(std::find_if(displays_.begin(), displays_.end(), |
| + DisplayIdPredicate(id)), |
| + displays_.end()); |
| + displays_.push_back(MakeSnapshot(id, size)); |
| + } |
| + |
| + // Removes display snapshot with specified ID. |
| + void RemoveDisplay(int64_t id) { |
| + size_t old_size = displays_.size(); |
| + displays_.erase(std::remove_if(displays_.begin(), displays_.end(), |
| + DisplayIdPredicate(id))); |
| + ASSERT_EQ(old_size, displays_.size() + 1); |
| + } |
| + |
| + // Modify the size of the display snapshot with specified ID. |
| + void ModifyDisplay(int64_t id, const gfx::Size& size) { |
| + auto iter = std::find_if(displays_.begin(), displays_.end(), |
| + DisplayIdPredicate(id)); |
| + ASSERT_NE(iter, displays_.end()); |
| + DisplaySnapshot* snapshot = (*iter).get(); |
| + |
| + const DisplayMode* new_mode = nullptr; |
| + for (auto& mode : snapshot->modes()) { |
| + if (mode->size() == size) { |
| + new_mode = mode.get(); |
| + break; |
| + } |
| + } |
| + |
| + if (!new_mode) { |
| + snapshot->add_mode(new DisplayMode(size, false, 30.0f)); |
| + new_mode = snapshot->modes().back().get(); |
| + } |
| + |
| + snapshot->set_current_mode(new_mode); |
| + } |
| + |
| + // Calls OnDisplayModeChanged with our list of display snapshots. |
| + void TriggerOnDisplayModeChanged() { |
| + std::vector<DisplaySnapshot*> snapshots; |
| + for (auto& snapshot : displays_) { |
| + snapshots.push_back(snapshot.get()); |
| + } |
| + static_cast<DisplayConfigurator::Observer*>(platform_screen_.get()) |
| + ->OnDisplayModeChanged(snapshots); |
| + } |
| + |
| + private: |
| + // testing::Test: |
| + void SetUp() override { |
| + testing::Test::SetUp(); |
| + ui::OzonePlatform::InitializeForUI(); |
| + platform_screen_ = base::MakeUnique<PlatformScreenImplOzone>(); |
| + platform_screen_->Init(&delegate_); |
| + |
| + // Double check the expected display exists and clear counters. |
| + ASSERT_EQ(1u, delegate_.added().size()); |
| + ASSERT_EQ(kDefaultDisplayId, delegate_.added()[0].id); |
| + delegate_.Reset(); |
| + |
| + // Make the initial list of snapshots match what exists. |
| + AddDisplay(kDefaultDisplayId); |
| + } |
| + |
| + void TearDown() override { |
| + displays_.clear(); |
| + delegate_.Reset(); |
| + platform_screen_.reset(); |
| + } |
| + |
| + TestPlatformScreenDelegate delegate_; |
| + std::unique_ptr<PlatformScreenImplOzone> platform_screen_; |
| + std::vector<std::unique_ptr<DisplaySnapshot>> displays_; |
| +}; |
| + |
| +} // namespace |
| + |
| +TEST_F(PlatformScreenOzoneTest, AddDisplay) { |
| + AddDisplay(2); |
| + TriggerOnDisplayModeChanged(); |
| + |
| + // Check that display 2 was added. |
| + ASSERT_EQ(1u, delegate()->added().size()); |
| + EXPECT_EQ(2, delegate()->added()[0].id); |
| + EXPECT_EQ(0u, delegate()->removed().size()); |
| + EXPECT_EQ(0u, delegate()->modified().size()); |
| +} |
| + |
| +TEST_F(PlatformScreenOzoneTest, RemoveDisplay) { |
| + AddDisplay(2); |
| + TriggerOnDisplayModeChanged(); |
| + delegate()->Reset(); |
| + |
| + RemoveDisplay(2); |
| + TriggerOnDisplayModeChanged(); |
| + |
| + // Check that display 2 was removed. |
| + ASSERT_EQ(1u, delegate()->removed().size()); |
| + EXPECT_EQ(2, delegate()->removed()[0]); |
| + EXPECT_EQ(0u, delegate()->added().size()); |
| + EXPECT_EQ(0u, delegate()->added().size()); |
| +} |
| + |
| +TEST_F(PlatformScreenOzoneTest, RemovePrimaryDisplay) { |
| + AddDisplay(2); |
| + TriggerOnDisplayModeChanged(); |
| + delegate()->Reset(); |
| + |
| + RemoveDisplay(kDefaultDisplayId); |
| + TriggerOnDisplayModeChanged(); |
| + |
| + // Check that the default display was removed and display 2 was modified due |
| + // to the origin changing. |
| + EXPECT_EQ(0u, delegate()->added().size()); |
| + ASSERT_EQ(1u, delegate()->removed().size()); |
| + EXPECT_EQ(kDefaultDisplayId, delegate()->removed()[0]); |
| + ASSERT_EQ(1u, delegate()->modified().size()); |
| + EXPECT_EQ(2, delegate()->modified()[0].id); |
| + EXPECT_EQ(0, delegate()->modified()[0].bounds.x()); |
| + |
| + // Also check the primary display changed because the old primary was removed. |
| + EXPECT_EQ(2, platform_screen()->GetPrimaryDisplayId()); |
| +} |
| + |
| +TEST_F(PlatformScreenOzoneTest, RemoveMultipleDisplay) { |
| + AddDisplay(2); |
| + AddDisplay(3); |
| + TriggerOnDisplayModeChanged(); |
| + delegate()->Reset(); |
| + |
| + RemoveDisplay(2); |
| + TriggerOnDisplayModeChanged(); |
| + |
| + // Check that display 2 was removed. |
| + EXPECT_EQ(1u, delegate()->removed().size()); |
| + EXPECT_EQ(2, delegate()->removed()[0]); |
| + |
| + delegate()->Reset(); |
| + RemoveDisplay(3); |
| + TriggerOnDisplayModeChanged(); |
| + |
| + // Check that display 3 was removed. |
| + EXPECT_EQ(1u, delegate()->removed().size()); |
| + EXPECT_EQ(3, delegate()->removed()[0]); |
| +} |
| + |
| +TEST_F(PlatformScreenOzoneTest, ModifyDisplaySize) { |
| + const gfx::Size size1(1920, 1200); |
| + const gfx::Size size2(1680, 1050); |
| + |
| + AddDisplay(2, size1); |
| + TriggerOnDisplayModeChanged(); |
| + |
| + // Check that display 2 was added with expected size. |
| + EXPECT_EQ(1u, delegate()->added().size()); |
| + EXPECT_EQ(2, delegate()->added()[0].id); |
| + EXPECT_EQ(size1, delegate()->added()[0].bounds.size()); |
| + delegate()->Reset(); |
| + |
| + ModifyDisplay(2, size2); |
| + TriggerOnDisplayModeChanged(); |
| + |
| + // Check that display 2 was modified to have the new expected size. |
| + ASSERT_EQ(1u, delegate()->modified().size()); |
| + EXPECT_EQ(2, delegate()->modified()[0].id); |
| + EXPECT_EQ(size2, delegate()->modified()[0].bounds.size()); |
| +} |
| + |
| +TEST_F(PlatformScreenOzoneTest, ModifyDisplayTriggerOriginChange) { |
| + const gfx::Size size2(1920, 1200); |
| + |
| + AddDisplay(2, size2); |
| + TriggerOnDisplayModeChanged(); |
| + |
| + // Check that display two has the expected initial origin. |
| + ASSERT_EQ(1u, delegate()->added().size()); |
| + EXPECT_EQ(1024, delegate()->added()[0].bounds.x()); |
| + delegate()->Reset(); |
| + |
| + ModifyDisplay(kDefaultDisplayId, size2); |
| + TriggerOnDisplayModeChanged(); |
| + |
| + // Check that the default display was modified with a new size and display 2 |
| + // was modified with a new origin. |
| + ASSERT_EQ(2u, delegate()->modified().size()); |
| + EXPECT_EQ(kDefaultDisplayId, delegate()->modified()[0].id); |
| + EXPECT_EQ(size2, delegate()->modified()[0].bounds.size()); |
| + EXPECT_EQ(2, delegate()->modified()[1].id); |
| + EXPECT_EQ(1920, delegate()->modified()[1].bounds.x()); |
| +} |
| + |
| +} // namespace display |