Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(56)

Side by Side Diff: components/omnibox/search_suggestion_parser_unittest.cc

Issue 669573005: Add a class to parse answer json. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Tweaked API and finished unit tests Created 6 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 "components/omnibox/search_suggestion_parser.h" 5 #include "components/omnibox/search_suggestion_parser.h"
6 6
7 #include "base/json/json_reader.h" 7 #include "base/json/json_reader.h"
8 #include "base/memory/scoped_ptr.h" 8 #include "base/memory/scoped_ptr.h"
9 #include "base/values.h" 9 #include "base/values.h"
10 #include "components/omnibox/suggestion_answer.h"
10 #include "testing/gtest/include/gtest/gtest.h" 11 #include "testing/gtest/include/gtest/gtest.h"
11 #include "url/gurl.h" 12 #include "url/gurl.h"
12 13
13 namespace { 14 namespace {
14 15
15 scoped_ptr<base::DictionaryValue> AsDictionary(const std::string& json) { 16 scoped_ptr<base::DictionaryValue> AsDictionary(const std::string& json) {
16 base::Value* value = base::JSONReader::Read(json); 17 base::Value* value = base::JSONReader::Read(json);
17 base::DictionaryValue* dict; 18 base::DictionaryValue* dict;
18 if (value && value->GetAsDictionary(&dict)) 19 if (value && value->GetAsDictionary(&dict))
19 return scoped_ptr<base::DictionaryValue>(dict); 20 return scoped_ptr<base::DictionaryValue>(dict);
20 21
21 delete value; 22 delete value;
22 return scoped_ptr<base::DictionaryValue>(new base::DictionaryValue); 23 return scoped_ptr<base::DictionaryValue>(new base::DictionaryValue);
23 } 24 }
24 25
25 } // namespace 26 } // namespace
26 27
27 TEST(SearchSuggestionParser, GetAnswersImageURLsWithoutImagelines) { 28 TEST(SearchSuggestionParser, GetAnswersImageURLsWithoutImagelines) {
28 std::vector<GURL> urls; 29 std::vector<GURL> urls;
29 30
30 // No "l" entry in the dictionary. 31 // No "l" entry in the dictionary.
31 SearchSuggestionParser::GetAnswersImageURLs(AsDictionary("").get(), &urls); 32 SuggestionAnswer answer;
33 SuggestionAnswer::ParseAnswer(AsDictionary("").get(), &answer);
34 SearchSuggestionParser::GetAnswersImageURLs(answer, &urls);
32 EXPECT_TRUE(urls.empty()); 35 EXPECT_TRUE(urls.empty());
33 36
34 // Empty "l" entry in the dictionary. 37 // Empty "l" entry in the dictionary.
35 SearchSuggestionParser::GetAnswersImageURLs( 38 SuggestionAnswer::ParseAnswer(AsDictionary("{ \"l\" : {} } ").get(), &answer);
36 AsDictionary("{ \"l\" : {} } ").get(), &urls); 39 SearchSuggestionParser::GetAnswersImageURLs(answer, &urls);
37 EXPECT_TRUE(urls.empty()); 40 EXPECT_TRUE(urls.empty());
38 } 41 }
39 42
40 TEST(SearchSuggestionParser, GetAnswersImageURLsWithValidImage) { 43 TEST(SearchSuggestionParser, GetAnswersImageURLsWithValidImage) {
41 std::vector<GURL> urls; 44 std::vector<GURL> urls;
42 45
43 const char answer_json[] = 46 const char answer_json[] =
44 "{ \"l\" : [{\"il\": { \"i\": {\"d\": " 47 "{ \"l\" : ["
45 "\"//ssl.gstatic.com/foo.png\",\"t\": 3}}}]}"; 48 "{\"il\": { \"t\": [{\"t\": \"some text\", \"tt\": 5}]}},"
46 SearchSuggestionParser::GetAnswersImageURLs(AsDictionary(answer_json).get(), 49 "{\"il\": { \"t\": [{\"t\": \"other text\", \"tt\": 8}],"
47 &urls); 50 " \"i\": {\"d\": \"//ssl.gstatic.com/foo.png\", \"t\": 3}}}]}";
51 SuggestionAnswer answer;
52 answer.SetType("532");
53 SuggestionAnswer::ParseAnswer(AsDictionary(answer_json).get(), &answer);
54 SearchSuggestionParser::GetAnswersImageURLs(answer, &urls);
48 ASSERT_EQ(1U, urls.size()); 55 ASSERT_EQ(1U, urls.size());
49 EXPECT_EQ("https://ssl.gstatic.com/foo.png", urls[0].spec()); 56 EXPECT_EQ("https://ssl.gstatic.com/foo.png", urls[0].spec());
50 } 57 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698