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
3// found in the LICENSE file.
4
5package org.chromium.chrome.browser.omnibox;
6
7import android.test.suitebuilder.annotation.SmallTest;
8
9import junit.framework.TestCase;
10
11public class SuggestionAnswerTest extends TestCase {
12    @SmallTest
13    public void testMalformedJsonReturnsNull() {
14        String json = "} malformed json {";
15        SuggestionAnswer answer = SuggestionAnswer.parseAnswerContents(json);
16        assertNull(answer);
17    }
18
19    @SmallTest
20    public void testEmpyJsonReturnsNull() {
21        String json = "";
22        SuggestionAnswer answer = SuggestionAnswer.parseAnswerContents(json);
23        assertNull(answer);
24    }
25
26    @SmallTest
27    public void testOneLineReturnsNull() {
28        String json =
29                "{ 'l': [" +
30                "  { 'il': { 't': [{ 't': 'text', 'tt': 8 }] } }, " +
31                "] }";
32        SuggestionAnswer answer = SuggestionAnswer.parseAnswerContents(json);
33        assertNull(answer);
34    }
35
36    @SmallTest
37    public void testTwoLinesDoesntReturnNull() {
38        String json =
39                "{ 'l': [" +
40                "  { 'il': { 't': [{ 't': 'text', 'tt': 8 }] } }, " +
41                "  { 'il': { 't': [{ 't': 'other text', 'tt': 5 }] } }" +
42                "] }";
43        SuggestionAnswer answer = SuggestionAnswer.parseAnswerContents(json);
44        assertNotNull(answer);
45    }
46
47    @SmallTest
48    public void testThreeLinesReturnsNull() {
49        String json =
50                "{ 'l': [" +
51                "  { 'il': { 't': [{ 't': 'text', 'tt': 8 }] } }, " +
52                "  { 'il': { 't': [{ 't': 'other text', 'tt': 5 }] } }" +
53                "  { 'il': { 't': [{ 't': 'yet more text', 'tt': 13 }] } }" +
54                "] }";
55        SuggestionAnswer answer = SuggestionAnswer.parseAnswerContents(json);
56        assertNull(answer);
57    }
58
59    @SmallTest
60    public void testFiveLinesReturnsNull() {
61        String json =
62                "{ 'l': [" +
63                "  { 'il': { 't': [{ 't': 'line 1', 'tt': 0 }] } }, " +
64                "  { 'il': { 't': [{ 't': 'line 2', 'tt': 5 }] } }" +
65                "  { 'il': { 't': [{ 't': 'line 3', 'tt': 13 }] } }" +
66                "  { 'il': { 't': [{ 't': 'line 4', 'tt': 14 }] } }" +
67                "  { 'il': { 't': [{ 't': 'line 5', 'tt': 5 }] } }" +
68                "] }";
69        SuggestionAnswer answer = SuggestionAnswer.parseAnswerContents(json);
70        assertNull(answer);
71    }
72
73    @SmallTest
74    public void testPropertyPresence() {
75        String json =
76                "{ 'l': [" +
77                "  { 'il': { 't': [{ 't': 'text', 'tt': 8 }, { 't': 'moar', 'tt': 0 }], " +
78                "            'i': { 'd': 'http://example.com/foo.jpg' } } }, " +
79                "  { 'il': { 't': [{ 't': 'other text', 'tt': 5 }], " +
80                "            'at': { 't': 'slatfotf', 'tt': 42 }, " +
81                "            'st': { 't': 'oh hi, Mark', 'tt': 7666 } } } " +
82                "] }";
83        SuggestionAnswer answer = SuggestionAnswer.parseAnswerContents(json);
84
85        SuggestionAnswer.ImageLine firstLine = answer.getFirstLine();
86        assertEquals(2, firstLine.getTextFields().size());
87        assertFalse(firstLine.hasAdditionalText());
88        assertFalse(firstLine.hasStatusText());
89        assertTrue(firstLine.hasImage());
90
91        SuggestionAnswer.ImageLine secondLine = answer.getSecondLine();
92        assertEquals(1, secondLine.getTextFields().size());
93        assertTrue(secondLine.hasAdditionalText());
94        assertTrue(secondLine.hasStatusText());
95        assertFalse(secondLine.hasImage());
96    }
97
98    @SmallTest
99    public void testContents() {
100        String json =
101                "{ 'l': [" +
102                "  { 'il': { 't': [{ 't': 'text', 'tt': 8 }, { 't': 'moar', 'tt': 0 }], " +
103                "            'at': { 't': 'hi there', 'tt': 7 } } }, " +
104                "  { 'il': { 't': [{ 't': 'ftw', 'tt': 6006 }], " +
105                "            'st': { 't': 'shop S-Mart', 'tt': 666 }, " +
106                "            'i': { 'd': 'Npb24gZnJvbSBvdGhlciBhbmltYWxzLCB3aGlj' } } } " +
107                "] }";
108        SuggestionAnswer answer = SuggestionAnswer.parseAnswerContents(json);
109
110        SuggestionAnswer.ImageLine firstLine = answer.getFirstLine();
111        assertEquals("text", firstLine.getTextFields().get(0).getText());
112        assertEquals(8, firstLine.getTextFields().get(0).getType());
113        assertEquals("moar", firstLine.getTextFields().get(1).getText());
114        assertEquals(0, firstLine.getTextFields().get(1).getType());
115        assertEquals("hi there", firstLine.getAdditionalText().getText());
116        assertEquals(7, firstLine.getAdditionalText().getType());
117
118        SuggestionAnswer.ImageLine secondLine = answer.getSecondLine();
119        assertEquals("ftw", secondLine.getTextFields().get(0).getText());
120        assertEquals(6006, secondLine.getTextFields().get(0).getType());
121        assertEquals("shop S-Mart", secondLine.getStatusText().getText());
122        assertEquals(666, secondLine.getStatusText().getType());
123        assertEquals("Npb24gZnJvbSBvdGhlciBhbmltYWxzLCB3aGlj", secondLine.getImage());
124    }
125}
126