• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2010 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.quicksearchbox;
18 
19 import junit.framework.Assert;
20 
21 /**
22  * Test utilities for {@link ShortcutCursor}.
23  */
24 public class SuggestionCursorUtil extends Assert {
25 
assertNoSuggestions(SuggestionCursor suggestions)26     public static void assertNoSuggestions(SuggestionCursor suggestions) {
27         assertNoSuggestions("", suggestions);
28     }
29 
assertNoSuggestions(String message, SuggestionCursor suggestions)30     public static void assertNoSuggestions(String message, SuggestionCursor suggestions) {
31         assertNotNull(suggestions);
32         assertEquals(message, 0, suggestions.getCount());
33     }
34 
assertSameSuggestion(String message, int position, SuggestionCursor expected, SuggestionCursor observed)35     public static void assertSameSuggestion(String message, int position,
36             SuggestionCursor expected, SuggestionCursor observed) {
37         assertSameSuggestion(message, expected, position, observed, position);
38     }
39 
assertSameSuggestion(String message, SuggestionCursor expected, int positionExpected, SuggestionCursor observed, int positionObserved)40     public static void assertSameSuggestion(String message,
41             SuggestionCursor expected, int positionExpected,
42             SuggestionCursor observed, int positionObserved) {
43         message +=  " at positions " + positionExpected + "(expected) "
44                 + positionObserved + " (observed)";
45         expected.moveTo(positionExpected);
46         observed.moveTo(positionObserved);
47         assertSuggestionEquals(message, expected, observed);
48     }
49 
assertSameSuggestions(SuggestionCursor expected, SuggestionCursor observed)50     public static void assertSameSuggestions(SuggestionCursor expected, SuggestionCursor observed) {
51         assertSameSuggestions("", expected, observed, false);
52     }
53 
assertSameSuggestions(SuggestionCursor expected, SuggestionCursor observed, boolean allowExtras)54     public static void assertSameSuggestions(SuggestionCursor expected, SuggestionCursor observed,
55             boolean allowExtras) {
56         assertSameSuggestions("", expected, observed, allowExtras);
57     }
58 
assertSameSuggestions( String message, SuggestionCursor expected, SuggestionCursor observed)59     public static void assertSameSuggestions(
60             String message, SuggestionCursor expected, SuggestionCursor observed) {
61         assertSameSuggestions(message, expected, observed, false);
62     }
63 
assertSameSuggestions( String message, SuggestionCursor expected, SuggestionCursor observed, boolean allowExtras)64     public static void assertSameSuggestions(
65             String message, SuggestionCursor expected, SuggestionCursor observed,
66             boolean allowExtras) {
67         assertNotNull(expected);
68         assertNotNull(message, observed);
69         if (!allowExtras) {
70             assertEquals(message + ", count", expected.getCount(), observed.getCount());
71         } else {
72             assertTrue(message + "count", expected.getCount() <= observed.getCount());
73         }
74         assertEquals(message + ", userQuery", expected.getUserQuery(), observed.getUserQuery());
75         int count = expected.getCount();
76         for (int i = 0; i < count; i++) {
77             assertSameSuggestion(message, i, expected, observed);
78         }
79     }
80 
slice(SuggestionCursor cursor, int start, int length)81     public static ListSuggestionCursor slice(SuggestionCursor cursor, int start, int length) {
82         ListSuggestionCursor out = new ListSuggestionCursor(cursor.getUserQuery());
83         for (int i = start; i < start + length; i++) {
84             out.add(new SuggestionPosition(cursor, i));
85         }
86         return out;
87     }
88 
assertSuggestionEquals(Suggestion expected, Suggestion observed)89     public static void assertSuggestionEquals(Suggestion expected, Suggestion observed) {
90         assertSuggestionEquals(null, expected, observed);
91     }
92 
assertSuggestionEquals(String message, Suggestion expected, Suggestion observed)93     public static void assertSuggestionEquals(String message, Suggestion expected,
94             Suggestion observed) {
95         assertFieldEquals(message, "source", expected.getSuggestionSource(),
96                 observed.getSuggestionSource());
97         assertFieldEquals(message, "shortcutId", expected.getShortcutId(),
98                 observed.getShortcutId());
99         assertFieldEquals(message, "spinnerWhileRefreshing", expected.isSpinnerWhileRefreshing(),
100                 observed.isSpinnerWhileRefreshing());
101         assertFieldEquals(message, "format", expected.getSuggestionFormat(),
102                 observed.getSuggestionFormat());
103         assertFieldEquals(message, "icon1", expected.getSuggestionIcon1(),
104                 observed.getSuggestionIcon1());
105         assertFieldEquals(message, "icon2", expected.getSuggestionIcon2(),
106                 observed.getSuggestionIcon2());
107         assertFieldEquals(message, "text1", expected.getSuggestionText1(),
108                 observed.getSuggestionText1());
109         assertFieldEquals(message, "text2", expected.getSuggestionText2(),
110                 observed.getSuggestionText2());
111         assertFieldEquals(message, "text2Url", expected.getSuggestionText2Url(),
112                 observed.getSuggestionText2Url());
113         assertFieldEquals(message, "action", expected.getSuggestionIntentAction(),
114                 observed.getSuggestionIntentAction());
115         assertFieldEquals(message, "data", expected.getSuggestionIntentDataString(),
116                 observed.getSuggestionIntentDataString());
117         assertFieldEquals(message, "extraData", expected.getSuggestionIntentExtraData(),
118                 observed.getSuggestionIntentExtraData());
119         assertFieldEquals(message, "query", expected.getSuggestionQuery(),
120                 observed.getSuggestionQuery());
121         assertFieldEquals(message, "logType", expected.getSuggestionLogType(),
122                 observed.getSuggestionLogType());
123     }
124 
assertFieldEquals(String message, String field, Object expected, Object observed)125     private static void assertFieldEquals(String message, String field,
126             Object expected, Object observed) {
127         String msg = (message == null) ? field : message + ", " + field;
128         assertEquals(msg, expected, observed);
129     }
130 
addAll(ListSuggestionCursor to, SuggestionCursor from)131     public static void addAll(ListSuggestionCursor to, SuggestionCursor from) {
132         if (from == null) return;
133         int count = from.getCount();
134         for (int i = 0; i < count; i++) {
135             to.add(new SuggestionPosition(from, i));
136         }
137     }
138 }
139