| 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_tab_helper.h" | 5 #include "chrome/browser/ui/search/search_tab_helper.h" |
| 6 | 6 |
| 7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
| 8 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
| 9 #include "base/strings/utf_string_conversions.h" | 9 #include "base/strings/utf_string_conversions.h" |
| 10 #include "chrome/browser/search/search.h" | 10 #include "chrome/browser/search/search.h" |
| 11 #include "chrome/browser/search_engines/template_url_service.h" | 11 #include "chrome/browser/search_engines/template_url_service.h" |
| 12 #include "chrome/browser/search_engines/template_url_service_factory.h" | 12 #include "chrome/browser/search_engines/template_url_service_factory.h" |
| 13 #include "chrome/browser/signin/fake_signin_manager.h" | 13 #include "chrome/browser/signin/fake_signin_manager.h" |
| 14 #include "chrome/browser/signin/signin_manager_factory.h" | 14 #include "chrome/browser/signin/signin_manager_factory.h" |
| 15 #include "chrome/browser/sync/profile_sync_service.h" |
| 16 #include "chrome/browser/sync/profile_sync_service_factory.h" |
| 17 #include "chrome/browser/sync/profile_sync_service_mock.h" |
| 15 #include "chrome/browser/ui/search/search_ipc_router.h" | 18 #include "chrome/browser/ui/search/search_ipc_router.h" |
| 16 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 19 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 17 #include "chrome/common/chrome_switches.h" | 20 #include "chrome/common/chrome_switches.h" |
| 18 #include "chrome/common/ntp_logging_events.h" | 21 #include "chrome/common/ntp_logging_events.h" |
| 19 #include "chrome/common/omnibox_focus_state.h" | 22 #include "chrome/common/omnibox_focus_state.h" |
| 20 #include "chrome/common/render_messages.h" | 23 #include "chrome/common/render_messages.h" |
| 21 #include "chrome/common/url_constants.h" | 24 #include "chrome/common/url_constants.h" |
| 22 #include "chrome/test/base/browser_with_test_window_test.h" | 25 #include "chrome/test/base/browser_with_test_window_test.h" |
| 23 #include "chrome/test/base/chrome_render_view_host_test_harness.h" | 26 #include "chrome/test/base/chrome_render_view_host_test_harness.h" |
| 24 #include "chrome/test/base/testing_profile.h" | 27 #include "chrome/test/base/testing_profile.h" |
| 25 #include "chrome/test/base/ui_test_utils.h" | 28 #include "chrome/test/base/ui_test_utils.h" |
| 26 #include "content/public/browser/navigation_controller.h" | 29 #include "content/public/browser/navigation_controller.h" |
| 27 #include "content/public/browser/navigation_entry.h" | 30 #include "content/public/browser/navigation_entry.h" |
| 28 #include "content/public/browser/web_contents.h" | 31 #include "content/public/browser/web_contents.h" |
| 29 #include "content/public/test/mock_render_process_host.h" | 32 #include "content/public/test/mock_render_process_host.h" |
| 30 #include "grit/generated_resources.h" | 33 #include "grit/generated_resources.h" |
| 31 #include "ipc/ipc_message.h" | 34 #include "ipc/ipc_message.h" |
| 32 #include "ipc/ipc_test_sink.h" | 35 #include "ipc/ipc_test_sink.h" |
| 33 #include "net/base/net_errors.h" | 36 #include "net/base/net_errors.h" |
| 34 #include "testing/gmock/include/gmock/gmock.h" | 37 #include "testing/gmock/include/gmock/gmock.h" |
| 35 #include "testing/gtest/include/gtest/gtest.h" | 38 #include "testing/gtest/include/gtest/gtest.h" |
| 36 #include "ui/base/l10n/l10n_util.h" | 39 #include "ui/base/l10n/l10n_util.h" |
| 37 #include "url/gurl.h" | 40 #include "url/gurl.h" |
| 38 | 41 |
| 42 using testing::Return; |
| 43 |
| 39 namespace { | 44 namespace { |
| 40 | 45 |
| 41 class MockSearchIPCRouterDelegate : public SearchIPCRouter::Delegate { | 46 class MockSearchIPCRouterDelegate : public SearchIPCRouter::Delegate { |
| 42 public: | 47 public: |
| 43 virtual ~MockSearchIPCRouterDelegate() {} | 48 virtual ~MockSearchIPCRouterDelegate() {} |
| 44 | 49 |
| 45 MOCK_METHOD1(OnInstantSupportDetermined, void(bool supports_instant)); | 50 MOCK_METHOD1(OnInstantSupportDetermined, void(bool supports_instant)); |
| 46 MOCK_METHOD1(OnSetVoiceSearchSupport, void(bool supports_voice_search)); | 51 MOCK_METHOD1(OnSetVoiceSearchSupport, void(bool supports_voice_search)); |
| 47 MOCK_METHOD1(FocusOmnibox, void(OmniboxFocusState state)); | 52 MOCK_METHOD1(FocusOmnibox, void(OmniboxFocusState state)); |
| 48 MOCK_METHOD3(NavigateToURL, void(const GURL&, WindowOpenDisposition, bool)); | 53 MOCK_METHOD3(NavigateToURL, void(const GURL&, WindowOpenDisposition, bool)); |
| (...skipping 15 matching lines...) Expand all Loading... |
| 64 public: | 69 public: |
| 65 virtual void SetUp() { | 70 virtual void SetUp() { |
| 66 ChromeRenderViewHostTestHarness::SetUp(); | 71 ChromeRenderViewHostTestHarness::SetUp(); |
| 67 SearchTabHelper::CreateForWebContents(web_contents()); | 72 SearchTabHelper::CreateForWebContents(web_contents()); |
| 68 } | 73 } |
| 69 | 74 |
| 70 virtual content::BrowserContext* CreateBrowserContext() OVERRIDE { | 75 virtual content::BrowserContext* CreateBrowserContext() OVERRIDE { |
| 71 TestingProfile::Builder builder; | 76 TestingProfile::Builder builder; |
| 72 builder.AddTestingFactory(SigninManagerFactory::GetInstance(), | 77 builder.AddTestingFactory(SigninManagerFactory::GetInstance(), |
| 73 FakeSigninManagerBase::Build); | 78 FakeSigninManagerBase::Build); |
| 79 builder.AddTestingFactory( |
| 80 ProfileSyncServiceFactory::GetInstance(), |
| 81 ProfileSyncServiceMock::BuildMockProfileSyncService); |
| 74 return builder.Build().release(); | 82 return builder.Build().release(); |
| 75 } | 83 } |
| 76 | 84 |
| 77 // Creates a sign-in manager for tests. If |username| is not empty, the | 85 // Creates a sign-in manager for tests. If |username| is not empty, the |
| 78 // testing profile of the WebContents will be connected to the given account. | 86 // testing profile of the WebContents will be connected to the given account. |
| 79 void CreateSigninManager(const std::string& username) { | 87 // The account can be configured to |sync_history| or not. |
| 88 void CreateSigninManager(const std::string& username, bool sync_history) { |
| 80 SigninManagerBase* signin_manager = static_cast<SigninManagerBase*>( | 89 SigninManagerBase* signin_manager = static_cast<SigninManagerBase*>( |
| 81 SigninManagerFactory::GetForProfile(profile())); | 90 SigninManagerFactory::GetForProfile(profile())); |
| 82 | 91 |
| 83 if (!username.empty()) { | 92 if (!username.empty()) { |
| 84 ASSERT_TRUE(signin_manager); | 93 ASSERT_TRUE(signin_manager); |
| 85 signin_manager->SetAuthenticatedUsername(username); | 94 signin_manager->SetAuthenticatedUsername(username); |
| 86 } | 95 } |
| 96 |
| 97 ProfileSyncServiceMock* sync_service = static_cast<ProfileSyncServiceMock*>( |
| 98 ProfileSyncServiceFactory::GetInstance()->GetForProfile(profile())); |
| 99 |
| 100 EXPECT_CALL(*sync_service, sync_initialized()).WillRepeatedly(Return(true)); |
| 101 syncer::ModelTypeSet result; |
| 102 if (sync_history) { |
| 103 result.Put(syncer::HISTORY_DELETE_DIRECTIVES); |
| 104 } |
| 105 EXPECT_CALL(*sync_service, GetActiveDataTypes()) |
| 106 .WillRepeatedly(Return(result)); |
| 87 } | 107 } |
| 88 | 108 |
| 89 bool MessageWasSent(uint32 id) { | 109 bool MessageWasSent(uint32 id) { |
| 90 return process()->sink().GetFirstMessageMatching(id) != NULL; | 110 return process()->sink().GetFirstMessageMatching(id) != NULL; |
| 91 } | 111 } |
| 92 | 112 |
| 93 MockSearchIPCRouterDelegate* mock_delegate() { return &delegate_; } | 113 MockSearchIPCRouterDelegate* mock_delegate() { return &delegate_; } |
| 94 | 114 |
| 95 private: | 115 private: |
| 96 MockSearchIPCRouterDelegate delegate_; | 116 MockSearchIPCRouterDelegate delegate_; |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 139 SearchTabHelper::FromWebContents(web_contents()); | 159 SearchTabHelper::FromWebContents(web_contents()); |
| 140 ASSERT_NE(static_cast<SearchTabHelper*>(NULL), search_tab_helper); | 160 ASSERT_NE(static_cast<SearchTabHelper*>(NULL), search_tab_helper); |
| 141 search_tab_helper->ipc_router().set_delegate_for_testing(mock_delegate()); | 161 search_tab_helper->ipc_router().set_delegate_for_testing(mock_delegate()); |
| 142 search_tab_helper->DetermineIfPageSupportsInstant(); | 162 search_tab_helper->DetermineIfPageSupportsInstant(); |
| 143 ASSERT_FALSE(MessageWasSent( | 163 ASSERT_FALSE(MessageWasSent( |
| 144 ChromeViewMsg_DetermineIfPageSupportsInstant::ID)); | 164 ChromeViewMsg_DetermineIfPageSupportsInstant::ID)); |
| 145 } | 165 } |
| 146 | 166 |
| 147 TEST_F(SearchTabHelperTest, OnChromeIdentityCheckMatch) { | 167 TEST_F(SearchTabHelperTest, OnChromeIdentityCheckMatch) { |
| 148 NavigateAndCommit(GURL(chrome::kChromeSearchLocalNtpUrl)); | 168 NavigateAndCommit(GURL(chrome::kChromeSearchLocalNtpUrl)); |
| 149 CreateSigninManager(std::string("foo@bar.com")); | 169 CreateSigninManager(std::string("foo@bar.com"), true); |
| 150 SearchTabHelper* search_tab_helper = | 170 SearchTabHelper* search_tab_helper = |
| 151 SearchTabHelper::FromWebContents(web_contents()); | 171 SearchTabHelper::FromWebContents(web_contents()); |
| 152 ASSERT_NE(static_cast<SearchTabHelper*>(NULL), search_tab_helper); | 172 ASSERT_NE(static_cast<SearchTabHelper*>(NULL), search_tab_helper); |
| 153 | 173 |
| 154 const base::string16 test_identity = base::ASCIIToUTF16("foo@bar.com"); | 174 const base::string16 test_identity = base::ASCIIToUTF16("foo@bar.com"); |
| 155 search_tab_helper->OnChromeIdentityCheck(test_identity); | 175 search_tab_helper->OnChromeIdentityCheck(test_identity); |
| 156 | 176 |
| 157 const IPC::Message* message = process()->sink().GetUniqueMessageMatching( | 177 const IPC::Message* message = process()->sink().GetUniqueMessageMatching( |
| 158 ChromeViewMsg_ChromeIdentityCheckResult::ID); | 178 ChromeViewMsg_ChromeIdentityCheckResult::ID); |
| 159 ASSERT_TRUE(message != NULL); | 179 ASSERT_TRUE(message != NULL); |
| 160 | 180 |
| 161 ChromeViewMsg_ChromeIdentityCheckResult::Param params; | 181 ChromeViewMsg_ChromeIdentityCheckResult::Param params; |
| 162 ChromeViewMsg_ChromeIdentityCheckResult::Read(message, ¶ms); | 182 ChromeViewMsg_ChromeIdentityCheckResult::Read(message, ¶ms); |
| 163 EXPECT_EQ(test_identity, params.a); | 183 EXPECT_EQ(test_identity, params.a); |
| 164 ASSERT_TRUE(params.b); | 184 ASSERT_TRUE(params.b); |
| 165 } | 185 } |
| 166 | 186 |
| 167 TEST_F(SearchTabHelperTest, OnChromeIdentityCheckMismatch) { | 187 TEST_F(SearchTabHelperTest, OnChromeIdentityCheckMismatch) { |
| 168 NavigateAndCommit(GURL(chrome::kChromeSearchLocalNtpUrl)); | 188 NavigateAndCommit(GURL(chrome::kChromeSearchLocalNtpUrl)); |
| 169 CreateSigninManager(std::string("foo@bar.com")); | 189 CreateSigninManager(std::string("foo@bar.com"), true); |
| 170 SearchTabHelper* search_tab_helper = | 190 SearchTabHelper* search_tab_helper = |
| 171 SearchTabHelper::FromWebContents(web_contents()); | 191 SearchTabHelper::FromWebContents(web_contents()); |
| 172 ASSERT_NE(static_cast<SearchTabHelper*>(NULL), search_tab_helper); | 192 ASSERT_NE(static_cast<SearchTabHelper*>(NULL), search_tab_helper); |
| 173 | 193 |
| 174 const base::string16 test_identity = base::ASCIIToUTF16("bar@foo.com"); | 194 const base::string16 test_identity = base::ASCIIToUTF16("bar@foo.com"); |
| 175 search_tab_helper->OnChromeIdentityCheck(test_identity); | 195 search_tab_helper->OnChromeIdentityCheck(test_identity); |
| 176 | 196 |
| 177 const IPC::Message* message = process()->sink().GetUniqueMessageMatching( | 197 const IPC::Message* message = process()->sink().GetUniqueMessageMatching( |
| 178 ChromeViewMsg_ChromeIdentityCheckResult::ID); | 198 ChromeViewMsg_ChromeIdentityCheckResult::ID); |
| 179 ASSERT_TRUE(message != NULL); | 199 ASSERT_TRUE(message != NULL); |
| 180 | 200 |
| 181 ChromeViewMsg_ChromeIdentityCheckResult::Param params; | 201 ChromeViewMsg_ChromeIdentityCheckResult::Param params; |
| 182 ChromeViewMsg_ChromeIdentityCheckResult::Read(message, ¶ms); | 202 ChromeViewMsg_ChromeIdentityCheckResult::Read(message, ¶ms); |
| 183 EXPECT_EQ(test_identity, params.a); | 203 EXPECT_EQ(test_identity, params.a); |
| 184 ASSERT_FALSE(params.b); | 204 ASSERT_FALSE(params.b); |
| 185 } | 205 } |
| 186 | 206 |
| 187 TEST_F(SearchTabHelperTest, OnChromeIdentityCheckSignedOutMatch) { | 207 TEST_F(SearchTabHelperTest, OnChromeIdentityCheckSignedOutMatch) { |
| 188 NavigateAndCommit(GURL(chrome::kChromeSearchLocalNtpUrl)); | 208 NavigateAndCommit(GURL(chrome::kChromeSearchLocalNtpUrl)); |
| 189 // This test does not sign in. | 209 // This test does not sign in. |
| 210 ProfileSyncServiceMock* sync_service = static_cast<ProfileSyncServiceMock*>( |
| 211 ProfileSyncServiceFactory::GetInstance()->GetForProfile(profile())); |
| 212 EXPECT_CALL(*sync_service, sync_initialized()).WillRepeatedly(Return(false)); |
| 190 SearchTabHelper* search_tab_helper = | 213 SearchTabHelper* search_tab_helper = |
| 191 SearchTabHelper::FromWebContents(web_contents()); | 214 SearchTabHelper::FromWebContents(web_contents()); |
| 192 ASSERT_NE(static_cast<SearchTabHelper*>(NULL), search_tab_helper); | 215 ASSERT_NE(static_cast<SearchTabHelper*>(NULL), search_tab_helper); |
| 193 | 216 |
| 194 const base::string16 test_identity; | 217 const base::string16 test_identity; |
| 195 search_tab_helper->OnChromeIdentityCheck(test_identity); | 218 search_tab_helper->OnChromeIdentityCheck(test_identity); |
| 196 | 219 |
| 197 const IPC::Message* message = process()->sink().GetUniqueMessageMatching( | 220 const IPC::Message* message = process()->sink().GetUniqueMessageMatching( |
| 198 ChromeViewMsg_ChromeIdentityCheckResult::ID); | 221 ChromeViewMsg_ChromeIdentityCheckResult::ID); |
| 199 ASSERT_TRUE(message != NULL); | 222 ASSERT_TRUE(message != NULL); |
| 200 | 223 |
| 201 ChromeViewMsg_ChromeIdentityCheckResult::Param params; | 224 ChromeViewMsg_ChromeIdentityCheckResult::Param params; |
| 202 ChromeViewMsg_ChromeIdentityCheckResult::Read(message, ¶ms); | 225 ChromeViewMsg_ChromeIdentityCheckResult::Read(message, ¶ms); |
| 203 EXPECT_EQ(test_identity, params.a); | 226 EXPECT_EQ(test_identity, params.a); |
| 204 ASSERT_TRUE(params.b); | 227 ASSERT_FALSE(params.b); |
| 205 } | 228 } |
| 206 | 229 |
| 207 TEST_F(SearchTabHelperTest, OnChromeIdentityCheckSignedOutMismatch) { | 230 TEST_F(SearchTabHelperTest, OnChromeIdentityCheckSignedOutMismatch) { |
| 208 NavigateAndCommit(GURL(chrome::kChromeSearchLocalNtpUrl)); | 231 NavigateAndCommit(GURL(chrome::kChromeSearchLocalNtpUrl)); |
| 209 // This test does not sign in. | 232 // This test does not sign in. |
| 233 ProfileSyncServiceMock* sync_service = static_cast<ProfileSyncServiceMock*>( |
| 234 ProfileSyncServiceFactory::GetInstance()->GetForProfile(profile())); |
| 235 EXPECT_CALL(*sync_service, sync_initialized()).WillRepeatedly(Return(false)); |
| 210 SearchTabHelper* search_tab_helper = | 236 SearchTabHelper* search_tab_helper = |
| 211 SearchTabHelper::FromWebContents(web_contents()); | 237 SearchTabHelper::FromWebContents(web_contents()); |
| 212 ASSERT_NE(static_cast<SearchTabHelper*>(NULL), search_tab_helper); | 238 ASSERT_NE(static_cast<SearchTabHelper*>(NULL), search_tab_helper); |
| 213 | 239 |
| 214 const base::string16 test_identity = base::ASCIIToUTF16("bar@foo.com"); | 240 const base::string16 test_identity = base::ASCIIToUTF16("bar@foo.com"); |
| 215 search_tab_helper->OnChromeIdentityCheck(test_identity); | 241 search_tab_helper->OnChromeIdentityCheck(test_identity); |
| 216 | 242 |
| 217 const IPC::Message* message = process()->sink().GetUniqueMessageMatching( | 243 const IPC::Message* message = process()->sink().GetUniqueMessageMatching( |
| 218 ChromeViewMsg_ChromeIdentityCheckResult::ID); | 244 ChromeViewMsg_ChromeIdentityCheckResult::ID); |
| 219 ASSERT_TRUE(message != NULL); | 245 ASSERT_TRUE(message != NULL); |
| 220 | 246 |
| 221 ChromeViewMsg_ChromeIdentityCheckResult::Param params; | 247 ChromeViewMsg_ChromeIdentityCheckResult::Param params; |
| 222 ChromeViewMsg_ChromeIdentityCheckResult::Read(message, ¶ms); | 248 ChromeViewMsg_ChromeIdentityCheckResult::Read(message, ¶ms); |
| 223 EXPECT_EQ(test_identity, params.a); | 249 EXPECT_EQ(test_identity, params.a); |
| 224 ASSERT_FALSE(params.b); | 250 ASSERT_FALSE(params.b); |
| 225 } | 251 } |
| 226 | 252 |
| 253 TEST_F(SearchTabHelperTest, OnChromeIdentityCheckMatchNotSyncing) { |
| 254 NavigateAndCommit(GURL(chrome::kChromeSearchLocalNtpUrl)); |
| 255 CreateSigninManager(std::string("foo@bar.com"), false); |
| 256 SearchTabHelper* search_tab_helper = |
| 257 SearchTabHelper::FromWebContents(web_contents()); |
| 258 ASSERT_NE(static_cast<SearchTabHelper*>(NULL), search_tab_helper); |
| 259 |
| 260 const base::string16 test_identity = base::ASCIIToUTF16("foo@bar.com"); |
| 261 search_tab_helper->OnChromeIdentityCheck(test_identity); |
| 262 |
| 263 const IPC::Message* message = process()->sink().GetUniqueMessageMatching( |
| 264 ChromeViewMsg_ChromeIdentityCheckResult::ID); |
| 265 ASSERT_TRUE(message != NULL); |
| 266 |
| 267 ChromeViewMsg_ChromeIdentityCheckResult::Param params; |
| 268 ChromeViewMsg_ChromeIdentityCheckResult::Read(message, ¶ms); |
| 269 EXPECT_EQ(test_identity, params.a); |
| 270 ASSERT_FALSE(params.b); |
| 271 } |
| 272 |
| 227 class TabTitleObserver : public content::WebContentsObserver { | 273 class TabTitleObserver : public content::WebContentsObserver { |
| 228 public: | 274 public: |
| 229 explicit TabTitleObserver(content::WebContents* contents) | 275 explicit TabTitleObserver(content::WebContents* contents) |
| 230 : WebContentsObserver(contents) {} | 276 : WebContentsObserver(contents) {} |
| 231 | 277 |
| 232 base::string16 title_on_start() { return title_on_start_; } | 278 base::string16 title_on_start() { return title_on_start_; } |
| 233 base::string16 title_on_commit() { return title_on_commit_; } | 279 base::string16 title_on_commit() { return title_on_commit_; } |
| 234 | 280 |
| 235 private: | 281 private: |
| 236 virtual void DidStartProvisionalLoadForFrame( | 282 virtual void DidStartProvisionalLoadForFrame( |
| (...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 338 SearchTabHelper::FromWebContents(contents); | 384 SearchTabHelper::FromWebContents(contents); |
| 339 ASSERT_NE(static_cast<SearchTabHelper*>(NULL), search_tab_helper); | 385 ASSERT_NE(static_cast<SearchTabHelper*>(NULL), search_tab_helper); |
| 340 | 386 |
| 341 // Any other web page shouldn't be redirected when provisional load fails. | 387 // Any other web page shouldn't be redirected when provisional load fails. |
| 342 search_tab_helper->DidFailProvisionalLoad(1, base::string16(), true, | 388 search_tab_helper->DidFailProvisionalLoad(1, base::string16(), true, |
| 343 GURL("http://www.example.com"), 1, base::string16(), NULL); | 389 GURL("http://www.example.com"), 1, base::string16(), NULL); |
| 344 CommitPendingLoad(controller); | 390 CommitPendingLoad(controller); |
| 345 EXPECT_NE(GURL(chrome::kChromeSearchLocalNtpUrl), | 391 EXPECT_NE(GURL(chrome::kChromeSearchLocalNtpUrl), |
| 346 controller->GetLastCommittedEntry()->GetURL()); | 392 controller->GetLastCommittedEntry()->GetURL()); |
| 347 } | 393 } |
| OLD | NEW |