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" |
11 #include "chrome/browser/search/instant_service.h" | 11 #include "chrome/browser/search/instant_service.h" |
12 #include "chrome/browser/search/instant_service_observer.h" | 12 #include "chrome/browser/search/instant_service_observer.h" |
13 #include "chrome/browser/search/instant_unittest_base.h" | 13 #include "chrome/browser/search/instant_unittest_base.h" |
14 #include "chrome/browser/search/search.h" | 14 #include "chrome/browser/search/search.h" |
15 #include "chrome/browser/ui/browser.h" | 15 #include "chrome/browser/ui/browser.h" |
16 #include "chrome/browser/ui/browser_window.h" | 16 #include "chrome/browser/ui/browser_window.h" |
17 #include "chrome/browser/ui/host_desktop.h" | 17 #include "chrome/browser/ui/host_desktop.h" |
18 #include "chrome/browser/ui/search/instant_search_prerenderer.h" | 18 #include "chrome/browser/ui/search/instant_search_prerenderer.h" |
19 #include "chrome/common/render_messages.h" | 19 #include "chrome/common/render_messages.h" |
20 #include "components/variations/entropy_provider.h" | 20 #include "components/variations/entropy_provider.h" |
21 #include "content/public/browser/notification_service.h" | 21 #include "content/public/browser/notification_service.h" |
22 #include "content/public/browser/notification_types.h" | 22 #include "content/public/browser/notification_types.h" |
23 #include "content/public/browser/web_contents.h" | |
24 #include "content/public/browser/web_contents_observer.h" | |
25 #include "content/public/test/mock_render_process_host.h" | 23 #include "content/public/test/mock_render_process_host.h" |
26 #include "ipc/ipc_message.h" | 24 #include "ipc/ipc_message.h" |
27 #include "ipc/ipc_test_sink.h" | 25 #include "ipc/ipc_test_sink.h" |
28 #include "testing/gmock/include/gmock/gmock.h" | 26 #include "testing/gmock/include/gmock/gmock.h" |
29 #include "url/gurl.h" | 27 #include "url/gurl.h" |
30 | 28 |
31 class MockInstantServiceObserver : public InstantServiceObserver { | 29 class MockInstantServiceObserver : public InstantServiceObserver { |
32 public: | 30 public: |
33 MOCK_METHOD0(DefaultSearchProviderChanged, void()); | 31 MOCK_METHOD0(DefaultSearchProviderChanged, void()); |
34 MOCK_METHOD0(GoogleURLUpdated, void()); | 32 MOCK_METHOD0(GoogleURLUpdated, void()); |
35 }; | 33 }; |
36 | 34 |
37 class MockWebContentsObserver : public content::WebContentsObserver { | |
38 public: | |
39 MOCK_METHOD1(WebContentsDestroyed, void(content::WebContents*)); | |
40 | |
41 // Dumb override to make MSVC happy. | |
42 void Observe_(content::WebContents* contents) { | |
43 content::WebContentsObserver::Observe(contents); | |
44 } | |
45 | |
46 protected: | |
47 friend class InstantServiceTest; | |
48 FRIEND_TEST_ALL_PREFIXES(InstantServiceTest, | |
49 DispatchDefaultSearchProviderChanged); | |
50 FRIEND_TEST_ALL_PREFIXES(InstantServiceTest, DispatchGoogleURLUpdated); | |
51 }; | |
52 | |
53 class InstantServiceTest : public InstantUnitTestBase { | 35 class InstantServiceTest : public InstantUnitTestBase { |
54 protected: | 36 protected: |
55 virtual void SetUp() OVERRIDE { | 37 virtual void SetUp() OVERRIDE { |
56 InstantUnitTestBase::SetUpWithoutCacheableNTP(); | 38 InstantUnitTestBase::SetUpWithoutCacheableNTP(); |
57 | 39 |
58 instant_service_observer_.reset(new MockInstantServiceObserver()); | 40 instant_service_observer_.reset(new MockInstantServiceObserver()); |
59 instant_service_->AddObserver(instant_service_observer_.get()); | 41 instant_service_->AddObserver(instant_service_observer_.get()); |
60 | |
61 instant_ntp_contents_observer_.reset(new MockWebContentsObserver()); | |
62 instant_ntp_contents_observer_->Observe_( | |
63 instant_service_->GetNTPContents()); | |
64 } | 42 } |
65 | 43 |
66 virtual void TearDown() OVERRIDE { | 44 virtual void TearDown() OVERRIDE { |
67 instant_service_->RemoveObserver(instant_service_observer_.get()); | 45 instant_service_->RemoveObserver(instant_service_observer_.get()); |
68 instant_ntp_contents_observer_->Observe_(NULL); | |
69 InstantUnitTestBase::TearDown(); | 46 InstantUnitTestBase::TearDown(); |
70 } | 47 } |
71 | 48 |
72 InstantSearchPrerenderer* GetInstantSearchPrerenderer() { | 49 InstantSearchPrerenderer* GetInstantSearchPrerenderer() { |
73 return instant_service_->instant_search_prerenderer(); | 50 return instant_service_->instant_search_prerenderer(); |
74 } | 51 } |
75 | 52 |
76 scoped_ptr<MockInstantServiceObserver> instant_service_observer_; | 53 scoped_ptr<MockInstantServiceObserver> instant_service_observer_; |
77 scoped_ptr<MockWebContentsObserver> instant_ntp_contents_observer_; | |
78 }; | 54 }; |
79 | 55 |
80 TEST_F(InstantServiceTest, DispatchDefaultSearchProviderChanged) { | 56 TEST_F(InstantServiceTest, DispatchDefaultSearchProviderChanged) { |
81 EXPECT_CALL(*instant_service_observer_.get(), DefaultSearchProviderChanged()) | 57 EXPECT_CALL(*instant_service_observer_.get(), DefaultSearchProviderChanged()) |
82 .Times(1); | 58 .Times(1); |
83 EXPECT_CALL(*instant_ntp_contents_observer_.get(), | |
84 WebContentsDestroyed(instant_service_->GetNTPContents())) | |
85 .Times(1); | |
86 | 59 |
87 GURL ntp_url = instant_service_->GetNTPContents()->GetURL(); | |
88 const std::string& new_base_url = "https://bar.com/"; | 60 const std::string& new_base_url = "https://bar.com/"; |
89 SetDefaultSearchProvider(new_base_url); | 61 SetDefaultSearchProvider(new_base_url); |
90 GURL new_ntp_url = instant_service_->GetNTPContents()->GetURL(); | |
91 EXPECT_NE(ntp_url, new_ntp_url); | |
92 EXPECT_TRUE(StartsWithASCII(new_ntp_url.spec(), new_base_url, true)); | |
93 } | 62 } |
94 | 63 |
95 TEST_F(InstantServiceTest, DispatchGoogleURLUpdated) { | 64 TEST_F(InstantServiceTest, DispatchGoogleURLUpdated) { |
96 EXPECT_CALL(*instant_service_observer_.get(), GoogleURLUpdated()).Times(1); | 65 EXPECT_CALL(*instant_service_observer_.get(), GoogleURLUpdated()).Times(1); |
97 EXPECT_CALL(*instant_ntp_contents_observer_.get(), | |
98 WebContentsDestroyed(instant_service_->GetNTPContents())) | |
99 .Times(1); | |
100 | 66 |
101 GURL ntp_url = instant_service_->GetNTPContents()->GetURL(); | |
102 const std::string& new_base_url = "https://www.google.es/"; | 67 const std::string& new_base_url = "https://www.google.es/"; |
103 NotifyGoogleBaseURLUpdate(new_base_url); | 68 NotifyGoogleBaseURLUpdate(new_base_url); |
104 GURL new_ntp_url = instant_service_->GetNTPContents()->GetURL(); | |
105 EXPECT_NE(ntp_url, new_ntp_url); | |
106 EXPECT_TRUE(StartsWithASCII(new_ntp_url.spec(), new_base_url, true)); | |
107 } | 69 } |
108 | 70 |
109 TEST_F(InstantServiceTest, SendsSearchURLsToRenderer) { | 71 TEST_F(InstantServiceTest, SendsSearchURLsToRenderer) { |
110 ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial("EmbeddedSearch", | 72 ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial("EmbeddedSearch", |
111 "Group1 use_cacheable_ntp:1")); | 73 "Group1 use_cacheable_ntp:1")); |
112 scoped_ptr<content::MockRenderProcessHost> rph( | 74 scoped_ptr<content::MockRenderProcessHost> rph( |
113 new content::MockRenderProcessHost(profile())); | 75 new content::MockRenderProcessHost(profile())); |
114 rph->sink().ClearMessages(); | 76 rph->sink().ClearMessages(); |
115 instant_service_->Observe( | 77 instant_service_->Observe( |
116 content::NOTIFICATION_RENDERER_PROCESS_CREATED, | 78 content::NOTIFICATION_RENDERER_PROCESS_CREATED, |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
168 EXPECT_CALL(*instant_service_observer_.get(), GoogleURLUpdated()).Times(1); | 130 EXPECT_CALL(*instant_service_observer_.get(), GoogleURLUpdated()).Times(1); |
169 | 131 |
170 SetDefaultSearchProvider("https://google.com/"); | 132 SetDefaultSearchProvider("https://google.com/"); |
171 InstantSearchPrerenderer* old_prerenderer = GetInstantSearchPrerenderer(); | 133 InstantSearchPrerenderer* old_prerenderer = GetInstantSearchPrerenderer(); |
172 EXPECT_NE(static_cast<InstantSearchPrerenderer*>(NULL), old_prerenderer); | 134 EXPECT_NE(static_cast<InstantSearchPrerenderer*>(NULL), old_prerenderer); |
173 | 135 |
174 const std::string& new_base_url = "https://www.google.es/"; | 136 const std::string& new_base_url = "https://www.google.es/"; |
175 NotifyGoogleBaseURLUpdate(new_base_url); | 137 NotifyGoogleBaseURLUpdate(new_base_url); |
176 EXPECT_NE(old_prerenderer, GetInstantSearchPrerenderer()); | 138 EXPECT_NE(old_prerenderer, GetInstantSearchPrerenderer()); |
177 } | 139 } |
OLD | NEW |