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 content { | |
| 20 | |
| 21 class CommitObserver : public RenderViewObserver { | |
| 22 public: | |
| 23 CommitObserver(RenderView* render_view, const base::Closure& quit_closure) | |
| 24 : RenderViewObserver(render_view), | |
| 25 quit_closure_(quit_closure), | |
| 26 commit_count_(0) {} | |
| 27 | |
| 28 void DidCommitCompositorFrame() override { | |
| 29 commit_count_++; | |
| 30 quit_closure_.Run(); | |
| 31 } | |
| 32 | |
| 33 int GetCommitCount() { return commit_count_; } | |
| 34 | |
| 35 private: | |
| 36 base::Closure quit_closure_; | |
| 37 int commit_count_; | |
| 38 }; | |
| 39 | |
| 40 class VisualStateTest : public ContentBrowserTest { | |
| 41 public: | |
| 42 VisualStateTest() : callback_count_(0) {} | |
| 43 | |
| 44 void SetUpCommandLine(base::CommandLine* command_line) override { | |
| 45 command_line->AppendSwitch(switches::kSingleProcess); | |
| 46 } | |
| 47 | |
| 48 void WaitForCommit(int routing_id) { | |
| 49 scoped_refptr<MessageLoopRunner> runner = new MessageLoopRunner; | |
| 50 CommitObserver observer(GetRenderView(routing_id), runner->QuitClosure()); | |
| 51 runner->Run(); | |
| 52 EXPECT_EQ(1, observer.GetCommitCount()); | |
|
Sami
2015/02/19 10:59:34
I think if you add a DCHECK For MessageLoop::IsIdl
Ignacio Solla
2015/02/19 11:53:54
Done.
| |
| 53 } | |
| 54 | |
| 55 void InvokeVisualStateCallback(bool result) { | |
| 56 EXPECT_TRUE(result); | |
| 57 callback_count_++; | |
| 58 } | |
| 59 | |
| 60 int GetCallbackCount() { return callback_count_; } | |
| 61 | |
| 62 private: | |
| 63 RenderView* GetRenderView(int routing_id) { | |
| 64 return RenderView::FromRoutingID(routing_id); | |
| 65 } | |
| 66 | |
| 67 int callback_count_; | |
| 68 }; | |
| 69 | |
| 70 // This test verifies that visual state callbacks do not deadlock. In other | |
| 71 // words, the visual state callback should be received even if there are no | |
| 72 // pending updates or commits. | |
| 73 IN_PROC_BROWSER_TEST_F(VisualStateTest, CallbackDoesNotDeadlock) { | |
| 74 // This test relies on the fact that loading "about:blank" only requires a | |
| 75 // single commit. We first load "about:blank" and wait for this single | |
| 76 // commit. At that point we know that the page has stabilized and no | |
| 77 // further commits are expected. We then insert a visual state callback | |
| 78 // and verify that this causes an additional commit in order to deliver | |
| 79 // the callback. | |
| 80 // Unfortunately, if loading "about:blank" changes and starts requiring | |
| 81 // two commits then this test will prove nothing. We could detect this | |
| 82 // with a high level of confidence if we used a timeout, but that's | |
| 83 // discouraged (see https://codereview.chromium.org/939673002). | |
| 84 NavigateToURL(shell(), GURL("about:blank")); | |
| 85 | |
| 86 // Wait for the commit corresponding to the load. | |
| 87 PostTaskToInProcessRendererAndWait( | |
| 88 base::Bind(&VisualStateTest::WaitForCommit, base::Unretained(this), | |
| 89 shell()->web_contents()->GetRoutingID())); | |
| 90 | |
| 91 // Insert a visual state callback. | |
| 92 shell()->web_contents()->GetMainFrame()->InsertVisualStateCallback(base::Bind( | |
| 93 &VisualStateTest::InvokeVisualStateCallback, base::Unretained(this))); | |
| 94 | |
| 95 // Verify that the callback is invoked and a new commit completed. | |
| 96 PostTaskToInProcessRendererAndWait( | |
| 97 base::Bind(&VisualStateTest::WaitForCommit, base::Unretained(this), | |
| 98 shell()->web_contents()->GetRoutingID())); | |
| 99 EXPECT_EQ(1, GetCallbackCount()); | |
| 100 } | |
| 101 | |
| 102 } // namespace content | |
| OLD | NEW |