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" |
23 #include "content/public/test/mock_render_process_host.h" | 25 #include "content/public/test/mock_render_process_host.h" |
24 #include "ipc/ipc_message.h" | 26 #include "ipc/ipc_message.h" |
25 #include "ipc/ipc_test_sink.h" | 27 #include "ipc/ipc_test_sink.h" |
26 #include "testing/gmock/include/gmock/gmock.h" | 28 #include "testing/gmock/include/gmock/gmock.h" |
27 #include "url/gurl.h" | 29 #include "url/gurl.h" |
28 | 30 |
29 class MockInstantServiceObserver : public InstantServiceObserver { | 31 class MockInstantServiceObserver : public InstantServiceObserver { |
30 public: | 32 public: |
31 MOCK_METHOD0(DefaultSearchProviderChanged, void()); | 33 MOCK_METHOD0(DefaultSearchProviderChanged, void()); |
32 MOCK_METHOD0(GoogleURLUpdated, void()); | 34 MOCK_METHOD0(GoogleURLUpdated, void()); |
33 }; | 35 }; |
34 | 36 |
| 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 |
35 class InstantServiceTest : public InstantUnitTestBase { | 53 class InstantServiceTest : public InstantUnitTestBase { |
36 protected: | 54 protected: |
37 virtual void SetUp() OVERRIDE { | 55 virtual void SetUp() OVERRIDE { |
38 InstantUnitTestBase::SetUpWithoutCacheableNTP(); | 56 InstantUnitTestBase::SetUpWithoutCacheableNTP(); |
39 | 57 |
40 instant_service_observer_.reset(new MockInstantServiceObserver()); | 58 instant_service_observer_.reset(new MockInstantServiceObserver()); |
41 instant_service_->AddObserver(instant_service_observer_.get()); | 59 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()); |
42 } | 64 } |
43 | 65 |
44 virtual void TearDown() OVERRIDE { | 66 virtual void TearDown() OVERRIDE { |
45 instant_service_->RemoveObserver(instant_service_observer_.get()); | 67 instant_service_->RemoveObserver(instant_service_observer_.get()); |
| 68 instant_ntp_contents_observer_->Observe_(NULL); |
46 InstantUnitTestBase::TearDown(); | 69 InstantUnitTestBase::TearDown(); |
47 } | 70 } |
48 | 71 |
49 InstantSearchPrerenderer* GetInstantSearchPrerenderer() { | 72 InstantSearchPrerenderer* GetInstantSearchPrerenderer() { |
50 return instant_service_->instant_search_prerenderer(); | 73 return instant_service_->instant_search_prerenderer(); |
51 } | 74 } |
52 | 75 |
53 scoped_ptr<MockInstantServiceObserver> instant_service_observer_; | 76 scoped_ptr<MockInstantServiceObserver> instant_service_observer_; |
| 77 scoped_ptr<MockWebContentsObserver> instant_ntp_contents_observer_; |
54 }; | 78 }; |
55 | 79 |
56 TEST_F(InstantServiceTest, DispatchDefaultSearchProviderChanged) { | 80 TEST_F(InstantServiceTest, DispatchDefaultSearchProviderChanged) { |
57 EXPECT_CALL(*instant_service_observer_.get(), DefaultSearchProviderChanged()) | 81 EXPECT_CALL(*instant_service_observer_.get(), DefaultSearchProviderChanged()) |
58 .Times(1); | 82 .Times(1); |
| 83 EXPECT_CALL(*instant_ntp_contents_observer_.get(), |
| 84 WebContentsDestroyed(instant_service_->GetNTPContents())) |
| 85 .Times(1); |
59 | 86 |
| 87 GURL ntp_url = instant_service_->GetNTPContents()->GetURL(); |
60 const std::string& new_base_url = "https://bar.com/"; | 88 const std::string& new_base_url = "https://bar.com/"; |
61 SetDefaultSearchProvider(new_base_url); | 89 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)); |
62 } | 93 } |
63 | 94 |
64 TEST_F(InstantServiceTest, DispatchGoogleURLUpdated) { | 95 TEST_F(InstantServiceTest, DispatchGoogleURLUpdated) { |
65 EXPECT_CALL(*instant_service_observer_.get(), GoogleURLUpdated()).Times(1); | 96 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); |
66 | 100 |
| 101 GURL ntp_url = instant_service_->GetNTPContents()->GetURL(); |
67 const std::string& new_base_url = "https://www.google.es/"; | 102 const std::string& new_base_url = "https://www.google.es/"; |
68 NotifyGoogleBaseURLUpdate(new_base_url); | 103 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)); |
69 } | 107 } |
70 | 108 |
71 TEST_F(InstantServiceTest, SendsSearchURLsToRenderer) { | 109 TEST_F(InstantServiceTest, SendsSearchURLsToRenderer) { |
72 ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial("EmbeddedSearch", | 110 ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial("EmbeddedSearch", |
73 "Group1 use_cacheable_ntp:1")); | 111 "Group1 use_cacheable_ntp:1")); |
74 scoped_ptr<content::MockRenderProcessHost> rph( | 112 scoped_ptr<content::MockRenderProcessHost> rph( |
75 new content::MockRenderProcessHost(profile())); | 113 new content::MockRenderProcessHost(profile())); |
76 rph->sink().ClearMessages(); | 114 rph->sink().ClearMessages(); |
77 instant_service_->Observe( | 115 instant_service_->Observe( |
78 content::NOTIFICATION_RENDERER_PROCESS_CREATED, | 116 content::NOTIFICATION_RENDERER_PROCESS_CREATED, |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
130 EXPECT_CALL(*instant_service_observer_.get(), GoogleURLUpdated()).Times(1); | 168 EXPECT_CALL(*instant_service_observer_.get(), GoogleURLUpdated()).Times(1); |
131 | 169 |
132 SetDefaultSearchProvider("https://google.com/"); | 170 SetDefaultSearchProvider("https://google.com/"); |
133 InstantSearchPrerenderer* old_prerenderer = GetInstantSearchPrerenderer(); | 171 InstantSearchPrerenderer* old_prerenderer = GetInstantSearchPrerenderer(); |
134 EXPECT_NE(static_cast<InstantSearchPrerenderer*>(NULL), old_prerenderer); | 172 EXPECT_NE(static_cast<InstantSearchPrerenderer*>(NULL), old_prerenderer); |
135 | 173 |
136 const std::string& new_base_url = "https://www.google.es/"; | 174 const std::string& new_base_url = "https://www.google.es/"; |
137 NotifyGoogleBaseURLUpdate(new_base_url); | 175 NotifyGoogleBaseURLUpdate(new_base_url); |
138 EXPECT_NE(old_prerenderer, GetInstantSearchPrerenderer()); | 176 EXPECT_NE(old_prerenderer, GetInstantSearchPrerenderer()); |
139 } | 177 } |
OLD | NEW |