web_contents_impl_browsertest.cc revision 424c4d7b64af9d0d8fd9624f381f469654d5e3d2
1c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// Copyright 2013 The Chromium Authors. All rights reserved. 2c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be 3c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// found in the LICENSE file. 4c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) 5c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "base/values.h" 6b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)#include "content/browser/renderer_host/render_view_host_impl.h" 7c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "content/browser/web_contents/web_contents_impl.h" 8c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "content/public/browser/load_notification_details.h" 9c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "content/public/browser/navigation_controller.h" 10c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "content/public/browser/notification_details.h" 11c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "content/public/browser/notification_observer.h" 12c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "content/public/browser/notification_types.h" 13424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)#include "content/public/browser/render_widget_host_view.h" 14868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "content/public/browser/web_contents_observer.h" 15424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)#include "content/public/browser/web_contents_view.h" 1690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "content/public/common/content_paths.h" 17c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "content/public/test/browser_test_utils.h" 18c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "content/public/test/test_utils.h" 193551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)#include "content/shell/browser/shell.h" 20c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "content/test/content_browser_test.h" 21c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "content/test/content_browser_test_utils.h" 2290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "net/test/embedded_test_server/embedded_test_server.h" 23c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) 24c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)namespace content { 25c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) 26424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)void ResizeWebContentsView(Shell* shell, const gfx::Size& size, 27424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) bool set_start_page) { 28424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) // Shell::SizeTo is not implemented on Aura; WebContentsView::SizeContents 29424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) // works on Win and ChromeOS but not Linux - we need to resize the shell 30424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) // window on Linux because if we don't, the next layout of the unchanged shell 31424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) // window will resize WebContentsView back to the previous size. 32424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) // The cleaner and shorter SizeContents is preferred as more platforms convert 33424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) // to Aura. 34424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)#if defined(TOOLKIT_GTK) || defined(OS_MACOSX) 35424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) shell->SizeTo(size.width(), size.height()); 36424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) // If |set_start_page| is true, start with blank page to make sure resize 37424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) // takes effect. 38424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) if (set_start_page) 39424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) NavigateToURL(shell, GURL("about://blank")); 40424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)#else 41424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) shell->web_contents()->GetView()->SizeContents(size); 42424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)#endif // defined(TOOLKIT_GTK) || defined(OS_MACOSX) 43424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)} 44424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) 45c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)class WebContentsImplBrowserTest : public ContentBrowserTest { 46c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) public: 47c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) WebContentsImplBrowserTest() {} 48c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) 49c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) private: 50c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) DISALLOW_COPY_AND_ASSIGN(WebContentsImplBrowserTest); 51c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}; 52c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) 53c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// Keeps track of data from LoadNotificationDetails so we can later verify that 54c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// they are correct, after the LoadNotificationDetails object is deleted. 55c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)class LoadStopNotificationObserver : public WindowedNotificationObserver { 56c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) public: 57c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) LoadStopNotificationObserver(NavigationController* controller) 58c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) : WindowedNotificationObserver(NOTIFICATION_LOAD_STOP, 59c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) Source<NavigationController>(controller)), 60c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) session_index_(-1), 61c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) controller_(NULL) { 62c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) } 63c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) virtual void Observe(int type, 64c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) const NotificationSource& source, 65c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) const NotificationDetails& details) OVERRIDE { 66c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) if (type == NOTIFICATION_LOAD_STOP) { 67c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) const Details<LoadNotificationDetails> load_details(details); 68c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) url_ = load_details->url; 69c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) session_index_ = load_details->session_index; 70c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) controller_ = load_details->controller; 71c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) } 72c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) WindowedNotificationObserver::Observe(type, source, details); 73c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) } 74c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) 75c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) GURL url_; 76c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) int session_index_; 77c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) NavigationController* controller_; 78c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}; 79c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) 80c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// Starts a new navigation as soon as the current one commits, but does not 81c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// wait for it to complete. This allows us to observe DidStopLoading while 82c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// a pending entry is present. 83868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)class NavigateOnCommitObserver : public WebContentsObserver { 84c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) public: 85c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) NavigateOnCommitObserver(Shell* shell, GURL url) 86868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) : WebContentsObserver(shell->web_contents()), 87c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) shell_(shell), 88c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) url_(url), 89c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) done_(false) { 90c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) } 91868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 92868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) // WebContentsObserver: 93868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) virtual void NavigationEntryCommitted( 94868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) const LoadCommittedDetails& load_details) OVERRIDE { 95868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) if (!done_) { 96c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) done_ = true; 97c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) shell_->LoadURL(url_); 98c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) } 99c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) } 100c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) 101c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) Shell* shell_; 102c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) GURL url_; 103c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) bool done_; 104c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}; 105c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) 106424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)class RenderViewSizeDelegate : public WebContentsDelegate { 107424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) public: 108424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) void set_size_insets(const gfx::Size& size_insets) { 109424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) size_insets_ = size_insets; 110424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) } 111424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) 112424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) // WebContentsDelegate: 113424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) virtual gfx::Size GetSizeForNewRenderView( 114424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) const WebContents* web_contents) const OVERRIDE { 115424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) gfx::Size size(web_contents->GetView()->GetContainerSize()); 116424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) size.Enlarge(size_insets_.width(), size_insets_.height()); 117424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) return size; 118424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) } 119424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) 120424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) private: 121424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) gfx::Size size_insets_; 122424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)}; 123424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) 124424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)class RenderViewSizeObserver : public WebContentsObserver { 125424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) public: 126424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) RenderViewSizeObserver(Shell* shell, const gfx::Size& wcv_new_size) 127424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) : WebContentsObserver(shell->web_contents()), 128424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) shell_(shell), 129424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) wcv_new_size_(wcv_new_size) { 130424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) } 131424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) 132424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) // WebContentsObserver: 133424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) virtual void RenderViewCreated(RenderViewHost* rvh) OVERRIDE { 134424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) rwhv_create_size_ = rvh->GetView()->GetViewBounds().size(); 135424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) } 136424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) 137424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) virtual void NavigateToPendingEntry( 138424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) const GURL& url, 139424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) NavigationController::ReloadType reload_type) OVERRIDE { 140424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) ResizeWebContentsView(shell_, wcv_new_size_, false); 141424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) } 142424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) 143424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) gfx::Size rwhv_create_size() const { return rwhv_create_size_; } 144424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) 145424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) private: 146424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) Shell* shell_; // Weak ptr. 147424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) gfx::Size wcv_new_size_; 148424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) gfx::Size rwhv_create_size_; 149424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)}; 150424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) 151c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// Test that DidStopLoading includes the correct URL in the details. 152c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)IN_PROC_BROWSER_TEST_F(WebContentsImplBrowserTest, DidStopLoadingDetails) { 15390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady()); 154c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) 155c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) LoadStopNotificationObserver load_observer( 156c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) &shell()->web_contents()->GetController()); 15790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) NavigateToURL(shell(), embedded_test_server()->GetURL("/title1.html")); 158c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) load_observer.Wait(); 159c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) 16090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) EXPECT_EQ("/title1.html", load_observer.url_.path()); 161c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) EXPECT_EQ(0, load_observer.session_index_); 162c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) EXPECT_EQ(&shell()->web_contents()->GetController(), 163c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) load_observer.controller_); 164c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)} 165c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) 166c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// Test that DidStopLoading includes the correct URL in the details when a 167c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// pending entry is present. 168c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)IN_PROC_BROWSER_TEST_F(WebContentsImplBrowserTest, 169c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) DidStopLoadingDetailsWithPending) { 17090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady()); 171c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) 172c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) // Listen for the first load to stop. 173c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) LoadStopNotificationObserver load_observer( 174c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) &shell()->web_contents()->GetController()); 175c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) // Start a new pending navigation as soon as the first load commits. 176c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) // We will hear a DidStopLoading from the first load as the new load 177c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) // is started. 178c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) NavigateOnCommitObserver commit_observer( 17990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) shell(), embedded_test_server()->GetURL("/title2.html")); 18090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) NavigateToURL(shell(), embedded_test_server()->GetURL("/title1.html")); 181c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) load_observer.Wait(); 182c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) 18390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) EXPECT_EQ("/title1.html", load_observer.url_.path()); 184c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) EXPECT_EQ(0, load_observer.session_index_); 185c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) EXPECT_EQ(&shell()->web_contents()->GetController(), 186c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) load_observer.controller_); 187c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)} 188c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) 189b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)// Test that the browser receives the proper frame attach/detach messages from 190b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)// the renderer and builds proper frame tree. 191b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)IN_PROC_BROWSER_TEST_F(WebContentsImplBrowserTest, FrameTree) { 19290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady()); 193b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 19490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) NavigateToURL(shell(), 19590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) embedded_test_server()->GetURL("/frame_tree/top.html")); 196b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 197b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) WebContentsImpl* wc = static_cast<WebContentsImpl*>(shell()->web_contents()); 198b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) RenderViewHostImpl* rvh = static_cast<RenderViewHostImpl*>( 199b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) wc->GetRenderViewHost()); 200b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) FrameTreeNode* root = wc->GetFrameTreeRootForTesting(); 201b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 202b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) // Check that the root node is properly created with the frame id of the 203b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) // initial navigation. 204b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) EXPECT_EQ(3UL, root->child_count()); 205b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) EXPECT_EQ(std::string(), root->frame_name()); 206b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) EXPECT_EQ(rvh->main_frame_id(), root->frame_id()); 207b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 208b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) EXPECT_EQ(2UL, root->child_at(0)->child_count()); 209b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) EXPECT_STREQ("1-1-name", root->child_at(0)->frame_name().c_str()); 210b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 211b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) // Verify the deepest node exists and has the right name. 212b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) EXPECT_EQ(2UL, root->child_at(2)->child_count()); 213b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) EXPECT_EQ(1UL, root->child_at(2)->child_at(1)->child_count()); 214b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) EXPECT_EQ(0UL, root->child_at(2)->child_at(1)->child_at(0)->child_count()); 215b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) EXPECT_STREQ("3-1-id", 216b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) root->child_at(2)->child_at(1)->child_at(0)->frame_name().c_str()); 217b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 218b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) // Navigate to about:blank, which should leave only the root node of the frame 219b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) // tree in the browser process. 22090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) NavigateToURL(shell(), embedded_test_server()->GetURL("/title1.html")); 221b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 222b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) root = wc->GetFrameTreeRootForTesting(); 223b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) EXPECT_EQ(0UL, root->child_count()); 224b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) EXPECT_EQ(std::string(), root->frame_name()); 225b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) EXPECT_EQ(rvh->main_frame_id(), root->frame_id()); 226b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)} 227b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 228424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)// TODO(sail): enable this for MAC when auto resizing of WebContentsViewCocoa is 229424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)// fixed. 230424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)#if defined(OS_MACOSX) || defined(OS_ANDROID) 231424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)#define MAYBE_GetSizeForNewRenderView DISABLED_GetSizeForNewRenderView 232424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)#else 233424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)#define MAYBE_GetSizeForNewRenderView GetSizeForNewRenderView 234424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)#endif 235424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)// Test that RenderViewHost is created and updated at the size specified by 236424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)// WebContentsDelegate::GetSizeForNewRenderView(). 237424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)IN_PROC_BROWSER_TEST_F(WebContentsImplBrowserTest, 238424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) MAYBE_GetSizeForNewRenderView) { 239424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) scoped_ptr<RenderViewSizeDelegate> delegate(new RenderViewSizeDelegate()); 240424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) shell()->web_contents()->SetDelegate(delegate.get()); 241424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) ASSERT_TRUE(shell()->web_contents()->GetDelegate() == delegate.get()); 242424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) 243424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) // When no size is set, RenderWidgetHostView adopts the size of 244424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) // WebContenntsView. 245424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) NavigateToURL(shell(), GURL("http://foo0.com")); 246424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) EXPECT_EQ(shell()->web_contents()->GetView()->GetContainerSize(), 247424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) shell()->web_contents()->GetRenderWidgetHostView()->GetViewBounds(). 248424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) size()); 249424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) 250424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) // When a size is set, RenderWidgetHostView and WebContentsView honor this 251424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) // size. 252424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) gfx::Size size(300, 300); 253424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) gfx::Size size_insets(-10, -15); 254424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) ResizeWebContentsView(shell(), size, true); 255424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) delegate->set_size_insets(size_insets); 256424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) NavigateToURL(shell(), GURL("http://foo1.com")); 257424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) size.Enlarge(size_insets.width(), size_insets.height()); 258424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) EXPECT_EQ(size, 259424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) shell()->web_contents()->GetRenderWidgetHostView()->GetViewBounds(). 260424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) size()); 261424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) EXPECT_EQ(size, shell()->web_contents()->GetView()->GetContainerSize()); 262424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) 263424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) // If WebContentsView is resized after RenderWidgetHostView is created but 264424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) // before pending navigation entry is committed, both RenderWidgetHostView and 265424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) // WebContentsView use the new size of WebContentsView. 266424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) gfx::Size init_size(200, 200); 267424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) gfx::Size new_size(100, 100); 268424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) size_insets = gfx::Size(-20, -30); 269424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) ResizeWebContentsView(shell(), init_size, true); 270424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) delegate->set_size_insets(size_insets); 271424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) RenderViewSizeObserver observer(shell(), new_size); 272424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) NavigateToURL(shell(), GURL("http://foo2.com")); 273424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) // RenderWidgetHostView is created at specified size. 274424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) init_size.Enlarge(size_insets.width(), size_insets.height()); 275424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) EXPECT_EQ(init_size, observer.rwhv_create_size()); 276424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) // RenderViewSizeObserver resizes WebContentsView in NavigateToPendingEntry, 277424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) // so both WebContentsView and RenderWidgetHostView adopt this new size. 278424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) new_size.Enlarge(size_insets.width(), size_insets.height()); 279424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) EXPECT_EQ(new_size, 280424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) shell()->web_contents()->GetRenderWidgetHostView()->GetViewBounds(). 281424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) size()); 282424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) EXPECT_EQ(new_size, shell()->web_contents()->GetView()->GetContainerSize()); 283424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)} 284424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) 285c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)} // namespace content 286