Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2012 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 "content/browser/web_contents/web_contents_view_aura.h" | |
| 6 | |
| 7 #include "base/command_line.h" | |
| 8 #include "base/run_loop.h" | |
| 9 #include "base/test/test_timeouts.h" | |
| 10 #include "base/utf_string_conversions.h" | |
| 11 #include "content/browser/renderer_host/render_view_host_impl.h" | |
| 12 #include "content/browser/web_contents/web_contents_impl.h" | |
| 13 #include "content/public/common/content_switches.h" | |
| 14 #include "content/public/test/browser_test_utils.h" | |
| 15 #include "content/public/test/test_utils.h" | |
| 16 #include "content/shell/shell.h" | |
| 17 #include "content/test/content_browser_test.h" | |
| 18 #include "content/test/content_browser_test_utils.h" | |
| 19 #include "ui/aura/root_window.h" | |
| 20 #include "ui/aura/test/event_generator.h" | |
| 21 #include "ui/aura/window.h" | |
| 22 | |
| 23 namespace content { | |
| 24 | |
| 25 class WebContentsViewAuraTest : public ContentBrowserTest { | |
| 26 public: | |
| 27 WebContentsViewAuraTest() {} | |
| 28 | |
| 29 virtual void SetUpCommandLine(CommandLine* command_line) { | |
| 30 command_line->AppendSwitch(switches::kEnableOverscrollHistoryNavigation); | |
| 31 } | |
| 32 | |
| 33 // Executes the javascript synchronously and makes sure the returned value is | |
| 34 // freed properly. | |
| 35 void ExecuteSyncJSFunction(RenderViewHost* rvh, const std::string& jscript) { | |
| 36 scoped_ptr<base::Value> value(rvh->ExecuteJavascriptAndGetValue( | |
| 37 string16(), ASCIIToUTF16(jscript))); | |
| 38 } | |
| 39 | |
| 40 // Starts the test server and navigates to the given url. Sets a large enough | |
| 41 // size to the root window. Returns after the navigation to the url is | |
| 42 // complete. | |
| 43 void StartTestWithPage(const std::string& url) { | |
| 44 ASSERT_TRUE(test_server()->Start()); | |
| 45 GURL test_url(test_server()->GetURL(url)); | |
| 46 NavigateToURL(shell(), test_url); | |
| 47 aura::Window* content = shell()->web_contents()->GetContentNativeView(); | |
| 48 content->GetRootWindow()->SetHostSize(gfx::Size(800, 600)); | |
| 49 } | |
| 50 | |
| 51 void TestOverscrollNavigation(bool touch_handler) { | |
| 52 StartTestWithPage("files/overscroll_navigation.html"); | |
|
sky
2012/11/19 17:14:35
Sine StartTestWithPage may assert you need to wrap
sadrul
2012/11/19 17:22:00
Nice! Didn't know about this. Thanks!
| |
| 53 WebContentsImpl* web_contents = | |
| 54 static_cast<WebContentsImpl*>(shell()->web_contents()); | |
| 55 NavigationController& controller = web_contents->GetController(); | |
| 56 RenderViewHostImpl* view_host = static_cast<RenderViewHostImpl*>( | |
| 57 web_contents->GetRenderViewHost()); | |
| 58 | |
| 59 EXPECT_FALSE(controller.CanGoBack()); | |
| 60 EXPECT_FALSE(controller.CanGoForward()); | |
| 61 int index = -1; | |
| 62 scoped_ptr<base::Value> value; | |
| 63 value.reset(view_host->ExecuteJavascriptAndGetValue(string16(), | |
| 64 ASCIIToUTF16("get_current()"))); | |
| 65 ASSERT_TRUE(value->GetAsInteger(&index)); | |
| 66 EXPECT_EQ(0, index); | |
| 67 | |
| 68 if (touch_handler) | |
| 69 ExecuteSyncJSFunction(view_host, "install_touch_handler()"); | |
| 70 | |
| 71 ExecuteSyncJSFunction(view_host, "navigate_next()"); | |
| 72 ExecuteSyncJSFunction(view_host, "navigate_next()"); | |
| 73 value.reset(view_host->ExecuteJavascriptAndGetValue(string16(), | |
| 74 ASCIIToUTF16("get_current()"))); | |
| 75 ASSERT_TRUE(value->GetAsInteger(&index)); | |
| 76 EXPECT_EQ(2, index); | |
| 77 EXPECT_TRUE(controller.CanGoBack()); | |
| 78 EXPECT_FALSE(controller.CanGoForward()); | |
| 79 | |
| 80 aura::Window* content = web_contents->GetContentNativeView(); | |
| 81 gfx::Rect bounds = content->GetBoundsInRootWindow(); | |
| 82 aura::test::EventGenerator generator(content->GetRootWindow(), content); | |
| 83 | |
| 84 { | |
| 85 // Do a swipe-right now. That should navigate backwards. | |
| 86 string16 expected_title = ASCIIToUTF16("Title: #1"); | |
| 87 content::TitleWatcher title_watcher(web_contents, expected_title); | |
| 88 generator.GestureScrollSequence( | |
| 89 gfx::Point(bounds.x() + 2, bounds.y() + 10), | |
| 90 gfx::Point(bounds.right() - 10, bounds.y() + 10), | |
| 91 base::TimeDelta::FromMilliseconds(20), | |
| 92 1); | |
| 93 string16 actual_title = title_watcher.WaitAndGetTitle(); | |
| 94 EXPECT_EQ(expected_title, actual_title); | |
| 95 value.reset(view_host->ExecuteJavascriptAndGetValue(string16(), | |
| 96 ASCIIToUTF16("get_current()"))); | |
| 97 ASSERT_TRUE(value->GetAsInteger(&index)); | |
| 98 EXPECT_EQ(1, index); | |
| 99 EXPECT_TRUE(controller.CanGoBack()); | |
| 100 EXPECT_TRUE(controller.CanGoForward()); | |
| 101 } | |
| 102 | |
| 103 { | |
| 104 // Do a fling-right now. That should navigate backwards. | |
| 105 string16 expected_title = ASCIIToUTF16("Title:"); | |
| 106 content::TitleWatcher title_watcher(web_contents, expected_title); | |
| 107 generator.GestureScrollSequence( | |
| 108 gfx::Point(bounds.x() + 2, bounds.y() + 10), | |
| 109 gfx::Point(bounds.right() - 10, bounds.y() + 10), | |
| 110 base::TimeDelta::FromMilliseconds(20), | |
| 111 10); | |
| 112 string16 actual_title = title_watcher.WaitAndGetTitle(); | |
| 113 EXPECT_EQ(expected_title, actual_title); | |
| 114 value.reset(view_host->ExecuteJavascriptAndGetValue(string16(), | |
| 115 ASCIIToUTF16("get_current()"))); | |
| 116 ASSERT_TRUE(value->GetAsInteger(&index)); | |
| 117 EXPECT_EQ(0, index); | |
| 118 EXPECT_FALSE(controller.CanGoBack()); | |
| 119 EXPECT_TRUE(controller.CanGoForward()); | |
| 120 } | |
| 121 | |
| 122 { | |
| 123 // Do a swipe-left now. That should navigate forward. | |
| 124 string16 expected_title = ASCIIToUTF16("Title: #1"); | |
| 125 content::TitleWatcher title_watcher(web_contents, expected_title); | |
| 126 generator.GestureScrollSequence( | |
| 127 gfx::Point(bounds.right() - 10, bounds.y() + 10), | |
| 128 gfx::Point(bounds.x() + 2, bounds.y() + 10), | |
| 129 base::TimeDelta::FromMilliseconds(20), | |
| 130 10); | |
| 131 string16 actual_title = title_watcher.WaitAndGetTitle(); | |
| 132 EXPECT_EQ(expected_title, actual_title); | |
| 133 value.reset(view_host->ExecuteJavascriptAndGetValue(string16(), | |
| 134 ASCIIToUTF16("get_current()"))); | |
| 135 ASSERT_TRUE(value->GetAsInteger(&index)); | |
| 136 EXPECT_EQ(1, index); | |
| 137 EXPECT_TRUE(controller.CanGoBack()); | |
| 138 EXPECT_TRUE(controller.CanGoForward()); | |
| 139 } | |
| 140 } | |
| 141 | |
| 142 private: | |
| 143 DISALLOW_COPY_AND_ASSIGN(WebContentsViewAuraTest); | |
| 144 }; | |
| 145 | |
| 146 IN_PROC_BROWSER_TEST_F(WebContentsViewAuraTest, | |
| 147 OverscrollNavigation) { | |
| 148 TestOverscrollNavigation(false); | |
| 149 } | |
| 150 | |
| 151 IN_PROC_BROWSER_TEST_F(WebContentsViewAuraTest, | |
| 152 OverscrollNavigationWithTouchHandler) { | |
| 153 TestOverscrollNavigation(true); | |
| 154 } | |
| 155 | |
| 156 } // namespace content | |
| OLD | NEW |