Index: ui/compositor/layer_unittest.cc |
diff --git a/ui/compositor/layer_unittest.cc b/ui/compositor/layer_unittest.cc |
index ae7a91f90357f47fb966c0eac90610077da4ebb7..3ca1b3e62b58b9557d00888d2601da87c0d6c635 100644 |
--- a/ui/compositor/layer_unittest.cc |
+++ b/ui/compositor/layer_unittest.cc |
@@ -88,9 +88,10 @@ const int kDrawWaitTimeOutMs = 1000; |
class DrawWaiter : public ui::CompositorObserver { |
public: |
- DrawWaiter() {} |
+ DrawWaiter() : did_draw_(false) {} |
- void Wait(ui::Compositor* compositor) { |
+ bool Wait(ui::Compositor* compositor) { |
+ did_draw_ = false; |
compositor->AddObserver(this); |
wait_run_loop_.reset(new base::RunLoop()); |
base::CancelableClosure timeout( |
@@ -101,6 +102,7 @@ class DrawWaiter : public ui::CompositorObserver { |
base::TimeDelta::FromMilliseconds(kDrawWaitTimeOutMs)); |
wait_run_loop_->Run(); |
compositor->RemoveObserver(this); |
+ return did_draw_; |
} |
private: |
@@ -114,6 +116,7 @@ class DrawWaiter : public ui::CompositorObserver { |
virtual void OnCompositingStarted(Compositor* compositor, |
base::TimeTicks start_time) OVERRIDE {} |
virtual void OnCompositingEnded(Compositor* compositor) OVERRIDE { |
+ did_draw_ = true; |
wait_run_loop_->Quit(); |
} |
virtual void OnCompositingAborted(Compositor* compositor) OVERRIDE {} |
@@ -123,6 +126,7 @@ class DrawWaiter : public ui::CompositorObserver { |
base::TimeDelta interval) OVERRIDE {} |
scoped_ptr<base::RunLoop> wait_run_loop_; |
+ bool did_draw_; |
DISALLOW_COPY_AND_ASSIGN(DrawWaiter); |
}; |
@@ -180,9 +184,9 @@ class LayerWithRealCompositorTest : public testing::Test { |
gfx::Rect(GetCompositor()->size())); |
} |
- void WaitForDraw() { |
+ bool WaitForDraw() { |
DrawWaiter draw_waiter; |
- draw_waiter.Wait(GetCompositor()); |
+ return draw_waiter.Wait(GetCompositor()); |
} |
// Invalidates the entire contents of the layer. |
@@ -425,14 +429,13 @@ TEST_F(LayerWithRealCompositorTest, MAYBE_Hierarchy) { |
class LayerWithDelegateTest : public testing::Test, public CompositorDelegate { |
public: |
- LayerWithDelegateTest() : schedule_draw_invoked_(false) {} |
+ LayerWithDelegateTest() {} |
virtual ~LayerWithDelegateTest() {} |
// Overridden from testing::Test: |
virtual void SetUp() OVERRIDE { |
ui::SetupTestCompositor(); |
- compositor_.reset(new Compositor( |
- this, gfx::kNullAcceleratedWidget)); |
+ compositor_.reset(new Compositor(this, gfx::kNullAcceleratedWidget)); |
compositor_->SetScaleAndSize(1.0f, gfx::Size(1000, 1000)); |
} |
@@ -459,7 +462,7 @@ class LayerWithDelegateTest : public testing::Test, public CompositorDelegate { |
void DrawTree(Layer* root) { |
compositor()->SetRootLayer(root); |
- compositor()->Draw(false); |
+ Draw(); |
} |
// Invalidates the entire contents of the layer. |
@@ -470,18 +473,25 @@ class LayerWithDelegateTest : public testing::Test, public CompositorDelegate { |
// Invokes DrawTree on the compositor. |
void Draw() { |
- compositor_->Draw(false); |
+ compositor()->ScheduleDraw(); |
+ WaitForDraw(); |
+ } |
+ |
+ bool WaitForDraw() { |
+ DrawWaiter draw_waiter; |
+ return draw_waiter.Wait(compositor()); |
} |
// CompositorDelegate overrides. |
virtual void ScheduleDraw() OVERRIDE { |
- schedule_draw_invoked_ = true; |
+ DCHECK(!ui::Compositor::WasInitializedWithThread()); |
+ if (compositor_) { |
+ MessageLoop::current()->PostTask( |
+ FROM_HERE, |
+ base::Bind(&Compositor::Draw, compositor_->AsWeakPtr(), false)); |
+ } |
} |
- protected: |
- // Set to true when ScheduleDraw (CompositorDelegate override) is invoked. |
- bool schedule_draw_invoked_; |
- |
private: |
scoped_ptr<Compositor> compositor_; |
@@ -538,7 +548,7 @@ TEST_F(LayerWithRealCompositorTest, MAYBE_Delegate) { |
scoped_ptr<Layer> l1(CreateColorLayer(SK_ColorBLACK, |
gfx::Rect(20, 20, 400, 400))); |
GetCompositor()->SetRootLayer(l1.get()); |
- WaitForDraw(); |
+ EXPECT_TRUE(WaitForDraw()); |
TestLayerDelegate delegate; |
l1->set_delegate(&delegate); |
@@ -547,18 +557,18 @@ TEST_F(LayerWithRealCompositorTest, MAYBE_Delegate) { |
delegate.AddColor(SK_ColorGREEN); |
l1->SchedulePaint(gfx::Rect(0, 0, 400, 400)); |
- WaitForDraw(); |
+ EXPECT_TRUE(WaitForDraw()); |
EXPECT_EQ(delegate.color_index(), 1); |
EXPECT_EQ(delegate.paint_size(), l1->bounds().size()); |
l1->SchedulePaint(gfx::Rect(10, 10, 200, 200)); |
- WaitForDraw(); |
+ EXPECT_TRUE(WaitForDraw()); |
EXPECT_EQ(delegate.color_index(), 2); |
EXPECT_EQ(delegate.paint_size(), gfx::Size(200, 200)); |
l1->SchedulePaint(gfx::Rect(5, 5, 50, 50)); |
- WaitForDraw(); |
+ EXPECT_TRUE(WaitForDraw()); |
EXPECT_EQ(delegate.color_index(), 0); |
EXPECT_EQ(delegate.paint_size(), gfx::Size(50, 50)); |
} |
@@ -574,7 +584,7 @@ TEST_F(LayerWithRealCompositorTest, MAYBE_DrawTree) { |
l2->Add(l3.get()); |
GetCompositor()->SetRootLayer(l1.get()); |
- WaitForDraw(); |
+ EXPECT_TRUE(WaitForDraw()); |
DrawTreeLayerDelegate d1; |
l1->set_delegate(&d1); |
@@ -584,7 +594,7 @@ TEST_F(LayerWithRealCompositorTest, MAYBE_DrawTree) { |
l3->set_delegate(&d3); |
l2->SchedulePaint(gfx::Rect(5, 5, 5, 5)); |
- WaitForDraw(); |
+ EXPECT_TRUE(WaitForDraw()); |
EXPECT_FALSE(d1.painted()); |
EXPECT_TRUE(d2.painted()); |
EXPECT_FALSE(d3.painted()); |
@@ -611,7 +621,7 @@ TEST_F(LayerWithRealCompositorTest, MAYBE_HierarchyNoTexture) { |
l2->Add(l3.get()); |
GetCompositor()->SetRootLayer(l1.get()); |
- WaitForDraw(); |
+ EXPECT_TRUE(WaitForDraw()); |
DrawTreeLayerDelegate d2; |
l2->set_delegate(&d2); |
@@ -620,7 +630,7 @@ TEST_F(LayerWithRealCompositorTest, MAYBE_HierarchyNoTexture) { |
l2->SchedulePaint(gfx::Rect(5, 5, 5, 5)); |
l3->SchedulePaint(gfx::Rect(5, 5, 5, 5)); |
- WaitForDraw(); |
+ EXPECT_TRUE(WaitForDraw()); |
// |d2| should not have received a paint notification since it has no texture. |
EXPECT_FALSE(d2.painted()); |
@@ -788,17 +798,15 @@ TEST_F(LayerWithNullDelegateTest, SetBoundsSchedulesPaint) { |
Draw(); |
- schedule_draw_invoked_ = false; |
l1->SetBounds(gfx::Rect(5, 5, 200, 200)); |
// The CompositorDelegate (us) should have been told to draw for a move. |
- EXPECT_TRUE(schedule_draw_invoked_); |
+ EXPECT_TRUE(WaitForDraw()); |
- schedule_draw_invoked_ = false; |
l1->SetBounds(gfx::Rect(5, 5, 100, 100)); |
// The CompositorDelegate (us) should have been told to draw for a resize. |
- EXPECT_TRUE(schedule_draw_invoked_); |
+ EXPECT_TRUE(WaitForDraw()); |
} |
// Checks that pixels are actually drawn to the screen with a read back. |
@@ -879,31 +887,31 @@ TEST_F(LayerWithRealCompositorTest, MAYBE_CompositorObservers) { |
// As should scheduling a draw and waiting. |
observer.Reset(); |
l1->ScheduleDraw(); |
- WaitForDraw(); |
+ EXPECT_TRUE(WaitForDraw()); |
EXPECT_TRUE(observer.notified()); |
// Moving, but not resizing, a layer should alert the observers. |
observer.Reset(); |
l2->SetBounds(gfx::Rect(0, 0, 350, 350)); |
- WaitForDraw(); |
+ EXPECT_TRUE(WaitForDraw()); |
EXPECT_TRUE(observer.notified()); |
// So should resizing a layer. |
observer.Reset(); |
l2->SetBounds(gfx::Rect(0, 0, 400, 400)); |
- WaitForDraw(); |
+ EXPECT_TRUE(WaitForDraw()); |
EXPECT_TRUE(observer.notified()); |
// Opacity changes should alert the observers. |
observer.Reset(); |
l2->SetOpacity(0.5f); |
- WaitForDraw(); |
+ EXPECT_TRUE(WaitForDraw()); |
EXPECT_TRUE(observer.notified()); |
// So should setting the opacity back. |
observer.Reset(); |
l2->SetOpacity(1.0f); |
- WaitForDraw(); |
+ EXPECT_TRUE(WaitForDraw()); |
EXPECT_TRUE(observer.notified()); |
// Setting the transform of a layer should alert the observers. |
@@ -913,7 +921,7 @@ TEST_F(LayerWithRealCompositorTest, MAYBE_CompositorObservers) { |
transform.Rotate(90.0); |
transform.Translate(-200.0, -200.0); |
l2->SetTransform(transform); |
- WaitForDraw(); |
+ EXPECT_TRUE(WaitForDraw()); |
EXPECT_TRUE(observer.notified()); |
// A change resulting in an aborted swap buffer should alert the observer |
@@ -921,7 +929,7 @@ TEST_F(LayerWithRealCompositorTest, MAYBE_CompositorObservers) { |
observer.Reset(); |
l2->SetOpacity(0.1f); |
GetCompositor()->OnSwapBuffersAborted(); |
- WaitForDraw(); |
+ EXPECT_TRUE(WaitForDraw()); |
EXPECT_TRUE(observer.notified()); |
EXPECT_TRUE(observer.aborted()); |
@@ -930,7 +938,7 @@ TEST_F(LayerWithRealCompositorTest, MAYBE_CompositorObservers) { |
// Opacity changes should no longer alert the removed observer. |
observer.Reset(); |
l2->SetOpacity(0.5f); |
- WaitForDraw(); |
+ EXPECT_TRUE(WaitForDraw()); |
EXPECT_FALSE(observer.notified()); |
} |
@@ -1089,22 +1097,19 @@ TEST_F(LayerWithDelegateTest, SchedulePaintFromOnPaintLayer) { |
SchedulePaintForLayer(root.get()); |
DrawTree(root.get()); |
- schedule_draw_invoked_ = false; |
child->SchedulePaint(gfx::Rect(0, 0, 20, 20)); |
child_delegate.GetPaintCountAndClear(); |
- EXPECT_TRUE(schedule_draw_invoked_); |
- schedule_draw_invoked_ = false; |
+ |
// Set a rect so that when OnPaintLayer() is invoked SchedulePaint is invoked |
// again. |
child_delegate.SetSchedulePaintRect(gfx::Rect(10, 10, 30, 30)); |
- DrawTree(root.get()); |
+ EXPECT_TRUE(WaitForDraw()); |
// |child| should have been painted once. |
EXPECT_EQ(1, child_delegate.GetPaintCountAndClear()); |
- // ScheduleDraw() should have been invoked. |
- EXPECT_TRUE(schedule_draw_invoked_); |
+ |
// Because SchedulePaint() was invoked from OnPaintLayer() |child| should |
// still need to be painted. |
- DrawTree(root.get()); |
+ EXPECT_TRUE(WaitForDraw()); |
EXPECT_EQ(1, child_delegate.GetPaintCountAndClear()); |
EXPECT_TRUE(child_delegate.last_clip_rect().Contains( |
gfx::Rect(10, 10, 30, 30))); |
@@ -1126,7 +1131,7 @@ TEST_F(LayerWithRealCompositorTest, MAYBE_ScaleUpDown) { |
GetCompositor()->SetScaleAndSize(1.0f, gfx::Size(500, 500)); |
GetCompositor()->SetRootLayer(root.get()); |
root->Add(l1.get()); |
- WaitForDraw(); |
+ EXPECT_TRUE(WaitForDraw()); |
EXPECT_EQ("10,20 200x220", root->bounds().ToString()); |
EXPECT_EQ("10,20 140x180", l1->bounds().ToString()); |
@@ -1155,7 +1160,7 @@ TEST_F(LayerWithRealCompositorTest, MAYBE_ScaleUpDown) { |
EXPECT_EQ(2.0f, l1_delegate.device_scale_factor()); |
// Canvas size must have been scaled down up. |
- WaitForDraw(); |
+ EXPECT_TRUE(WaitForDraw()); |
EXPECT_EQ("400x440", root_delegate.paint_size().ToString()); |
EXPECT_EQ("2.0 2.0", root_delegate.ToScaleString()); |
EXPECT_EQ("280x360", l1_delegate.paint_size().ToString()); |
@@ -1175,7 +1180,7 @@ TEST_F(LayerWithRealCompositorTest, MAYBE_ScaleUpDown) { |
EXPECT_EQ(1.0f, l1_delegate.device_scale_factor()); |
// Canvas size must have been scaled down too. |
- WaitForDraw(); |
+ EXPECT_TRUE(WaitForDraw()); |
EXPECT_EQ("200x220", root_delegate.paint_size().ToString()); |
EXPECT_EQ("1.0 1.0", root_delegate.ToScaleString()); |
EXPECT_EQ("140x180", l1_delegate.paint_size().ToString()); |
@@ -1189,7 +1194,7 @@ TEST_F(LayerWithRealCompositorTest, MAYBE_ScaleUpDown) { |
// No scale change, so no scale notification. |
EXPECT_EQ(0.0f, root_delegate.device_scale_factor()); |
EXPECT_EQ(0.0f, l1_delegate.device_scale_factor()); |
- WaitForDraw(); |
+ EXPECT_TRUE(WaitForDraw()); |
EXPECT_EQ("0x0", root_delegate.paint_size().ToString()); |
EXPECT_EQ("0.0 0.0", root_delegate.ToScaleString()); |
EXPECT_EQ("0x0", l1_delegate.paint_size().ToString()); |
@@ -1207,7 +1212,7 @@ TEST_F(LayerWithRealCompositorTest, MAYBE_ScaleReparent) { |
GetCompositor()->SetScaleAndSize(1.0f, gfx::Size(500, 500)); |
GetCompositor()->SetRootLayer(root.get()); |
- WaitForDraw(); |
+ EXPECT_TRUE(WaitForDraw()); |
root->Add(l1.get()); |
EXPECT_EQ("10,20 140x180", l1->bounds().ToString()); |
@@ -1215,7 +1220,7 @@ TEST_F(LayerWithRealCompositorTest, MAYBE_ScaleReparent) { |
EXPECT_EQ("140x180", size_in_pixel.ToString()); |
EXPECT_EQ(0.0f, l1_delegate.device_scale_factor()); |
- WaitForDraw(); |
+ EXPECT_TRUE(WaitForDraw()); |
EXPECT_EQ("140x180", l1_delegate.paint_size().ToString()); |
EXPECT_EQ("1.0 1.0", l1_delegate.ToScaleString()); |
@@ -1235,7 +1240,7 @@ TEST_F(LayerWithRealCompositorTest, MAYBE_ScaleReparent) { |
size_in_pixel = l1->cc_layer()->bounds(); |
EXPECT_EQ("280x360", size_in_pixel.ToString()); |
EXPECT_EQ(2.0f, l1_delegate.device_scale_factor()); |
- WaitForDraw(); |
+ EXPECT_TRUE(WaitForDraw()); |
EXPECT_EQ("280x360", l1_delegate.paint_size().ToString()); |
EXPECT_EQ("2.0 2.0", l1_delegate.ToScaleString()); |
} |
@@ -1257,7 +1262,7 @@ TEST_F(LayerWithRealCompositorTest, MAYBE_NoScaleCanvas) { |
// Scale factor change is notified regardless of scale_content flag. |
EXPECT_EQ(2.0f, l1_delegate.device_scale_factor()); |
- WaitForDraw(); |
+ EXPECT_TRUE(WaitForDraw()); |
EXPECT_EQ("280x360", l1_delegate.paint_size().ToString()); |
EXPECT_EQ("1.0 1.0", l1_delegate.ToScaleString()); |
} |
@@ -1282,26 +1287,24 @@ TEST_F(LayerWithDelegateTest, SetBoundsWhenInvisible) { |
// Reset into invisible state. |
child->SetVisible(false); |
DrawTree(root.get()); |
- schedule_draw_invoked_ = false; |
delegate.Reset(); |
// Move layer. |
child->SetBounds(gfx::Rect(200, 200, 500, 500)); |
child->SetVisible(true); |
- EXPECT_TRUE(schedule_draw_invoked_); |
+ EXPECT_TRUE(WaitForDraw()); |
DrawTree(root.get()); |
EXPECT_FALSE(delegate.painted()); |
// Reset into invisible state. |
child->SetVisible(false); |
DrawTree(root.get()); |
- schedule_draw_invoked_ = false; |
delegate.Reset(); |
// Resize layer. |
child->SetBounds(gfx::Rect(200, 200, 400, 400)); |
child->SetVisible(true); |
- EXPECT_TRUE(schedule_draw_invoked_); |
+ EXPECT_TRUE(WaitForDraw()); |
DrawTree(root.get()); |
EXPECT_TRUE(delegate.painted()); |
} |