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/metrics/field_trial.h" |
10 #include "base/strings/string_util.h" | 10 #include "base/strings/string_util.h" |
(...skipping 11 matching lines...) Expand all Loading... |
22 #include "content/public/browser/notification_types.h" | 22 #include "content/public/browser/notification_types.h" |
23 #include "content/public/test/mock_render_process_host.h" | 23 #include "content/public/test/mock_render_process_host.h" |
24 #include "ipc/ipc_message.h" | 24 #include "ipc/ipc_message.h" |
25 #include "ipc/ipc_test_sink.h" | 25 #include "ipc/ipc_test_sink.h" |
26 #include "testing/gmock/include/gmock/gmock.h" | 26 #include "testing/gmock/include/gmock/gmock.h" |
27 #include "url/gurl.h" | 27 #include "url/gurl.h" |
28 | 28 |
29 class MockInstantServiceObserver : public InstantServiceObserver { | 29 class MockInstantServiceObserver : public InstantServiceObserver { |
30 public: | 30 public: |
31 MOCK_METHOD0(DefaultSearchProviderChanged, void()); | 31 MOCK_METHOD0(DefaultSearchProviderChanged, void()); |
32 MOCK_METHOD0(GoogleURLUpdated, void()); | |
33 MOCK_METHOD1(OmniboxStartMarginChanged, void(int)); | 32 MOCK_METHOD1(OmniboxStartMarginChanged, void(int)); |
34 }; | 33 }; |
35 | 34 |
36 class InstantServiceTest : public InstantUnitTestBase { | 35 class InstantServiceTest : public InstantUnitTestBase { |
37 protected: | 36 protected: |
38 virtual void SetUp() OVERRIDE { | 37 virtual void SetUp() OVERRIDE { |
39 InstantUnitTestBase::SetUp(); | 38 InstantUnitTestBase::SetUp(); |
40 | 39 |
41 instant_service_observer_.reset(new MockInstantServiceObserver()); | 40 instant_service_observer_.reset(new MockInstantServiceObserver()); |
42 instant_service_->AddObserver(instant_service_observer_.get()); | 41 instant_service_->AddObserver(instant_service_observer_.get()); |
43 } | 42 } |
44 | 43 |
45 virtual void TearDown() OVERRIDE { | 44 virtual void TearDown() OVERRIDE { |
46 instant_service_->RemoveObserver(instant_service_observer_.get()); | 45 instant_service_->RemoveObserver(instant_service_observer_.get()); |
47 InstantUnitTestBase::TearDown(); | 46 InstantUnitTestBase::TearDown(); |
48 } | 47 } |
49 | 48 |
50 InstantSearchPrerenderer* GetInstantSearchPrerenderer() { | 49 InstantSearchPrerenderer* GetInstantSearchPrerenderer() { |
51 return instant_service_->instant_search_prerenderer(); | 50 return instant_service_->instant_search_prerenderer(); |
52 } | 51 } |
53 | 52 |
54 void UpdateOmniboxStartMargin(int start_margin) { | 53 void UpdateOmniboxStartMargin(int start_margin) { |
55 instant_service_->OnOmniboxStartMarginChanged(start_margin); | 54 instant_service_->OnOmniboxStartMarginChanged(start_margin); |
56 } | 55 } |
57 | 56 |
58 scoped_ptr<MockInstantServiceObserver> instant_service_observer_; | 57 scoped_ptr<MockInstantServiceObserver> instant_service_observer_; |
59 }; | 58 }; |
60 | 59 |
61 TEST_F(InstantServiceTest, DispatchDefaultSearchProviderChanged) { | 60 class InstantServiceEnabledTest : public InstantServiceTest { |
| 61 protected: |
| 62 virtual void SetUp() OVERRIDE { |
| 63 ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial( |
| 64 "EmbeddedSearch", "Group1 use_cacheable_ntp:1 prefetch_results:1")); |
| 65 InstantServiceTest::SetUp(); |
| 66 } |
| 67 }; |
| 68 |
| 69 TEST_F(InstantServiceEnabledTest, DispatchDefaultSearchProviderChanged) { |
62 EXPECT_CALL(*instant_service_observer_.get(), DefaultSearchProviderChanged()) | 70 EXPECT_CALL(*instant_service_observer_.get(), DefaultSearchProviderChanged()) |
63 .Times(1); | 71 .Times(1); |
64 | 72 |
65 const std::string& new_base_url = "https://bar.com/"; | 73 const std::string& new_base_url = "https://bar.com/"; |
66 SetUserSelectedDefaultSearchProvider(new_base_url); | 74 SetUserSelectedDefaultSearchProvider(new_base_url); |
67 } | 75 } |
68 | 76 |
69 TEST_F(InstantServiceTest, DispatchGoogleURLUpdated) { | 77 TEST_F(InstantServiceTest, DispatchGoogleURLUpdated) { |
70 EXPECT_CALL(*instant_service_observer_.get(), GoogleURLUpdated()).Times(1); | 78 // In this test, the default search engine does not use instant, so changing |
| 79 // the Google base URL should not result in the instant service observer being |
| 80 // notified. |
| 81 EXPECT_CALL(*instant_service_observer_.get(), DefaultSearchProviderChanged()) |
| 82 .Times(0); |
71 | 83 |
72 const std::string& new_base_url = "https://www.google.es/"; | 84 const std::string& new_base_url = "https://www.google.es/"; |
73 NotifyGoogleBaseURLUpdate(new_base_url); | 85 NotifyGoogleBaseURLUpdate(new_base_url); |
74 } | 86 } |
75 | 87 |
76 TEST_F(InstantServiceTest, SendsSearchURLsToRenderer) { | 88 TEST_F(InstantServiceEnabledTest, DispatchGoogleURLUpdated) { |
77 ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial("EmbeddedSearch", | 89 // Like the above test, but this time we've enabled instant, so changing the |
78 "Group1 use_cacheable_ntp:1")); | 90 // Google base URL should result in the instant service observer being |
| 91 // notified. |
| 92 EXPECT_CALL(*instant_service_observer_.get(), DefaultSearchProviderChanged()) |
| 93 .Times(1); |
| 94 |
| 95 const std::string& new_base_url = "https://www.google.es/"; |
| 96 NotifyGoogleBaseURLUpdate(new_base_url); |
| 97 } |
| 98 |
| 99 TEST_F(InstantServiceEnabledTest, SendsSearchURLsToRenderer) { |
79 scoped_ptr<content::MockRenderProcessHost> rph( | 100 scoped_ptr<content::MockRenderProcessHost> rph( |
80 new content::MockRenderProcessHost(profile())); | 101 new content::MockRenderProcessHost(profile())); |
81 rph->sink().ClearMessages(); | 102 rph->sink().ClearMessages(); |
82 instant_service_->Observe( | 103 instant_service_->Observe( |
83 content::NOTIFICATION_RENDERER_PROCESS_CREATED, | 104 content::NOTIFICATION_RENDERER_PROCESS_CREATED, |
84 content::Source<content::MockRenderProcessHost>(rph.get()), | 105 content::Source<content::MockRenderProcessHost>(rph.get()), |
85 content::NotificationService::NoDetails()); | 106 content::NotificationService::NoDetails()); |
86 EXPECT_EQ(1U, rph->sink().message_count()); | 107 EXPECT_EQ(1U, rph->sink().message_count()); |
87 const IPC::Message* msg = rph->sink().GetMessageAt(0); | 108 const IPC::Message* msg = rph->sink().GetMessageAt(0); |
88 ASSERT_TRUE(msg); | 109 ASSERT_TRUE(msg); |
89 std::vector<GURL> search_urls; | 110 std::vector<GURL> search_urls; |
90 GURL new_tab_page_url; | 111 GURL new_tab_page_url; |
91 ChromeViewMsg_SetSearchURLs::Read(msg, &search_urls, &new_tab_page_url); | 112 ChromeViewMsg_SetSearchURLs::Read(msg, &search_urls, &new_tab_page_url); |
92 EXPECT_EQ(2U, search_urls.size()); | 113 EXPECT_EQ(2U, search_urls.size()); |
93 EXPECT_EQ("https://www.google.com/alt#quux=", search_urls[0].spec()); | 114 EXPECT_EQ("https://www.google.com/alt#quux=", search_urls[0].spec()); |
94 EXPECT_EQ("https://www.google.com/url?bar=", search_urls[1].spec()); | 115 EXPECT_EQ("https://www.google.com/url?bar=", search_urls[1].spec()); |
95 EXPECT_EQ("https://www.google.com/newtab", new_tab_page_url.spec()); | 116 EXPECT_EQ("https://www.google.com/newtab", new_tab_page_url.spec()); |
96 } | 117 } |
97 | 118 |
98 TEST_F(InstantServiceTest, InstantSearchDisabled) { | 119 TEST_F(InstantServiceTest, InstantSearchDisabled) { |
99 // 'prefetch_results' flag is not enabled in field trials. Make sure | 120 // 'prefetch_results' flag is not enabled in field trials. Make sure |
100 // InstantSearchPrerenderer is not initialized. | 121 // InstantSearchPrerenderer is not initialized. |
101 EXPECT_EQ(static_cast<InstantSearchPrerenderer*>(NULL), | 122 EXPECT_EQ(static_cast<InstantSearchPrerenderer*>(NULL), |
102 GetInstantSearchPrerenderer()); | 123 GetInstantSearchPrerenderer()); |
103 } | 124 } |
104 | 125 |
105 TEST_F(InstantServiceTest, | 126 TEST_F(InstantServiceEnabledTest, |
106 ResetInstantSearchPrerenderer_DefaultProviderChanged) { | 127 ResetInstantSearchPrerenderer_DefaultProviderChanged) { |
107 ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial( | |
108 "EmbeddedSearch", "Group1 use_cacheable_ntp:1 prefetch_results:1")); | |
109 EXPECT_CALL(*instant_service_observer_.get(), DefaultSearchProviderChanged()) | 128 EXPECT_CALL(*instant_service_observer_.get(), DefaultSearchProviderChanged()) |
110 .Times(2); | 129 .Times(2); |
111 | 130 |
112 // Set a default search provider that doesn't support Instant. | 131 // Set a default search provider that doesn't support Instant. |
113 TemplateURLData data; | 132 TemplateURLData data; |
114 data.SetURL("https://foobar.com/url?bar={searchTerms}"); | 133 data.SetURL("https://foobar.com/url?bar={searchTerms}"); |
115 TemplateURL* template_url = new TemplateURL(profile(), data); | 134 TemplateURL* template_url = new TemplateURL(profile(), data); |
116 // Takes ownership of |template_url|. | 135 // Takes ownership of |template_url|. |
117 template_url_service_->Add(template_url); | 136 template_url_service_->Add(template_url); |
118 template_url_service_->SetUserSelectedDefaultSearchProvider(template_url); | 137 template_url_service_->SetUserSelectedDefaultSearchProvider(template_url); |
119 | 138 |
120 EXPECT_EQ(static_cast<InstantSearchPrerenderer*>(NULL), | 139 EXPECT_EQ(static_cast<InstantSearchPrerenderer*>(NULL), |
121 GetInstantSearchPrerenderer()); | 140 GetInstantSearchPrerenderer()); |
122 | 141 |
123 // Set a default search provider that supports Instant and make sure | 142 // Set a default search provider that supports Instant and make sure |
124 // InstantSearchPrerenderer is valid. | 143 // InstantSearchPrerenderer is valid. |
125 SetUserSelectedDefaultSearchProvider("https://google.com/"); | 144 SetUserSelectedDefaultSearchProvider("https://google.com/"); |
126 EXPECT_NE(static_cast<InstantSearchPrerenderer*>(NULL), | 145 EXPECT_NE(static_cast<InstantSearchPrerenderer*>(NULL), |
127 GetInstantSearchPrerenderer()); | 146 GetInstantSearchPrerenderer()); |
128 } | 147 } |
129 | 148 |
130 TEST_F(InstantServiceTest, ResetInstantSearchPrerenderer_GoogleBaseURLUpdated) { | 149 TEST_F(InstantServiceEnabledTest, |
131 ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial( | 150 ResetInstantSearchPrerenderer_GoogleBaseURLUpdated) { |
132 "EmbeddedSearch", "Group1 use_cacheable_ntp:1 prefetch_results:1")); | |
133 EXPECT_CALL(*instant_service_observer_.get(), DefaultSearchProviderChanged()) | 151 EXPECT_CALL(*instant_service_observer_.get(), DefaultSearchProviderChanged()) |
134 .Times(1); | 152 .Times(1); |
135 EXPECT_CALL(*instant_service_observer_.get(), GoogleURLUpdated()).Times(1); | |
136 | 153 |
137 SetUserSelectedDefaultSearchProvider("https://google.com/"); | |
138 InstantSearchPrerenderer* old_prerenderer = GetInstantSearchPrerenderer(); | 154 InstantSearchPrerenderer* old_prerenderer = GetInstantSearchPrerenderer(); |
139 EXPECT_NE(static_cast<InstantSearchPrerenderer*>(NULL), old_prerenderer); | 155 EXPECT_TRUE(old_prerenderer != NULL); |
140 | 156 |
141 const std::string& new_base_url = "https://www.google.es/"; | 157 const std::string& new_base_url = "https://www.google.es/"; |
142 NotifyGoogleBaseURLUpdate(new_base_url); | 158 NotifyGoogleBaseURLUpdate(new_base_url); |
143 EXPECT_NE(old_prerenderer, GetInstantSearchPrerenderer()); | 159 EXPECT_NE(old_prerenderer, GetInstantSearchPrerenderer()); |
144 } | 160 } |
145 | 161 |
146 TEST_F(InstantServiceTest, OmniboxStartMarginChanged) { | 162 TEST_F(InstantServiceTest, OmniboxStartMarginChanged) { |
147 int new_start_margin = 92; | 163 int new_start_margin = 92; |
148 EXPECT_CALL(*instant_service_observer_.get(), | 164 EXPECT_CALL(*instant_service_observer_.get(), |
149 OmniboxStartMarginChanged(new_start_margin)).Times(1); | 165 OmniboxStartMarginChanged(new_start_margin)).Times(1); |
150 UpdateOmniboxStartMargin(new_start_margin); | 166 UpdateOmniboxStartMargin(new_start_margin); |
151 } | 167 } |
OLD | NEW |