• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 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.globalsearch;
18 
19 import junit.framework.TestCase;
20 
21 import android.content.ComponentName;
22 
23 /**
24  * Tests {@link SuggestionSession.SessionCache}
25  */
26 public class SessionCacheTest extends TestCase {
27     private SuggestionSession.SessionCache mCache;
28     private TestSuggestionSource.Builder mBuilder;
29     private ComponentName mSourceName;
30 
31     @Override
setUp()32     protected void setUp() throws Exception {
33         super.setUp();
34 
35         mCache = new SuggestionSession.SessionCache(true);
36 
37         mSourceName = new ComponentName(
38                 "com.android.globalsearch", "com.android.globalsearch.GlobalSearch");
39 
40         mBuilder = new TestSuggestionSource.Builder()
41                 .setComponent(mSourceName);
42     }
43 
testZeroResultPrefix()44     public void testZeroResultPrefix() {
45         final TestSuggestionSource source = mBuilder.create();
46         assertFalse(source.queryAfterZeroResults());
47 
48         mCache.reportSourceResult("yo", new SuggestionResult(source));
49         assertTrue(mCache.hasReportedZeroResultsForPrefix("yo man", mSourceName));
50     }
51 
testZeroResultPrefix_sourceNotIgnored()52     public void testZeroResultPrefix_sourceNotIgnored() {
53         final TestSuggestionSource source = mBuilder.setQueryAfterZeroResults(true).create();
54         assertTrue(source.queryAfterZeroResults());
55 
56         mCache.reportSourceResult("yo", new SuggestionResult(source));
57         assertFalse(mCache.hasReportedZeroResultsForPrefix("yo man", mSourceName));
58     }
59 
testShortcutRefreshing_noIcon()60     public void testShortcutRefreshing_noIcon() {
61         final ComponentName name = new ComponentName("com.android.yo", "com.android.yo.Dog");
62         final String shortcutId = "shortcut1";
63         final SuggestionData refreshed = new SuggestionData.Builder(name)
64                 .spinnerWhileRefreshing(true)
65                 .shortcutId(shortcutId)
66                 .title("hi")
67                 .description("hi")
68                 .intentAction("VIEW").intentData("id1").build();
69         assertFalse("hasShortcutBeenRefreshed", mCache.hasShortcutBeenRefreshed(name, shortcutId));
70         mCache.reportRefreshedShortcut(name, shortcutId, refreshed);
71         assertTrue("hasShortcutBeenRefreshed", mCache.hasShortcutBeenRefreshed(name, shortcutId));
72         assertNull("getRefreshedShortcutIcon2", mCache.getRefreshedShortcutIcon2(name, shortcutId));
73     }
74 
testShortcutRefreshing_withIcon2NotSpinnerWhileRefreshing()75     public void testShortcutRefreshing_withIcon2NotSpinnerWhileRefreshing() {
76         final ComponentName name = new ComponentName("com.android.yo", "com.android.yo.Dog");
77         final String shortcutId = "shortcut1";
78         final SuggestionData refreshed = new SuggestionData.Builder(name)
79                 .spinnerWhileRefreshing(false)
80                 .shortcutId(shortcutId)
81                 .title("hi")
82                 .description("hi")
83                 .icon2("icon2")
84                 .intentAction("VIEW").intentData("id1").build();
85         mCache.reportRefreshedShortcut(name, shortcutId, refreshed);
86         assertTrue("hasShortcutBeenRefreshed", mCache.hasShortcutBeenRefreshed(name, shortcutId));
87         assertNull("getRefreshedShortcutIcon2", mCache.getRefreshedShortcutIcon2(name, shortcutId));
88     }
89 
testShortcutRefreshing_withIcon2SpinnerWhileRefreshing()90     public void testShortcutRefreshing_withIcon2SpinnerWhileRefreshing() {
91         final ComponentName name = new ComponentName("com.android.yo", "com.android.yo.Dog");
92         final String shortcutId = "shortcut1";
93         final SuggestionData refreshed = new SuggestionData.Builder(name)
94                 .spinnerWhileRefreshing(true)
95                 .shortcutId(shortcutId)
96                 .title("hi")
97                 .description("hi")
98                 .icon2("icon2")
99                 .intentAction("VIEW").intentData("id1").build();
100         mCache.reportRefreshedShortcut(name, shortcutId, refreshed);
101         assertTrue("hasShortcutBeenRefreshed", mCache.hasShortcutBeenRefreshed(name, shortcutId));
102         assertEquals("icon2", mCache.getRefreshedShortcutIcon2(name, shortcutId));
103     }
104 
testShortcutRefreshing_sameIdDifferentSources()105     public void testShortcutRefreshing_sameIdDifferentSources() {
106         final ComponentName name = new ComponentName("com.android.yo", "com.android.yo.Dog");
107         final String shortcutId = "shortcut1";
108         final SuggestionData refreshed = new SuggestionData.Builder(name)
109                 .spinnerWhileRefreshing(true)
110                 .shortcutId(shortcutId)
111                 .title("hi")
112                 .description("hi")
113                 .icon2("icon2")
114                 .intentAction("VIEW").intentData("id1").build();
115 
116         final ComponentName otherName = new ComponentName("com.android.yo", "com.android.yo.Cat");
117         mCache.reportRefreshedShortcut(name, shortcutId, refreshed);
118         assertTrue("hasShortcutBeenRefreshed", mCache.hasShortcutBeenRefreshed(name, shortcutId));
119         assertFalse("hasShortcutBeenRefreshed should be false for same id but different source",
120                 mCache.hasShortcutBeenRefreshed(otherName, shortcutId));
121 
122     }
123 
124 }
125