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

Unified Diff: components/omnibox/suggestion_answer_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 side-by-side diff with in-line comments
Download patch
Index: components/omnibox/suggestion_answer_unittest.cc
diff --git a/components/omnibox/suggestion_answer_unittest.cc b/components/omnibox/suggestion_answer_unittest.cc
new file mode 100644
index 0000000000000000000000000000000000000000..5a8c6e5f1f6dc07f364b8898d8eea90996134d70
--- /dev/null
+++ b/components/omnibox/suggestion_answer_unittest.cc
@@ -0,0 +1,273 @@
+// 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/suggestion_answer.h"
+
+#include "testing/gtest/include/gtest/gtest.h"
+
+TEST(SuggestionAnswerTest, DefaultAreEqual) {
+ SuggestionAnswer answer1;
+ SuggestionAnswer answer2;
+ EXPECT_EQ(answer1, answer2);
+}
+
+TEST(SuggestionAnswerTest, CopiesAreEqual) {
+ SuggestionAnswer answer1;
+ SuggestionAnswer answer1copy(answer1);
+ EXPECT_EQ(answer1, answer1copy);
+
+ SuggestionAnswer answer2;
+ answer2.SetType("832345");
+ SuggestionAnswer answer2copy1(answer2);
+ EXPECT_EQ(answer2, answer2copy1);
+
+ std::string json =
+ "{ \"l\": ["
+ " { \"il\": { \"t\": [{ \"t\": \"text\", \"tt\": 8 }] } }, "
+ " { \"il\": { \"t\": [{ \"t\": \"other text\", \"tt\": 5 }] } } "
+ "] }";
+ EXPECT_TRUE(SuggestionAnswer::ParseAnswer(json, &answer2));
+ EXPECT_TRUE(answer2.is_valid());
+ SuggestionAnswer answer2copy2(answer2);
+ EXPECT_EQ(answer2, answer2copy2);
+
+ json =
+ "{ \"l\": [ "
+ " { \"il\": { \"t\": [{ \"t\": \"text\", \"tt\": 8 }] } }, "
+ " { \"il\": { \"t\": [{ \"t\": \"other text\", \"tt\": 5 }] } } "
+ "] }";
+ SuggestionAnswer answer3;
+ answer3.SetType("84");
+ EXPECT_TRUE(SuggestionAnswer::ParseAnswer(json, &answer3));
+ EXPECT_TRUE(answer3.is_valid());
+ SuggestionAnswer answer3copy(answer3);
+ EXPECT_EQ(answer3, answer3copy);
+}
+
+TEST(SuggestionAnswerTest, EmptyAnswerIsInvalid) {
+ SuggestionAnswer answer;
+ EXPECT_FALSE(answer.is_valid());
+}
+
+TEST(SuggestionAnswerTest, AnswerWithTypeOnlyIsInvalid) {
+ SuggestionAnswer answer;
+ answer.SetType("2940");
+ EXPECT_FALSE(answer.is_valid());
+}
+
+TEST(SuggestionAnswerTest, EmptyJsonIsInvalid) {
+ std::string json = "";
+ SuggestionAnswer answer;
+ answer.SetType("1");
+ EXPECT_FALSE(SuggestionAnswer::ParseAnswer(json, &answer));
+ EXPECT_FALSE(answer.is_valid());
+}
+
+TEST(SuggestionAnswerTest, MalformedJsonIsInvalid) {
+ std::string json = "} malformed json {";
+ SuggestionAnswer answer;
+ answer.SetType("921");
+ EXPECT_FALSE(SuggestionAnswer::ParseAnswer(json, &answer));
+ EXPECT_FALSE(answer.is_valid());
+}
+
+TEST(SuggestionAnswerTest, AnswerWithoutTypeIsInvalid) {
+ std::string json =
+ "{ \"l\": [ "
+ " { \"il\": { \"t\": [{ \"t\": \"text\", \"tt\": 8 }] } }, "
+ " { \"il\": { \"t\": [{ \"t\": \"other text\", \"tt\": 5 }] } } "
+ "] }";
+ SuggestionAnswer answer;
+ EXPECT_TRUE(SuggestionAnswer::ParseAnswer(json, &answer));
+ EXPECT_FALSE(answer.is_valid());
+}
+
+TEST(SuggestionAnswerTest, TypeMustBeNonNegativeToBeValid) {
+ std::string json =
+ "{ \"l\": [ "
+ " { \"il\": { \"t\": [{ \"t\": \"text\", \"tt\": 8 }] } }, "
+ " { \"il\": { \"t\": [{ \"t\": \"other text\", \"tt\": 5 }] } } "
+ "] }";
+ SuggestionAnswer answer;
+ answer.SetType("-238");
groby-ooo-7-16 2014/10/23 00:46:20 Do we need all the tests? Do they actually cover d
Justin Donnelly 2014/10/23 17:59:37 You could definitely argue this is overkill but I
+ EXPECT_TRUE(SuggestionAnswer::ParseAnswer(json, &answer));
+ EXPECT_FALSE(answer.is_valid());
+
+ answer.SetType("-1");
+ EXPECT_FALSE(answer.is_valid());
+
+ answer.SetType("-14982");
+ EXPECT_FALSE(answer.is_valid());
+
+ answer.SetType("0");
+ EXPECT_TRUE(answer.is_valid());
+
+ answer.SetType("1");
+ EXPECT_TRUE(answer.is_valid());
+
+ answer.SetType("3284327");
+ EXPECT_TRUE(answer.is_valid());
+}
+
+TEST(SuggestionAnswerTest, ClearedAnswerIsInvalid) {
+ std::string json =
+ "{ \"l\": [ "
+ " { \"il\": { \"t\": [{ \"t\": \"text\", \"tt\": 8 }] } }, "
+ " { \"il\": { \"t\": [{ \"t\": \"other text\", \"tt\": 5 }] } } "
+ "] }";
+ SuggestionAnswer answer;
+ answer.SetType("921");
+ EXPECT_TRUE(SuggestionAnswer::ParseAnswer(json, &answer));
+ EXPECT_TRUE(answer.is_valid());
+
+ answer.Clear();
+ EXPECT_FALSE(answer.is_valid());
+}
+
+TEST(SuggestionAnswerTest, TextFieldsRequireBothTextAndType) {
+ std::string json =
+ "{ \"l\": [ "
+ " { \"il\": { \"t\": [{ \"t\": \"text\" }] } }, "
+ "] }";
+ SuggestionAnswer answer1;
+ answer1.SetType("894");
+ EXPECT_FALSE(SuggestionAnswer::ParseAnswer(json, &answer1));
+ EXPECT_FALSE(answer1.is_valid());
+
+ json =
+ "{ \"l\": [ "
+ " { \"il\": { \"t\": [{ \"tt\": 8 }] } }, "
+ "] }";
+ SuggestionAnswer answer2;
+ answer2.SetType("12");
+ EXPECT_FALSE(SuggestionAnswer::ParseAnswer(json, &answer2));
+ EXPECT_FALSE(answer2.is_valid());
+}
+
+TEST(SuggestionAnswerTest, ImageLinesMustContainAtLeastOneTextField) {
+ std::string json =
+ "{ \"l\": [ "
+ " { \"il\": { \"t\": [{ \"t\": \"text\", \"tt\": 8 }, "
+ " { \"t\": \"moar text\", \"tt\": 0 }], "
+ " \"i\": { \"d\": \"//example.com/foo.jpg\" } } }, "
+ " { \"il\": { \"t\": [], "
+ " \"at\": { \"t\": \"slatfatf\", \"tt\": 42 }, "
+ " \"st\": { \"t\": \"oh hi, Mark\", \"tt\": 729347 } } } "
+ "] }";
+ SuggestionAnswer answer;
+ answer.SetType("205");
+ EXPECT_FALSE(SuggestionAnswer::ParseAnswer(json, &answer));
+ EXPECT_FALSE(answer.is_valid());
+}
+
+TEST(SuggestionAnswerTest, ExactlyTwoLinesRequired) {
+ std::string json =
+ "{ \"l\": [ "
+ " { \"il\": { \"t\": [{ \"t\": \"text\", \"tt\": 8 }] } }, "
+ "] }";
+ SuggestionAnswer answer1;
+ answer1.SetType("8943");
+ EXPECT_FALSE(SuggestionAnswer::ParseAnswer(json, &answer1));
+ EXPECT_FALSE(answer1.is_valid());
+
+ json =
+ "{ \"l\": [ "
+ " { \"il\": { \"t\": [{ \"t\": \"text\", \"tt\": 8 }] } }, "
+ " { \"il\": { \"t\": [{ \"t\": \"other text\", \"tt\": 5 }] } } "
+ "] }";
+ SuggestionAnswer answer2;
+ answer2.SetType("4028023");
+ EXPECT_TRUE(SuggestionAnswer::ParseAnswer(json, &answer2));
+ EXPECT_TRUE(answer2.is_valid());
+
+ json =
+ "{ \"l\": [ "
+ " { \"il\": { \"t\": [{ \"t\": \"text\", \"tt\": 8 }] } }, "
+ " { \"il\": { \"t\": [{ \"t\": \"other text\", \"tt\": 5 }] } } "
+ " { \"il\": { \"t\": [{ \"t\": \"yet more text\", \"tt\": 13 }] } } "
+ "] }";
+ SuggestionAnswer answer3;
+ answer3.SetType("23");
+ EXPECT_FALSE(SuggestionAnswer::ParseAnswer(json, &answer3));
+ EXPECT_FALSE(answer3.is_valid());
+}
+
+TEST(SuggestionAnswerTest, URLPresent) {
+ std::string json =
+ "{ \"l\": [ "
+ " { \"il\": { \"t\": [{ \"t\": \"text\", \"tt\": 8 }] } }, "
+ " { \"il\": { \"t\": [{ \"t\": \"other text\", \"tt\": 5 }], "
+ " \"i\": { \"d\": \"\" } } } "
+ "] }";
+ SuggestionAnswer answer1;
+ answer1.SetType("1");
+ EXPECT_FALSE(SuggestionAnswer::ParseAnswer(json, &answer1));
+ EXPECT_FALSE(answer1.is_valid());
+
+ json =
+ "{ \"l\": [ "
+ " { \"il\": { \"t\": [{ \"t\": \"text\", \"tt\": 8 }] } }, "
+ " { \"il\": { \"t\": [{ \"t\": \"other text\", \"tt\": 5 }], "
+ " \"i\": { \"d\": \"https://example.com/foo.jpg\" } } } "
+ "] }";
+ SuggestionAnswer answer2;
+ answer2.SetType("8");
+ EXPECT_TRUE(SuggestionAnswer::ParseAnswer(json, &answer2));
+ EXPECT_TRUE(answer2.is_valid());
+
+ json =
+ "{ \"l\": [ "
+ " { \"il\": { \"t\": [{ \"t\": \"text\", \"tt\": 8 }] } }, "
+ " { \"il\": { \"t\": [{ \"t\": \"other text\", \"tt\": 5 }], "
+ " \"i\": { \"d\": \"//example.com/foo.jpg\" } } } "
+ "] }";
+ SuggestionAnswer answer3;
+ answer3.SetType("4");
+ EXPECT_TRUE(SuggestionAnswer::ParseAnswer(json, &answer3));
+ EXPECT_TRUE(answer3.is_valid());
+}
+
+TEST(SuggestionAnswerTest, ValidPropertyValues) {
+ std::string json =
+ "{ \"l\": [ "
+ " { \"il\": { \"t\": [{ \"t\": \"text\", \"tt\": 8 }, "
+ " { \"t\": \"moar text\", \"tt\": 0 }], "
+ " \"i\": { \"d\": \"//example.com/foo.jpg\" } } }, "
+ " { \"il\": { \"t\": [{ \"t\": \"other text\", \"tt\": 5 }], "
+ " \"at\": { \"t\": \"slatfatf\", \"tt\": 42 }, "
+ " \"st\": { \"t\": \"oh hi, Mark\", \"tt\": 729347 } } } "
+ "] }";
+ SuggestionAnswer answer;
+ answer.SetType("420527");
+ EXPECT_TRUE(SuggestionAnswer::ParseAnswer(json, &answer));
+ EXPECT_TRUE(answer.is_valid());
+
+ const SuggestionAnswer::ImageLine& first_line = answer.first_line();
+ EXPECT_EQ(2U, first_line.text_fields().size());
+ EXPECT_EQ("text", first_line.text_fields()[0].text());
+ EXPECT_EQ(8, first_line.text_fields()[0].type());
+ EXPECT_EQ("moar text", first_line.text_fields()[1].text());
+ EXPECT_EQ(0, first_line.text_fields()[1].type());
+
+ EXPECT_FALSE(first_line.has_additional_text());
+ EXPECT_FALSE(first_line.has_status_text());
+
+ EXPECT_TRUE(first_line.has_image_url());
+ EXPECT_EQ("https://example.com/foo.jpg", first_line.image_url().spec());
+
+ const SuggestionAnswer::ImageLine& second_line = answer.second_line();
+ EXPECT_EQ(1U, second_line.text_fields().size());
+ EXPECT_EQ("other text", second_line.text_fields()[0].text());
+ EXPECT_EQ(5, second_line.text_fields()[0].type());
+
+ EXPECT_TRUE(second_line.has_additional_text());
+ EXPECT_EQ("slatfatf", second_line.additional_text().text());
+ EXPECT_EQ(42, second_line.additional_text().type());
+
+ EXPECT_TRUE(second_line.has_status_text());
+ EXPECT_EQ("oh hi, Mark", second_line.status_text().text());
+ EXPECT_EQ(729347, second_line.status_text().type());
+
+ EXPECT_FALSE(second_line.has_image_url());
+}
« components/omnibox/suggestion_answer.cc ('K') | « components/omnibox/suggestion_answer.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698