1 /* 2 * Copyright (C) 2012 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.providers.partnerbookmarks; 18 19 import android.content.ContentProviderClient; 20 import android.database.Cursor; 21 import android.test.AndroidTestCase; 22 import android.test.suitebuilder.annotation.SmallTest; 23 import android.util.Log; 24 25 import junit.framework.TestCase; 26 27 public class PartnerBookmarksProviderTest extends AndroidTestCase { 28 private static final String TAG = "PartnerBookmarksProviderTest"; 29 private static final long FIXED_ID_PARTNER_BOOKMARKS_ROOT = 30 PartnerBookmarksContract.Bookmarks.BOOKMARK_PARENT_ROOT_ID + 1; 31 private static final long NO_FOLDER_FILTER = -1; 32 countBookmarksInFolder(long folderFilter)33 private int countBookmarksInFolder(long folderFilter) throws android.os.RemoteException { 34 ContentProviderClient providerClient = 35 getContext().getContentResolver().acquireContentProviderClient( 36 PartnerBookmarksContract.Bookmarks.CONTENT_URI); 37 assertNotNull( 38 "Failed to acquire " + PartnerBookmarksContract.Bookmarks.CONTENT_URI, 39 providerClient); 40 Cursor cursor = null; 41 try { 42 cursor = providerClient.query(PartnerBookmarksContract.Bookmarks.CONTENT_URI, 43 null, null, null, null); 44 assertNotNull("Failed to query for bookmarks", cursor); 45 int bookmarksCount = 0; 46 while (cursor.moveToNext()) { 47 long id = cursor.getLong( 48 cursor.getColumnIndexOrThrow(PartnerBookmarksContract.Bookmarks.ID)); 49 long parent = cursor.getLong( 50 cursor.getColumnIndexOrThrow(PartnerBookmarksContract.Bookmarks.PARENT)); 51 boolean isFolder = cursor.getInt( 52 cursor.getColumnIndexOrThrow(PartnerBookmarksContract.Bookmarks.TYPE)) 53 == PartnerBookmarksContract.Bookmarks.BOOKMARK_TYPE_FOLDER; 54 String url = cursor.getString( 55 cursor.getColumnIndexOrThrow(PartnerBookmarksContract.Bookmarks.URL)); 56 String title = cursor.getString( 57 cursor.getColumnIndexOrThrow(PartnerBookmarksContract.Bookmarks.TITLE)); 58 assertFalse("bookmark id must be non-zero", id == 0); 59 assertFalse("url should not be empty if not a folder", !isFolder && url.isEmpty()); 60 assertFalse("title should never be empty", title.isEmpty()); 61 if (folderFilter == NO_FOLDER_FILTER || folderFilter == parent) { 62 bookmarksCount++; 63 } 64 } 65 return bookmarksCount; 66 } finally { 67 if (cursor != null) { 68 cursor.close(); 69 } 70 providerClient.release(); 71 } 72 } 73 74 @SmallTest testExactlyOneRoot()75 public void testExactlyOneRoot() throws android.os.RemoteException { 76 int totalBookmarks = countBookmarksInFolder(NO_FOLDER_FILTER); 77 if (totalBookmarks > 0) { 78 assertEquals("There must be at most one root", 79 countBookmarksInFolder( 80 PartnerBookmarksContract.Bookmarks.BOOKMARK_PARENT_ROOT_ID), 81 1); 82 } 83 } 84 85 @SmallTest testRootMustBeNonEmptyIfPresent()86 public void testRootMustBeNonEmptyIfPresent() throws android.os.RemoteException { 87 int totalBookmarks = countBookmarksInFolder(NO_FOLDER_FILTER); 88 if (totalBookmarks > 0) { 89 assertTrue("If the root exists, it must be non-empty", 90 countBookmarksInFolder(FIXED_ID_PARTNER_BOOKMARKS_ROOT) > 0); 91 } 92 } 93 94 @SmallTest testDefaultPBPSupportsOnlyFlatListOfBookmarks()95 public void testDefaultPBPSupportsOnlyFlatListOfBookmarks() 96 throws android.os.RemoteException { 97 int totalBookmarks = countBookmarksInFolder(NO_FOLDER_FILTER); 98 if (totalBookmarks > 0) { 99 assertEquals("Default PBP supports only flat list of bookmarks", 100 countBookmarksInFolder(FIXED_ID_PARTNER_BOOKMARKS_ROOT), 101 totalBookmarks - 1); 102 } 103 } 104 } 105