Fix black screen when navigating to somewhere after js alert dialog

The renderer's surface should not be destroyed while it is still
visible, because future CompositorFrames to that surface will be
rejected.

Bug: 933374
Change-Id: I46c34bad903f052bfd2ee43e6b673a8ef26fca30
Reviewed-on: https://chromium-review.googlesource.com/c/1495368
Commit-Queue: Saman Sami <samans@chromium.org>
Reviewed-by: Eric Karl <ericrk@chromium.org>
Cr-Commit-Position: refs/heads/master@{#636567}
diff --git a/ui/android/delegated_frame_host_android.cc b/ui/android/delegated_frame_host_android.cc
index 78c270fb..2d82cc8 100644
--- a/ui/android/delegated_frame_host_android.cc
+++ b/ui/android/delegated_frame_host_android.cc
@@ -198,7 +198,7 @@
     content_layer_->RemoveFromParent();
     content_layer_ = nullptr;
   }
-  if (!HasSavedFrame())
+  if (!HasSavedFrame() || frame_evictor_->visible())
     return;
   std::vector<viz::SurfaceId> surface_ids = {
       viz::SurfaceId(frame_sink_id_, local_surface_id_)};
diff --git a/ui/android/delegated_frame_host_android_unittest.cc b/ui/android/delegated_frame_host_android_unittest.cc
index ad53248..2736d64 100644
--- a/ui/android/delegated_frame_host_android_unittest.cc
+++ b/ui/android/delegated_frame_host_android_unittest.cc
@@ -367,16 +367,15 @@
 // Make sure frame evictor is notified of the newly embedded surface after
 // WasShown.
 TEST_F(DelegatedFrameHostAndroidVizTest, EmbedWhileHidden) {
-  {
-    EXPECT_CALL(client_, WasEvicted());
-    frame_host_->EvictDelegatedFrame();
-  }
+  // Ensure there is currently no frame.
+  frame_host_->WasHidden();
+  frame_host_->EvictDelegatedFrame();
   EXPECT_FALSE(frame_host_->HasSavedFrame());
+
   allocator_.GenerateId();
   viz::LocalSurfaceId id =
       allocator_.GetCurrentLocalSurfaceIdAllocation().local_surface_id();
   gfx::Size size(100, 100);
-  frame_host_->WasHidden();
   frame_host_->EmbedSurface(id, size, cc::DeadlinePolicy::UseDefaultDeadline());
   EXPECT_FALSE(frame_host_->HasSavedFrame());
   frame_host_->WasShown(id, size);