| 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 <string> | 5 #include <string> |
| 6 #include <vector> | 6 #include <vector> |
| 7 | 7 |
| 8 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
| 9 #include "base/metrics/field_trial.h" |
| 9 #include "base/strings/string_util.h" | 10 #include "base/strings/string_util.h" |
| 10 #include "chrome/browser/search/instant_service.h" | 11 #include "chrome/browser/search/instant_service.h" |
| 11 #include "chrome/browser/search/instant_service_observer.h" | 12 #include "chrome/browser/search/instant_service_observer.h" |
| 12 #include "chrome/browser/search/instant_unittest_base.h" | 13 #include "chrome/browser/search/instant_unittest_base.h" |
| 13 #include "chrome/browser/search/search.h" | 14 #include "chrome/browser/search/search.h" |
| 14 #include "chrome/browser/ui/browser.h" | 15 #include "chrome/browser/ui/browser.h" |
| 15 #include "chrome/browser/ui/browser_window.h" | 16 #include "chrome/browser/ui/browser_window.h" |
| 16 #include "chrome/browser/ui/host_desktop.h" | 17 #include "chrome/browser/ui/host_desktop.h" |
| 18 #include "chrome/common/render_messages.h" |
| 19 #include "components/variations/entropy_provider.h" |
| 20 #include "content/public/browser/notification_service.h" |
| 21 #include "content/public/browser/notification_types.h" |
| 17 #include "content/public/browser/web_contents.h" | 22 #include "content/public/browser/web_contents.h" |
| 18 #include "content/public/browser/web_contents_observer.h" | 23 #include "content/public/browser/web_contents_observer.h" |
| 24 #include "content/public/test/mock_render_process_host.h" |
| 25 #include "ipc/ipc_message.h" |
| 26 #include "ipc/ipc_test_sink.h" |
| 19 #include "testing/gmock/include/gmock/gmock.h" | 27 #include "testing/gmock/include/gmock/gmock.h" |
| 20 | 28 #include "url/gurl.h" |
| 21 namespace chrome { | |
| 22 | |
| 23 namespace { | |
| 24 | 29 |
| 25 class MockInstantServiceObserver : public InstantServiceObserver { | 30 class MockInstantServiceObserver : public InstantServiceObserver { |
| 26 public: | 31 public: |
| 27 MOCK_METHOD0(DefaultSearchProviderChanged, void()); | 32 MOCK_METHOD0(DefaultSearchProviderChanged, void()); |
| 28 MOCK_METHOD0(GoogleURLUpdated, void()); | 33 MOCK_METHOD0(GoogleURLUpdated, void()); |
| 29 }; | 34 }; |
| 30 | 35 |
| 31 class MockWebContentsObserver : public content::WebContentsObserver { | 36 class MockWebContentsObserver : public content::WebContentsObserver { |
| 32 public: | 37 public: |
| 33 MOCK_METHOD1(WebContentsDestroyed, void(content::WebContents*)); | 38 MOCK_METHOD1(WebContentsDestroyed, void(content::WebContents*)); |
| 34 | 39 |
| 35 // Dumb override to make MSVC happy. | 40 // Dumb override to make MSVC happy. |
| 36 void Observe_(content::WebContents* contents) { | 41 void Observe_(content::WebContents* contents) { |
| 37 content::WebContentsObserver::Observe(contents); | 42 content::WebContentsObserver::Observe(contents); |
| 38 } | 43 } |
| 39 | 44 |
| 40 protected: | 45 protected: |
| 41 friend class InstantServiceTest; | 46 friend class InstantServiceTest; |
| 42 FRIEND_TEST_ALL_PREFIXES(InstantServiceTest, | 47 FRIEND_TEST_ALL_PREFIXES(InstantServiceTest, |
| 43 DispatchDefaultSearchProviderChanged); | 48 DispatchDefaultSearchProviderChanged); |
| 44 FRIEND_TEST_ALL_PREFIXES(InstantServiceTest, DispatchGoogleURLUpdated); | 49 FRIEND_TEST_ALL_PREFIXES(InstantServiceTest, DispatchGoogleURLUpdated); |
| 45 }; | 50 }; |
| 46 | 51 |
| 47 class InstantServiceTest : public InstantUnitTestBase { | 52 class InstantServiceTest : public InstantUnitTestBase { |
| 48 protected: | 53 protected: |
| 49 virtual void SetUp() OVERRIDE { | 54 virtual void SetUp() OVERRIDE { |
| 50 InstantUnitTestBase::SetUp(); | 55 InstantUnitTestBase::SetUp(); |
| 56 field_trial_list_.reset(new base::FieldTrialList( |
| 57 new metrics::SHA1EntropyProvider("42"))); |
| 51 | 58 |
| 52 instant_service_observer_.reset(new MockInstantServiceObserver()); | 59 instant_service_observer_.reset(new MockInstantServiceObserver()); |
| 53 instant_service_->AddObserver(instant_service_observer_.get()); | 60 instant_service_->AddObserver(instant_service_observer_.get()); |
| 54 | 61 |
| 55 instant_ntp_contents_observer_.reset(new MockWebContentsObserver()); | 62 instant_ntp_contents_observer_.reset(new MockWebContentsObserver()); |
| 56 instant_ntp_contents_observer_->Observe_( | 63 instant_ntp_contents_observer_->Observe_( |
| 57 instant_service_->GetNTPContents()); | 64 instant_service_->GetNTPContents()); |
| 58 } | 65 } |
| 59 | 66 |
| 60 virtual void TearDown() OVERRIDE { | 67 virtual void TearDown() OVERRIDE { |
| 61 instant_service_->RemoveObserver(instant_service_observer_.get()); | 68 instant_service_->RemoveObserver(instant_service_observer_.get()); |
| 62 instant_ntp_contents_observer_->Observe_(NULL); | 69 instant_ntp_contents_observer_->Observe_(NULL); |
| 63 InstantUnitTestBase::TearDown(); | 70 InstantUnitTestBase::TearDown(); |
| 64 } | 71 } |
| 65 | 72 |
| 66 scoped_ptr<MockInstantServiceObserver> instant_service_observer_; | 73 scoped_ptr<MockInstantServiceObserver> instant_service_observer_; |
| 67 scoped_ptr<MockWebContentsObserver> instant_ntp_contents_observer_; | 74 scoped_ptr<MockWebContentsObserver> instant_ntp_contents_observer_; |
| 75 scoped_ptr<base::FieldTrialList> field_trial_list_; |
| 68 }; | 76 }; |
| 69 | 77 |
| 70 TEST_F(InstantServiceTest, DispatchDefaultSearchProviderChanged) { | 78 TEST_F(InstantServiceTest, DispatchDefaultSearchProviderChanged) { |
| 71 EXPECT_CALL(*instant_service_observer_.get(), DefaultSearchProviderChanged()) | 79 EXPECT_CALL(*instant_service_observer_.get(), DefaultSearchProviderChanged()) |
| 72 .Times(1); | 80 .Times(1); |
| 73 EXPECT_CALL(*instant_ntp_contents_observer_.get(), | 81 EXPECT_CALL(*instant_ntp_contents_observer_.get(), |
| 74 WebContentsDestroyed(instant_service_->GetNTPContents())) | 82 WebContentsDestroyed(instant_service_->GetNTPContents())) |
| 75 .Times(1); | 83 .Times(1); |
| 76 | 84 |
| 77 GURL ntp_url = instant_service_->GetNTPContents()->GetURL(); | 85 GURL ntp_url = instant_service_->GetNTPContents()->GetURL(); |
| (...skipping 11 matching lines...) Expand all Loading... |
| 89 .Times(1); | 97 .Times(1); |
| 90 | 98 |
| 91 GURL ntp_url = instant_service_->GetNTPContents()->GetURL(); | 99 GURL ntp_url = instant_service_->GetNTPContents()->GetURL(); |
| 92 const std::string& new_base_url = "https://www.google.es/"; | 100 const std::string& new_base_url = "https://www.google.es/"; |
| 93 NotifyGoogleBaseURLUpdate(new_base_url); | 101 NotifyGoogleBaseURLUpdate(new_base_url); |
| 94 GURL new_ntp_url = instant_service_->GetNTPContents()->GetURL(); | 102 GURL new_ntp_url = instant_service_->GetNTPContents()->GetURL(); |
| 95 EXPECT_NE(ntp_url, new_ntp_url); | 103 EXPECT_NE(ntp_url, new_ntp_url); |
| 96 EXPECT_TRUE(StartsWithASCII(new_ntp_url.spec(), new_base_url, true)); | 104 EXPECT_TRUE(StartsWithASCII(new_ntp_url.spec(), new_base_url, true)); |
| 97 } | 105 } |
| 98 | 106 |
| 99 } // namespace | 107 TEST_F(InstantServiceTest, SendsSearchURLsToRenderer) { |
| 100 | 108 ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial("InstantExtended", |
| 101 } // namespace chrome | 109 "Group1 use_cacheable_ntp:1")); |
| 110 scoped_ptr<content::MockRenderProcessHost> rph( |
| 111 new content::MockRenderProcessHost(profile())); |
| 112 rph->sink().ClearMessages(); |
| 113 instant_service_->Observe( |
| 114 content::NOTIFICATION_RENDERER_PROCESS_CREATED, |
| 115 content::Source<content::MockRenderProcessHost>(rph.get()), |
| 116 content::NotificationService::NoDetails()); |
| 117 EXPECT_EQ(1U, rph->sink().message_count()); |
| 118 const IPC::Message* msg = rph->sink().GetMessageAt(0); |
| 119 ASSERT_TRUE(msg); |
| 120 std::vector<GURL> search_urls; |
| 121 GURL new_tab_page_url; |
| 122 ChromeViewMsg_SetSearchURLs::Read(msg, &search_urls, &new_tab_page_url); |
| 123 EXPECT_EQ(2U, search_urls.size()); |
| 124 EXPECT_EQ("https://www.google.com/alt#quux=", search_urls[0].spec()); |
| 125 EXPECT_EQ("https://www.google.com/url?bar=", search_urls[1].spec()); |
| 126 EXPECT_EQ("https://www.google.com/newtab", new_tab_page_url.spec()); |
| 127 } |
| OLD | NEW |