OLD | NEW |
---|---|
(Empty) | |
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. | |
4 | |
5 #include <string> | |
6 #include <vector> | |
7 | |
8 #include "base/memory/scoped_ptr.h" | |
9 #include "base/strings/string_util.h" | |
10 #include "chrome/browser/search/instant_service.h" | |
11 #include "chrome/browser/search/instant_service_observer.h" | |
12 #include "chrome/browser/search/instant_unittest_base.h" | |
13 #include "chrome/browser/search/search.h" | |
14 #include "chrome/browser/ui/browser.h" | |
15 #include "chrome/browser/ui/browser_window.h" | |
16 #include "chrome/browser/ui/host_desktop.h" | |
17 #include "content/public/browser/web_contents.h" | |
18 #include "content/public/browser/web_contents_observer.h" | |
19 #include "testing/gmock/include/gmock/gmock.h" | |
20 | |
21 namespace chrome { | |
22 | |
23 namespace { | |
24 | |
25 class MockInstantServiceObserver : public InstantServiceObserver { | |
26 public: | |
27 MOCK_METHOD0(DefaultSearchProviderChanged, void()); | |
28 MOCK_METHOD0(GoogleURLUpdated, void()); | |
29 }; | |
30 | |
31 class MockWebContentsObserver : public content::WebContentsObserver { | |
32 public: | |
33 MOCK_METHOD1(WebContentsDestroyed, void(content::WebContents*)); | |
34 | |
35 protected: | |
36 friend class InstantServiceTest; | |
37 }; | |
38 | |
39 class InstantServiceTest : public InstantUnitTestBase { | |
40 protected: | |
41 virtual void SetUp() OVERRIDE { | |
42 InstantUnitTestBase::SetUp(); | |
43 | |
44 instant_service_observer_.reset(new MockInstantServiceObserver()); | |
45 instant_service_->AddObserver(instant_service_observer()); | |
46 | |
47 instant_ntp_contents_observer_.reset(new MockWebContentsObserver()); | |
48 instant_ntp_contents_observer()->Observe( | |
49 instant_service_->GetNTPContents()); | |
50 } | |
51 | |
52 virtual void TearDown() OVERRIDE { | |
53 instant_service_->RemoveObserver(instant_service_observer()); | |
54 instant_ntp_contents_observer()->Observe(NULL); | |
Jered
2013/08/16 00:13:23
instant_ntp_contents_observer_->
Anuj
2013/08/22 00:40:45
Done.
| |
55 InstantUnitTestBase::TearDown(); | |
56 } | |
57 | |
58 MockInstantServiceObserver* instant_service_observer() { | |
Jered
2013/08/16 00:13:23
nit: Instead of adding accessors to get raw pointe
Anuj
2013/08/22 00:40:45
Done.
| |
59 return instant_service_observer_.get(); | |
60 } | |
61 | |
62 MockWebContentsObserver* instant_ntp_contents_observer() { | |
63 return instant_ntp_contents_observer_.get(); | |
64 } | |
65 | |
66 scoped_ptr<MockInstantServiceObserver> instant_service_observer_; | |
67 scoped_ptr<MockWebContentsObserver> instant_ntp_contents_observer_; | |
68 }; | |
69 | |
70 TEST_F(InstantServiceTest, DispatchDefaultSearchProviderChanged) { | |
71 EXPECT_CALL(*instant_service_observer(), DefaultSearchProviderChanged()) | |
72 .Times(1); | |
73 EXPECT_CALL(*instant_ntp_contents_observer(), | |
74 WebContentsDestroyed(instant_service_->GetNTPContents())) | |
75 .Times(1); | |
76 | |
77 GURL ntp_url = instant_service_->GetNTPContents()->GetURL(); | |
78 const std::string& new_base_url = "https://bar.com/"; | |
79 SetSearchProvider(new_base_url); | |
80 GURL new_ntp_url = instant_service_->GetNTPContents()->GetURL(); | |
81 EXPECT_NE(ntp_url, new_ntp_url); | |
82 EXPECT_TRUE(StartsWithASCII(new_ntp_url.spec(), new_base_url, true)); | |
83 } | |
84 | |
85 TEST_F(InstantServiceTest, DispatchGoogleURLUpdated) { | |
86 EXPECT_CALL(*instant_service_observer(), GoogleURLUpdated()).Times(1); | |
87 EXPECT_CALL(*instant_ntp_contents_observer(), | |
88 WebContentsDestroyed(instant_service_->GetNTPContents())) | |
89 .Times(1); | |
90 | |
91 GURL ntp_url = instant_service_->GetNTPContents()->GetURL(); | |
92 const std::string& new_base_url = "https://www.google.es/"; | |
93 NotifyGoogleBaseURLUpdate(new_base_url); | |
94 GURL new_ntp_url = instant_service_->GetNTPContents()->GetURL(); | |
95 EXPECT_NE(ntp_url, new_ntp_url); | |
96 EXPECT_TRUE(StartsWithASCII(new_ntp_url.spec(), new_base_url, true)); | |
97 } | |
98 | |
99 TEST_F(InstantServiceTest, BrowserWindowLifecycle) { | |
Jered
2013/08/16 00:13:23
This test probably belongs in browser_instant_cont
Anuj
2013/08/22 00:40:45
Done.
| |
100 | |
101 size_t initial_size = NumObservers(instant_service_); | |
102 scoped_ptr<BrowserWindow> window(CreateBrowserWindow()); | |
103 Browser::CreateParams params(profile(), chrome::HOST_DESKTOP_TYPE_NATIVE); | |
104 params.window = window.get(); | |
105 scoped_ptr<Browser> browser(new Browser(params)); | |
106 size_t num_observers = NumObservers(instant_service_); | |
107 EXPECT_EQ(initial_size + 2, num_observers) | |
108 << "New BrowserInstantController should register as InstantServiceObserver"; | |
109 | |
110 browser.reset(NULL); | |
111 window.reset(NULL); | |
112 num_observers = NumObservers(instant_service_); | |
113 EXPECT_EQ(initial_size + 1, num_observers) | |
114 << "BrowserInstantController should unregister as InstantServiceObserver"; | |
115 } | |
116 | |
117 } // namespace | |
118 | |
119 } // namespace chrome | |
OLD | NEW |