| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/favicon/favicon_tab_helper.h" | 5 #include "chrome/browser/favicon/favicon_tab_helper.h" |
| 6 #include "chrome/browser/prefs/pref_service.h" | 6 #include "chrome/browser/prefs/pref_service.h" |
| 7 #include "chrome/browser/ui/bookmarks/bookmark_tab_helper.h" | 7 #include "chrome/browser/ui/bookmarks/bookmark_tab_helper.h" |
| 8 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" | 8 #include "chrome/browser/ui/tab_contents/tab_contents.h" |
| 9 #include "chrome/browser/ui/tab_contents/test_tab_contents.h" | 9 #include "chrome/browser/ui/tab_contents/test_tab_contents.h" |
| 10 #include "chrome/common/url_constants.h" | 10 #include "chrome/common/url_constants.h" |
| 11 #include "chrome/test/base/testing_profile.h" | 11 #include "chrome/test/base/testing_profile.h" |
| 12 #include "content/public/browser/navigation_controller.h" | 12 #include "content/public/browser/navigation_controller.h" |
| 13 #include "content/public/browser/site_instance.h" | 13 #include "content/public/browser/site_instance.h" |
| 14 #include "content/public/browser/web_contents.h" | 14 #include "content/public/browser/web_contents.h" |
| 15 #include "content/public/common/referrer.h" | 15 #include "content/public/common/referrer.h" |
| 16 #include "content/public/test/test_browser_thread.h" | 16 #include "content/public/test/test_browser_thread.h" |
| 17 #include "content/public/test/test_renderer_host.h" | 17 #include "content/public/test/test_renderer_host.h" |
| 18 #include "content/public/test/web_contents_tester.h" | 18 #include "content/public/test/web_contents_tester.h" |
| 19 #include "testing/gtest/include/gtest/gtest.h" | 19 #include "testing/gtest/include/gtest/gtest.h" |
| 20 | 20 |
| 21 using content::BrowserThread; | 21 using content::BrowserThread; |
| 22 using content::NavigationController; | 22 using content::NavigationController; |
| 23 using content::RenderViewHost; | 23 using content::RenderViewHost; |
| 24 using content::RenderViewHostTester; | 24 using content::RenderViewHostTester; |
| 25 using content::SiteInstance; | 25 using content::SiteInstance; |
| 26 using content::WebContents; | 26 using content::WebContents; |
| 27 using content::WebContentsTester; | 27 using content::WebContentsTester; |
| 28 | 28 |
| 29 class WebUITest : public TabContentsTestHarness { | 29 class WebUITest : public TabContentsTestHarness { |
| 30 public: | 30 public: |
| 31 WebUITest() : ui_thread_(BrowserThread::UI, MessageLoop::current()) {} | 31 WebUITest() : ui_thread_(BrowserThread::UI, MessageLoop::current()) {} |
| 32 | 32 |
| 33 // Tests navigating with a Web UI from a fresh (nothing pending or committed) | 33 // Tests navigating with a Web UI from a fresh (nothing pending or committed) |
| 34 // state, through pending, committed, then another navigation. The first page | 34 // state, through pending, committed, then another navigation. The first page |
| 35 // ID that we should use is passed as a parameter. We'll use the next two | 35 // ID that we should use is passed as a parameter. We'll use the next two |
| 36 // values. This must be increasing for the life of the tests. | 36 // values. This must be increasing for the life of the tests. |
| 37 static void DoNavigationTest(TabContentsWrapper* wrapper, int page_id) { | 37 static void DoNavigationTest(TabContents* tab_contents, int page_id) { |
| 38 WebContents* contents = wrapper->web_contents(); | 38 WebContents* contents = tab_contents->web_contents(); |
| 39 NavigationController* controller = &contents->GetController(); | 39 NavigationController* controller = &contents->GetController(); |
| 40 | 40 |
| 41 // Start a pending load. | 41 // Start a pending load. |
| 42 GURL new_tab_url(chrome::kChromeUINewTabURL); | 42 GURL new_tab_url(chrome::kChromeUINewTabURL); |
| 43 controller->LoadURL(new_tab_url, content::Referrer(), | 43 controller->LoadURL(new_tab_url, content::Referrer(), |
| 44 content::PAGE_TRANSITION_LINK, | 44 content::PAGE_TRANSITION_LINK, |
| 45 std::string()); | 45 std::string()); |
| 46 | 46 |
| 47 // The navigation entry should be pending with no committed entry. | 47 // The navigation entry should be pending with no committed entry. |
| 48 ASSERT_TRUE(controller->GetPendingEntry()); | 48 ASSERT_TRUE(controller->GetPendingEntry()); |
| 49 ASSERT_FALSE(controller->GetLastCommittedEntry()); | 49 ASSERT_FALSE(controller->GetLastCommittedEntry()); |
| 50 | 50 |
| 51 // Check the things the pending Web UI should have set. | 51 // Check the things the pending Web UI should have set. |
| 52 EXPECT_FALSE(wrapper->favicon_tab_helper()->ShouldDisplayFavicon()); | 52 EXPECT_FALSE(tab_contents->favicon_tab_helper()->ShouldDisplayFavicon()); |
| 53 EXPECT_TRUE(contents->FocusLocationBarByDefault()); | 53 EXPECT_TRUE(contents->FocusLocationBarByDefault()); |
| 54 | 54 |
| 55 // Now commit the load. | 55 // Now commit the load. |
| 56 RenderViewHostTester::For( | 56 RenderViewHostTester::For( |
| 57 contents->GetRenderViewHost())->SendNavigate(page_id, new_tab_url); | 57 contents->GetRenderViewHost())->SendNavigate(page_id, new_tab_url); |
| 58 | 58 |
| 59 // The same flags should be set as before now that the load has committed. | 59 // The same flags should be set as before now that the load has committed. |
| 60 EXPECT_FALSE(wrapper->favicon_tab_helper()->ShouldDisplayFavicon()); | 60 EXPECT_FALSE(tab_contents->favicon_tab_helper()->ShouldDisplayFavicon()); |
| 61 EXPECT_TRUE(contents->FocusLocationBarByDefault()); | 61 EXPECT_TRUE(contents->FocusLocationBarByDefault()); |
| 62 | 62 |
| 63 // Start a pending navigation to a regular page. | 63 // Start a pending navigation to a regular page. |
| 64 GURL next_url("http://google.com/"); | 64 GURL next_url("http://google.com/"); |
| 65 controller->LoadURL(next_url, content::Referrer(), | 65 controller->LoadURL(next_url, content::Referrer(), |
| 66 content::PAGE_TRANSITION_LINK, | 66 content::PAGE_TRANSITION_LINK, |
| 67 std::string()); | 67 std::string()); |
| 68 | 68 |
| 69 // Check the flags. Some should reflect the new page (URL, title), some | 69 // Check the flags. Some should reflect the new page (URL, title), some |
| 70 // should reflect the old one (bookmark bar) until it has committed. | 70 // should reflect the old one (bookmark bar) until it has committed. |
| 71 EXPECT_TRUE(wrapper->favicon_tab_helper()->ShouldDisplayFavicon()); | 71 EXPECT_TRUE(tab_contents->favicon_tab_helper()->ShouldDisplayFavicon()); |
| 72 EXPECT_FALSE(contents->FocusLocationBarByDefault()); | 72 EXPECT_FALSE(contents->FocusLocationBarByDefault()); |
| 73 | 73 |
| 74 // Commit the regular page load. Note that we must send it to the "pending" | 74 // Commit the regular page load. Note that we must send it to the "pending" |
| 75 // RenderViewHost if there is one, since this transition will also cause a | 75 // RenderViewHost if there is one, since this transition will also cause a |
| 76 // process transition, and our RVH pointer will be the "committed" one. | 76 // process transition, and our RVH pointer will be the "committed" one. |
| 77 // In the second call to this function from WebUIToStandard, it won't | 77 // In the second call to this function from WebUIToStandard, it won't |
| 78 // actually be pending, which is the point of this test. | 78 // actually be pending, which is the point of this test. |
| 79 RenderViewHost* pending_rvh = | 79 RenderViewHost* pending_rvh = |
| 80 RenderViewHostTester::GetPendingForController(controller); | 80 RenderViewHostTester::GetPendingForController(controller); |
| 81 if (pending_rvh) { | 81 if (pending_rvh) { |
| 82 RenderViewHostTester::For( | 82 RenderViewHostTester::For( |
| 83 pending_rvh)->SendNavigate(page_id + 1, next_url); | 83 pending_rvh)->SendNavigate(page_id + 1, next_url); |
| 84 } else { | 84 } else { |
| 85 RenderViewHostTester::For( | 85 RenderViewHostTester::For( |
| 86 contents->GetRenderViewHost())->SendNavigate(page_id + 1, next_url); | 86 contents->GetRenderViewHost())->SendNavigate(page_id + 1, next_url); |
| 87 } | 87 } |
| 88 | 88 |
| 89 // The state should now reflect a regular page. | 89 // The state should now reflect a regular page. |
| 90 EXPECT_TRUE(wrapper->favicon_tab_helper()->ShouldDisplayFavicon()); | 90 EXPECT_TRUE(tab_contents->favicon_tab_helper()->ShouldDisplayFavicon()); |
| 91 EXPECT_FALSE(contents->FocusLocationBarByDefault()); | 91 EXPECT_FALSE(contents->FocusLocationBarByDefault()); |
| 92 } | 92 } |
| 93 | 93 |
| 94 private: | 94 private: |
| 95 content::TestBrowserThread ui_thread_; | 95 content::TestBrowserThread ui_thread_; |
| 96 | 96 |
| 97 DISALLOW_COPY_AND_ASSIGN(WebUITest); | 97 DISALLOW_COPY_AND_ASSIGN(WebUITest); |
| 98 }; | 98 }; |
| 99 | 99 |
| 100 // Tests that the New Tab Page flags are correctly set and propogated by | 100 // Tests that the New Tab Page flags are correctly set and propogated by |
| 101 // WebContents when we first navigate to a Web UI page, then to a standard | 101 // WebContents when we first navigate to a Web UI page, then to a standard |
| 102 // non-DOM-UI page. | 102 // non-DOM-UI page. |
| 103 TEST_F(WebUITest, WebUIToStandard) { | 103 TEST_F(WebUITest, WebUIToStandard) { |
| 104 DoNavigationTest(tab_contents(), 1); | 104 DoNavigationTest(tab_contents(), 1); |
| 105 | 105 |
| 106 // Test the case where we're not doing the initial navigation. This is | 106 // Test the case where we're not doing the initial navigation. This is |
| 107 // slightly different than the very-first-navigation case since the | 107 // slightly different than the very-first-navigation case since the |
| 108 // SiteInstance will be the same (the original WebContents must still be | 108 // SiteInstance will be the same (the original WebContents must still be |
| 109 // alive), which will trigger different behavior in RenderViewHostManager. | 109 // alive), which will trigger different behavior in RenderViewHostManager. |
| 110 WebContents* contents2 = | 110 WebContents* contents2 = |
| 111 WebContentsTester::CreateTestWebContents(profile(), NULL); | 111 WebContentsTester::CreateTestWebContents(profile(), NULL); |
| 112 TabContentsWrapper wrapper2(contents2); | 112 TabContents tab_contents2(contents2); |
| 113 | 113 |
| 114 DoNavigationTest(&wrapper2, 101); | 114 DoNavigationTest(&tab_contents2, 101); |
| 115 } | 115 } |
| 116 | 116 |
| 117 TEST_F(WebUITest, WebUIToWebUI) { | 117 TEST_F(WebUITest, WebUIToWebUI) { |
| 118 // Do a load (this state is tested above). | 118 // Do a load (this state is tested above). |
| 119 GURL new_tab_url(chrome::kChromeUINewTabURL); | 119 GURL new_tab_url(chrome::kChromeUINewTabURL); |
| 120 controller().LoadURL(new_tab_url, content::Referrer(), | 120 controller().LoadURL(new_tab_url, content::Referrer(), |
| 121 content::PAGE_TRANSITION_LINK, | 121 content::PAGE_TRANSITION_LINK, |
| 122 std::string()); | 122 std::string()); |
| 123 rvh_tester()->SendNavigate(1, new_tab_url); | 123 rvh_tester()->SendNavigate(1, new_tab_url); |
| 124 | 124 |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 205 focus_called = wct->GetNumberOfFocusCalls(); | 205 focus_called = wct->GetNumberOfFocusCalls(); |
| 206 ASSERT_TRUE(controller().CanGoForward()); | 206 ASSERT_TRUE(controller().CanGoForward()); |
| 207 controller().GoForward(); | 207 controller().GoForward(); |
| 208 old_rvh = rvh(); | 208 old_rvh = rvh(); |
| 209 RenderViewHostTester::For(old_rvh)->SendShouldCloseACK(true); | 209 RenderViewHostTester::For(old_rvh)->SendShouldCloseACK(true); |
| 210 RenderViewHostTester::For( | 210 RenderViewHostTester::For( |
| 211 pending_rvh())->SendNavigate(next_page_id, next_url); | 211 pending_rvh())->SendNavigate(next_page_id, next_url); |
| 212 RenderViewHostTester::For(old_rvh)->SimulateSwapOutACK(); | 212 RenderViewHostTester::For(old_rvh)->SimulateSwapOutACK(); |
| 213 EXPECT_EQ(focus_called, wct->GetNumberOfFocusCalls()); | 213 EXPECT_EQ(focus_called, wct->GetNumberOfFocusCalls()); |
| 214 } | 214 } |
| OLD | NEW |