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.browser; 18 19 import com.android.browser.provider.BrowserProvider; 20 import com.android.browser.provider.BrowserProvider2; 21 import com.android.browser.tests.utils.ProviderTestCase3; 22 23 import android.app.SearchManager; 24 import android.content.ContentValues; 25 import android.database.Cursor; 26 import android.net.Uri; 27 import android.provider.BrowserContract; 28 import android.test.suitebuilder.annotation.MediumTest; 29 30 import java.util.ArrayList; 31 import java.util.Arrays; 32 33 /** 34 * Unit tests for {@link BrowserProvider}. 35 */ 36 @MediumTest 37 public class BrowserProviderTests extends ProviderTestCase3<BrowserProvider2> { 38 39 private ArrayList<Uri> mDeleteUris; 40 BrowserProviderTests()41 public BrowserProviderTests() { 42 super(BrowserProvider2.class, 43 BrowserContract.AUTHORITY, BrowserProvider2.LEGACY_AUTHORITY); 44 } 45 46 @Override setUp()47 protected void setUp() throws Exception { 48 mDeleteUris = new ArrayList<Uri>(); 49 super.setUp(); 50 } 51 52 @Override tearDown()53 protected void tearDown() throws Exception { 54 for (Uri uri : mDeleteUris) { 55 deleteUri(uri); 56 } 57 super.tearDown(); 58 } 59 testHasDefaultBookmarks()60 public void testHasDefaultBookmarks() { 61 Cursor c = getBookmarksSuggest(""); 62 try { 63 assertTrue("No default bookmarks", c.getCount() > 0); 64 } finally { 65 c.close(); 66 } 67 } 68 testPartialFirstTitleWord()69 public void testPartialFirstTitleWord() { 70 assertInsertQuery("http://www.example.com/rasdfe", "nfgjra sdfywe", "nfgj"); 71 } 72 testFullFirstTitleWord()73 public void testFullFirstTitleWord() { 74 assertInsertQuery("http://www.example.com/", "nfgjra dfger", "nfgjra"); 75 } 76 testFullFirstTitleWordPartialSecond()77 public void testFullFirstTitleWordPartialSecond() { 78 assertInsertQuery("http://www.example.com/", "nfgjra dfger", "nfgjra df"); 79 } 80 testFullTitle()81 public void testFullTitle() { 82 assertInsertQuery("http://www.example.com/", "nfgjra dfger", "nfgjra dfger"); 83 } 84 85 // Not implemented in BrowserProvider 86 // public void testFullSecondTitleWord() { 87 // assertInsertQuery("http://www.example.com/rasdfe", "nfgjra sdfywe", "sdfywe"); 88 // } 89 testFullTitleJapanese()90 public void testFullTitleJapanese() { 91 String title = "\u30ae\u30e3\u30e9\u30ea\u30fc\u30fcGoogle\u691c\u7d22"; 92 assertInsertQuery("http://www.example.com/sdaga", title, title); 93 } 94 testPartialTitleJapanese()95 public void testPartialTitleJapanese() { 96 String title = "\u30ae\u30e3\u30e9\u30ea\u30fc\u30fcGoogle\u691c\u7d22"; 97 String query = "\u30ae\u30e3\u30e9\u30ea\u30fc"; 98 assertInsertQuery("http://www.example.com/sdaga", title, query); 99 } 100 101 // Test for http://b/issue?id=2152749 testSoundmarkTitleJapanese()102 public void testSoundmarkTitleJapanese() { 103 String title = "\u30ae\u30e3\u30e9\u30ea\u30fc\u30fcGoogle\u691c\u7d22"; 104 String query = "\u30ad\u30e3\u30e9\u30ea\u30fc"; 105 assertInsertQuery("http://www.example.com/sdaga", title, query); 106 } 107 108 // 109 // Utilities 110 // 111 assertInsertQuery(String url, String title, String query)112 private void assertInsertQuery(String url, String title, String query) { 113 addBookmark(url, title); 114 assertQueryReturns(url, title, query); 115 } 116 assertQueryReturns(String url, String title, String query)117 private void assertQueryReturns(String url, String title, String query) { 118 Cursor c = getBookmarksSuggest(query); 119 try { 120 assertTrue(title + " not matched by " + query, c.getCount() > 0); 121 assertTrue("More than one result for " + query, c.getCount() == 1); 122 while (c.moveToNext()) { 123 String text1 = getCol(c, SearchManager.SUGGEST_COLUMN_TEXT_1); 124 assertNotNull(text1); 125 assertEquals("Bad title", title, text1); 126 String text2 = getCol(c, SearchManager.SUGGEST_COLUMN_TEXT_2); 127 assertNotNull(text2); 128 String data = getCol(c, SearchManager.SUGGEST_COLUMN_INTENT_DATA); 129 assertNotNull(data); 130 assertEquals("Bad URL", url, data); 131 } 132 } finally { 133 c.close(); 134 } 135 } 136 getBookmarksSuggest(String query)137 private Cursor getBookmarksSuggest(String query) { 138 Uri suggestUri = Uri.parse("content://browser/bookmarks/search_suggest_query"); 139 String[] selectionArgs = { query }; 140 Cursor c = getMockContentResolver().query(suggestUri, null, "url LIKE ?", 141 selectionArgs, null); 142 assertNotNull(c); 143 return c; 144 } 145 addBookmark(String url, String title)146 private void addBookmark(String url, String title) { 147 Uri uri = insertBookmark(url, title); 148 assertNotNull(uri); 149 assertFalse(android.provider.Browser.BOOKMARKS_URI.equals(uri)); 150 mDeleteUris.add(uri); 151 } 152 insertBookmark(String url, String title)153 private Uri insertBookmark(String url, String title) { 154 ContentValues values = new ContentValues(); 155 values.put("title", title); 156 values.put("url", url); 157 values.put("visits", 0); 158 values.put("date", 0); 159 values.put("created", 0); 160 values.put("bookmark", 1); 161 return getMockContentResolver().insert(android.provider.Browser.BOOKMARKS_URI, 162 values); 163 } 164 deleteUri(Uri uri)165 private void deleteUri(Uri uri) { 166 int count = getMockContentResolver().delete(uri, null, null); 167 assertEquals("Failed to delete " + uri, 1, count); 168 } 169 getCol(Cursor c, String name)170 private static String getCol(Cursor c, String name) { 171 int col = c.getColumnIndex(name); 172 String msg = "Column " + name + " not found, columns: " 173 + Arrays.toString(c.getColumnNames()); 174 assertTrue(msg, col >= 0); 175 return c.getString(col); 176 } 177 } 178