[TreesInViz] Fix a few cc_unittests expectations.

* LayerTreeHostPictureTestForceRecalculateScales
* LayerTreeHostTestDamageWithScale

In TreesInViz mode, deleting a tiling requires a request to viz and ack
from viz. Therefore, it is delayed comparing with non TreesInViz mode,
leading to different testing behaviors.

TEST=cc_unittests

Bug: 406251942
Change-Id: If92109051778d90dee1da06dd4225279a1ab6a9c
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/7255625
Commit-Queue: Victor Miura <vmiura@chromium.org>
Auto-Submit: Zhenyao Mo <zmo@chromium.org>
Reviewed-by: Victor Miura <vmiura@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1558273}
diff --git a/cc/trees/layer_tree_host_unittest.cc b/cc/trees/layer_tree_host_unittest.cc
index 17371ee7..bd971b2 100644
--- a/cc/trees/layer_tree_host_unittest.cc
+++ b/cc/trees/layer_tree_host_unittest.cc
@@ -157,6 +157,10 @@
   return frame_scale;
 }
 
+bool TreesInViz() {
+  return base::FeatureList::IsEnabled(features::kTreesInViz);
+}
+
 using LayerTreeHostTest = LayerTreeTest;
 
 class LayerTreeHostTestHasImplThreadTest : public LayerTreeHostTest {
@@ -3187,9 +3191,14 @@
     // add tiling, it will be gone by the time we draw because of aggressive
     // cleanup. AddTilingUntilNextDraw ensures that it remains there during
     // damage calculation.
+    // In TreesInViz mode, we query viz before deleting a tiling, so its
+    // removal is delayed comparing with non TreesInViz mode and there is
+    // no need to add it again in the next frame.
     FakePictureLayerImpl* child_layer_impl = static_cast<FakePictureLayerImpl*>(
         host_impl->active_tree()->LayerById(child_layer_->id()));
-    child_layer_impl->AddTilingUntilNextDraw(1.3f);
+    if (!TreesInViz() || host_impl->active_tree()->source_frame_number() == 0) {
+      child_layer_impl->AddTilingUntilNextDraw(1.3f);
+    }
   }
 
   void BeginTest() override { PostSetNeedsCommitToMainThread(); }
@@ -3216,10 +3225,16 @@
         FakePictureLayerImpl* child_layer_impl =
             static_cast<FakePictureLayerImpl*>(
                 host_impl->active_tree()->LayerById(child_layer_->id()));
-        // We remove tilings pretty aggressively if they are not ideal. Add this
-        // back in so that we can compare
-        // child_layer_impl->GetEnclosingVisibleRectInTargetSpace to the damage.
-        child_layer_impl->AddTilingUntilNextDraw(1.3f);
+        if (!TreesInViz()) {
+          // We remove tilings pretty aggressively if they are not ideal.
+          // Add this back in so that we can compare
+          // child_layer_impl->GetEnclosingVisibleRectInTargetSpace to the
+          // damage.
+          // In TreesInViz mode, we query viz before deleting a tiling, so its
+          // removal is delayed comparing with non TreesInViz mode and there is
+          // no need to add it again in the next frame.
+          child_layer_impl->AddTilingUntilNextDraw(1.3f);
+        }
 
         EXPECT_EQ(gfx::Rect(26, 26), root_damage_rect);
         EXPECT_EQ(child_layer_impl->GetEnclosingVisibleRectInTargetSpace(),
@@ -4472,10 +4487,6 @@
         FakeScopedUIResource::Create(layer_tree_host()->GetUIResourceManager());
   }
 
-  bool TreesInViz() {
-    return base::FeatureList::IsEnabled(features::kTreesInViz);
-  }
-
   std::array<std::unique_ptr<FakeScopedUIResource>, 5> ui_resources_;
   int num_ui_resources_;
 };
