Commit 6949b0fe authored by Daniel Nicoara's avatar Daniel Nicoara Committed by Commit Bot

[Chromecast] Fix window ordering

The curent implementation tries to order one window in a list of
potentially unordered windows. The window list is not always ordered
since we never order hidden windows.

Use stable_sort to order all windows by ID. Between windows with the
same ID, promote the window currently becoming visible.

BUG=NONE
TEST=Verified on device and wrote unittests to cover window ordering.

Change-Id: I52a760a6fbb84752a523c89ba39963f443ab4712
Reviewed-on: https://chromium-review.googlesource.com/1103296Reviewed-by: default avatarLuke Halliwell <halliwell@chromium.org>
Commit-Queue: Daniel Nicoara <dnicoara@chromium.org>
Cr-Commit-Position: refs/heads/master@{#567989}
parent 358f2b26
......@@ -177,27 +177,21 @@ void CastLayoutManager::OnChildWindowVisibilityChanged(aura::Window* child,
// Determine z-order relative to existing windows.
aura::Window::Windows windows = parent->children();
aura::Window* above = nullptr;
aura::Window* below = nullptr;
for (auto* other : windows) {
if (other == child) {
continue;
}
if ((other->id() < child->id()) && (!below || other->id() > below->id())) {
below = other;
} else if ((other->id() > child->id()) &&
(!above || other->id() < above->id())) {
above = other;
}
}
// Adjust the z-order of the new child window.
if (above) {
parent->StackChildBelow(child, above);
} else if (below) {
parent->StackChildAbove(child, below);
} else {
parent->StackChildAtBottom(child);
std::stable_sort(windows.begin(), windows.end(),
[child](aura::Window* lhs, aura::Window* rhs) {
// Promote |child| to the top of the stack of windows with
// the same ID.
if (lhs->id() == rhs->id() && rhs == child)
return true;
return lhs->id() < rhs->id();
});
for (size_t i = 0; i < windows.size(); ++i) {
if (i == 0)
parent->StackChildAtBottom(windows[i]);
else
parent->StackChildAbove(windows[i], windows[i - 1]);
}
}
......
......@@ -35,6 +35,23 @@ class CastTestWindowDelegate : public aura::test::TestWindowDelegate {
DISALLOW_COPY_AND_ASSIGN(CastTestWindowDelegate);
};
class TestWindow {
public:
TestWindow(int id) : window_(&delegate_) {
window_.Init(ui::LAYER_NOT_DRAWN);
window_.set_id(id);
window_.SetBounds(gfx::Rect(0, 0, 1280, 720));
}
aura::Window* window() { return &window_; }
private:
CastTestWindowDelegate delegate_;
aura::Window window_;
DISALLOW_COPY_AND_ASSIGN(TestWindow);
};
TEST_F(CastWindowManagerAuraTest, InitialWindowId) {
CastTestWindowDelegate window_delegate;
aura::Window window(&window_delegate);
......@@ -102,5 +119,59 @@ TEST_F(CastWindowManagerAuraTest, WindowInputDisabled) {
EXPECT_EQ(ui::VKEY_UNKNOWN, window_delegate.key_code());
}
void VerifyWindowOrder(aura::Window* root_window) {
for (size_t i = 0; i < root_window->children().size() - 1; ++i)
EXPECT_LE(root_window->children()[i]->id(),
root_window->children()[i + 1]->id());
}
TEST_F(CastWindowManagerAuraTest, CheckProperWindowOrdering) {
std::unique_ptr<CastWindowManagerAura> window_manager =
std::make_unique<CastWindowManagerAura>(false /* enable input */);
TestWindow window1(1);
TestWindow window3(3);
window_manager->AddWindow(window1.window());
window_manager->AddWindow(window3.window());
window1.window()->Show();
window3.window()->Show();
// Verify update for top window.
VerifyWindowOrder(window_manager->GetRootWindow());
TestWindow window0(0);
window_manager->AddWindow(window0.window());
window0.window()->Show();
// Verify update for bottom window.
VerifyWindowOrder(window_manager->GetRootWindow());
TestWindow window2(2);
window_manager->AddWindow(window2.window());
window2.window()->Show();
// Verify update for middle window.
VerifyWindowOrder(window_manager->GetRootWindow());
TestWindow window4(4);
TestWindow window5(5);
TestWindow window6(6);
window_manager->AddWindow(window6.window());
window_manager->AddWindow(window4.window());
window_manager->AddWindow(window5.window());
window5.window()->Show();
// Verify update with hidden windows.
VerifyWindowOrder(window_manager->GetRootWindow());
TestWindow window7(2);
window_manager->AddWindow(window7.window());
window7.window()->Show();
// Verify update with same window ID.
VerifyWindowOrder(window_manager->GetRootWindow());
EXPECT_EQ(window7.window(), window_manager->GetRootWindow()->children()[3]);
window2.window()->Hide();
window2.window()->Show();
// Verify update ordering with same window ID.
EXPECT_EQ(window2.window(), window_manager->GetRootWindow()->children()[3]);
}
} // namespace test
} // namespace chromecast
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment