OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "chrome/browser/search/suggestions/suggestions_service.h" | 5 #include "chrome/browser/search/suggestions/suggestions_service.h" |
6 | 6 |
7 #include <map> | 7 #include <map> |
8 #include <string> | 8 #include <string> |
9 | 9 |
10 #include "base/bind.h" | |
11 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
12 #include "base/metrics/field_trial.h" | 11 #include "base/metrics/field_trial.h" |
13 #include "base/prefs/pref_service.h" | 12 #include "base/prefs/pref_service.h" |
14 #include "base/strings/utf_string_conversions.h" | 13 #include "base/strings/utf_string_conversions.h" |
15 #include "chrome/browser/history/history_types.h" | 14 #include "chrome/browser/history/history_types.h" |
16 #include "chrome/browser/search/suggestions/proto/suggestions.pb.h" | 15 #include "chrome/browser/search/suggestions/proto/suggestions.pb.h" |
17 #include "chrome/browser/search/suggestions/suggestions_service_factory.h" | 16 #include "chrome/browser/search/suggestions/suggestions_service_factory.h" |
18 #include "chrome/test/base/testing_profile.h" | 17 #include "chrome/test/base/testing_profile.h" |
19 #include "components/variations/entropy_provider.h" | 18 #include "components/variations/entropy_provider.h" |
20 #include "components/variations/variations_associated_data.h" | 19 #include "components/variations/variations_associated_data.h" |
21 #include "content/public/test/test_browser_thread_bundle.h" | 20 #include "content/public/test/test_browser_thread_bundle.h" |
22 #include "net/http/http_response_headers.h" | 21 #include "net/http/http_response_headers.h" |
23 #include "net/http/http_status_code.h" | 22 #include "net/http/http_status_code.h" |
24 #include "net/url_request/test_url_fetcher_factory.h" | 23 #include "net/url_request/test_url_fetcher_factory.h" |
25 #include "net/url_request/url_request_status.h" | 24 #include "net/url_request/url_request_status.h" |
26 #include "net/url_request/url_request_test_util.h" | 25 #include "net/url_request/url_request_test_util.h" |
27 #include "testing/gtest/include/gtest/gtest.h" | 26 #include "testing/gtest/include/gtest/gtest.h" |
28 | 27 |
29 namespace { | 28 namespace { |
30 | 29 |
31 const char kFakeSuggestionsURL[] = "https://mysuggestions.com/proto"; | 30 const char kFakeSuggestionsURL[] = "https://mysuggestions.com/proto"; |
32 | 31 |
33 const char kTestTitle[] = "a title"; | |
34 const char kTestUrl[] = "http://go.com"; | |
35 | |
36 scoped_ptr<net::FakeURLFetcher> CreateURLFetcher( | 32 scoped_ptr<net::FakeURLFetcher> CreateURLFetcher( |
37 const GURL& url, net::URLFetcherDelegate* delegate, | 33 const GURL& url, net::URLFetcherDelegate* delegate, |
38 const std::string& response_data, net::HttpStatusCode response_code, | 34 const std::string& response_data, net::HttpStatusCode response_code, |
39 net::URLRequestStatus::Status status) { | 35 net::URLRequestStatus::Status status) { |
40 scoped_ptr<net::FakeURLFetcher> fetcher(new net::FakeURLFetcher( | 36 scoped_ptr<net::FakeURLFetcher> fetcher(new net::FakeURLFetcher( |
41 url, delegate, response_data, response_code, status)); | 37 url, delegate, response_data, response_code, status)); |
42 | 38 |
43 if (response_code == net::HTTP_OK) { | 39 if (response_code == net::HTTP_OK) { |
44 scoped_refptr<net::HttpResponseHeaders> download_headers( | 40 scoped_refptr<net::HttpResponseHeaders> download_headers( |
45 new net::HttpResponseHeaders("")); | 41 new net::HttpResponseHeaders("")); |
46 download_headers->AddHeader("Content-Type: text/html"); | 42 download_headers->AddHeader("Content-Type: text/html"); |
47 fetcher->set_response_headers(download_headers); | 43 fetcher->set_response_headers(download_headers); |
48 } | 44 } |
49 return fetcher.Pass(); | 45 return fetcher.Pass(); |
50 } | 46 } |
51 | 47 |
52 } // namespace | 48 } // namespace |
53 | 49 |
54 namespace suggestions { | 50 namespace suggestions { |
55 | 51 |
56 class SuggestionsServiceTest : public testing::Test { | 52 class SuggestionsServiceTest : public testing::Test { |
57 public: | |
58 void CheckSuggestionsData(const SuggestionsProfile& suggestions_profile) { | |
59 EXPECT_EQ(1, suggestions_profile.suggestions_size()); | |
60 EXPECT_EQ(kTestTitle, suggestions_profile.suggestions(0).title()); | |
61 EXPECT_EQ(kTestUrl, suggestions_profile.suggestions(0).url()); | |
62 ++suggestions_data_check_count_; | |
63 } | |
64 | |
65 int suggestions_data_check_count_; | |
66 | |
67 protected: | 53 protected: |
68 SuggestionsServiceTest() | 54 SuggestionsServiceTest() |
69 : suggestions_data_check_count_(0), | 55 : factory_(NULL, base::Bind(&CreateURLFetcher)) { |
70 factory_(NULL, base::Bind(&CreateURLFetcher)) { | |
71 profile_ = profile_builder_.Build(); | 56 profile_ = profile_builder_.Build(); |
72 } | 57 } |
73 virtual ~SuggestionsServiceTest() {} | 58 virtual ~SuggestionsServiceTest() {} |
74 | 59 |
75 // Enables the "ChromeSuggestions.Group1" field trial. | 60 // Enables the "ChromeSuggestions.Group1" field trial. |
76 void EnableFieldTrial(const std::string& url) { | 61 void EnableFieldTrial(const std::string& url) { |
77 // Clear the existing |field_trial_list_| to avoid firing a DCHECK. | 62 // Clear the existing |field_trial_list_| to avoid firing a DCHECK. |
78 field_trial_list_.reset(NULL); | 63 field_trial_list_.reset(NULL); |
79 field_trial_list_.reset( | 64 field_trial_list_.reset( |
80 new base::FieldTrialList(new metrics::SHA1EntropyProvider("foo"))); | 65 new base::FieldTrialList(new metrics::SHA1EntropyProvider("foo"))); |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
118 EnableFieldTrial(""); | 103 EnableFieldTrial(""); |
119 EXPECT_TRUE(CreateSuggestionsService() != NULL); | 104 EXPECT_TRUE(CreateSuggestionsService() != NULL); |
120 } | 105 } |
121 | 106 |
122 TEST_F(SuggestionsServiceTest, FetchSuggestionsData) { | 107 TEST_F(SuggestionsServiceTest, FetchSuggestionsData) { |
123 // Field trial enabled with a specific suggestions URL. | 108 // Field trial enabled with a specific suggestions URL. |
124 EnableFieldTrial(kFakeSuggestionsURL); | 109 EnableFieldTrial(kFakeSuggestionsURL); |
125 SuggestionsService* suggestions_service = CreateSuggestionsService(); | 110 SuggestionsService* suggestions_service = CreateSuggestionsService(); |
126 EXPECT_TRUE(suggestions_service != NULL); | 111 EXPECT_TRUE(suggestions_service != NULL); |
127 | 112 |
| 113 // Suggestions profile is instantiated empty. |
| 114 EXPECT_EQ(0, suggestions_service->suggestions().suggestions_size()); |
128 SuggestionsProfile suggestions_profile; | 115 SuggestionsProfile suggestions_profile; |
129 ChromeSuggestion* suggestion = suggestions_profile.add_suggestions(); | 116 ChromeSuggestion* suggestion = suggestions_profile.add_suggestions(); |
130 suggestion->set_title(kTestTitle); | 117 suggestion->set_title("a title"); |
131 suggestion->set_url(kTestUrl); | 118 suggestion->set_url("http://go.com"); |
132 factory_.SetFakeResponse(GURL(kFakeSuggestionsURL), | 119 factory_.SetFakeResponse(GURL(kFakeSuggestionsURL), |
133 suggestions_profile.SerializeAsString(), | 120 suggestions_profile.SerializeAsString(), |
134 net::HTTP_OK, | 121 net::HTTP_OK, |
135 net::URLRequestStatus::SUCCESS); | 122 net::URLRequestStatus::SUCCESS); |
| 123 suggestions_service->FetchSuggestionsData(); |
136 | 124 |
137 // Send the request. The data will be returned to the callback. | 125 // For testing only: wait until suggestion fetch is complete. |
138 suggestions_service->FetchSuggestionsData( | |
139 base::Bind(&SuggestionsServiceTest::CheckSuggestionsData, | |
140 base::Unretained(this))); | |
141 | |
142 // Send the request a second time. | |
143 suggestions_service->FetchSuggestionsData( | |
144 base::Bind(&SuggestionsServiceTest::CheckSuggestionsData, | |
145 base::Unretained(this))); | |
146 | |
147 // (Testing only) wait until suggestion fetch is complete. | |
148 base::MessageLoop::current()->RunUntilIdle(); | 126 base::MessageLoop::current()->RunUntilIdle(); |
149 | 127 |
150 // Ensure that CheckSuggestionsData() ran twice. | 128 SuggestionsProfile returned_profile = suggestions_service->suggestions(); |
151 EXPECT_EQ(2, suggestions_data_check_count_); | 129 EXPECT_EQ(1, returned_profile.suggestions_size()); |
| 130 EXPECT_EQ("a title", returned_profile.suggestions(0).title()); |
| 131 EXPECT_EQ("http://go.com", returned_profile.suggestions(0).url()); |
152 } | 132 } |
153 | 133 |
154 } // namespace suggestions | 134 } // namespace suggestions |
OLD | NEW |