Commit bf481022 authored by groby's avatar groby Committed by Commit bot

[AiS] Fix parsing for Answers Images.

1) Image parsing missed one nesting level of dictionaries, and so
   always failed.
2) Images are returned without a scheme prefix - prepend that.

R=mpearson@chromium.org
BUG=none

Review URL: https://codereview.chromium.org/470403003

Cr-Commit-Position: refs/heads/master@{#291828}
parent f1c005b2
...@@ -144,6 +144,7 @@ ...@@ -144,6 +144,7 @@
'omnibox/autocomplete_result_unittest.cc', 'omnibox/autocomplete_result_unittest.cc',
'omnibox/keyword_provider_unittest.cc', 'omnibox/keyword_provider_unittest.cc',
'omnibox/omnibox_field_trial_unittest.cc', 'omnibox/omnibox_field_trial_unittest.cc',
'omnibox/search_suggestion_parser_unittest.cc',
'os_crypt/ie7_password_win_unittest.cc', 'os_crypt/ie7_password_win_unittest.cc',
'os_crypt/keychain_password_mac_unittest.mm', 'os_crypt/keychain_password_mac_unittest.mm',
'os_crypt/os_crypt_unittest.cc', 'os_crypt/os_crypt_unittest.cc',
......
...@@ -17,6 +17,7 @@ ...@@ -17,6 +17,7 @@
#include "net/base/net_util.h" #include "net/base/net_util.h"
#include "net/http/http_response_headers.h" #include "net/http/http_response_headers.h"
#include "net/url_request/url_fetcher.h" #include "net/url_request/url_fetcher.h"
#include "url/url_constants.h"
namespace { namespace {
...@@ -483,22 +484,29 @@ void SearchSuggestionParser::GetAnswersImageURLs( ...@@ -483,22 +484,29 @@ void SearchSuggestionParser::GetAnswersImageURLs(
const base::DictionaryValue* answer_json, const base::DictionaryValue* answer_json,
std::vector<GURL>* urls) { std::vector<GURL>* urls) {
DCHECK(answer_json); DCHECK(answer_json);
const base::ListValue* lines = NULL; const base::ListValue* lines = NULL;
answer_json->GetList("l", &lines); if (!answer_json->GetList("l", &lines) || !lines || lines->GetSize() == 0)
if (!lines || lines->GetSize() == 0)
return; return;
for (size_t line = 0; line < lines->GetSize(); ++line) { for (base::ListValue::const_iterator iter = lines->begin();
const base::DictionaryValue* imageLine = NULL; iter != lines->end();
lines->GetDictionary(line, &imageLine); ++iter) {
if (!imageLine) const base::DictionaryValue* line = NULL;
if (!(*iter)->GetAsDictionary(&line) || !line)
continue; continue;
const base::DictionaryValue* imageData = NULL;
imageLine->GetDictionary("i", &imageData); std::string image_host_and_path;
if (!imageData) if (!line->GetString("il.i.d", &image_host_and_path) ||
image_host_and_path.empty())
continue; continue;
std::string imageUrl; // Concatenate scheme and host/path using only ':' as separator. This is
imageData->GetString("d", &imageUrl); // due to the results delivering strings of the form '//host/path', which
urls->push_back(GURL(imageUrl)); // is web-speak for "use the enclosing page's scheme", but not a valid path
// of an URL.
GURL image_url(
GURL(std::string(url::kHttpsScheme) + ":" + image_host_and_path));
if (image_url.is_valid())
urls->push_back(image_url);
} }
} }
...@@ -9,6 +9,7 @@ ...@@ -9,6 +9,7 @@
#include <vector> #include <vector>
#include "base/basictypes.h" #include "base/basictypes.h"
#include "base/gtest_prod_util.h"
#include "base/strings/string16.h" #include "base/strings/string16.h"
#include "components/omnibox/autocomplete_match.h" #include "components/omnibox/autocomplete_match.h"
#include "components/omnibox/autocomplete_match_type.h" #include "components/omnibox/autocomplete_match_type.h"
...@@ -281,6 +282,11 @@ class SearchSuggestionParser { ...@@ -281,6 +282,11 @@ class SearchSuggestionParser {
Results* results); Results* results);
private: private:
FRIEND_TEST_ALL_PREFIXES(SearchSuggestionParser,
GetAnswersImageURLsWithoutImagelines);
FRIEND_TEST_ALL_PREFIXES(SearchSuggestionParser,
GetAnswersImageURLsWithValidImage);
// Gets URLs of any images in Answers results. // Gets URLs of any images in Answers results.
static void GetAnswersImageURLs(const base::DictionaryValue* answer_json, static void GetAnswersImageURLs(const base::DictionaryValue* answer_json,
std::vector<GURL>* urls); std::vector<GURL>* urls);
......
// Copyright 2014 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 "components/omnibox/search_suggestion_parser.h"
#include "base/json/json_reader.h"
#include "base/memory/scoped_ptr.h"
#include "base/values.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "url/gurl.h"
namespace {
scoped_ptr<base::DictionaryValue> AsDictionary(const std::string& json) {
base::Value* value = base::JSONReader::Read(json);
base::DictionaryValue* dict;
if (value && value->GetAsDictionary(&dict))
return scoped_ptr<base::DictionaryValue>(dict);
delete value;
return scoped_ptr<base::DictionaryValue>(new base::DictionaryValue);
}
} // namespace
TEST(SearchSuggestionParser, GetAnswersImageURLsWithoutImagelines) {
std::vector<GURL> urls;
// No "l" entry in the dictionary.
SearchSuggestionParser::GetAnswersImageURLs(AsDictionary("").get(), &urls);
EXPECT_TRUE(urls.empty());
// Empty "l" entry in the dictionary.
SearchSuggestionParser::GetAnswersImageURLs(
AsDictionary("{ \"l\" : {} } ").get(), &urls);
EXPECT_TRUE(urls.empty());
}
TEST(SearchSuggestionParser, GetAnswersImageURLsWithValidImage) {
std::vector<GURL> urls;
const char answer_json[] =
"{ \"l\" : [{\"il\": { \"i\": {\"d\": "
"\"//ssl.gstatic.com/foo.png\",\"t\": 3}}}]}";
SearchSuggestionParser::GetAnswersImageURLs(AsDictionary(answer_json).get(),
&urls);
ASSERT_EQ(1U, urls.size());
EXPECT_EQ("https://ssl.gstatic.com/foo.png", urls[0].spec());
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment