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
3 // found in the LICENSE file.
8 #include "base/memory/scoped_ptr.h"
9 #include "base/metrics/field_trial.h"
10 #include "base/strings/string_util.h"
11 #include "chrome/browser/search/instant_service.h"
12 #include "chrome/browser/search/instant_service_observer.h"
13 #include "chrome/browser/search/instant_unittest_base.h"
14 #include "chrome/browser/search/search.h"
15 #include "chrome/browser/ui/browser.h"
16 #include "chrome/browser/ui/browser_window.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"
22 #include "content/public/browser/web_contents.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"
27 #include "testing/gmock/include/gmock/gmock.h"
30 class MockInstantServiceObserver : public InstantServiceObserver {
32 MOCK_METHOD0(DefaultSearchProviderChanged, void());
33 MOCK_METHOD0(GoogleURLUpdated, void());
36 class MockWebContentsObserver : public content::WebContentsObserver {
38 MOCK_METHOD1(WebContentsDestroyed, void(content::WebContents*));
40 // Dumb override to make MSVC happy.
41 void Observe_(content::WebContents* contents) {
42 content::WebContentsObserver::Observe(contents);
46 friend class InstantServiceTest;
47 FRIEND_TEST_ALL_PREFIXES(InstantServiceTest,
48 DispatchDefaultSearchProviderChanged);
49 FRIEND_TEST_ALL_PREFIXES(InstantServiceTest, DispatchGoogleURLUpdated);
52 class InstantServiceTest : public InstantUnitTestBase {
54 virtual void SetUp() OVERRIDE {
55 InstantUnitTestBase::SetUp();
56 field_trial_list_.reset(new base::FieldTrialList(
57 new metrics::SHA1EntropyProvider("42")));
59 instant_service_observer_.reset(new MockInstantServiceObserver());
60 instant_service_->AddObserver(instant_service_observer_.get());
62 instant_ntp_contents_observer_.reset(new MockWebContentsObserver());
63 instant_ntp_contents_observer_->Observe_(
64 instant_service_->GetNTPContents());
67 virtual void TearDown() OVERRIDE {
68 instant_service_->RemoveObserver(instant_service_observer_.get());
69 instant_ntp_contents_observer_->Observe_(NULL);
70 InstantUnitTestBase::TearDown();
73 scoped_ptr<MockInstantServiceObserver> instant_service_observer_;
74 scoped_ptr<MockWebContentsObserver> instant_ntp_contents_observer_;
75 scoped_ptr<base::FieldTrialList> field_trial_list_;
78 TEST_F(InstantServiceTest, DispatchDefaultSearchProviderChanged) {
79 EXPECT_CALL(*instant_service_observer_.get(), DefaultSearchProviderChanged())
81 EXPECT_CALL(*instant_ntp_contents_observer_.get(),
82 WebContentsDestroyed(instant_service_->GetNTPContents()))
85 GURL ntp_url = instant_service_->GetNTPContents()->GetURL();
86 const std::string& new_base_url = "https://bar.com/";
87 SetDefaultSearchProvider(new_base_url);
88 GURL new_ntp_url = instant_service_->GetNTPContents()->GetURL();
89 EXPECT_NE(ntp_url, new_ntp_url);
90 EXPECT_TRUE(StartsWithASCII(new_ntp_url.spec(), new_base_url, true));
93 TEST_F(InstantServiceTest, DispatchGoogleURLUpdated) {
94 EXPECT_CALL(*instant_service_observer_.get(), GoogleURLUpdated()).Times(1);
95 EXPECT_CALL(*instant_ntp_contents_observer_.get(),
96 WebContentsDestroyed(instant_service_->GetNTPContents()))
99 GURL ntp_url = instant_service_->GetNTPContents()->GetURL();
100 const std::string& new_base_url = "https://www.google.es/";
101 NotifyGoogleBaseURLUpdate(new_base_url);
102 GURL new_ntp_url = instant_service_->GetNTPContents()->GetURL();
103 EXPECT_NE(ntp_url, new_ntp_url);
104 EXPECT_TRUE(StartsWithASCII(new_ntp_url.spec(), new_base_url, true));
107 TEST_F(InstantServiceTest, SendsSearchURLsToRenderer) {
108 ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial("InstantExtended",
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);
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());