diff --git a/cc/trees/layer_tree_host_unittest_picture.cc b/cc/trees/layer_tree_host_unittest_picture.cc
index e3959ee..f4bb0094f 100644
--- a/cc/trees/layer_tree_host_unittest_picture.cc
+++ b/cc/trees/layer_tree_host_unittest_picture.cc
@@ -18,6 +18,10 @@
 namespace cc {
 namespace {
 
+bool TreesInViz() {
+  return base::FeatureList::IsEnabled(features::kTreesInViz);
+}
+
 // These tests deal with picture layers.
 class LayerTreeHostPictureTest : public LayerTreeTest {
  protected:
@@ -640,14 +644,25 @@
         break;
       case 1:
         // On 2nd commit after scaling up to 2, the normal layer will adjust its
-        // scale and the will change layer should not (as it is will change.
+        // scale and the will change layer should not (as it is will change).
         ASSERT_EQ(1u, will_change_layer->tilings()->num_tilings());
         EXPECT_EQ(
             gfx::AxisTransform2d(),
             will_change_layer->tilings()->tiling_at(0)->raster_transform());
-        ASSERT_EQ(1u, normal_layer->tilings()->num_tilings());
-        EXPECT_EQ(gfx::AxisTransform2d(2.f, gfx::Vector2dF()),
-                  normal_layer->tilings()->tiling_at(0)->raster_transform());
+        if (TreesInViz()) {
+          // In TreesInViz mode, we query viz before deleting a tiling, so its
+          // removal is delayed comparing with non TreesInViz mode and there is
+          // no need to add it again in the next frame.
+          ASSERT_EQ(2u, normal_layer->tilings()->num_tilings());
+          EXPECT_EQ(gfx::AxisTransform2d(2.f, gfx::Vector2dF()),
+                    normal_layer->tilings()->tiling_at(0)->raster_transform());
+          EXPECT_EQ(gfx::AxisTransform2d(),
+                    normal_layer->tilings()->tiling_at(1)->raster_transform());
+        } else {
+          ASSERT_EQ(1u, normal_layer->tilings()->num_tilings());
+          EXPECT_EQ(gfx::AxisTransform2d(2.f, gfx::Vector2dF()),
+                    normal_layer->tilings()->tiling_at(0)->raster_transform());
+        }
 
         MainThreadTaskRunner()->PostTask(
             FROM_HERE,
@@ -658,13 +673,36 @@
       case 2:
         // On 3rd commit, both layers should adjust scales due to forced
         // recalculating.
-        ASSERT_EQ(1u, will_change_layer->tilings()->num_tilings());
-        EXPECT_EQ(
-            gfx::AxisTransform2d(4.f, gfx::Vector2dF()),
-            will_change_layer->tilings()->tiling_at(0)->raster_transform());
-        ASSERT_EQ(1u, normal_layer->tilings()->num_tilings());
-        EXPECT_EQ(gfx::AxisTransform2d(4.f, gfx::Vector2dF()),
-                  normal_layer->tilings()->tiling_at(0)->raster_transform());
+        if (TreesInViz()) {
+          // In TreesInViz mode, we query viz before deleting a tiling, so its
+          // removal is delayed comparing with non TreesInViz mode and there is
+          // no need to add it again in the next frame.
+          ASSERT_EQ(2u, will_change_layer->tilings()->num_tilings());
+          EXPECT_EQ(
+              gfx::AxisTransform2d(4.f, gfx::Vector2dF()),
+              will_change_layer->tilings()->tiling_at(0)->raster_transform());
+          EXPECT_EQ(
+              gfx::AxisTransform2d(),
+              will_change_layer->tilings()->tiling_at(1)->raster_transform());
+
+          ASSERT_EQ(3u, normal_layer->tilings()->num_tilings());
+          EXPECT_EQ(gfx::AxisTransform2d(4.f, gfx::Vector2dF()),
+                    normal_layer->tilings()->tiling_at(0)->raster_transform());
+          EXPECT_EQ(gfx::AxisTransform2d(2.f, gfx::Vector2dF()),
+                    normal_layer->tilings()->tiling_at(1)->raster_transform());
+          EXPECT_EQ(gfx::AxisTransform2d(),
+                    normal_layer->tilings()->tiling_at(2)->raster_transform());
+        } else {
+          ASSERT_EQ(1u, will_change_layer->tilings()->num_tilings());
+          EXPECT_EQ(
+              gfx::AxisTransform2d(4.f, gfx::Vector2dF()),
+              will_change_layer->tilings()->tiling_at(0)->raster_transform());
+
+          ASSERT_EQ(1u, normal_layer->tilings()->num_tilings());
+          EXPECT_EQ(gfx::AxisTransform2d(4.f, gfx::Vector2dF()),
+                    normal_layer->tilings()->tiling_at(0)->raster_transform());
+        }
+
         EndTest();
         break;
     }
diff --git a/testing/buildbot/filters/trees_in_viz.cc_unittests.filter b/testing/buildbot/filters/trees_in_viz.cc_unittests.filter
index 713141a4..6b8aadb 100644
--- a/testing/buildbot/filters/trees_in_viz.cc_unittests.filter
+++ b/testing/buildbot/filters/trees_in_viz.cc_unittests.filter
@@ -2,11 +2,6 @@
 # doesn't do in TreesInViz mode.
 -CommitToPendingTreeLayerTreeHostImplTest.FarAwayQuadsDontNeedAA
 
-# TODO(crbug.com/406251942) Investigate PictureLayerTilingSet::CleanUpTilings path
--LayerTreeHostPictureTestForceRecalculateScales.RunSingleThread_DelegatingRenderer
--LayerTreeHostTestDamageWithScale.RunMultiThread_DelegatingRenderer
--LayerTreeHostTestDamageWithScale.RunSingleThread_DelegatingRenderer
-
 # TODO(crbug.com/448874765): Fix RunMultiThread_DelegatingRenderer unittests
 -LayerTreeHostAnimationTestNoDamageAnimation.RunMultiThread_DelegatingRenderer
 -LayerTreeHostTestVideoLayerInvalidate.RunSingleThread_DelegatingRenderer