| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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_tab_helper.h" |
| 6 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" |
| 7 #include "chrome/browser/ui/tab_contents/test_tab_contents_wrapper.h" |
| 5 #include "chrome/browser/ui/webui/new_tab_ui.h" | 8 #include "chrome/browser/ui/webui/new_tab_ui.h" |
| 6 #include "chrome/common/url_constants.h" | 9 #include "chrome/common/url_constants.h" |
| 7 #include "chrome/test/testing_profile.h" | 10 #include "chrome/test/testing_profile.h" |
| 8 #include "content/browser/browser_thread.h" | 11 #include "content/browser/browser_thread.h" |
| 9 #include "content/browser/renderer_host/test_render_view_host.h" | |
| 10 #include "content/browser/site_instance.h" | 12 #include "content/browser/site_instance.h" |
| 11 #include "content/browser/tab_contents/navigation_controller.h" | 13 #include "content/browser/tab_contents/navigation_controller.h" |
| 12 #include "content/browser/tab_contents/test_tab_contents.h" | 14 #include "content/browser/tab_contents/test_tab_contents.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
| 14 | 16 |
| 15 class WebUITest : public RenderViewHostTestHarness { | 17 class WebUITest : public TabContentsWrapperTestHarness { |
| 16 public: | 18 public: |
| 17 WebUITest() : ui_thread_(BrowserThread::UI, MessageLoop::current()) {} | 19 WebUITest() : ui_thread_(BrowserThread::UI, MessageLoop::current()) {} |
| 18 | 20 |
| 19 // Tests navigating with a Web UI from a fresh (nothing pending or committed) | 21 // Tests navigating with a Web UI from a fresh (nothing pending or committed) |
| 20 // state, through pending, committed, then another navigation. The first page | 22 // state, through pending, committed, then another navigation. The first page |
| 21 // ID that we should use is passed as a parameter. We'll use the next two | 23 // ID that we should use is passed as a parameter. We'll use the next two |
| 22 // values. This must be increasing for the life of the tests. | 24 // values. This must be increasing for the life of the tests. |
| 23 static void DoNavigationTest(TabContents* contents, int page_id) { | 25 static void DoNavigationTest(TabContentsWrapper* wrapper, int page_id) { |
| 26 TabContents* contents = wrapper->tab_contents(); |
| 24 NavigationController* controller = &contents->controller(); | 27 NavigationController* controller = &contents->controller(); |
| 25 | 28 |
| 26 // Start a pending load. | 29 // Start a pending load. |
| 27 GURL new_tab_url(chrome::kChromeUINewTabURL); | 30 GURL new_tab_url(chrome::kChromeUINewTabURL); |
| 28 controller->LoadURL(new_tab_url, GURL(), PageTransition::LINK); | 31 controller->LoadURL(new_tab_url, GURL(), PageTransition::LINK); |
| 29 | 32 |
| 30 // The navigation entry should be pending with no committed entry. | 33 // The navigation entry should be pending with no committed entry. |
| 31 ASSERT_TRUE(controller->pending_entry()); | 34 ASSERT_TRUE(controller->pending_entry()); |
| 32 ASSERT_FALSE(controller->GetLastCommittedEntry()); | 35 ASSERT_FALSE(controller->GetLastCommittedEntry()); |
| 33 | 36 |
| 34 // Check the things the pending Web UI should have set. | 37 // Check the things the pending Web UI should have set. |
| 35 EXPECT_FALSE(contents->ShouldDisplayURL()); | 38 EXPECT_FALSE(contents->ShouldDisplayURL()); |
| 36 EXPECT_FALSE(contents->ShouldDisplayFavicon()); | 39 EXPECT_FALSE(wrapper->favicon_tab_helper()->ShouldDisplayFavicon()); |
| 37 EXPECT_TRUE(contents->ShouldShowBookmarkBar()); | 40 EXPECT_TRUE(contents->ShouldShowBookmarkBar()); |
| 38 EXPECT_TRUE(contents->FocusLocationBarByDefault()); | 41 EXPECT_TRUE(contents->FocusLocationBarByDefault()); |
| 39 | 42 |
| 40 // Now commit the load. | 43 // Now commit the load. |
| 41 static_cast<TestRenderViewHost*>( | 44 static_cast<TestRenderViewHost*>( |
| 42 contents->render_view_host())->SendNavigate(page_id, new_tab_url); | 45 contents->render_view_host())->SendNavigate(page_id, new_tab_url); |
| 43 | 46 |
| 44 // The same flags should be set as before now that the load has committed. | 47 // The same flags should be set as before now that the load has committed. |
| 45 EXPECT_FALSE(contents->ShouldDisplayURL()); | 48 EXPECT_FALSE(contents->ShouldDisplayURL()); |
| 46 EXPECT_FALSE(contents->ShouldDisplayFavicon()); | 49 EXPECT_FALSE(wrapper->favicon_tab_helper()->ShouldDisplayFavicon()); |
| 47 EXPECT_TRUE(contents->ShouldShowBookmarkBar()); | 50 EXPECT_TRUE(contents->ShouldShowBookmarkBar()); |
| 48 EXPECT_TRUE(contents->FocusLocationBarByDefault()); | 51 EXPECT_TRUE(contents->FocusLocationBarByDefault()); |
| 49 | 52 |
| 50 // Start a pending navigation to a regular page. | 53 // Start a pending navigation to a regular page. |
| 51 GURL next_url("http://google.com/"); | 54 GURL next_url("http://google.com/"); |
| 52 controller->LoadURL(next_url, GURL(), PageTransition::LINK); | 55 controller->LoadURL(next_url, GURL(), PageTransition::LINK); |
| 53 | 56 |
| 54 // Check the flags. Some should reflect the new page (URL, title), some | 57 // Check the flags. Some should reflect the new page (URL, title), some |
| 55 // should reflect the old one (bookmark bar) until it has committed. | 58 // should reflect the old one (bookmark bar) until it has committed. |
| 56 EXPECT_TRUE(contents->ShouldDisplayURL()); | 59 EXPECT_TRUE(contents->ShouldDisplayURL()); |
| 57 EXPECT_TRUE(contents->ShouldDisplayFavicon()); | 60 EXPECT_TRUE(wrapper->favicon_tab_helper()->ShouldDisplayFavicon()); |
| 58 EXPECT_TRUE(contents->ShouldShowBookmarkBar()); | 61 EXPECT_TRUE(contents->ShouldShowBookmarkBar()); |
| 59 EXPECT_FALSE(contents->FocusLocationBarByDefault()); | 62 EXPECT_FALSE(contents->FocusLocationBarByDefault()); |
| 60 | 63 |
| 61 // Commit the regular page load. Note that we must send it to the "pending" | 64 // Commit the regular page load. Note that we must send it to the "pending" |
| 62 // RenderViewHost if there is one, since this transition will also cause a | 65 // RenderViewHost if there is one, since this transition will also cause a |
| 63 // process transition, and our RVH pointer will be the "committed" one. | 66 // process transition, and our RVH pointer will be the "committed" one. |
| 64 // In the second call to this function from WebUIToStandard, it won't | 67 // In the second call to this function from WebUIToStandard, it won't |
| 65 // actually be pending, which is the point of this test. | 68 // actually be pending, which is the point of this test. |
| 66 if (contents->render_manager()->pending_render_view_host()) { | 69 if (contents->render_manager()->pending_render_view_host()) { |
| 67 static_cast<TestRenderViewHost*>( | 70 static_cast<TestRenderViewHost*>( |
| 68 contents->render_manager()->pending_render_view_host())->SendNavigate( | 71 contents->render_manager()->pending_render_view_host())->SendNavigate( |
| 69 page_id + 1, next_url); | 72 page_id + 1, next_url); |
| 70 } else { | 73 } else { |
| 71 static_cast<TestRenderViewHost*>( | 74 static_cast<TestRenderViewHost*>( |
| 72 contents->render_view_host())->SendNavigate(page_id + 1, next_url); | 75 contents->render_view_host())->SendNavigate(page_id + 1, next_url); |
| 73 } | 76 } |
| 74 | 77 |
| 75 // The state should now reflect a regular page. | 78 // The state should now reflect a regular page. |
| 76 EXPECT_TRUE(contents->ShouldDisplayURL()); | 79 EXPECT_TRUE(contents->ShouldDisplayURL()); |
| 77 EXPECT_TRUE(contents->ShouldDisplayFavicon()); | 80 EXPECT_TRUE(wrapper->favicon_tab_helper()->ShouldDisplayFavicon()); |
| 78 EXPECT_FALSE(contents->ShouldShowBookmarkBar()); | 81 EXPECT_FALSE(contents->ShouldShowBookmarkBar()); |
| 79 EXPECT_FALSE(contents->FocusLocationBarByDefault()); | 82 EXPECT_FALSE(contents->FocusLocationBarByDefault()); |
| 80 } | 83 } |
| 81 | 84 |
| 82 private: | 85 private: |
| 83 BrowserThread ui_thread_; | 86 BrowserThread ui_thread_; |
| 84 | 87 |
| 85 DISALLOW_COPY_AND_ASSIGN(WebUITest); | 88 DISALLOW_COPY_AND_ASSIGN(WebUITest); |
| 86 }; | 89 }; |
| 87 | 90 |
| 88 // Tests that the New Tab Page flags are correctly set and propogated by | 91 // Tests that the New Tab Page flags are correctly set and propogated by |
| 89 // TabContents when we first navigate to a Web UI page, then to a standard | 92 // TabContents when we first navigate to a Web UI page, then to a standard |
| 90 // non-DOM-UI page. | 93 // non-DOM-UI page. |
| 91 TEST_F(WebUITest, WebUIToStandard) { | 94 TEST_F(WebUITest, WebUIToStandard) { |
| 92 DoNavigationTest(contents(), 1); | 95 DoNavigationTest(contents_wrapper(), 1); |
| 93 | 96 |
| 94 // Test the case where we're not doing the initial navigation. This is | 97 // Test the case where we're not doing the initial navigation. This is |
| 95 // slightly different than the very-first-navigation case since the | 98 // slightly different than the very-first-navigation case since the |
| 96 // SiteInstance will be the same (the original TabContents must still be | 99 // SiteInstance will be the same (the original TabContents must still be |
| 97 // alive), which will trigger different behavior in RenderViewHostManager. | 100 // alive), which will trigger different behavior in RenderViewHostManager. |
| 98 TestTabContents contents2(profile_.get(), NULL); | 101 TestTabContents* contents2 = new TestTabContents(profile_.get(), NULL); |
| 102 TabContentsWrapper wrapper2(contents2); |
| 99 | 103 |
| 100 DoNavigationTest(&contents2, 101); | 104 DoNavigationTest(&wrapper2, 101); |
| 101 } | 105 } |
| 102 | 106 |
| 103 TEST_F(WebUITest, WebUIToWebUI) { | 107 TEST_F(WebUITest, WebUIToWebUI) { |
| 104 // Do a load (this state is tested above). | 108 // Do a load (this state is tested above). |
| 105 GURL new_tab_url(chrome::kChromeUINewTabURL); | 109 GURL new_tab_url(chrome::kChromeUINewTabURL); |
| 106 controller().LoadURL(new_tab_url, GURL(), PageTransition::LINK); | 110 controller().LoadURL(new_tab_url, GURL(), PageTransition::LINK); |
| 107 rvh()->SendNavigate(1, new_tab_url); | 111 rvh()->SendNavigate(1, new_tab_url); |
| 108 | 112 |
| 109 // Start another pending load of the new tab page. | 113 // Start another pending load of the new tab page. |
| 110 controller().LoadURL(new_tab_url, GURL(), PageTransition::LINK); | 114 controller().LoadURL(new_tab_url, GURL(), PageTransition::LINK); |
| 111 rvh()->SendNavigate(2, new_tab_url); | 115 rvh()->SendNavigate(2, new_tab_url); |
| 112 | 116 |
| 113 // The flags should be the same as the non-pending state. | 117 // The flags should be the same as the non-pending state. |
| 114 EXPECT_FALSE(contents()->ShouldDisplayURL()); | 118 EXPECT_FALSE(contents()->ShouldDisplayURL()); |
| 115 EXPECT_FALSE(contents()->ShouldDisplayFavicon()); | 119 EXPECT_FALSE( |
| 120 contents_wrapper()->favicon_tab_helper()->ShouldDisplayFavicon()); |
| 116 EXPECT_TRUE(contents()->ShouldShowBookmarkBar()); | 121 EXPECT_TRUE(contents()->ShouldShowBookmarkBar()); |
| 117 EXPECT_TRUE(contents()->FocusLocationBarByDefault()); | 122 EXPECT_TRUE(contents()->FocusLocationBarByDefault()); |
| 118 } | 123 } |
| 119 | 124 |
| 120 TEST_F(WebUITest, StandardToWebUI) { | 125 TEST_F(WebUITest, StandardToWebUI) { |
| 121 // Start a pending navigation to a regular page. | 126 // Start a pending navigation to a regular page. |
| 122 GURL std_url("http://google.com/"); | 127 GURL std_url("http://google.com/"); |
| 123 | 128 |
| 124 controller().LoadURL(std_url, GURL(), PageTransition::LINK); | 129 controller().LoadURL(std_url, GURL(), PageTransition::LINK); |
| 125 | 130 |
| 126 // The state should now reflect the default. | 131 // The state should now reflect the default. |
| 127 EXPECT_TRUE(contents()->ShouldDisplayURL()); | 132 EXPECT_TRUE(contents()->ShouldDisplayURL()); |
| 128 EXPECT_TRUE(contents()->ShouldDisplayFavicon()); | 133 EXPECT_TRUE(contents_wrapper()->favicon_tab_helper()->ShouldDisplayFavicon()); |
| 129 EXPECT_FALSE(contents()->ShouldShowBookmarkBar()); | 134 EXPECT_FALSE(contents()->ShouldShowBookmarkBar()); |
| 130 EXPECT_FALSE(contents()->FocusLocationBarByDefault()); | 135 EXPECT_FALSE(contents()->FocusLocationBarByDefault()); |
| 131 | 136 |
| 132 // Commit the load, the state should be the same. | 137 // Commit the load, the state should be the same. |
| 133 rvh()->SendNavigate(1, std_url); | 138 rvh()->SendNavigate(1, std_url); |
| 134 EXPECT_TRUE(contents()->ShouldDisplayURL()); | 139 EXPECT_TRUE(contents()->ShouldDisplayURL()); |
| 135 EXPECT_TRUE(contents()->ShouldDisplayFavicon()); | 140 EXPECT_TRUE(contents_wrapper()->favicon_tab_helper()->ShouldDisplayFavicon()); |
| 136 EXPECT_FALSE(contents()->ShouldShowBookmarkBar()); | 141 EXPECT_FALSE(contents()->ShouldShowBookmarkBar()); |
| 137 EXPECT_FALSE(contents()->FocusLocationBarByDefault()); | 142 EXPECT_FALSE(contents()->FocusLocationBarByDefault()); |
| 138 | 143 |
| 139 // Start a pending load for a WebUI. | 144 // Start a pending load for a WebUI. |
| 140 GURL new_tab_url(chrome::kChromeUINewTabURL); | 145 GURL new_tab_url(chrome::kChromeUINewTabURL); |
| 141 controller().LoadURL(new_tab_url, GURL(), PageTransition::LINK); | 146 controller().LoadURL(new_tab_url, GURL(), PageTransition::LINK); |
| 142 EXPECT_FALSE(contents()->ShouldDisplayURL()); | 147 EXPECT_FALSE(contents()->ShouldDisplayURL()); |
| 143 EXPECT_TRUE(contents()->ShouldDisplayFavicon()); | 148 EXPECT_TRUE(contents_wrapper()->favicon_tab_helper()->ShouldDisplayFavicon()); |
| 144 EXPECT_FALSE(contents()->ShouldShowBookmarkBar()); | 149 EXPECT_FALSE(contents()->ShouldShowBookmarkBar()); |
| 145 EXPECT_TRUE(contents()->FocusLocationBarByDefault()); | 150 EXPECT_TRUE(contents()->FocusLocationBarByDefault()); |
| 146 | 151 |
| 147 // Committing Web UI is tested above. | 152 // Committing Web UI is tested above. |
| 148 } | 153 } |
| 149 | 154 |
| 150 class TabContentsForFocusTest : public TestTabContents { | 155 class TabContentsForFocusTest : public TestTabContents { |
| 151 public: | 156 public: |
| 152 TabContentsForFocusTest(Profile* profile, SiteInstance* instance) | 157 TabContentsForFocusTest(Profile* profile, SiteInstance* instance) |
| 153 : TestTabContents(profile, instance), focus_called_(0) { | 158 : TestTabContents(profile, instance), focus_called_(0) { |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 187 pending_rvh()->SendNavigate(page_id, new_tab_url); | 192 pending_rvh()->SendNavigate(page_id, new_tab_url); |
| 188 EXPECT_LT(focus_called, tc->focus_called()); | 193 EXPECT_LT(focus_called, tc->focus_called()); |
| 189 | 194 |
| 190 // Navigate forward. Shouldn't focus the location bar. | 195 // Navigate forward. Shouldn't focus the location bar. |
| 191 focus_called = tc->focus_called(); | 196 focus_called = tc->focus_called(); |
| 192 ASSERT_TRUE(controller().CanGoForward()); | 197 ASSERT_TRUE(controller().CanGoForward()); |
| 193 controller().GoForward(); | 198 controller().GoForward(); |
| 194 pending_rvh()->SendNavigate(next_page_id, next_url); | 199 pending_rvh()->SendNavigate(next_page_id, next_url); |
| 195 EXPECT_EQ(focus_called, tc->focus_called()); | 200 EXPECT_EQ(focus_called, tc->focus_called()); |
| 196 } | 201 } |
| OLD | NEW |