Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / search / instant_service_unittest.cc
bloba4e9163e4f0ce5acec45131664b36d1d761f5f56
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.
5 #include <string>
6 #include <vector>
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/browser/ui/search/instant_search_prerenderer.h"
19 #include "chrome/common/render_messages.h"
20 #include "components/variations/entropy_provider.h"
21 #include "content/public/browser/notification_service.h"
22 #include "content/public/browser/notification_types.h"
23 #include "content/public/test/mock_render_process_host.h"
24 #include "ipc/ipc_message.h"
25 #include "ipc/ipc_test_sink.h"
26 #include "testing/gmock/include/gmock/gmock.h"
27 #include "url/gurl.h"
29 class MockInstantServiceObserver : public InstantServiceObserver {
30 public:
31 MOCK_METHOD0(DefaultSearchProviderChanged, void());
32 MOCK_METHOD0(GoogleURLUpdated, void());
35 class InstantServiceTest : public InstantUnitTestBase {
36 protected:
37 virtual void SetUp() OVERRIDE {
38 InstantUnitTestBase::SetUpWithoutCacheableNTP();
40 instant_service_observer_.reset(new MockInstantServiceObserver());
41 instant_service_->AddObserver(instant_service_observer_.get());
44 virtual void TearDown() OVERRIDE {
45 instant_service_->RemoveObserver(instant_service_observer_.get());
46 InstantUnitTestBase::TearDown();
49 InstantSearchPrerenderer* GetInstantSearchPrerenderer() {
50 return instant_service_->instant_search_prerenderer();
53 scoped_ptr<MockInstantServiceObserver> instant_service_observer_;
56 TEST_F(InstantServiceTest, DispatchDefaultSearchProviderChanged) {
57 EXPECT_CALL(*instant_service_observer_.get(), DefaultSearchProviderChanged())
58 .Times(1);
60 const std::string& new_base_url = "https://bar.com/";
61 SetDefaultSearchProvider(new_base_url);
64 TEST_F(InstantServiceTest, DispatchGoogleURLUpdated) {
65 EXPECT_CALL(*instant_service_observer_.get(), GoogleURLUpdated()).Times(1);
67 const std::string& new_base_url = "https://www.google.es/";
68 NotifyGoogleBaseURLUpdate(new_base_url);
71 TEST_F(InstantServiceTest, SendsSearchURLsToRenderer) {
72 ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial("EmbeddedSearch",
73 "Group1 use_cacheable_ntp:1"));
74 scoped_ptr<content::MockRenderProcessHost> rph(
75 new content::MockRenderProcessHost(profile()));
76 rph->sink().ClearMessages();
77 instant_service_->Observe(
78 content::NOTIFICATION_RENDERER_PROCESS_CREATED,
79 content::Source<content::MockRenderProcessHost>(rph.get()),
80 content::NotificationService::NoDetails());
81 EXPECT_EQ(1U, rph->sink().message_count());
82 const IPC::Message* msg = rph->sink().GetMessageAt(0);
83 ASSERT_TRUE(msg);
84 std::vector<GURL> search_urls;
85 GURL new_tab_page_url;
86 ChromeViewMsg_SetSearchURLs::Read(msg, &search_urls, &new_tab_page_url);
87 EXPECT_EQ(2U, search_urls.size());
88 EXPECT_EQ("https://www.google.com/alt#quux=", search_urls[0].spec());
89 EXPECT_EQ("https://www.google.com/url?bar=", search_urls[1].spec());
90 EXPECT_EQ("https://www.google.com/newtab", new_tab_page_url.spec());
93 TEST_F(InstantServiceTest, InstantSearchDisabled) {
94 // 'prefetch_results' flag is not enabled in field trials. Make sure
95 // InstantSearchPrerenderer is not initialized.
96 EXPECT_EQ(static_cast<InstantSearchPrerenderer*>(NULL),
97 GetInstantSearchPrerenderer());
100 TEST_F(InstantServiceTest,
101 ResetInstantSearchPrerenderer_DefaultProviderChanged) {
102 ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial(
103 "EmbeddedSearch", "Group1 use_cacheable_ntp:1 prefetch_results:1"));
104 EXPECT_CALL(*instant_service_observer_.get(), DefaultSearchProviderChanged())
105 .Times(2);
107 // Set a default search provider that doesn't support Instant.
108 TemplateURLData data;
109 data.SetURL("https://foobar.com/url?bar={searchTerms}");
110 TemplateURL* template_url = new TemplateURL(profile(), data);
111 // Takes ownership of |template_url|.
112 template_url_service_->Add(template_url);
113 template_url_service_->SetDefaultSearchProvider(template_url);
115 EXPECT_EQ(static_cast<InstantSearchPrerenderer*>(NULL),
116 GetInstantSearchPrerenderer());
118 // Set a default search provider that supports Instant and make sure
119 // InstantSearchPrerenderer is valid.
120 SetDefaultSearchProvider("https://google.com/");
121 EXPECT_NE(static_cast<InstantSearchPrerenderer*>(NULL),
122 GetInstantSearchPrerenderer());
125 TEST_F(InstantServiceTest, ResetInstantSearchPrerenderer_GoogleBaseURLUpdated) {
126 ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial(
127 "EmbeddedSearch", "Group1 use_cacheable_ntp:1 prefetch_results:1"));
128 EXPECT_CALL(*instant_service_observer_.get(), DefaultSearchProviderChanged())
129 .Times(1);
130 EXPECT_CALL(*instant_service_observer_.get(), GoogleURLUpdated()).Times(1);
132 SetDefaultSearchProvider("https://google.com/");
133 InstantSearchPrerenderer* old_prerenderer = GetInstantSearchPrerenderer();
134 EXPECT_NE(static_cast<InstantSearchPrerenderer*>(NULL), old_prerenderer);
136 const std::string& new_base_url = "https://www.google.es/";
137 NotifyGoogleBaseURLUpdate(new_base_url);
138 EXPECT_NE(old_prerenderer, GetInstantSearchPrerenderer());