OLD | NEW |
1 // Copyright 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 2012 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 package org.chromium.chrome.browser.provider; | 5 package org.chromium.chrome.browser.provider; |
6 | 6 |
7 import android.content.ContentValues; | 7 import android.content.ContentValues; |
8 import android.database.Cursor; | 8 import android.database.Cursor; |
9 import android.net.Uri; | 9 import android.net.Uri; |
10 import android.support.test.filters.MediumTest; | 10 import android.support.test.filters.MediumTest; |
11 | 11 |
| 12 import org.junit.After; |
| 13 import org.junit.Assert; |
| 14 import org.junit.Before; |
| 15 import org.junit.Rule; |
| 16 import org.junit.Test; |
| 17 import org.junit.runner.RunWith; |
| 18 |
| 19 import org.chromium.base.test.util.CommandLineFlags; |
12 import org.chromium.base.test.util.Feature; | 20 import org.chromium.base.test.util.Feature; |
13 import org.chromium.base.test.util.RetryOnFailure; | 21 import org.chromium.base.test.util.RetryOnFailure; |
| 22 import org.chromium.chrome.browser.ChromeSwitches; |
| 23 import org.chromium.chrome.test.ChromeActivityTestRule; |
| 24 import org.chromium.chrome.test.ChromeJUnit4ClassRunner; |
14 | 25 |
15 import java.util.Date; | 26 import java.util.Date; |
16 | 27 |
17 /** | 28 /** |
18 * Tests the use of the Searches URI as part of the Android provider public API. | 29 * Tests the use of the Searches URI as part of the Android provider public API. |
19 */ | 30 */ |
20 public class ProviderSearchesUriTest extends ProviderTestBase { | 31 @RunWith(ChromeJUnit4ClassRunner.class) |
| 32 @CommandLineFlags.Add({ |
| 33 ChromeSwitches.DISABLE_FIRST_RUN_EXPERIENCE, |
| 34 ChromeActivityTestRule.DISABLE_NETWORK_PREDICTION_FLAG, |
| 35 }) |
| 36 public class ProviderSearchesUriTest { |
| 37 @Rule |
| 38 public ProviderTestRule mProviderTestRule = new ProviderTestRule(); |
21 | 39 |
22 private Uri mSearchesUri; | 40 private Uri mSearchesUri; |
23 | 41 |
24 @Override | 42 @Before |
25 protected void setUp() throws Exception { | 43 public void setUp() throws Exception { |
26 super.setUp(); | 44 mSearchesUri = ChromeBrowserProvider.getSearchesApiUri(mProviderTestRule
.getActivity()); |
27 mSearchesUri = ChromeBrowserProvider.getSearchesApiUri(getActivity()); | 45 mProviderTestRule.getContentResolver().delete(mSearchesUri, null, null); |
28 getContentResolver().delete(mSearchesUri, null, null); | |
29 } | 46 } |
30 | 47 |
31 @Override | 48 @After |
32 protected void tearDown() throws Exception { | 49 public void tearDown() throws Exception { |
33 getContentResolver().delete(mSearchesUri, null, null); | 50 mProviderTestRule.getContentResolver().delete(mSearchesUri, null, null); |
34 super.tearDown(); | |
35 } | 51 } |
36 | 52 |
37 private Uri addSearchTerm(String searchTerm, long searchTime) { | 53 private Uri addSearchTerm(String searchTerm, long searchTime) { |
38 ContentValues values = new ContentValues(); | 54 ContentValues values = new ContentValues(); |
39 values.put(SearchColumns.SEARCH, searchTerm); | 55 values.put(SearchColumns.SEARCH, searchTerm); |
40 values.put(SearchColumns.DATE, searchTime); | 56 values.put(SearchColumns.DATE, searchTime); |
41 return getContentResolver().insert(mSearchesUri, values); | 57 return mProviderTestRule.getContentResolver().insert(mSearchesUri, value
s); |
42 } | 58 } |
43 | 59 |
| 60 @Test |
44 @MediumTest | 61 @MediumTest |
45 @Feature({"Android-ContentProvider"}) | 62 @Feature({"Android-ContentProvider"}) |
46 @RetryOnFailure | 63 @RetryOnFailure |
47 public void testAddSearchTerm() { | 64 public void testAddSearchTerm() { |
48 long searchTime = System.currentTimeMillis(); | 65 long searchTime = System.currentTimeMillis(); |
49 String searchTerm = "chrome"; | 66 String searchTerm = "chrome"; |
50 Uri uri = addSearchTerm(searchTerm, searchTime); | 67 Uri uri = addSearchTerm(searchTerm, searchTime); |
51 assertNotNull(uri); | 68 Assert.assertNotNull(uri); |
52 String[] selectionArgs = { searchTerm, String.valueOf(searchTime) }; | 69 String[] selectionArgs = { searchTerm, String.valueOf(searchTime) }; |
53 Cursor cursor = getContentResolver().query(uri, null, SearchColumns.SEAR
CH + "=? AND " | 70 Cursor cursor = mProviderTestRule.getContentResolver().query(uri, null, |
54 + SearchColumns.DATE + " = ? ", selectionArgs, null); | 71 SearchColumns.SEARCH + "=? AND " + SearchColumns.DATE + " = ? ",
selectionArgs, |
55 assertNotNull(cursor); | 72 null); |
56 assertEquals(1, cursor.getCount()); | 73 Assert.assertNotNull(cursor); |
57 assertTrue(cursor.moveToNext()); | 74 Assert.assertEquals(1, cursor.getCount()); |
| 75 Assert.assertTrue(cursor.moveToNext()); |
58 int index = cursor.getColumnIndex(SearchColumns.SEARCH); | 76 int index = cursor.getColumnIndex(SearchColumns.SEARCH); |
59 assertTrue(-1 != index); | 77 Assert.assertTrue(-1 != index); |
60 assertEquals(searchTerm, cursor.getString(index)); | 78 Assert.assertEquals(searchTerm, cursor.getString(index)); |
61 index = cursor.getColumnIndex(SearchColumns.DATE); | 79 index = cursor.getColumnIndex(SearchColumns.DATE); |
62 assertTrue(-1 != index); | 80 Assert.assertTrue(-1 != index); |
63 assertEquals(searchTime, cursor.getLong(index)); | 81 Assert.assertEquals(searchTime, cursor.getLong(index)); |
64 } | 82 } |
65 | 83 |
| 84 @Test |
66 @MediumTest | 85 @MediumTest |
67 @Feature({"Android-ContentProvider"}) | 86 @Feature({"Android-ContentProvider"}) |
68 public void testUpdateSearchTerm() { | 87 public void testUpdateSearchTerm() { |
69 long[] searchTime = { System.currentTimeMillis(), System.currentTimeMill
is() - 1000 }; | 88 long[] searchTime = { System.currentTimeMillis(), System.currentTimeMill
is() - 1000 }; |
70 String[] searchTerm = { "chrome", "chromium" }; | 89 String[] searchTerm = { "chrome", "chromium" }; |
71 Uri uri = addSearchTerm(searchTerm[0], searchTime[0]); | 90 Uri uri = addSearchTerm(searchTerm[0], searchTime[0]); |
72 ContentValues values = new ContentValues(); | 91 ContentValues values = new ContentValues(); |
73 values.put(SearchColumns.SEARCH, searchTerm[1]); | 92 values.put(SearchColumns.SEARCH, searchTerm[1]); |
74 values.put(SearchColumns.DATE, searchTime[1]); | 93 values.put(SearchColumns.DATE, searchTime[1]); |
75 getContentResolver().update(uri, values, null, null); | 94 mProviderTestRule.getContentResolver().update(uri, values, null, null); |
76 String[] selectionArgs = { searchTerm[0] }; | 95 String[] selectionArgs = { searchTerm[0] }; |
77 Cursor cursor = getContentResolver().query(mSearchesUri, null, SearchCol
umns.SEARCH + "=?", | 96 Cursor cursor = mProviderTestRule.getContentResolver().query( |
78 selectionArgs, null); | 97 mSearchesUri, null, SearchColumns.SEARCH + "=?", selectionArgs,
null); |
79 assertNotNull(cursor); | 98 Assert.assertNotNull(cursor); |
80 assertEquals(0, cursor.getCount()); | 99 Assert.assertEquals(0, cursor.getCount()); |
81 String[] selectionArgs1 = { searchTerm[1] }; | 100 String[] selectionArgs1 = { searchTerm[1] }; |
82 cursor = getContentResolver().query(mSearchesUri, null, SearchColumns.SE
ARCH + "=?", | 101 cursor = mProviderTestRule.getContentResolver().query( |
83 selectionArgs1, null); | 102 mSearchesUri, null, SearchColumns.SEARCH + "=?", selectionArgs1,
null); |
84 assertNotNull(cursor); | 103 Assert.assertNotNull(cursor); |
85 assertEquals(1, cursor.getCount()); | 104 Assert.assertEquals(1, cursor.getCount()); |
86 assertTrue(cursor.moveToNext()); | 105 Assert.assertTrue(cursor.moveToNext()); |
87 int index = cursor.getColumnIndex(SearchColumns.SEARCH); | 106 int index = cursor.getColumnIndex(SearchColumns.SEARCH); |
88 assertTrue(-1 != index); | 107 Assert.assertTrue(-1 != index); |
89 assertEquals(searchTerm[1], cursor.getString(index)); | 108 Assert.assertEquals(searchTerm[1], cursor.getString(index)); |
90 index = cursor.getColumnIndex(SearchColumns.DATE); | 109 index = cursor.getColumnIndex(SearchColumns.DATE); |
91 assertTrue(-1 != index); | 110 Assert.assertTrue(-1 != index); |
92 assertEquals(searchTime[1], cursor.getLong(index)); | 111 Assert.assertEquals(searchTime[1], cursor.getLong(index)); |
93 } | 112 } |
94 | 113 |
| 114 @Test |
95 @MediumTest | 115 @MediumTest |
96 @Feature({"Android-ContentProvider"}) | 116 @Feature({"Android-ContentProvider"}) |
97 @RetryOnFailure | 117 @RetryOnFailure |
98 public void testDeleteSearchTerm() { | 118 public void testDeleteSearchTerm() { |
99 long[] searchTime = { System.currentTimeMillis(), System.currentTimeMill
is() - 1000 }; | 119 long[] searchTime = { System.currentTimeMillis(), System.currentTimeMill
is() - 1000 }; |
100 String[] searchTerm = {"chrome", "chromium"}; | 120 String[] searchTerm = {"chrome", "chromium"}; |
101 Uri uri[] = new Uri[2]; | 121 Uri uri[] = new Uri[2]; |
102 for (int i = 0; i < uri.length; i++) { | 122 for (int i = 0; i < uri.length; i++) { |
103 uri[i] = addSearchTerm(searchTerm[i], searchTime[i]); | 123 uri[i] = addSearchTerm(searchTerm[i], searchTime[i]); |
104 } | 124 } |
105 getContentResolver().delete(uri[0], null, null); | 125 mProviderTestRule.getContentResolver().delete(uri[0], null, null); |
106 String[] selectionArgs = { searchTerm[0] }; | 126 String[] selectionArgs = { searchTerm[0] }; |
107 Cursor cursor = getContentResolver().query(mSearchesUri, null, SearchCol
umns.SEARCH + "=?", | 127 Cursor cursor = mProviderTestRule.getContentResolver().query( |
108 selectionArgs, null); | 128 mSearchesUri, null, SearchColumns.SEARCH + "=?", selectionArgs,
null); |
109 assertNotNull(cursor); | 129 Assert.assertNotNull(cursor); |
110 assertEquals(0, cursor.getCount()); | 130 Assert.assertEquals(0, cursor.getCount()); |
111 String[] selectionArgs1 = { searchTerm[1] }; | 131 String[] selectionArgs1 = { searchTerm[1] }; |
112 cursor = getContentResolver().query(mSearchesUri, null, SearchColumns.SE
ARCH + "=?", | 132 cursor = mProviderTestRule.getContentResolver().query( |
113 selectionArgs1, null); | 133 mSearchesUri, null, SearchColumns.SEARCH + "=?", selectionArgs1,
null); |
114 assertNotNull(cursor); | 134 Assert.assertNotNull(cursor); |
115 assertEquals(1, cursor.getCount()); | 135 Assert.assertEquals(1, cursor.getCount()); |
116 assertTrue(cursor.moveToNext()); | 136 Assert.assertTrue(cursor.moveToNext()); |
117 int index = cursor.getColumnIndex(SearchColumns.SEARCH); | 137 int index = cursor.getColumnIndex(SearchColumns.SEARCH); |
118 assertTrue(-1 != index); | 138 Assert.assertTrue(-1 != index); |
119 assertEquals(searchTerm[1], cursor.getString(index)); | 139 Assert.assertEquals(searchTerm[1], cursor.getString(index)); |
120 index = cursor.getColumnIndex(SearchColumns.DATE); | 140 index = cursor.getColumnIndex(SearchColumns.DATE); |
121 assertTrue(-1 != index); | 141 Assert.assertTrue(-1 != index); |
122 assertEquals(searchTime[1], cursor.getLong(index)); | 142 Assert.assertEquals(searchTime[1], cursor.getLong(index)); |
123 getContentResolver().delete(uri[1], null, null); | 143 mProviderTestRule.getContentResolver().delete(uri[1], null, null); |
124 cursor = getContentResolver().query(uri[1], null, null, null, null); | 144 cursor = mProviderTestRule.getContentResolver().query(uri[1], null, null
, null, null); |
125 assertNotNull(cursor); | 145 Assert.assertNotNull(cursor); |
126 assertEquals(0, cursor.getCount()); | 146 Assert.assertEquals(0, cursor.getCount()); |
127 } | 147 } |
128 | 148 |
129 // Copied from CTS test with minor adaptations. | 149 // Copied from CTS test with minor adaptations. |
| 150 @Test |
130 @MediumTest | 151 @MediumTest |
131 @Feature({"Android-ContentProvider"}) | 152 @Feature({"Android-ContentProvider"}) |
132 @RetryOnFailure | 153 @RetryOnFailure |
133 public void testSearchesTable() { | 154 public void testSearchesTable() { |
134 final int idIndex = 0; | 155 final int idIndex = 0; |
135 String insertSearch = "search_insert"; | 156 String insertSearch = "search_insert"; |
136 String updateSearch = "search_update"; | 157 String updateSearch = "search_update"; |
137 | 158 |
138 // Test: insert | 159 // Test: insert |
139 ContentValues value = new ContentValues(); | 160 ContentValues value = new ContentValues(); |
140 long createDate = new Date().getTime(); | 161 long createDate = new Date().getTime(); |
141 value.put(SearchColumns.SEARCH, insertSearch); | 162 value.put(SearchColumns.SEARCH, insertSearch); |
142 value.put(SearchColumns.DATE, createDate); | 163 value.put(SearchColumns.DATE, createDate); |
143 | 164 |
144 Uri insertUri = getContentResolver().insert(mSearchesUri, value); | 165 Uri insertUri = mProviderTestRule.getContentResolver().insert(mSearchesU
ri, value); |
145 Cursor cursor = getContentResolver().query(mSearchesUri, | 166 Cursor cursor = mProviderTestRule.getContentResolver().query(mSearchesUr
i, |
146 ChromeBrowserProvider.SEARCHES_PROJECTION, SearchColumns.SEARCH
+ " = ?", | 167 ChromeBrowserProvider.SEARCHES_PROJECTION, SearchColumns.SEARCH
+ " = ?", |
147 new String[] { insertSearch }, null); | 168 new String[] {insertSearch}, null); |
148 assertTrue(cursor.moveToNext()); | 169 Assert.assertTrue(cursor.moveToNext()); |
149 assertEquals(insertSearch, | 170 Assert.assertEquals(insertSearch, |
150 cursor.getString(ChromeBrowserProvider.SEARCHES_PROJECTION_SEARC
H_INDEX)); | 171 cursor.getString(ChromeBrowserProvider.SEARCHES_PROJECTION_SEARC
H_INDEX)); |
151 assertEquals(createDate, | 172 Assert.assertEquals( |
152 cursor.getLong(ChromeBrowserProvider.SEARCHES_PROJECTION_DATE_IN
DEX)); | 173 createDate, cursor.getLong(ChromeBrowserProvider.SEARCHES_PROJEC
TION_DATE_INDEX)); |
153 int id = cursor.getInt(idIndex); | 174 int id = cursor.getInt(idIndex); |
154 cursor.close(); | 175 cursor.close(); |
155 | 176 |
156 // Test: update | 177 // Test: update |
157 value.clear(); | 178 value.clear(); |
158 long updateDate = new Date().getTime(); | 179 long updateDate = new Date().getTime(); |
159 value.put(SearchColumns.SEARCH, updateSearch); | 180 value.put(SearchColumns.SEARCH, updateSearch); |
160 value.put(SearchColumns.DATE, updateDate); | 181 value.put(SearchColumns.DATE, updateDate); |
161 | 182 |
162 getContentResolver().update(mSearchesUri, value, | 183 mProviderTestRule.getContentResolver().update( |
163 SearchColumns.ID + " = " + id, null); | 184 mSearchesUri, value, SearchColumns.ID + " = " + id, null); |
164 cursor = getContentResolver().query(mSearchesUri, | 185 cursor = mProviderTestRule.getContentResolver().query(mSearchesUri, |
165 ChromeBrowserProvider.SEARCHES_PROJECTION, | 186 ChromeBrowserProvider.SEARCHES_PROJECTION, SearchColumns.ID + "
= " + id, null, |
166 SearchColumns.ID + " = " + id, null, null); | 187 null); |
167 assertTrue(cursor.moveToNext()); | 188 Assert.assertTrue(cursor.moveToNext()); |
168 assertEquals(updateSearch, | 189 Assert.assertEquals(updateSearch, |
169 cursor.getString(ChromeBrowserProvider.SEARCHES_PROJECTION_SEARC
H_INDEX)); | 190 cursor.getString(ChromeBrowserProvider.SEARCHES_PROJECTION_SEARC
H_INDEX)); |
170 assertEquals(updateDate, | 191 Assert.assertEquals( |
171 cursor.getLong(ChromeBrowserProvider.SEARCHES_PROJECTION_DATE_IN
DEX)); | 192 updateDate, cursor.getLong(ChromeBrowserProvider.SEARCHES_PROJEC
TION_DATE_INDEX)); |
172 assertEquals(id, cursor.getInt(idIndex)); | 193 Assert.assertEquals(id, cursor.getInt(idIndex)); |
173 | 194 |
174 // Test: delete | 195 // Test: delete |
175 getContentResolver().delete(insertUri, null, null); | 196 mProviderTestRule.getContentResolver().delete(insertUri, null, null); |
176 cursor = getContentResolver().query(mSearchesUri, | 197 cursor = mProviderTestRule.getContentResolver().query(mSearchesUri, |
177 ChromeBrowserProvider.SEARCHES_PROJECTION, | 198 ChromeBrowserProvider.SEARCHES_PROJECTION, SearchColumns.ID + "
= " + id, null, |
178 SearchColumns.ID + " = " + id, null, null); | 199 null); |
179 assertEquals(0, cursor.getCount()); | 200 Assert.assertEquals(0, cursor.getCount()); |
180 } | 201 } |
181 } | 202 } |
OLD | NEW |