Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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" | |
| 6 #include "base/run_loop.h" | |
| 7 #include "content/public/browser/render_frame_host.h" | |
| 8 #include "content/public/browser/web_contents.h" | |
| 9 #include "content/public/common/content_switches.h" | |
| 10 #include "content/public/renderer/render_view.h" | |
| 11 #include "content/public/renderer/render_view_observer.h" | |
| 12 #include "content/public/test/browser_test_utils.h" | |
| 13 #include "content/public/test/content_browser_test.h" | |
| 14 #include "content/public/test/content_browser_test_utils.h" | |
| 15 #include "content/public/test/test_utils.h" | |
| 16 #include "content/renderer/render_frame_impl.h" | |
| 17 #include "content/shell/browser/shell.h" | |
| 18 | |
| 19 namespace { | |
| 20 // The first RenderFrame is routing ID 1, and the first RenderView is 2. | |
| 21 const int kRenderViewRoutingId = 2; | |
|
Sami
2015/02/18 16:12:22
Is there a way to query this, anyone? This seems a
Ignacio Solla
2015/02/18 16:42:34
Hardcoding it seems to be an extended practice in
| |
| 22 | |
| 23 const std::string kBlankUrl = "about:blank"; | |
| 24 | |
| 25 // Timeout to wait for a commit. This number has been obtained by trial and | |
| 26 // error. Try to keep this number as low as possible as one of the tests | |
| 27 // will wait for this long. If the tests flake or start failing increasing this | |
| 28 // number will likely fix it. | |
| 29 const base::TimeDelta kCommitTimeout = base::TimeDelta::FromMilliseconds(150); | |
|
piman
2015/02/18 16:37:18
Please no timeout. This is a sure way to make this
Ignacio Solla
2015/02/18 17:01:59
Without the timeout this test is useless. If the t
| |
| 30 } | |
| 31 | |
| 32 namespace content { | |
| 33 | |
| 34 class CommitObserver : public RenderViewObserver { | |
| 35 public: | |
| 36 CommitObserver(RenderView* render_view, const base::Closure& quit_closure) | |
| 37 : RenderViewObserver(render_view), | |
| 38 quit_closure_(quit_closure), | |
| 39 commit_count_(0) {} | |
| 40 | |
| 41 void DidCommitCompositorFrame() override { | |
| 42 commit_count_++; | |
| 43 quit_closure_.Run(); | |
| 44 } | |
| 45 | |
| 46 int GetCommitCount() { return commit_count_; } | |
| 47 | |
| 48 private: | |
| 49 base::Closure quit_closure_; | |
| 50 int commit_count_; | |
| 51 }; | |
| 52 | |
| 53 class VisualStateTest : public ContentBrowserTest { | |
| 54 public: | |
| 55 VisualStateTest() : callback_count_(0) {} | |
| 56 | |
| 57 void SetUpCommandLine(base::CommandLine* command_line) override { | |
| 58 command_line->AppendSwitch(switches::kSingleProcess); | |
|
Sami
2015/02/18 16:12:22
Do we really need to enforce this?
piman
2015/02/18 16:37:18
Indeed, I don't think we want to run tests in that
Ignacio Solla
2015/02/18 16:42:34
Yes, to be able to use PostTaskToInProcessRenderer
| |
| 59 } | |
| 60 | |
| 61 // Waits at most kCommitTimeout and verifies that a single | |
| 62 // commit has completed. | |
| 63 void WaitForOneCommit() { WaitForCommits(1); } | |
| 64 | |
| 65 // Waits for kCommitTimeout and verifies that no commits have | |
| 66 // completed. | |
| 67 void WaitForZeroCommits() { WaitForCommits(0); } | |
| 68 | |
| 69 void InvokeVisualStateCallback(bool result) { | |
| 70 EXPECT_TRUE(result); | |
| 71 callback_count_++; | |
| 72 } | |
| 73 | |
| 74 int GetCallbackCount() { return callback_count_; } | |
| 75 | |
| 76 private: | |
| 77 RenderView* GetRenderView() { | |
| 78 // We could have the test on the UI thread get the WebContent's routing ID, | |
| 79 // but we know this will be the first RV so skip that and just hardcode it. | |
|
piman
2015/02/18 16:37:18
please no.
One day something will change in how th
Ignacio Solla
2015/02/18 17:01:59
Hardcoding it seems to be an extended practice:
ht
| |
| 80 return RenderView::FromRoutingID(kRenderViewRoutingId); | |
| 81 } | |
| 82 | |
| 83 RenderFrameImpl* GetRenderFrameImpl() { | |
| 84 return static_cast<RenderFrameImpl*>(GetRenderView()->GetMainRenderFrame()); | |
| 85 } | |
| 86 | |
| 87 void WaitForCommits(int commit_count) { | |
| 88 scoped_refptr<MessageLoopRunner> runner = new MessageLoopRunner; | |
| 89 CommitObserver observer(GetRenderView(), runner->QuitClosure()); | |
| 90 base::MessageLoop::current()->PostDelayedTask( | |
| 91 FROM_HERE, runner->QuitClosure(), kCommitTimeout); | |
| 92 runner->Run(); | |
| 93 EXPECT_EQ(commit_count, observer.GetCommitCount()); | |
| 94 } | |
| 95 | |
| 96 int callback_count_; | |
| 97 }; | |
| 98 | |
| 99 // These two tests (ie. CallbackDoesNotDeadlockPart[1|2]) verify that visual | |
| 100 // state callbacks do not deadlock. In other words, the visual state callback | |
| 101 // should be received even if there are no pending updates or commits. To | |
| 102 // achieve that the request to insert the callback requests a new commit that | |
| 103 // should complete successfully (ie. LayerTreeHost::CommitComplete()). | |
| 104 // CallbackDoesNotDeadlockPart1: verifies that this commit goes through. | |
| 105 // CallbackDoesNotDeadlockPart2: verifies that loading kBlankUrl only requires a | |
| 106 // single commit, so that the second commit in CallbackDoesNotDeadlockPart1 is | |
| 107 // indeed caused by inserting the visual state callback. | |
| 108 // Please see crbug/458577 for more details. | |
| 109 IN_PROC_BROWSER_TEST_F(VisualStateTest, CallbackDoesNotDeadlockPart1) { | |
| 110 // By part 2 we know that loading kBlankUrl requires a single commit. | |
| 111 GURL url(kBlankUrl); | |
| 112 NavigateToURL(shell(), url); | |
| 113 | |
| 114 // Wait for the commit corresponding to the load. | |
| 115 PostTaskToInProcessRendererAndWait( | |
| 116 base::Bind(&VisualStateTest::WaitForOneCommit, base::Unretained(this))); | |
| 117 | |
| 118 // Insert a visual state callback. At this point we know by part 2 that there | |
| 119 // are no pending commits. | |
| 120 shell()->web_contents()->GetMainFrame()->InsertVisualStateCallback(base::Bind( | |
| 121 &VisualStateTest::InvokeVisualStateCallback, base::Unretained(this))); | |
| 122 | |
| 123 // Verify that the callback is invoked and a new commit completed. | |
| 124 PostTaskToInProcessRendererAndWait( | |
| 125 base::Bind(&VisualStateTest::WaitForOneCommit, base::Unretained(this))); | |
| 126 EXPECT_EQ(1, GetCallbackCount()); | |
| 127 } | |
| 128 | |
| 129 IN_PROC_BROWSER_TEST_F(VisualStateTest, CallbackDoesNotDeadlockPart2) { | |
| 130 // Load kBlankUrl and wait for its commit. | |
| 131 GURL url(kBlankUrl); | |
| 132 NavigateToURL(shell(), url); | |
| 133 PostTaskToInProcessRendererAndWait( | |
| 134 base::Bind(&VisualStateTest::WaitForOneCommit, base::Unretained(this))); | |
| 135 | |
| 136 // Verify that a second commit does not happen. Please note that in part 1 | |
| 137 // the second commit happens within kCommitTimeout and here we wait for that | |
| 138 // long so we get a high level of confidence that this second commit will not | |
| 139 // happen (and manual tests in which we waited for much longer confirmed that | |
| 140 // it never happens) | |
| 141 PostTaskToInProcessRendererAndWait( | |
| 142 base::Bind(&VisualStateTest::WaitForZeroCommits, base::Unretained(this))); | |
| 143 EXPECT_EQ(0, GetCallbackCount()); | |
| 144 } | |
| 145 | |
| 146 } // namespace content | |
| OLD | NEW |