Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/ui/search/instant_tab.h" | 5 #include "chrome/browser/ui/search/instant_tab.h" |
| 6 | 6 |
| 7 #include <stdint.h> | 7 #include <stdint.h> |
| 8 | 8 |
| 9 #include <memory> | 9 #include <memory> |
| 10 | 10 |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 28 namespace { | 28 namespace { |
| 29 | 29 |
| 30 class FakePageDelegate : public InstantTab::Delegate { | 30 class FakePageDelegate : public InstantTab::Delegate { |
| 31 public: | 31 public: |
| 32 virtual ~FakePageDelegate() { | 32 virtual ~FakePageDelegate() { |
| 33 } | 33 } |
| 34 | 34 |
| 35 MOCK_METHOD2(InstantSupportDetermined, | 35 MOCK_METHOD2(InstantSupportDetermined, |
| 36 void(const content::WebContents* contents, | 36 void(const content::WebContents* contents, |
| 37 bool supports_instant)); | 37 bool supports_instant)); |
| 38 MOCK_METHOD1(InstantTabRenderProcessGone, | |
| 39 void(const content::WebContents* contents)); | |
| 40 MOCK_METHOD2(InstantTabAboutToNavigateMainFrame, | 38 MOCK_METHOD2(InstantTabAboutToNavigateMainFrame, |
| 41 void(const content::WebContents* contents, | 39 void(const content::WebContents* contents, |
| 42 const GURL& url)); | 40 const GURL& url)); |
| 43 MOCK_METHOD5(NavigateToURL, | |
| 44 void(const content::WebContents* contents, | |
| 45 const GURL& url, | |
| 46 ui::PageTransition transition, | |
| 47 WindowOpenDisposition disposition, | |
| 48 bool is_search_type)); | |
| 49 }; | 41 }; |
| 50 | 42 |
| 51 } // namespace | 43 } // namespace |
| 52 | 44 |
| 53 class InstantTabTest : public ChromeRenderViewHostTestHarness { | 45 class InstantTabTest : public ChromeRenderViewHostTestHarness { |
| 54 public: | 46 public: |
| 55 void SetUp() override; | 47 void SetUp() override; |
| 56 | 48 |
| 57 bool MessageWasSent(uint32_t id) { | 49 SearchTabHelper* search_tab() { |
| 58 return process()->sink().GetFirstMessageMatching(id) != NULL; | 50 return SearchTabHelper::FromWebContents(web_contents()); |
| 59 } | 51 } |
| 60 | 52 |
| 61 std::unique_ptr<InstantTab> page; | 53 std::unique_ptr<InstantTab> page; |
| 62 FakePageDelegate delegate; | 54 FakePageDelegate delegate; |
| 63 }; | 55 }; |
| 64 | 56 |
| 65 void InstantTabTest::SetUp() { | 57 void InstantTabTest::SetUp() { |
| 66 ChromeRenderViewHostTestHarness::SetUp(); | 58 ChromeRenderViewHostTestHarness::SetUp(); |
| 67 SearchTabHelper::CreateForWebContents(web_contents()); | 59 SearchTabHelper::CreateForWebContents(web_contents()); |
| 68 } | 60 } |
| 69 | 61 |
| 70 TEST_F(InstantTabTest, IsLocal) { | 62 TEST_F(InstantTabTest, DetermineIfPageSupportsInstant_Local) { |
| 71 page.reset(new InstantTab(&delegate)); | 63 page.reset(new InstantTab(&delegate, web_contents())); |
| 72 EXPECT_FALSE(page->supports_instant()); | 64 EXPECT_FALSE(search_tab()->SupportsInstant()); |
| 73 EXPECT_FALSE(page->IsLocal()); | |
| 74 page->Init(web_contents()); | |
| 75 NavigateAndCommit(GURL(chrome::kChromeSearchLocalNtpUrl)); | 65 NavigateAndCommit(GURL(chrome::kChromeSearchLocalNtpUrl)); |
| 76 EXPECT_TRUE(page->IsLocal()); | |
| 77 NavigateAndCommit(GURL("http://example.com")); | |
| 78 EXPECT_FALSE(page->IsLocal()); | |
| 79 } | |
| 80 | |
| 81 TEST_F(InstantTabTest, DetermineIfPageSupportsInstant_Local) { | |
| 82 page.reset(new InstantTab(&delegate)); | |
| 83 EXPECT_FALSE(page->supports_instant()); | |
| 84 page->Init(web_contents()); | |
| 85 NavigateAndCommit(GURL(chrome::kChromeSearchLocalNtpUrl)); | |
| 86 EXPECT_TRUE(page->IsLocal()); | |
| 87 EXPECT_CALL(delegate, InstantSupportDetermined(web_contents(), true)) | 66 EXPECT_CALL(delegate, InstantSupportDetermined(web_contents(), true)) |
| 88 .Times(1); | 67 .Times(1); |
|
sfiera
2016/07/22 12:51:00
Not needed; gmock will infer .Times(1) when there
Marc Treib
2016/07/22 13:11:17
Does it? I thought that without the explicit ".Tim
sfiera
2016/07/25 08:20:06
Nope, .Times(1) is the same thing.
In general, I
Marc Treib
2016/07/25 08:56:25
Alright, removed the "Times(1)" everywhere.
| |
| 89 SearchTabHelper::FromWebContents(web_contents())-> | 68 SearchTabHelper::FromWebContents(web_contents())-> |
| 90 DetermineIfPageSupportsInstant(); | 69 DetermineIfPageSupportsInstant(); |
| 91 EXPECT_TRUE(page->supports_instant()); | 70 EXPECT_TRUE(search_tab()->SupportsInstant()); |
| 92 } | 71 } |
| 93 | 72 |
| 94 TEST_F(InstantTabTest, DetermineIfPageSupportsInstant_NonLocal) { | 73 TEST_F(InstantTabTest, DetermineIfPageSupportsInstant_NonLocal) { |
| 95 page.reset(new InstantTab(&delegate)); | 74 page.reset(new InstantTab(&delegate, web_contents())); |
| 96 EXPECT_FALSE(page->supports_instant()); | 75 EXPECT_FALSE(search_tab()->SupportsInstant()); |
| 97 page->Init(web_contents()); | |
| 98 NavigateAndCommit(GURL("chrome-search://foo/bar")); | 76 NavigateAndCommit(GURL("chrome-search://foo/bar")); |
| 99 EXPECT_FALSE(page->IsLocal()); | |
| 100 process()->sink().ClearMessages(); | 77 process()->sink().ClearMessages(); |
| 101 SearchTabHelper::FromWebContents(web_contents())-> | 78 SearchTabHelper::FromWebContents(web_contents())-> |
| 102 DetermineIfPageSupportsInstant(); | 79 DetermineIfPageSupportsInstant(); |
| 103 const IPC::Message* message = process()->sink().GetFirstMessageMatching( | 80 const IPC::Message* message = process()->sink().GetFirstMessageMatching( |
| 104 ChromeViewMsg_DetermineIfPageSupportsInstant::ID); | 81 ChromeViewMsg_DetermineIfPageSupportsInstant::ID); |
| 105 ASSERT_TRUE(message != NULL); | 82 ASSERT_TRUE(message != NULL); |
| 106 EXPECT_EQ(web_contents()->GetRoutingID(), message->routing_id()); | 83 EXPECT_EQ(web_contents()->GetRoutingID(), message->routing_id()); |
| 107 } | 84 } |
| 108 | 85 |
| 109 TEST_F(InstantTabTest, PageURLDoesntBelongToInstantRenderer) { | 86 TEST_F(InstantTabTest, PageURLDoesntBelongToInstantRenderer) { |
| 110 page.reset(new InstantTab(&delegate)); | 87 page.reset(new InstantTab(&delegate, web_contents())); |
| 111 EXPECT_FALSE(page->supports_instant()); | 88 EXPECT_FALSE(search_tab()->SupportsInstant()); |
| 112 NavigateAndCommit(GURL(chrome::kChromeSearchLocalNtpUrl)); | 89 NavigateAndCommit(GURL(chrome::kChromeSearchLocalNtpUrl)); |
| 113 page->Init(web_contents()); | |
| 114 | 90 |
| 115 // Navigate to a page URL that doesn't belong to Instant renderer. | 91 // Navigate to a page URL that doesn't belong to Instant renderer. |
| 116 // SearchTabHelper::DeterminerIfPageSupportsInstant() should return | 92 // SearchTabHelper::DeterminerIfPageSupportsInstant() should return |
| 117 // immediately without dispatching any message to the renderer. | 93 // immediately without dispatching any message to the renderer. |
| 118 NavigateAndCommit(GURL("http://www.example.com")); | 94 NavigateAndCommit(GURL("http://www.example.com")); |
| 119 EXPECT_FALSE(page->IsLocal()); | |
| 120 process()->sink().ClearMessages(); | 95 process()->sink().ClearMessages(); |
| 121 EXPECT_CALL(delegate, InstantSupportDetermined(web_contents(), false)) | 96 EXPECT_CALL(delegate, InstantSupportDetermined(web_contents(), false)) |
| 122 .Times(1); | 97 .Times(1); |
| 123 | 98 |
| 124 SearchTabHelper::FromWebContents(web_contents())-> | 99 SearchTabHelper::FromWebContents(web_contents())-> |
| 125 DetermineIfPageSupportsInstant(); | 100 DetermineIfPageSupportsInstant(); |
| 126 const IPC::Message* message = process()->sink().GetFirstMessageMatching( | 101 const IPC::Message* message = process()->sink().GetFirstMessageMatching( |
| 127 ChromeViewMsg_DetermineIfPageSupportsInstant::ID); | 102 ChromeViewMsg_DetermineIfPageSupportsInstant::ID); |
| 128 ASSERT_TRUE(message == NULL); | 103 ASSERT_TRUE(message == NULL); |
| 129 EXPECT_FALSE(page->supports_instant()); | 104 EXPECT_FALSE(search_tab()->SupportsInstant()); |
| 130 } | 105 } |
| 131 | 106 |
| 132 // Test to verify that ChromeViewMsg_DetermineIfPageSupportsInstant message | 107 // Test to verify that ChromeViewMsg_DetermineIfPageSupportsInstant message |
| 133 // reply handler updates the instant support state in InstantTab. | 108 // reply handler updates the instant support state in InstantTab. |
| 134 TEST_F(InstantTabTest, PageSupportsInstant) { | 109 TEST_F(InstantTabTest, PageSupportsInstant) { |
| 135 page.reset(new InstantTab(&delegate)); | 110 page.reset(new InstantTab(&delegate, web_contents())); |
| 136 EXPECT_FALSE(page->supports_instant()); | 111 EXPECT_FALSE(search_tab()->SupportsInstant()); |
| 137 page->Init(web_contents()); | |
| 138 NavigateAndCommit(GURL("chrome-search://foo/bar")); | 112 NavigateAndCommit(GURL("chrome-search://foo/bar")); |
| 139 process()->sink().ClearMessages(); | 113 process()->sink().ClearMessages(); |
| 140 SearchTabHelper::FromWebContents(web_contents())-> | 114 SearchTabHelper::FromWebContents(web_contents())-> |
| 141 DetermineIfPageSupportsInstant(); | 115 DetermineIfPageSupportsInstant(); |
| 142 const IPC::Message* message = process()->sink().GetFirstMessageMatching( | 116 const IPC::Message* message = process()->sink().GetFirstMessageMatching( |
| 143 ChromeViewMsg_DetermineIfPageSupportsInstant::ID); | 117 ChromeViewMsg_DetermineIfPageSupportsInstant::ID); |
| 144 ASSERT_TRUE(message != NULL); | 118 ASSERT_TRUE(message != NULL); |
| 145 EXPECT_EQ(web_contents()->GetRoutingID(), message->routing_id()); | 119 EXPECT_EQ(web_contents()->GetRoutingID(), message->routing_id()); |
| 146 | 120 |
| 147 EXPECT_CALL(delegate, InstantSupportDetermined(web_contents(), true)) | 121 EXPECT_CALL(delegate, InstantSupportDetermined(web_contents(), true)) |
| 148 .Times(1); | 122 .Times(1); |
| 149 | 123 |
| 150 // Assume the page supports instant. Invoke the message reply handler to make | 124 // Assume the page supports instant. Invoke the message reply handler to make |
| 151 // sure the InstantTab is notified about the instant support state. | 125 // sure the InstantTab is notified about the instant support state. |
| 152 const content::NavigationEntry* entry = | 126 const content::NavigationEntry* entry = |
| 153 web_contents()->GetController().GetLastCommittedEntry(); | 127 web_contents()->GetController().GetLastCommittedEntry(); |
| 154 EXPECT_TRUE(entry); | 128 EXPECT_TRUE(entry); |
| 155 SearchTabHelper::FromWebContents(web_contents())->InstantSupportChanged(true); | 129 SearchTabHelper::FromWebContents(web_contents())->InstantSupportChanged(true); |
| 156 EXPECT_TRUE(page->supports_instant()); | 130 EXPECT_TRUE(search_tab()->SupportsInstant()); |
| 157 } | 131 } |
| OLD | NEW |