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.
7 #include "base/basictypes.h"
9 #include "base/command_line.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/run_loop.h"
12 #include "base/strings/utf_string_conversions.h"
13 #include "chrome/browser/profiles/profile_manager.h"
14 #include "chrome/browser/ui/app_list/search/chrome_search_result.h"
15 #include "chrome/browser/ui/app_list/search/webstore/webstore_provider.h"
16 #include "chrome/browser/ui/app_list/search/webstore/webstore_result.h"
17 #include "chrome/common/chrome_switches.h"
18 #include "chrome/test/base/in_process_browser_test.h"
19 #include "content/public/browser/browser_thread.h"
20 #include "extensions/common/extension.h"
21 #include "net/test/embedded_test_server/embedded_test_server.h"
22 #include "net/test/embedded_test_server/http_request.h"
23 #include "net/test/embedded_test_server/http_response.h"
25 using content::BrowserThread;
26 using extensions::Manifest;
27 using net::test_server::BasicHttpResponse;
28 using net::test_server::HttpRequest;
29 using net::test_server::HttpResponse;
30 using net::test_server::EmbeddedTestServer;
37 const char kOneResult[] = "{"
38 "\"search_url\": \"http://host/search\","
41 "\"id\": \"app1_id\","
42 "\"localized_name\": \"app1 name\","
43 "\"icon_url\": \"http://host/icon\","
48 const char kThreeResults[] = "{"
49 "\"search_url\": \"http://host/search\","
52 "\"id\": \"app1_id\","
53 "\"localized_name\": \"one\","
54 "\"icon_url\": \"http://host/icon1\","
56 "\"item_type\": \"PLATFORM_APP\""
59 "\"id\": \"app2_id\","
60 "\"localized_name\": \"two\","
61 "\"icon_url\": \"http://host/icon2\","
63 "\"item_type\": \"HOSTED_APP\""
66 "\"id\": \"app3_id\","
67 "\"localized_name\": \"three\","
68 "\"icon_url\": \"http://host/icon3\","
70 "\"item_type\": \"LEGACY_PACKAGED_APP\""
74 struct ParsedSearchResult {
79 Manifest::Type item_type;
83 ParsedSearchResult kParsedOneResult[] = {{"app1_id", "app1 name",
84 "http://host/icon", false,
85 Manifest::TYPE_UNKNOWN, 1}};
87 ParsedSearchResult kParsedThreeResults[] = {
88 {"app1_id", "one", "http://host/icon1", true, Manifest::TYPE_PLATFORM_APP,
90 {"app2_id", "two", "http://host/icon2", false, Manifest::TYPE_HOSTED_APP,
92 {"app3_id", "three", "http://host/icon3", false,
93 Manifest::TYPE_LEGACY_PACKAGED_APP, 1}};
97 class WebstoreProviderTest : public InProcessBrowserTest {
99 WebstoreProviderTest() {}
100 virtual ~WebstoreProviderTest() {}
102 // InProcessBrowserTest overrides:
103 virtual void SetUpOnMainThread() OVERRIDE {
104 test_server_.reset(new EmbeddedTestServer);
106 ASSERT_TRUE(test_server_->InitializeAndWaitUntilReady());
107 test_server_->RegisterRequestHandler(
108 base::Bind(&WebstoreProviderTest::HandleRequest,
109 base::Unretained(this)));
110 CommandLine::ForCurrentProcess()->AppendSwitchASCII(
111 switches::kAppsGalleryURL, test_server_->base_url().spec());
112 CommandLine::ForCurrentProcess()->AppendSwitch(
113 switches::kEnableEphemeralApps);
115 webstore_provider_.reset(new WebstoreProvider(
116 ProfileManager::GetActiveUserProfile(), NULL));
117 webstore_provider_->set_webstore_search_fetched_callback(
118 base::Bind(&WebstoreProviderTest::OnSearchResultsFetched,
119 base::Unretained(this)));
120 // TODO(mukai): add test cases for throttling.
121 webstore_provider_->set_use_throttling(false);
124 virtual void TearDownOnMainThread() OVERRIDE {
125 EXPECT_TRUE(test_server_->ShutdownAndWaitUntilComplete());
126 test_server_.reset();
129 void RunQuery(const std::string& query,
130 const std::string& mock_server_response) {
131 webstore_provider_->Start(base::UTF8ToUTF16(query));
133 if (webstore_provider_->webstore_search_ && !mock_server_response.empty()) {
134 mock_server_response_ = mock_server_response;
137 run_loop_.reset(new base::RunLoop);
141 mock_server_response_.clear();
144 webstore_provider_->Stop();
147 std::string GetResultTitles() const {
149 for (SearchProvider::Results::const_iterator it =
150 webstore_provider_->results().begin();
151 it != webstore_provider_->results().end();
153 if (!results.empty())
155 results += base::UTF16ToUTF8((*it)->title());
160 void VerifyResults(const ParsedSearchResult* expected_results,
161 size_t expected_result_size) {
162 ASSERT_EQ(expected_result_size, webstore_provider_->results().size());
163 for (size_t i = 0; i < expected_result_size; ++i) {
164 const SearchResult* result = webstore_provider_->results()[i];
165 ASSERT_EQ(extensions::Extension::GetBaseURLFromExtensionId(
166 expected_results[i].id).spec(),
168 EXPECT_EQ(std::string(expected_results[i].title),
169 base::UTF16ToUTF8(result->title()));
171 // Ensure the number of action buttons is appropriate for the item type.
172 EXPECT_EQ(expected_results[i].num_actions, result->actions().size());
174 const WebstoreResult* webstore_result =
175 static_cast<const WebstoreResult*>(result);
176 EXPECT_EQ(expected_results[i].id, webstore_result->app_id());
177 EXPECT_EQ(expected_results[i].icon_url,
178 webstore_result->icon_url().spec());
179 EXPECT_EQ(expected_results[i].is_paid, webstore_result->is_paid());
180 EXPECT_EQ(expected_results[i].item_type, webstore_result->item_type());
184 void RunQueryAndVerify(const std::string& query,
185 const std::string& mock_server_response,
186 const ParsedSearchResult* expected_results,
187 size_t expected_result_size) {
188 RunQuery(query, mock_server_response);
189 VerifyResults(expected_results, expected_result_size);
192 WebstoreProvider* webstore_provider() { return webstore_provider_.get(); }
195 scoped_ptr<HttpResponse> HandleRequest(const HttpRequest& request) {
196 scoped_ptr<BasicHttpResponse> response(new BasicHttpResponse);
198 if (request.relative_url.find("/jsonsearch?") != std::string::npos) {
199 if (mock_server_response_ == "404") {
200 response->set_code(net::HTTP_NOT_FOUND);
201 } else if (mock_server_response_ == "500") {
202 response->set_code(net::HTTP_INTERNAL_SERVER_ERROR);
204 response->set_code(net::HTTP_OK);
205 response->set_content(mock_server_response_);
209 return response.PassAs<HttpResponse>();
212 void OnSearchResultsFetched() {
217 scoped_ptr<EmbeddedTestServer> test_server_;
218 scoped_ptr<base::RunLoop> run_loop_;
220 std::string mock_server_response_;
222 scoped_ptr<WebstoreProvider> webstore_provider_;
224 DISALLOW_COPY_AND_ASSIGN(WebstoreProviderTest);
227 // Flaky on CrOS and Windows: http://crbug.com/246136.
228 // TODO(erg): linux_aura bringup: http://crbug.com/163931
229 #if defined(OS_WIN) || defined(OS_LINUX)
230 #define MAYBE_Basic DISABLED_Basic
232 #define MAYBE_Basic Basic
234 IN_PROC_BROWSER_TEST_F(WebstoreProviderTest, MAYBE_Basic) {
237 const char* mock_server_response;
238 const char* expected_result_titles;
239 const ParsedSearchResult* expected_results;
240 size_t expected_result_size;
242 // "Search in web store" result with query text itself is used for
243 // synchronous placeholder, bad server response etc.
244 {"synchronous", "", "synchronous", NULL, 0 },
245 {"404", "404", "404", NULL, 0 },
246 {"500", "500", "500", NULL, 0 },
247 {"bad json", "invalid json", "bad json", NULL, 0 },
249 {"1 result", kOneResult, "app1 name", kParsedOneResult, 1 },
250 {"3 result", kThreeResults, "one,two,three", kParsedThreeResults, 3 },
253 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kTestCases); ++i) {
254 if (kTestCases[i].expected_result_titles) {
255 RunQuery(kTestCases[i].query, kTestCases[i].mock_server_response);
256 ASSERT_EQ(kTestCases[i].expected_result_titles, GetResultTitles())
257 << "Case " << i << ": q=" << kTestCases[i].query;
259 if (kTestCases[i].expected_results) {
260 VerifyResults(kTestCases[i].expected_results,
261 kTestCases[i].expected_result_size);
267 IN_PROC_BROWSER_TEST_F(WebstoreProviderTest, NoSearchForSensitiveData) {
268 // None of the following input strings should be accepted because they may
269 // contain private data.
270 const char* inputs[] = {
271 // file: scheme is bad.
274 // URLs with usernames, ports, queries or refs are bad.
275 "http://username:password@hostname/",
276 "http://www.example.com:1000",
278 "http://hostname/?query=q",
279 "http://hostname/path#ref",
280 // A https URL with path is bad.
281 "https://hostname/path",
284 for (size_t i = 0; i < arraysize(inputs); ++i) {
285 RunQueryAndVerify(inputs[i], kOneResult, NULL, 0);
289 IN_PROC_BROWSER_TEST_F(WebstoreProviderTest, NoSearchForShortQueries) {
290 RunQueryAndVerify("a", kOneResult, NULL, 0);
291 RunQueryAndVerify("ab", kOneResult, NULL, 0);
292 RunQueryAndVerify("abc", kOneResult, kParsedOneResult, 1);
295 // Flaky on CrOS and Windows: http://crbug.com/246136.
296 #if defined(OS_WIN) || defined(OS_CHROMEOS)
297 #define MAYBE_SearchCache DISABLED_SearchCache
299 #define MAYBE_SearchCache SearchCache
301 IN_PROC_BROWSER_TEST_F(WebstoreProviderTest, MAYBE_SearchCache) {
302 RunQueryAndVerify("foo", kOneResult, kParsedOneResult, 1);
304 // No result is provided but the provider gets the result from the cache.
305 RunQueryAndVerify("foo", "", kParsedOneResult, 1);
309 } // namespace app_list