| 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/search_ipc_router.h" | 5 #include "chrome/browser/ui/search/search_ipc_router.h" |
| 6 | 6 |
| 7 #include <stdint.h> | 7 #include <stdint.h> |
| 8 | 8 |
| 9 #include <memory> | 9 #include <memory> |
| 10 #include <tuple> | 10 #include <tuple> |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 43 #include "url/gurl.h" | 43 #include "url/gurl.h" |
| 44 | 44 |
| 45 namespace { | 45 namespace { |
| 46 | 46 |
| 47 class MockSearchIPCRouterDelegate : public SearchIPCRouter::Delegate { | 47 class MockSearchIPCRouterDelegate : public SearchIPCRouter::Delegate { |
| 48 public: | 48 public: |
| 49 virtual ~MockSearchIPCRouterDelegate() {} | 49 virtual ~MockSearchIPCRouterDelegate() {} |
| 50 | 50 |
| 51 MOCK_METHOD1(OnInstantSupportDetermined, void(bool supports_instant)); | 51 MOCK_METHOD1(OnInstantSupportDetermined, void(bool supports_instant)); |
| 52 MOCK_METHOD1(FocusOmnibox, void(OmniboxFocusState state)); | 52 MOCK_METHOD1(FocusOmnibox, void(OmniboxFocusState state)); |
| 53 MOCK_METHOD2(NavigateToURL, void(const GURL&, WindowOpenDisposition)); | |
| 54 MOCK_METHOD1(OnDeleteMostVisitedItem, void(const GURL& url)); | 53 MOCK_METHOD1(OnDeleteMostVisitedItem, void(const GURL& url)); |
| 55 MOCK_METHOD1(OnUndoMostVisitedDeletion, void(const GURL& url)); | 54 MOCK_METHOD1(OnUndoMostVisitedDeletion, void(const GURL& url)); |
| 56 MOCK_METHOD0(OnUndoAllMostVisitedDeletions, void()); | 55 MOCK_METHOD0(OnUndoAllMostVisitedDeletions, void()); |
| 57 MOCK_METHOD2(OnLogEvent, void(NTPLoggingEventType event, | 56 MOCK_METHOD2(OnLogEvent, void(NTPLoggingEventType event, |
| 58 base::TimeDelta time)); | 57 base::TimeDelta time)); |
| 59 MOCK_METHOD2(OnLogMostVisitedImpression, | 58 MOCK_METHOD2(OnLogMostVisitedImpression, |
| 60 void(int position, const base::string16& provider)); | 59 void(int position, const base::string16& provider)); |
| 61 MOCK_METHOD2(OnLogMostVisitedNavigation, | 60 MOCK_METHOD2(OnLogMostVisitedNavigation, |
| 62 void(int position, const base::string16& provider)); | 61 void(int position, const base::string16& provider)); |
| 63 MOCK_METHOD1(PasteIntoOmnibox, void(const base::string16&)); | 62 MOCK_METHOD1(PasteIntoOmnibox, void(const base::string16&)); |
| 64 MOCK_METHOD1(OnChromeIdentityCheck, void(const base::string16& identity)); | 63 MOCK_METHOD1(OnChromeIdentityCheck, void(const base::string16& identity)); |
| 65 MOCK_METHOD0(OnHistorySyncCheck, void()); | 64 MOCK_METHOD0(OnHistorySyncCheck, void()); |
| 66 }; | 65 }; |
| 67 | 66 |
| 68 class MockSearchIPCRouterPolicy : public SearchIPCRouter::Policy { | 67 class MockSearchIPCRouterPolicy : public SearchIPCRouter::Policy { |
| 69 public: | 68 public: |
| 70 virtual ~MockSearchIPCRouterPolicy() {} | 69 virtual ~MockSearchIPCRouterPolicy() {} |
| 71 | 70 |
| 72 MOCK_METHOD1(ShouldProcessFocusOmnibox, bool(bool)); | 71 MOCK_METHOD1(ShouldProcessFocusOmnibox, bool(bool)); |
| 73 MOCK_METHOD1(ShouldProcessNavigateToURL, bool(bool)); | |
| 74 MOCK_METHOD0(ShouldProcessDeleteMostVisitedItem, bool()); | 72 MOCK_METHOD0(ShouldProcessDeleteMostVisitedItem, bool()); |
| 75 MOCK_METHOD0(ShouldProcessUndoMostVisitedDeletion, bool()); | 73 MOCK_METHOD0(ShouldProcessUndoMostVisitedDeletion, bool()); |
| 76 MOCK_METHOD0(ShouldProcessUndoAllMostVisitedDeletions, bool()); | 74 MOCK_METHOD0(ShouldProcessUndoAllMostVisitedDeletions, bool()); |
| 77 MOCK_METHOD0(ShouldProcessLogEvent, bool()); | 75 MOCK_METHOD0(ShouldProcessLogEvent, bool()); |
| 78 MOCK_METHOD1(ShouldProcessPasteIntoOmnibox, bool(bool)); | 76 MOCK_METHOD1(ShouldProcessPasteIntoOmnibox, bool(bool)); |
| 79 MOCK_METHOD0(ShouldProcessChromeIdentityCheck, bool()); | 77 MOCK_METHOD0(ShouldProcessChromeIdentityCheck, bool()); |
| 80 MOCK_METHOD0(ShouldProcessHistorySyncCheck, bool()); | 78 MOCK_METHOD0(ShouldProcessHistorySyncCheck, bool()); |
| 81 MOCK_METHOD0(ShouldSendSetPromoInformation, bool()); | 79 MOCK_METHOD0(ShouldSendSetPromoInformation, bool()); |
| 82 MOCK_METHOD0(ShouldSendSetDisplayInstantResults, bool()); | 80 MOCK_METHOD0(ShouldSendSetDisplayInstantResults, bool()); |
| 83 MOCK_METHOD0(ShouldSendSetSuggestionToPrefetch, bool()); | 81 MOCK_METHOD0(ShouldSendSetSuggestionToPrefetch, bool()); |
| (...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 202 } | 200 } |
| 203 | 201 |
| 204 private: | 202 private: |
| 205 MockSearchIPCRouterDelegate delegate_; | 203 MockSearchIPCRouterDelegate delegate_; |
| 206 base::FieldTrialList field_trial_list_; | 204 base::FieldTrialList field_trial_list_; |
| 207 }; | 205 }; |
| 208 | 206 |
| 209 TEST_F(SearchIPCRouterTest, IgnoreMessagesFromNonInstantRenderers) { | 207 TEST_F(SearchIPCRouterTest, IgnoreMessagesFromNonInstantRenderers) { |
| 210 NavigateAndCommitActiveTab(GURL("file://foo/bar")); | 208 NavigateAndCommitActiveTab(GURL("file://foo/bar")); |
| 211 SetupMockDelegateAndPolicy(); | 209 SetupMockDelegateAndPolicy(); |
| 212 GURL destination_url("www.foo.com"); | 210 EXPECT_CALL(*mock_delegate(), FocusOmnibox(OMNIBOX_FOCUS_VISIBLE)).Times(0); |
| 213 EXPECT_CALL(*mock_delegate(), | |
| 214 NavigateToURL(destination_url, CURRENT_TAB)).Times(0); | |
| 215 content::WebContents* contents = web_contents(); | 211 content::WebContents* contents = web_contents(); |
| 216 bool is_active_tab = IsActiveTab(contents); | 212 bool is_active_tab = IsActiveTab(contents); |
| 217 EXPECT_TRUE(is_active_tab); | 213 EXPECT_TRUE(is_active_tab); |
| 218 | 214 |
| 219 MockSearchIPCRouterPolicy* policy = GetSearchIPCRouterPolicy(); | 215 MockSearchIPCRouterPolicy* policy = GetSearchIPCRouterPolicy(); |
| 220 EXPECT_CALL(*policy, ShouldProcessNavigateToURL(is_active_tab)).Times(0); | 216 EXPECT_CALL(*policy, ShouldProcessFocusOmnibox(is_active_tab)).Times(0); |
| 221 | 217 |
| 222 OnMessageReceived(ChromeViewHostMsg_SearchBoxNavigate( | 218 OnMessageReceived(ChromeViewHostMsg_FocusOmnibox( |
| 223 contents->GetRoutingID(), GetSearchIPCRouterSeqNo(), destination_url, | 219 contents->GetRoutingID(), GetSearchIPCRouterSeqNo(), |
| 224 CURRENT_TAB)); | 220 OMNIBOX_FOCUS_VISIBLE)); |
| 225 } | 221 } |
| 226 | 222 |
| 227 TEST_F(SearchIPCRouterTest, ProcessFocusOmniboxMsg) { | 223 TEST_F(SearchIPCRouterTest, ProcessFocusOmniboxMsg) { |
| 228 NavigateAndCommitActiveTab(GURL(chrome::kChromeSearchLocalNtpUrl)); | 224 NavigateAndCommitActiveTab(GURL(chrome::kChromeSearchLocalNtpUrl)); |
| 229 SetupMockDelegateAndPolicy(); | 225 SetupMockDelegateAndPolicy(); |
| 230 MockSearchIPCRouterPolicy* policy = GetSearchIPCRouterPolicy(); | 226 MockSearchIPCRouterPolicy* policy = GetSearchIPCRouterPolicy(); |
| 231 EXPECT_CALL(*mock_delegate(), FocusOmnibox(OMNIBOX_FOCUS_VISIBLE)).Times(1); | 227 EXPECT_CALL(*mock_delegate(), FocusOmnibox(OMNIBOX_FOCUS_VISIBLE)).Times(1); |
| 232 | 228 |
| 233 content::WebContents* contents = web_contents(); | 229 content::WebContents* contents = web_contents(); |
| 234 bool is_active_tab = IsActiveTab(contents); | 230 bool is_active_tab = IsActiveTab(contents); |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 271 EXPECT_EQ(0, browser()->tab_strip_model()->active_index()); | 267 EXPECT_EQ(0, browser()->tab_strip_model()->active_index()); |
| 272 EXPECT_FALSE(IsActiveTab(contents)); | 268 EXPECT_FALSE(IsActiveTab(contents)); |
| 273 | 269 |
| 274 // Activate the first tab. | 270 // Activate the first tab. |
| 275 browser()->tab_strip_model()->ActivateTabAt(1, false); | 271 browser()->tab_strip_model()->ActivateTabAt(1, false); |
| 276 EXPECT_EQ(browser()->tab_strip_model()->active_index(), | 272 EXPECT_EQ(browser()->tab_strip_model()->active_index(), |
| 277 browser()->tab_strip_model()->GetIndexOfWebContents(contents)); | 273 browser()->tab_strip_model()->GetIndexOfWebContents(contents)); |
| 278 EXPECT_TRUE(IsActiveTab(contents)); | 274 EXPECT_TRUE(IsActiveTab(contents)); |
| 279 } | 275 } |
| 280 | 276 |
| 281 TEST_F(SearchIPCRouterTest, ProcessNavigateToURLMsg) { | |
| 282 NavigateAndCommitActiveTab(GURL(chrome::kChromeSearchLocalNtpUrl)); | |
| 283 SetupMockDelegateAndPolicy(); | |
| 284 GURL destination_url("www.foo.com"); | |
| 285 EXPECT_CALL(*mock_delegate(), | |
| 286 NavigateToURL(destination_url, CURRENT_TAB)).Times(1); | |
| 287 content::WebContents* contents = web_contents(); | |
| 288 bool is_active_tab = IsActiveTab(contents); | |
| 289 EXPECT_TRUE(is_active_tab); | |
| 290 | |
| 291 MockSearchIPCRouterPolicy* policy = GetSearchIPCRouterPolicy(); | |
| 292 EXPECT_CALL(*policy, ShouldProcessNavigateToURL(is_active_tab)).Times(1) | |
| 293 .WillOnce(testing::Return(true)); | |
| 294 | |
| 295 OnMessageReceived(ChromeViewHostMsg_SearchBoxNavigate( | |
| 296 contents->GetRoutingID(), GetSearchIPCRouterSeqNo(), destination_url, | |
| 297 CURRENT_TAB)); | |
| 298 } | |
| 299 | |
| 300 TEST_F(SearchIPCRouterTest, IgnoreNavigateToURLMsg) { | |
| 301 NavigateAndCommitActiveTab(GURL("chrome-search://foo/bar")); | |
| 302 SetupMockDelegateAndPolicy(); | |
| 303 GURL destination_url("www.foo.com"); | |
| 304 EXPECT_CALL(*mock_delegate(), | |
| 305 NavigateToURL(destination_url, CURRENT_TAB)).Times(0); | |
| 306 content::WebContents* contents = web_contents(); | |
| 307 bool is_active_tab = IsActiveTab(contents); | |
| 308 EXPECT_TRUE(is_active_tab); | |
| 309 | |
| 310 MockSearchIPCRouterPolicy* policy = GetSearchIPCRouterPolicy(); | |
| 311 EXPECT_CALL(*policy, ShouldProcessNavigateToURL(is_active_tab)).Times(1) | |
| 312 .WillOnce(testing::Return(false)); | |
| 313 | |
| 314 OnMessageReceived(ChromeViewHostMsg_SearchBoxNavigate( | |
| 315 contents->GetRoutingID(), GetSearchIPCRouterSeqNo(), destination_url, | |
| 316 CURRENT_TAB)); | |
| 317 } | |
| 318 | |
| 319 TEST_F(SearchIPCRouterTest, ProcessLogEventMsg) { | 277 TEST_F(SearchIPCRouterTest, ProcessLogEventMsg) { |
| 320 base::TimeDelta delta = base::TimeDelta::FromMilliseconds(123); | 278 base::TimeDelta delta = base::TimeDelta::FromMilliseconds(123); |
| 321 NavigateAndCommitActiveTab(GURL(chrome::kChromeSearchLocalNtpUrl)); | 279 NavigateAndCommitActiveTab(GURL(chrome::kChromeSearchLocalNtpUrl)); |
| 322 SetupMockDelegateAndPolicy(); | 280 SetupMockDelegateAndPolicy(); |
| 323 MockSearchIPCRouterPolicy* policy = GetSearchIPCRouterPolicy(); | 281 MockSearchIPCRouterPolicy* policy = GetSearchIPCRouterPolicy(); |
| 324 EXPECT_CALL(*mock_delegate(), OnLogEvent(NTP_MOUSEOVER, delta)).Times(1); | 282 EXPECT_CALL(*mock_delegate(), OnLogEvent(NTP_MOUSEOVER, delta)).Times(1); |
| 325 EXPECT_CALL(*policy, ShouldProcessLogEvent()).Times(1) | 283 EXPECT_CALL(*policy, ShouldProcessLogEvent()).Times(1) |
| 326 .WillOnce(testing::Return(true)); | 284 .WillOnce(testing::Return(true)); |
| 327 | 285 |
| 328 content::WebContents* contents = web_contents(); | 286 content::WebContents* contents = web_contents(); |
| (...skipping 187 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 516 | 474 |
| 517 TEST_F(SearchIPCRouterTest, IgnoreMessageIfThePageIsNotActive) { | 475 TEST_F(SearchIPCRouterTest, IgnoreMessageIfThePageIsNotActive) { |
| 518 NavigateAndCommitActiveTab(GURL(chrome::kChromeSearchLocalNtpUrl)); | 476 NavigateAndCommitActiveTab(GURL(chrome::kChromeSearchLocalNtpUrl)); |
| 519 SetupMockDelegateAndPolicy(); | 477 SetupMockDelegateAndPolicy(); |
| 520 MockSearchIPCRouterPolicy* policy = GetSearchIPCRouterPolicy(); | 478 MockSearchIPCRouterPolicy* policy = GetSearchIPCRouterPolicy(); |
| 521 int page_seq_no = GetSearchIPCRouterSeqNo(); | 479 int page_seq_no = GetSearchIPCRouterSeqNo(); |
| 522 | 480 |
| 523 content::WebContents* contents = web_contents(); | 481 content::WebContents* contents = web_contents(); |
| 524 bool is_active_tab = IsActiveTab(contents); | 482 bool is_active_tab = IsActiveTab(contents); |
| 525 GURL item_url("www.foo.com"); | 483 GURL item_url("www.foo.com"); |
| 526 EXPECT_CALL(*mock_delegate(), NavigateToURL(item_url, CURRENT_TAB)).Times(0); | 484 |
| 527 // At this point, in a real test, the navigation would cause the | 485 // Navigate away from the NTP. Afterwards, all messages should be ignored. |
| 528 // SearchIPCRouter's page sequence to advance. In this test it doesn't, so | 486 NavigateAndCommitActiveTab(item_url); |
| 529 // we'll decrement the sequence number on this side to simulate it. | |
| 530 --page_seq_no; | |
| 531 EXPECT_CALL(*policy, ShouldProcessNavigateToURL(is_active_tab)).Times(0); | |
| 532 OnMessageReceived(ChromeViewHostMsg_SearchBoxNavigate( | |
| 533 contents->GetRoutingID(), page_seq_no, item_url, CURRENT_TAB)); | |
| 534 | 487 |
| 535 EXPECT_CALL(*mock_delegate(), OnDeleteMostVisitedItem(item_url)).Times(0); | 488 EXPECT_CALL(*mock_delegate(), OnDeleteMostVisitedItem(item_url)).Times(0); |
| 536 EXPECT_CALL(*policy, ShouldProcessDeleteMostVisitedItem()).Times(0); | 489 EXPECT_CALL(*policy, ShouldProcessDeleteMostVisitedItem()).Times(0); |
| 537 OnMessageReceived(ChromeViewHostMsg_SearchBoxDeleteMostVisitedItem( | 490 OnMessageReceived(ChromeViewHostMsg_SearchBoxDeleteMostVisitedItem( |
| 538 contents->GetRoutingID(), page_seq_no, item_url)); | 491 contents->GetRoutingID(), page_seq_no, item_url)); |
| 539 | 492 |
| 540 EXPECT_CALL(*mock_delegate(), OnUndoMostVisitedDeletion(item_url)).Times(0); | 493 EXPECT_CALL(*mock_delegate(), OnUndoMostVisitedDeletion(item_url)).Times(0); |
| 541 EXPECT_CALL(*policy, ShouldProcessUndoMostVisitedDeletion()).Times(0); | 494 EXPECT_CALL(*policy, ShouldProcessUndoMostVisitedDeletion()).Times(0); |
| 542 OnMessageReceived(ChromeViewHostMsg_SearchBoxUndoMostVisitedDeletion( | 495 OnMessageReceived(ChromeViewHostMsg_SearchBoxUndoMostVisitedDeletion( |
| 543 contents->GetRoutingID(), page_seq_no, item_url)); | 496 contents->GetRoutingID(), page_seq_no, item_url)); |
| (...skipping 287 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 831 | 784 |
| 832 // Construct a series of synthetic messages for each valid IPC message type, | 785 // Construct a series of synthetic messages for each valid IPC message type, |
| 833 // ensuring the router ignores them all. | 786 // ensuring the router ignores them all. |
| 834 for (int i = 0; i < LastIPCMsgStart; ++i) { | 787 for (int i = 0; i < LastIPCMsgStart; ++i) { |
| 835 const int message_id = i << 16; | 788 const int message_id = i << 16; |
| 836 ASSERT_EQ(IPC_MESSAGE_ID_CLASS(message_id), i); | 789 ASSERT_EQ(IPC_MESSAGE_ID_CLASS(message_id), i); |
| 837 IPC::Message msg(routing_id, message_id, IPC::Message::PRIORITY_LOW); | 790 IPC::Message msg(routing_id, message_id, IPC::Message::PRIORITY_LOW); |
| 838 EXPECT_FALSE(OnSpuriousMessageReceived(msg)) << i; | 791 EXPECT_FALSE(OnSpuriousMessageReceived(msg)) << i; |
| 839 } | 792 } |
| 840 } | 793 } |
| OLD | NEW |