| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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/dom_ui/new_tab_ui.h" | 5 #include "chrome/browser/dom_ui/new_tab_ui.h" |
| 6 #include "chrome/browser/renderer_host/test_render_view_host.h" | 6 #include "chrome/browser/renderer_host/test_render_view_host.h" |
| 7 #include "chrome/common/url_constants.h" | 7 #include "chrome/common/url_constants.h" |
| 8 #include "testing/gtest/include/gtest/gtest.h" | 8 #include "testing/gtest/include/gtest/gtest.h" |
| 9 | 9 |
| 10 class DOMUITest : public RenderViewHostTestHarness { | 10 class DOMUITest : public RenderViewHostTestHarness { |
| 11 public: | 11 public: |
| 12 DOMUITest() {} | 12 DOMUITest() {} |
| 13 | 13 |
| 14 // Tests navigating with a DOM UI from a fresh (nothing pending or committed) |
| 15 // state, through pending, committed, then another navigation. The first page |
| 16 // ID that we should use is passed as a parameter. We'll use the next two |
| 17 // values. This must be increasing for the life of the tests. |
| 18 static void DoNavigationTest(WebContents* contents, int page_id) { |
| 19 NavigationController* controller = contents->controller(); |
| 20 |
| 21 // Start a pending load. |
| 22 GURL new_tab_url(chrome::kChromeUINewTabURL); |
| 23 controller->LoadURL(new_tab_url, GURL(), PageTransition::LINK); |
| 24 |
| 25 // The navigation entry should be pending with no committed entry. |
| 26 ASSERT_TRUE(controller->GetPendingEntry()); |
| 27 ASSERT_FALSE(controller->GetLastCommittedEntry()); |
| 28 |
| 29 // Check the things the pending DOM UI should have set. |
| 30 EXPECT_FALSE(contents->ShouldDisplayURL()); |
| 31 EXPECT_FALSE(contents->ShouldDisplayFavIcon()); |
| 32 EXPECT_TRUE(contents->IsBookmarkBarAlwaysVisible()); |
| 33 EXPECT_TRUE(contents->FocusLocationBarByDefault()); |
| 34 |
| 35 // Now commit the load. |
| 36 static_cast<TestRenderViewHost*>( |
| 37 contents->render_view_host())->SendNavigate(page_id, new_tab_url); |
| 38 |
| 39 // The same flags should be set as before now that the load has committed. |
| 40 EXPECT_FALSE(contents->ShouldDisplayURL()); |
| 41 EXPECT_FALSE(contents->ShouldDisplayFavIcon()); |
| 42 EXPECT_TRUE(contents->IsBookmarkBarAlwaysVisible()); |
| 43 EXPECT_TRUE(contents->FocusLocationBarByDefault()); |
| 44 |
| 45 // Start a pending navigation to a regular page. |
| 46 GURL next_url("http://google.com/"); |
| 47 controller->LoadURL(next_url, GURL(), PageTransition::LINK); |
| 48 |
| 49 // Check the flags. Some should reflect the new page (URL, title), some |
| 50 // should reflect the old one (bookmark bar) until it has committed. |
| 51 EXPECT_TRUE(contents->ShouldDisplayURL()); |
| 52 EXPECT_TRUE(contents->ShouldDisplayFavIcon()); |
| 53 EXPECT_TRUE(contents->IsBookmarkBarAlwaysVisible()); |
| 54 EXPECT_FALSE(contents->FocusLocationBarByDefault()); |
| 55 |
| 56 // Commit the regular page load. Note that we must send it to the "pending" |
| 57 // RenderViewHost, since this transition will also cause a process |
| 58 // transition, and our RVH pointer will be the "committed" one. |
| 59 static_cast<TestRenderViewHost*>( |
| 60 contents->render_manager()->pending_render_view_host())->SendNavigate( |
| 61 page_id + 1, next_url); |
| 62 |
| 63 // The state should now reflect a regular page. |
| 64 EXPECT_TRUE(contents->ShouldDisplayURL()); |
| 65 EXPECT_TRUE(contents->ShouldDisplayFavIcon()); |
| 66 EXPECT_FALSE(contents->IsBookmarkBarAlwaysVisible()); |
| 67 EXPECT_FALSE(contents->FocusLocationBarByDefault()); |
| 68 } |
| 69 |
| 14 private: | 70 private: |
| 15 DISALLOW_COPY_AND_ASSIGN(DOMUITest); | 71 DISALLOW_COPY_AND_ASSIGN(DOMUITest); |
| 16 }; | 72 }; |
| 17 | 73 |
| 18 // Tests that the New Tab Page flags are correctly set and propogated by | 74 // Tests that the New Tab Page flags are correctly set and propogated by |
| 19 // WebContents when we first navigate to a DOM UI page, then to a standard | 75 // WebContents when we first navigate to a DOM UI page, then to a standard |
| 20 // non-DOM-UI page. | 76 // non-DOM-UI page. |
| 21 TEST_F(DOMUITest, DOMUIToStandard) { | 77 TEST_F(DOMUITest, DOMUIToStandard) { |
| 22 // Start a pending load. | 78 DoNavigationTest(contents(), 1); |
| 23 GURL new_tab_url(chrome::kChromeUINewTabURL); | |
| 24 controller()->LoadURL(new_tab_url, GURL(), PageTransition::LINK); | |
| 25 | 79 |
| 26 // The navigation entry should be pending with no committed entry. | 80 // Make |
| 27 ASSERT_TRUE(controller()->GetPendingEntry()); | 81 WebContents* contents2 = new TestWebContents(profile_.get(), NULL, |
| 28 ASSERT_FALSE(controller()->GetLastCommittedEntry()); | 82 &rvh_factory_); |
| 29 | 83 DoNavigationTest(contents2, 101); |
| 30 // Check the things the pending DOM UI should have set. | 84 contents2->CloseContents(); |
| 31 EXPECT_FALSE(contents()->ShouldDisplayURL()); | |
| 32 EXPECT_FALSE(contents()->ShouldDisplayFavIcon()); | |
| 33 EXPECT_TRUE(contents()->IsBookmarkBarAlwaysVisible()); | |
| 34 EXPECT_TRUE(contents()->FocusLocationBarByDefault()); | |
| 35 | |
| 36 // Now commit the load. | |
| 37 rvh()->SendNavigate(1, new_tab_url); | |
| 38 | |
| 39 // The same flags should be set as before now that the load has committed. | |
| 40 // Note that the location bar isn't focused now. Once the load commits, we | |
| 41 // don't care about this flag, so this value is OK. | |
| 42 EXPECT_FALSE(contents()->ShouldDisplayURL()); | |
| 43 EXPECT_FALSE(contents()->ShouldDisplayFavIcon()); | |
| 44 EXPECT_TRUE(contents()->IsBookmarkBarAlwaysVisible()); | |
| 45 EXPECT_FALSE(contents()->FocusLocationBarByDefault()); | |
| 46 | |
| 47 // Start a pending navigation to a regular page. | |
| 48 GURL next_url("http://google.com/"); | |
| 49 controller()->LoadURL(next_url, GURL(), PageTransition::LINK); | |
| 50 | |
| 51 // Check the flags. Some should reflect the new page (URL, title), some should | |
| 52 // reflect the old one (bookmark bar) until it has committed. | |
| 53 EXPECT_TRUE(contents()->ShouldDisplayURL()); | |
| 54 EXPECT_TRUE(contents()->ShouldDisplayFavIcon()); | |
| 55 EXPECT_TRUE(contents()->IsBookmarkBarAlwaysVisible()); | |
| 56 EXPECT_FALSE(contents()->FocusLocationBarByDefault()); | |
| 57 | |
| 58 // Commit the regular page load. Note that we must send it to the "pending" | |
| 59 // RenderViewHost, since this transition will also cause a process transition, | |
| 60 // and our RVH pointer will be the "committed" one. | |
| 61 static_cast<TestRenderViewHost*>( | |
| 62 contents()->render_manager()->pending_render_view_host())->SendNavigate( | |
| 63 2, next_url); | |
| 64 | |
| 65 // The state should now reflect a regular page. | |
| 66 EXPECT_TRUE(contents()->ShouldDisplayURL()); | |
| 67 EXPECT_TRUE(contents()->ShouldDisplayFavIcon()); | |
| 68 EXPECT_FALSE(contents()->IsBookmarkBarAlwaysVisible()); | |
| 69 EXPECT_FALSE(contents()->FocusLocationBarByDefault()); | |
| 70 } | 85 } |
| 71 | 86 |
| 72 TEST_F(DOMUITest, DOMUIToDOMUI) { | 87 TEST_F(DOMUITest, DOMUIToDOMUI) { |
| 73 // Do a load (this state is tested above). | 88 // Do a load (this state is tested above). |
| 74 GURL new_tab_url(chrome::kChromeUINewTabURL); | 89 GURL new_tab_url(chrome::kChromeUINewTabURL); |
| 75 controller()->LoadURL(new_tab_url, GURL(), PageTransition::LINK); | 90 controller()->LoadURL(new_tab_url, GURL(), PageTransition::LINK); |
| 76 rvh()->SendNavigate(1, new_tab_url); | 91 rvh()->SendNavigate(1, new_tab_url); |
| 77 | 92 |
| 78 // Start another pending load of the new tab page. | 93 // Start another pending load of the new tab page. |
| 79 controller()->LoadURL(new_tab_url, GURL(), PageTransition::LINK); | 94 controller()->LoadURL(new_tab_url, GURL(), PageTransition::LINK); |
| 80 rvh()->SendNavigate(2, new_tab_url); | 95 rvh()->SendNavigate(2, new_tab_url); |
| 81 | 96 |
| 82 // The flags should be the same as the non-pending state. | 97 // The flags should be the same as the non-pending state. |
| 83 EXPECT_FALSE(contents()->ShouldDisplayURL()); | 98 EXPECT_FALSE(contents()->ShouldDisplayURL()); |
| 84 EXPECT_FALSE(contents()->ShouldDisplayFavIcon()); | 99 EXPECT_FALSE(contents()->ShouldDisplayFavIcon()); |
| 85 EXPECT_TRUE(contents()->IsBookmarkBarAlwaysVisible()); | 100 EXPECT_TRUE(contents()->IsBookmarkBarAlwaysVisible()); |
| 86 EXPECT_FALSE(contents()->FocusLocationBarByDefault()); | 101 EXPECT_TRUE(contents()->FocusLocationBarByDefault()); |
| 87 } | 102 } |
| 88 | 103 |
| 89 TEST_F(DOMUITest, StandardToDOMUI) { | 104 TEST_F(DOMUITest, StandardToDOMUI) { |
| 90 // Start a pending navigation to a regular page. | 105 // Start a pending navigation to a regular page. |
| 91 GURL std_url("http://google.com/"); | 106 GURL std_url("http://google.com/"); |
| 92 controller()->LoadURL(std_url, GURL(), PageTransition::LINK); | 107 controller()->LoadURL(std_url, GURL(), PageTransition::LINK); |
| 93 | 108 |
| 94 // The state should now reflect the default. | 109 // The state should now reflect the default. |
| 95 EXPECT_TRUE(contents()->ShouldDisplayURL()); | 110 EXPECT_TRUE(contents()->ShouldDisplayURL()); |
| 96 EXPECT_TRUE(contents()->ShouldDisplayFavIcon()); | 111 EXPECT_TRUE(contents()->ShouldDisplayFavIcon()); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 107 // Start a pending load for a DOMUI. | 122 // Start a pending load for a DOMUI. |
| 108 GURL new_tab_url(chrome::kChromeUINewTabURL); | 123 GURL new_tab_url(chrome::kChromeUINewTabURL); |
| 109 controller()->LoadURL(new_tab_url, GURL(), PageTransition::LINK); | 124 controller()->LoadURL(new_tab_url, GURL(), PageTransition::LINK); |
| 110 EXPECT_FALSE(contents()->ShouldDisplayURL()); | 125 EXPECT_FALSE(contents()->ShouldDisplayURL()); |
| 111 EXPECT_FALSE(contents()->ShouldDisplayFavIcon()); | 126 EXPECT_FALSE(contents()->ShouldDisplayFavIcon()); |
| 112 EXPECT_FALSE(contents()->IsBookmarkBarAlwaysVisible()); | 127 EXPECT_FALSE(contents()->IsBookmarkBarAlwaysVisible()); |
| 113 EXPECT_TRUE(contents()->FocusLocationBarByDefault()); | 128 EXPECT_TRUE(contents()->FocusLocationBarByDefault()); |
| 114 | 129 |
| 115 // Committing DOM UI is tested above. | 130 // Committing DOM UI is tested above. |
| 116 } | 131 } |
| OLD | NEW |