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/logging.h" | |
9 #include "base/memory/scoped_ptr.h" | |
10 #include "base/metrics/histogram.h" | |
11 #include "base/metrics/histogram_samples.h" | |
12 #include "base/metrics/statistics_recorder.h" | |
13 #include "base/values.h" | |
14 #include "chrome/browser/chrome_notification_types.h" | |
15 #include "chrome/browser/google/google_url_tracker.h" | |
16 #include "chrome/browser/profiles/profile.h" | |
17 #include "chrome/browser/search/instant_service.h" | |
18 #include "chrome/browser/search/instant_service_factory.h" | |
19 #include "chrome/browser/search/instant_service_observer.h" | |
20 #include "chrome/browser/search/search.h" | |
21 #include "chrome/browser/search/search_test_utils.h" | |
22 #include "chrome/browser/search_engines/template_url_service.h" | |
23 #include "chrome/browser/search_engines/template_url_service_factory.h" | |
24 #include "chrome/common/instant_types.h" | |
25 #include "chrome/common/pref_names.h" | |
26 #include "chrome/test/base/browser_with_test_window_test.h" | |
27 #include "chrome/test/base/testing_pref_service_syncable.h" | |
28 #include "chrome/test/base/ui_test_utils.h" | |
29 #include "content/public/browser/notification_details.h" | |
30 #include "content/public/browser/notification_service.h" | |
31 #include "content/public/browser/notification_source.h" | |
32 #include "content/public/browser/web_contents.h" | |
33 #include "content/public/browser/web_contents_observer.h" | |
34 #include "testing/gmock/include/gmock/gmock.h" | |
35 | |
36 namespace chrome { | |
samarth
2013/08/02 21:51:15
Why is this in namespace chrome? InstantService is
Anuj
2013/08/09 07:22:00
Done.
| |
37 using base::HistogramBase; | |
samarth
2013/08/02 21:51:15
nit: add an empty line above
Anuj
2013/08/09 07:22:00
Done.
| |
38 using base::HistogramSamples; | |
39 using base::StatisticsRecorder; | |
40 using testing::_; | |
41 | |
42 class MockInstantServiceObserver : public InstantServiceObserver { | |
43 public: | |
44 MOCK_METHOD1(ThemeInfoChanged, void(const ThemeBackgroundInfo&)); | |
45 MOCK_METHOD1(MostVisitedItemsChanged, | |
46 void(const std::vector<InstantMostVisitedItem>&)); | |
47 MOCK_METHOD0(DefaultSearchProviderChanged, void()); | |
48 MOCK_METHOD0(GoogleURLUpdated, void()); | |
49 }; | |
50 | |
51 class MockWebContentsObserver : public content::WebContentsObserver { | |
52 public: | |
53 MOCK_METHOD1(WebContentsDestroyed, void(content::WebContents*)); | |
54 | |
55 protected: | |
56 friend class InstantServiceTest; | |
57 }; | |
58 | |
59 class InstantServiceTest : public BrowserWithTestWindowTest { | |
60 protected: | |
61 virtual void SetUp() OVERRIDE { | |
62 EnableInstantExtendedAPIForTesting(); | |
63 BrowserWithTestWindowTest::SetUp(); | |
64 TemplateURLServiceFactory::GetInstance()->SetTestingFactoryAndUse( | |
65 profile(), &TemplateURLServiceFactory::BuildInstanceFor); | |
66 TemplateURLService* template_url_service = | |
67 TemplateURLServiceFactory::GetForProfile(profile()); | |
68 ui_test_utils::WaitForTemplateURLServiceToLoad(template_url_service); | |
69 instant_service_ = InstantServiceFactory::GetForProfile(profile()); | |
70 instant_service_observer_.reset(new MockInstantServiceObserver()); | |
71 instant_ntp_contents_observer_.reset(new MockWebContentsObserver()); | |
72 instant_service_->AddObserver(instant_service_observer()); | |
73 instant_ntp_contents_observer()->Observe( | |
74 instant_service_->GetNTPContents()); | |
75 } | |
76 | |
77 MockInstantServiceObserver* instant_service_observer() { | |
78 return instant_service_observer_.get(); | |
79 } | |
80 | |
81 MockWebContentsObserver* instant_ntp_contents_observer() { | |
82 return instant_ntp_contents_observer_.get(); | |
83 } | |
84 | |
85 InstantService* instant_service_; | |
86 scoped_ptr<MockInstantServiceObserver> instant_service_observer_; | |
87 scoped_ptr<MockWebContentsObserver> instant_ntp_contents_observer_; | |
88 }; | |
89 | |
90 TEST_F(InstantServiceTest, DispatchDefaultSearchProviderChanged) { | |
91 EXPECT_CALL(*instant_service_observer(), DefaultSearchProviderChanged()) | |
92 .Times(1); | |
93 EXPECT_CALL(*instant_ntp_contents_observer(), | |
94 WebContentsDestroyed(instant_service_->GetNTPContents())) | |
95 .Times(1); | |
96 | |
97 search_test_utils::SetSearchProvider(profile()); | |
98 } | |
99 | |
100 TEST_F(InstantServiceTest, DispatchGoogleURLUpdated) { | |
101 TestingPrefServiceSyncable* service = profile()->GetTestingPrefService(); | |
102 service->SetUserPref(prefs::kLastPromptedGoogleURL, | |
103 new base::StringValue("https://www.google.com/")); | |
104 EXPECT_CALL(*instant_service_observer(), GoogleURLUpdated()).Times(1); | |
105 EXPECT_CALL(*instant_ntp_contents_observer(), | |
106 WebContentsDestroyed(instant_service_->GetNTPContents())) | |
107 .Times(1); | |
108 | |
109 GoogleURLTracker::UpdatedDetails details(GURL("https://www.google.com/"), | |
110 GURL("https://www.google.es/")); | |
111 content::NotificationService::current()->Notify( | |
112 chrome::NOTIFICATION_GOOGLE_URL_UPDATED, | |
113 content::Source<Profile>(profile()->GetOriginalProfile()), | |
114 content::Details<GoogleURLTracker::UpdatedDetails>(&details)); | |
115 } | |
116 } // namespace chrome | |
samarth
2013/08/02 21:51:15
nit: add an empty line above
Anuj
2013/08/09 07:22:00
Done.
| |
OLD | NEW |