Update broken references to image assets
[chromium-blink-merge.git] / chrome / browser / search / instant_service_unittest.cc
blob4b5d0f7d5916c174bbacf25ae09f3ddb229cefbb
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 "base/strings/utf_string_conversions.h"
12 #include "chrome/browser/search/instant_service.h"
13 #include "chrome/browser/search/instant_service_observer.h"
14 #include "chrome/browser/search/instant_unittest_base.h"
15 #include "chrome/browser/search/search.h"
16 #include "chrome/browser/ui/browser.h"
17 #include "chrome/browser/ui/browser_window.h"
18 #include "chrome/browser/ui/host_desktop.h"
19 #include "chrome/browser/ui/search/instant_search_prerenderer.h"
20 #include "chrome/common/render_messages.h"
21 #include "components/variations/entropy_provider.h"
22 #include "content/public/browser/notification_service.h"
23 #include "content/public/browser/notification_types.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"
28 #include "url/gurl.h"
30 class MockInstantServiceObserver : public InstantServiceObserver {
31 public:
32 MOCK_METHOD1(DefaultSearchProviderChanged, void(bool));
33 MOCK_METHOD1(OmniboxStartMarginChanged, void(int));
36 class InstantServiceTest : public InstantUnitTestBase {
37 protected:
38 void SetUp() override {
39 InstantUnitTestBase::SetUp();
41 instant_service_observer_.reset(new MockInstantServiceObserver());
42 instant_service_->AddObserver(instant_service_observer_.get());
45 void TearDown() override {
46 instant_service_->RemoveObserver(instant_service_observer_.get());
47 InstantUnitTestBase::TearDown();
50 InstantSearchPrerenderer* GetInstantSearchPrerenderer() {
51 return instant_service_->instant_search_prerenderer();
54 void UpdateOmniboxStartMargin(int start_margin) {
55 instant_service_->OnOmniboxStartMarginChanged(start_margin);
58 scoped_ptr<MockInstantServiceObserver> instant_service_observer_;
61 class InstantServiceEnabledTest : public InstantServiceTest {
62 protected:
63 void SetUp() override {
64 ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial(
65 "EmbeddedSearch", "Group1 use_cacheable_ntp:1"));
66 InstantServiceTest::SetUp();
70 TEST_F(InstantServiceEnabledTest, DispatchDefaultSearchProviderChanged) {
71 EXPECT_CALL(*instant_service_observer_.get(),
72 DefaultSearchProviderChanged(false)).Times(1);
74 const std::string new_base_url = "https://bar.com/";
75 SetUserSelectedDefaultSearchProvider(new_base_url);
78 TEST_F(InstantServiceTest, DontDispatchGoogleURLUpdatedForNonGoogleURLs) {
79 EXPECT_CALL(*instant_service_observer_.get(),
80 DefaultSearchProviderChanged(false)).Times(1);
81 const std::string new_dsp_url = "https://bar.com/";
82 SetUserSelectedDefaultSearchProvider(new_dsp_url);
83 testing::Mock::VerifyAndClearExpectations(instant_service_observer_.get());
85 EXPECT_CALL(*instant_service_observer_.get(),
86 DefaultSearchProviderChanged(false)).Times(0);
87 const std::string new_base_url = "https://www.google.es/";
88 NotifyGoogleBaseURLUpdate(new_base_url);
91 TEST_F(InstantServiceTest, DispatchGoogleURLUpdated) {
92 EXPECT_CALL(*instant_service_observer_.get(),
93 DefaultSearchProviderChanged(true)).Times(1);
95 const std::string new_base_url = "https://www.google.es/";
96 NotifyGoogleBaseURLUpdate(new_base_url);
99 TEST_F(InstantServiceEnabledTest, SendsSearchURLsToRenderer) {
100 scoped_ptr<content::MockRenderProcessHost> rph(
101 new content::MockRenderProcessHost(profile()));
102 rph->sink().ClearMessages();
103 instant_service_->Observe(
104 content::NOTIFICATION_RENDERER_PROCESS_CREATED,
105 content::Source<content::MockRenderProcessHost>(rph.get()),
106 content::NotificationService::NoDetails());
107 EXPECT_EQ(1U, rph->sink().message_count());
108 const IPC::Message* msg = rph->sink().GetMessageAt(0);
109 ASSERT_TRUE(msg);
110 ChromeViewMsg_SetSearchURLs::Param params;
111 ChromeViewMsg_SetSearchURLs::Read(msg, &params);
112 std::vector<GURL> search_urls = base::get<0>(params);
113 GURL new_tab_page_url = base::get<1>(params);
114 EXPECT_EQ(2U, search_urls.size());
115 EXPECT_EQ("https://www.google.com/alt#quux=", search_urls[0].spec());
116 EXPECT_EQ("https://www.google.com/url?bar=", search_urls[1].spec());
117 EXPECT_EQ("https://www.google.com/newtab", new_tab_page_url.spec());
120 TEST_F(InstantServiceTest, InstantSearchEnabled) {
121 EXPECT_NE(static_cast<InstantSearchPrerenderer*>(NULL),
122 GetInstantSearchPrerenderer());
125 TEST_F(InstantServiceEnabledTest,
126 ResetInstantSearchPrerenderer_DefaultProviderChanged) {
127 EXPECT_CALL(*instant_service_observer_.get(),
128 DefaultSearchProviderChanged(false)).Times(2);
130 // Set a default search provider that doesn't support Instant.
131 TemplateURLData data;
132 data.SetShortName(base::ASCIIToUTF16("foobar.com"));
133 data.SetURL("https://foobar.com/url?bar={searchTerms}");
134 TemplateURL* template_url = new TemplateURL(data);
135 // Takes ownership of |template_url|.
136 template_url_service_->Add(template_url);
137 template_url_service_->SetUserSelectedDefaultSearchProvider(template_url);
139 EXPECT_EQ(static_cast<InstantSearchPrerenderer*>(NULL),
140 GetInstantSearchPrerenderer());
142 // Set a default search provider that supports Instant and make sure
143 // InstantSearchPrerenderer is valid.
144 SetUserSelectedDefaultSearchProvider("https://google.com/");
145 EXPECT_NE(static_cast<InstantSearchPrerenderer*>(NULL),
146 GetInstantSearchPrerenderer());
149 TEST_F(InstantServiceEnabledTest,
150 ResetInstantSearchPrerenderer_GoogleBaseURLUpdated) {
151 EXPECT_CALL(*instant_service_observer_.get(),
152 DefaultSearchProviderChanged(true)).Times(1);
154 InstantSearchPrerenderer* old_prerenderer = GetInstantSearchPrerenderer();
155 EXPECT_TRUE(old_prerenderer != NULL);
157 const std::string new_base_url = "https://www.google.es/";
158 NotifyGoogleBaseURLUpdate(new_base_url);
159 EXPECT_NE(old_prerenderer, GetInstantSearchPrerenderer());
162 TEST_F(InstantServiceTest, OmniboxStartMarginChanged) {
163 int new_start_margin = 92;
164 EXPECT_CALL(*instant_service_observer_.get(),
165 OmniboxStartMarginChanged(new_start_margin)).Times(1);
166 UpdateOmniboxStartMargin(new_start_margin);