190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// Copyright 2013 The Chromium Authors. All rights reserved.
290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// found in the LICENSE file.
490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "cc/trees/layer_tree_host.h"
690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "cc/test/fake_content_layer_client.h"
890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "cc/test/fake_picture_layer.h"
990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "cc/test/fake_picture_layer_impl.h"
1090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "cc/test/layer_tree_test.h"
1190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "cc/trees/layer_tree_impl.h"
1290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
1390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)namespace cc {
1490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)namespace {
1590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
1690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// These tests deal with picture layers.
1790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)class LayerTreeHostPictureTest : public LayerTreeTest {
1890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) protected:
1990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  virtual void InitializeSettings(LayerTreeSettings* settings) OVERRIDE {
2090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    // PictureLayer can only be used with impl side painting enabled.
2190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    settings->impl_side_painting = true;
2290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  }
2390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)};
2490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
2590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)class LayerTreeHostPictureTestTwinLayer
2690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    : public LayerTreeHostPictureTest {
2790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  virtual void SetupTree() OVERRIDE {
2890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    LayerTreeHostPictureTest::SetupTree();
2990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
3090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    scoped_refptr<FakePictureLayer> picture =
3190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)        FakePictureLayer::Create(&client_);
3290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    layer_tree_host()->root_layer()->AddChild(picture);
3390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  }
3490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
3590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  virtual void BeginTest() OVERRIDE {
3690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    activates_ = 0;
3790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    PostSetNeedsCommitToMainThread();
3890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  }
3990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
4090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  virtual void DidCommit() OVERRIDE {
41bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdoch    switch (layer_tree_host()->source_frame_number()) {
4290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      case 2:
4390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)        // Drop the picture layer from the tree.
4490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)        layer_tree_host()->root_layer()->children()[0]->RemoveFromParent();
4590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)        break;
4690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      case 3:
4790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)        // Add a new picture layer.
4890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)        scoped_refptr<FakePictureLayer> picture =
4990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)            FakePictureLayer::Create(&client_);
5090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)        layer_tree_host()->root_layer()->AddChild(picture);
5190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)        break;
5290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    }
5390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  }
5490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
557d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  virtual void WillActivateTreeOnThread(LayerTreeHostImpl* impl) OVERRIDE {
5690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    LayerImpl* pending_root_impl = impl->pending_tree()->root_layer();
5790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    LayerImpl* active_root_impl = impl->active_tree()->root_layer();
5890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
5990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    if (pending_root_impl->children().empty()) {
6090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      EXPECT_EQ(2, activates_);
6190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      return;
6290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    }
6390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
6490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    FakePictureLayerImpl* pending_picture_impl =
6590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)        static_cast<FakePictureLayerImpl*>(pending_root_impl->children()[0]);
6690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
6790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    if (!active_root_impl) {
6890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      EXPECT_EQ(0, activates_);
6990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      EXPECT_EQ(NULL, pending_picture_impl->twin_layer());
7090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      return;
7190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    }
7290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
7390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    if (active_root_impl->children().empty()) {
7490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      EXPECT_EQ(3, activates_);
7590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      EXPECT_EQ(NULL, pending_picture_impl->twin_layer());
7690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      return;
7790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    }
7890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
7990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    FakePictureLayerImpl* active_picture_impl =
8090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)        static_cast<FakePictureLayerImpl*>(active_root_impl->children()[0]);
8190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
8290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    // After the first activation, when we commit again, we'll have a pending
8390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    // and active layer. Then we recreate a picture layer in the 4th activate
8490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    // and the next commit will have a pending and active twin again.
8590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    EXPECT_TRUE(activates_ == 1 || activates_ == 4);
8690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
8790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    EXPECT_EQ(pending_picture_impl, active_picture_impl->twin_layer());
8890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    EXPECT_EQ(active_picture_impl, pending_picture_impl->twin_layer());
8990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  }
9090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
917d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  virtual void DidActivateTreeOnThread(LayerTreeHostImpl* impl) OVERRIDE {
9290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    LayerImpl* active_root_impl = impl->active_tree()->root_layer();
9390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
9490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    if (active_root_impl->children().empty()) {
9590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      EXPECT_EQ(2, activates_);
9690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    } else {
9790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      FakePictureLayerImpl* active_picture_impl =
9890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)          static_cast<FakePictureLayerImpl*>(active_root_impl->children()[0]);
9990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
10090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      EXPECT_EQ(NULL, active_picture_impl->twin_layer());
10190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    }
10290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
10390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    ++activates_;
10490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    if (activates_ <= 4)
10590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      PostSetNeedsCommitToMainThread();
10690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    else
10790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      EndTest();
10890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  }
10990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
11090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  virtual void AfterTest() OVERRIDE {}
11190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
11290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  FakeContentLayerClient client_;
11390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  int activates_;
11490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)};
11590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
11690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)MULTI_THREAD_TEST_F(LayerTreeHostPictureTestTwinLayer);
11790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
11890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}  // namespace
11990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}  // namespace cc
120