Chromium Code Reviews| Index: chrome/browser/search/instant_service_unittest.cc |
| diff --git a/chrome/browser/search/instant_service_unittest.cc b/chrome/browser/search/instant_service_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..20ddcac02686e8d66d25f9a0267ae7e38db9b5c8 |
| --- /dev/null |
| +++ b/chrome/browser/search/instant_service_unittest.cc |
| @@ -0,0 +1,119 @@ |
| +// Copyright 2013 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include <string> |
| +#include <vector> |
| + |
| +#include "base/memory/scoped_ptr.h" |
| +#include "base/strings/string_util.h" |
| +#include "chrome/browser/search/instant_service.h" |
| +#include "chrome/browser/search/instant_service_observer.h" |
| +#include "chrome/browser/search/instant_unittest_base.h" |
| +#include "chrome/browser/search/search.h" |
| +#include "chrome/browser/ui/browser.h" |
| +#include "chrome/browser/ui/browser_window.h" |
| +#include "chrome/browser/ui/host_desktop.h" |
| +#include "content/public/browser/web_contents.h" |
| +#include "content/public/browser/web_contents_observer.h" |
| +#include "testing/gmock/include/gmock/gmock.h" |
| + |
| +namespace chrome { |
| + |
| +namespace { |
| + |
| +class MockInstantServiceObserver : public InstantServiceObserver { |
| + public: |
| + MOCK_METHOD0(DefaultSearchProviderChanged, void()); |
| + MOCK_METHOD0(GoogleURLUpdated, void()); |
| +}; |
| + |
| +class MockWebContentsObserver : public content::WebContentsObserver { |
| + public: |
| + MOCK_METHOD1(WebContentsDestroyed, void(content::WebContents*)); |
| + |
| + protected: |
| + friend class InstantServiceTest; |
| +}; |
| + |
| +class InstantServiceTest : public InstantUnitTestBase { |
| + protected: |
| + virtual void SetUp() OVERRIDE { |
| + InstantUnitTestBase::SetUp(); |
| + |
| + instant_service_observer_.reset(new MockInstantServiceObserver()); |
| + instant_service_->AddObserver(instant_service_observer()); |
| + |
| + instant_ntp_contents_observer_.reset(new MockWebContentsObserver()); |
| + instant_ntp_contents_observer()->Observe( |
| + instant_service_->GetNTPContents()); |
| + } |
| + |
| + virtual void TearDown() OVERRIDE { |
| + instant_service_->RemoveObserver(instant_service_observer()); |
| + 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.
|
| + InstantUnitTestBase::TearDown(); |
| + } |
| + |
| + 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.
|
| + return instant_service_observer_.get(); |
| + } |
| + |
| + MockWebContentsObserver* instant_ntp_contents_observer() { |
| + return instant_ntp_contents_observer_.get(); |
| + } |
| + |
| + scoped_ptr<MockInstantServiceObserver> instant_service_observer_; |
| + scoped_ptr<MockWebContentsObserver> instant_ntp_contents_observer_; |
| +}; |
| + |
| +TEST_F(InstantServiceTest, DispatchDefaultSearchProviderChanged) { |
| + EXPECT_CALL(*instant_service_observer(), DefaultSearchProviderChanged()) |
| + .Times(1); |
| + EXPECT_CALL(*instant_ntp_contents_observer(), |
| + WebContentsDestroyed(instant_service_->GetNTPContents())) |
| + .Times(1); |
| + |
| + GURL ntp_url = instant_service_->GetNTPContents()->GetURL(); |
| + const std::string& new_base_url = "https://bar.com/"; |
| + SetSearchProvider(new_base_url); |
| + GURL new_ntp_url = instant_service_->GetNTPContents()->GetURL(); |
| + EXPECT_NE(ntp_url, new_ntp_url); |
| + EXPECT_TRUE(StartsWithASCII(new_ntp_url.spec(), new_base_url, true)); |
| +} |
| + |
| +TEST_F(InstantServiceTest, DispatchGoogleURLUpdated) { |
| + EXPECT_CALL(*instant_service_observer(), GoogleURLUpdated()).Times(1); |
| + EXPECT_CALL(*instant_ntp_contents_observer(), |
| + WebContentsDestroyed(instant_service_->GetNTPContents())) |
| + .Times(1); |
| + |
| + GURL ntp_url = instant_service_->GetNTPContents()->GetURL(); |
| + const std::string& new_base_url = "https://www.google.es/"; |
| + NotifyGoogleBaseURLUpdate(new_base_url); |
| + GURL new_ntp_url = instant_service_->GetNTPContents()->GetURL(); |
| + EXPECT_NE(ntp_url, new_ntp_url); |
| + EXPECT_TRUE(StartsWithASCII(new_ntp_url.spec(), new_base_url, true)); |
| +} |
| + |
| +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.
|
| + |
| + size_t initial_size = NumObservers(instant_service_); |
| + scoped_ptr<BrowserWindow> window(CreateBrowserWindow()); |
| + Browser::CreateParams params(profile(), chrome::HOST_DESKTOP_TYPE_NATIVE); |
| + params.window = window.get(); |
| + scoped_ptr<Browser> browser(new Browser(params)); |
| + size_t num_observers = NumObservers(instant_service_); |
| + EXPECT_EQ(initial_size + 2, num_observers) |
| + << "New BrowserInstantController should register as InstantServiceObserver"; |
| + |
| + browser.reset(NULL); |
| + window.reset(NULL); |
| + num_observers = NumObservers(instant_service_); |
| + EXPECT_EQ(initial_size + 1, num_observers) |
| + << "BrowserInstantController should unregister as InstantServiceObserver"; |
| +} |
| + |
| +} // namespace |
| + |
| +} // namespace chrome |