tobiasjs | 609b99f8 | 2015-02-27 10:45:52 | [diff] [blame] | 1 | // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "base/command_line.h" |
Gabriel Charette | a6b9364 | 2018-04-24 22:01:34 | [diff] [blame^] | 6 | #include "base/message_loop/message_loop_current.h" |
tobiasjs | 609b99f8 | 2015-02-27 10:45:52 | [diff] [blame] | 7 | #include "base/run_loop.h" |
| 8 | #include "content/public/browser/render_frame_host.h" |
davidsac | 996be11 | 2016-11-30 08:56:38 | [diff] [blame] | 9 | #include "content/public/browser/render_view_host.h" |
tobiasjs | 609b99f8 | 2015-02-27 10:45:52 | [diff] [blame] | 10 | #include "content/public/browser/web_contents.h" |
| 11 | #include "content/public/common/content_switches.h" |
| 12 | #include "content/public/renderer/render_view.h" |
| 13 | #include "content/public/renderer/render_view_observer.h" |
| 14 | #include "content/public/test/browser_test_utils.h" |
| 15 | #include "content/public/test/content_browser_test.h" |
| 16 | #include "content/public/test/content_browser_test_utils.h" |
| 17 | #include "content/public/test/test_utils.h" |
| 18 | #include "content/renderer/render_frame_impl.h" |
| 19 | #include "content/shell/browser/shell.h" |
| 20 | |
| 21 | namespace content { |
| 22 | |
| 23 | class CommitObserver : public RenderViewObserver { |
| 24 | public: |
| 25 | CommitObserver(RenderView* render_view) |
| 26 | : RenderViewObserver(render_view), quit_closures_(), commit_count_(0) {} |
| 27 | |
| 28 | void DidCommitCompositorFrame() override { |
| 29 | commit_count_++; |
| 30 | for (base::Closure* closure : quit_closures_) { |
| 31 | closure->Run(); |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | void QuitAfterCommit(int commit_number, |
| 36 | scoped_refptr<MessageLoopRunner> runner) { |
| 37 | if (commit_number >= commit_count_) runner->Quit(); |
| 38 | } |
| 39 | |
| 40 | void WaitForCommitNumber(int commit_number) { |
| 41 | if (commit_number > commit_count_) { |
| 42 | scoped_refptr<MessageLoopRunner> runner = new MessageLoopRunner; |
| 43 | base::Closure quit_closure = |
| 44 | base::Bind(&CommitObserver::QuitAfterCommit, base::Unretained(this), |
| 45 | commit_number, runner); |
| 46 | quit_closures_.insert(&quit_closure); |
| 47 | runner->Run(); |
| 48 | quit_closures_.erase(&quit_closure); |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | int GetCommitCount() { return commit_count_; } |
| 53 | |
| 54 | private: |
xjz | 694b50a9 | 2016-06-07 21:49:37 | [diff] [blame] | 55 | // RenderViewObserver implementation. |
| 56 | void OnDestruct() override { delete this; } |
| 57 | |
tobiasjs | 609b99f8 | 2015-02-27 10:45:52 | [diff] [blame] | 58 | std::set<base::Closure*> quit_closures_; |
| 59 | int commit_count_; |
| 60 | }; |
| 61 | |
| 62 | class VisualStateTest : public ContentBrowserTest { |
| 63 | public: |
| 64 | VisualStateTest() : callback_count_(0) {} |
| 65 | |
| 66 | void SetUpCommandLine(base::CommandLine* command_line) override { |
| 67 | command_line->AppendSwitch(switches::kSingleProcess); |
| 68 | } |
| 69 | |
| 70 | void WaitForCommit(CommitObserver *observer, int commit_number) { |
| 71 | observer->WaitForCommitNumber(commit_number); |
| 72 | EXPECT_EQ(commit_number, observer->GetCommitCount()); |
| 73 | } |
| 74 | |
| 75 | void AssertIsIdle() { |
Gabriel Charette | a6b9364 | 2018-04-24 22:01:34 | [diff] [blame^] | 76 | ASSERT_TRUE(base::MessageLoopCurrent::Get()->IsIdleForTesting()); |
tobiasjs | 609b99f8 | 2015-02-27 10:45:52 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | void InvokeVisualStateCallback(bool result) { |
| 80 | EXPECT_TRUE(result); |
| 81 | callback_count_++; |
| 82 | } |
| 83 | |
| 84 | int GetCallbackCount() { return callback_count_; } |
| 85 | |
| 86 | private: |
| 87 | int callback_count_; |
| 88 | }; |
| 89 | |
| 90 | // This test verifies that visual state callbacks do not deadlock. In other |
| 91 | // words, the visual state callback should be received even if there are no |
| 92 | // pending updates or commits. |
grt | 9e383ca | 2015-02-27 15:36:06 | [diff] [blame] | 93 | // Disabled due to cross-platform flakes; http://crbug.com/462580. |
| 94 | IN_PROC_BROWSER_TEST_F(VisualStateTest, DISABLED_CallbackDoesNotDeadlock) { |
tobiasjs | 609b99f8 | 2015-02-27 10:45:52 | [diff] [blame] | 95 | // This test relies on the fact that loading "about:blank" only requires a |
| 96 | // single commit. We first load "about:blank" and wait for this single |
| 97 | // commit. At that point we know that the page has stabilized and no |
| 98 | // further commits are expected. We then insert a visual state callback |
| 99 | // and verify that this causes an additional commit in order to deliver |
| 100 | // the callback. |
| 101 | // Unfortunately, if loading "about:blank" changes and starts requiring |
| 102 | // two commits then this test will prove nothing. We could detect this |
| 103 | // with a high level of confidence if we used a timeout, but that's |
| 104 | // discouraged (see https://codereview.chromium.org/939673002). |
| 105 | NavigateToURL(shell(), GURL("about:blank")); |
davidsac | 996be11 | 2016-11-30 08:56:38 | [diff] [blame] | 106 | CommitObserver observer(RenderView::FromRoutingID( |
| 107 | shell()->web_contents()->GetRenderViewHost()->GetRoutingID())); |
tobiasjs | 609b99f8 | 2015-02-27 10:45:52 | [diff] [blame] | 108 | |
| 109 | // Wait for the commit corresponding to the load. |
| 110 | |
| 111 | PostTaskToInProcessRendererAndWait(base::Bind( |
| 112 | &VisualStateTest::WaitForCommit, base::Unretained(this), &observer, 1)); |
| 113 | |
| 114 | // Try our best to check that there are no pending updates or commits. |
| 115 | PostTaskToInProcessRendererAndWait( |
| 116 | base::Bind(&VisualStateTest::AssertIsIdle, base::Unretained(this))); |
| 117 | |
| 118 | // Insert a visual state callback. |
| 119 | shell()->web_contents()->GetMainFrame()->InsertVisualStateCallback(base::Bind( |
| 120 | &VisualStateTest::InvokeVisualStateCallback, base::Unretained(this))); |
| 121 | |
| 122 | // Verify that the callback is invoked and a new commit completed. |
| 123 | PostTaskToInProcessRendererAndWait(base::Bind( |
| 124 | &VisualStateTest::WaitForCommit, base::Unretained(this), &observer, 2)); |
| 125 | EXPECT_EQ(1, GetCallbackCount()); |
| 126 | } |
| 127 | |
| 128 | } // namespace content |