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_page.h" | 5 #include "chrome/browser/ui/search/instant_page.h" |
6 | 6 |
7 #include "base/command_line.h" | |
8 #include "base/memory/scoped_ptr.h" | 7 #include "base/memory/scoped_ptr.h" |
9 #include "chrome/browser/ui/search/search_tab_helper.h" | |
10 #include "chrome/common/chrome_switches.h" | |
11 #include "chrome/common/render_messages.h" | 8 #include "chrome/common/render_messages.h" |
12 #include "chrome/common/url_constants.h" | 9 #include "chrome/common/url_constants.h" |
13 #include "chrome/test/base/chrome_render_view_host_test_harness.h" | 10 #include "chrome/test/base/chrome_render_view_host_test_harness.h" |
14 #include "content/public/browser/navigation_entry.h" | |
15 #include "content/public/browser/web_contents.h" | 11 #include "content/public/browser/web_contents.h" |
16 #include "content/public/test/mock_render_process_host.h" | 12 #include "content/public/test/mock_render_process_host.h" |
17 #include "googleurl/src/gurl.h" | 13 #include "googleurl/src/gurl.h" |
18 #include "ipc/ipc_test_sink.h" | 14 #include "ipc/ipc_test_sink.h" |
19 #include "testing/gmock/include/gmock/gmock.h" | 15 #include "testing/gmock/include/gmock/gmock.h" |
20 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
21 | 17 |
22 namespace { | 18 namespace { |
23 | 19 |
24 class FakePageDelegate : public InstantPage::Delegate { | 20 class FakePageDelegate : public InstantPage::Delegate { |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
61 | 57 |
62 class FakePage : public InstantPage { | 58 class FakePage : public InstantPage { |
63 public: | 59 public: |
64 FakePage(Delegate* delegate, const std::string& instant_url) | 60 FakePage(Delegate* delegate, const std::string& instant_url) |
65 : InstantPage(delegate, instant_url) { | 61 : InstantPage(delegate, instant_url) { |
66 } | 62 } |
67 virtual ~FakePage() { | 63 virtual ~FakePage() { |
68 } | 64 } |
69 | 65 |
70 using InstantPage::SetContents; | 66 using InstantPage::SetContents; |
71 | |
72 private: | |
73 DISALLOW_COPY_AND_ASSIGN(FakePage); | |
74 }; | 67 }; |
75 | 68 |
76 } // namespace | 69 } // namespace |
77 | 70 |
78 class InstantPageTest : public ChromeRenderViewHostTestHarness { | 71 class InstantPageTest : public ChromeRenderViewHostTestHarness { |
79 public: | 72 public: |
80 virtual void SetUp() OVERRIDE; | |
81 | |
82 scoped_ptr<FakePage> page; | 73 scoped_ptr<FakePage> page; |
83 FakePageDelegate delegate; | 74 FakePageDelegate delegate; |
84 }; | 75 }; |
85 | 76 |
86 void InstantPageTest::SetUp() { | 77 TEST_F(InstantPageTest, IsLocal) { |
87 CommandLine::ForCurrentProcess()->AppendSwitch( | |
88 switches::kEnableInstantExtendedAPI); | |
89 ChromeRenderViewHostTestHarness::SetUp(); | |
90 SearchTabHelper::CreateForWebContents(web_contents()); | |
91 page.reset(new FakePage(&delegate, "")); | 78 page.reset(new FakePage(&delegate, "")); |
| 79 EXPECT_FALSE(page->IsLocal()); |
92 page->SetContents(web_contents()); | 80 page->SetContents(web_contents()); |
93 } | |
94 | |
95 TEST_F(InstantPageTest, IsLocal) { | |
96 EXPECT_FALSE(page->supports_instant()); | |
97 EXPECT_FALSE(page->IsLocal()); | |
98 NavigateAndCommit(GURL(chrome::kChromeSearchLocalNtpUrl)); | 81 NavigateAndCommit(GURL(chrome::kChromeSearchLocalNtpUrl)); |
99 EXPECT_TRUE(page->IsLocal()); | 82 EXPECT_TRUE(page->IsLocal()); |
100 NavigateAndCommit(GURL("http://example.com")); | 83 NavigateAndCommit(GURL("http://example.com")); |
101 EXPECT_FALSE(page->IsLocal()); | 84 EXPECT_FALSE(page->IsLocal()); |
102 NavigateAndCommit(GURL(chrome::kChromeSearchLocalGoogleNtpUrl)); | 85 NavigateAndCommit(GURL(chrome::kChromeSearchLocalGoogleNtpUrl)); |
103 EXPECT_TRUE(page->IsLocal()); | 86 EXPECT_TRUE(page->IsLocal()); |
104 } | 87 } |
105 | 88 |
106 TEST_F(InstantPageTest, DetermineIfPageSupportsInstant_Local) { | 89 TEST_F(InstantPageTest, DetermineIfPageSupportsInstant_Local) { |
| 90 page.reset(new FakePage(&delegate, "")); |
107 EXPECT_FALSE(page->supports_instant()); | 91 EXPECT_FALSE(page->supports_instant()); |
| 92 page->SetContents(web_contents()); |
108 NavigateAndCommit(GURL(chrome::kChromeSearchLocalNtpUrl)); | 93 NavigateAndCommit(GURL(chrome::kChromeSearchLocalNtpUrl)); |
109 EXPECT_TRUE(page->IsLocal()); | 94 EXPECT_TRUE(page->IsLocal()); |
110 EXPECT_CALL(delegate, InstantSupportDetermined(web_contents(), true)) | 95 EXPECT_CALL(delegate, InstantSupportDetermined(web_contents(), true)) |
111 .Times(1); | 96 .Times(1); |
112 SearchTabHelper::FromWebContents(web_contents())-> | 97 page->DetermineIfPageSupportsInstant(); |
113 DetermineIfPageSupportsInstant(); | |
114 EXPECT_TRUE(page->supports_instant()); | 98 EXPECT_TRUE(page->supports_instant()); |
115 } | 99 } |
116 | 100 |
117 TEST_F(InstantPageTest, DetermineIfPageSupportsInstant_NonLocal) { | 101 TEST_F(InstantPageTest, DetermineIfPageSupportsInstant_NonLocal) { |
| 102 page.reset(new FakePage(&delegate, "")); |
118 EXPECT_FALSE(page->supports_instant()); | 103 EXPECT_FALSE(page->supports_instant()); |
119 NavigateAndCommit(GURL("chrome-search://foo/bar")); | 104 page->SetContents(web_contents()); |
| 105 NavigateAndCommit(GURL("http://example.com/")); |
120 EXPECT_FALSE(page->IsLocal()); | 106 EXPECT_FALSE(page->IsLocal()); |
121 process()->sink().ClearMessages(); | 107 process()->sink().ClearMessages(); |
122 SearchTabHelper::FromWebContents(web_contents())-> | 108 page->DetermineIfPageSupportsInstant(); |
123 DetermineIfPageSupportsInstant(); | |
124 const IPC::Message* message = process()->sink().GetFirstMessageMatching( | 109 const IPC::Message* message = process()->sink().GetFirstMessageMatching( |
125 ChromeViewMsg_DetermineIfPageSupportsInstant::ID); | 110 ChromeViewMsg_DetermineIfPageSupportsInstant::ID); |
126 ASSERT_TRUE(message != NULL); | 111 ASSERT_TRUE(message != NULL); |
127 EXPECT_EQ(web_contents()->GetRoutingID(), message->routing_id()); | 112 EXPECT_EQ(web_contents()->GetRoutingID(), message->routing_id()); |
128 } | 113 } |
129 | 114 |
130 TEST_F(InstantPageTest, DispatchRequestToDeleteMostVisitedItem) { | 115 TEST_F(InstantPageTest, DispatchRequestToDeleteMostVisitedItem) { |
131 page.reset(new FakePage(&delegate, "")); | 116 page.reset(new FakePage(&delegate, "")); |
132 page->SetContents(web_contents()); | 117 page->SetContents(web_contents()); |
133 GURL item_url("www.foo.com"); | 118 GURL item_url("www.foo.com"); |
(...skipping 14 matching lines...) Expand all Loading... |
148 } | 133 } |
149 | 134 |
150 TEST_F(InstantPageTest, DispatchRequestToUndoAllMostVisitedDeletions) { | 135 TEST_F(InstantPageTest, DispatchRequestToUndoAllMostVisitedDeletions) { |
151 page.reset(new FakePage(&delegate, "")); | 136 page.reset(new FakePage(&delegate, "")); |
152 page->SetContents(web_contents()); | 137 page->SetContents(web_contents()); |
153 EXPECT_CALL(delegate, UndoAllMostVisitedDeletions()).Times(1); | 138 EXPECT_CALL(delegate, UndoAllMostVisitedDeletions()).Times(1); |
154 EXPECT_TRUE(page->OnMessageReceived( | 139 EXPECT_TRUE(page->OnMessageReceived( |
155 ChromeViewHostMsg_SearchBoxUndoAllMostVisitedDeletions( | 140 ChromeViewHostMsg_SearchBoxUndoAllMostVisitedDeletions( |
156 rvh()->GetRoutingID()))); | 141 rvh()->GetRoutingID()))); |
157 } | 142 } |
158 | |
159 TEST_F(InstantPageTest, PageURLDoesntBelongToInstantRenderer) { | |
160 EXPECT_FALSE(page->supports_instant()); | |
161 | |
162 // Navigate to a page URL that doesn't belong to Instant renderer. | |
163 // SearchTabHelper::DeterminerIfPageSupportsInstant() should return | |
164 // immediately without dispatching any message to the renderer. | |
165 NavigateAndCommit(GURL("http://www.example.com")); | |
166 EXPECT_FALSE(page->IsLocal()); | |
167 process()->sink().ClearMessages(); | |
168 EXPECT_CALL(delegate, InstantSupportDetermined(web_contents(), false)) | |
169 .Times(1); | |
170 | |
171 SearchTabHelper::FromWebContents(web_contents())-> | |
172 DetermineIfPageSupportsInstant(); | |
173 const IPC::Message* message = process()->sink().GetFirstMessageMatching( | |
174 ChromeViewMsg_DetermineIfPageSupportsInstant::ID); | |
175 ASSERT_TRUE(message == NULL); | |
176 EXPECT_FALSE(page->supports_instant()); | |
177 } | |
178 | |
179 // Test to verify that ChromeViewMsg_DetermineIfPageSupportsInstant message | |
180 // reply handler updates the instant support state in InstantPage. | |
181 TEST_F(InstantPageTest, PageSupportsInstant) { | |
182 EXPECT_FALSE(page->supports_instant()); | |
183 NavigateAndCommit(GURL("chrome-search://foo/bar")); | |
184 process()->sink().ClearMessages(); | |
185 SearchTabHelper::FromWebContents(web_contents())-> | |
186 DetermineIfPageSupportsInstant(); | |
187 const IPC::Message* message = process()->sink().GetFirstMessageMatching( | |
188 ChromeViewMsg_DetermineIfPageSupportsInstant::ID); | |
189 ASSERT_TRUE(message != NULL); | |
190 EXPECT_EQ(web_contents()->GetRoutingID(), message->routing_id()); | |
191 | |
192 EXPECT_CALL(delegate, InstantSupportDetermined(web_contents(), true)) | |
193 .Times(1); | |
194 | |
195 // Assume the page supports instant. Invoke the message reply handler to make | |
196 // sure the InstantPage is notified about the instant support state. | |
197 const content::NavigationEntry* entry = | |
198 web_contents()->GetController().GetActiveEntry(); | |
199 EXPECT_TRUE(entry); | |
200 SearchTabHelper::FromWebContents(web_contents())-> | |
201 OnInstantSupportDetermined(entry->GetPageID(), true); | |
202 EXPECT_TRUE(page->supports_instant()); | |
203 } | |
OLD | NEW |