1 /* 2 * Copyright (C) 2011 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.tests; 18 19 import com.android.browser.tests.utils.BP2TestCaseHelper; 20 21 import android.content.ContentUris; 22 import android.content.ContentValues; 23 import android.database.Cursor; 24 import android.graphics.Bitmap; 25 import android.graphics.Bitmap.Config; 26 import android.net.Uri; 27 import android.provider.BrowserContract; 28 import android.provider.BrowserContract.Images; 29 import android.test.suitebuilder.annotation.SmallTest; 30 31 import java.io.ByteArrayOutputStream; 32 33 @SmallTest 34 public class BP2ProviderTests extends BP2TestCaseHelper { 35 36 static final String[] PROJECTION = new String[] { 37 BrowserContract.Bookmarks.PARENT, 38 BrowserContract.Bookmarks.ACCOUNT_NAME, 39 BrowserContract.Bookmarks.ACCOUNT_TYPE, 40 }; 41 static final int INDEX_PARENT = 0; 42 static final int INDEX_ACCOUNT_NAME = 1; 43 static final int INDEX_ACCOUNT_TYPE = 2; 44 45 public void testUpdateImage() { 46 String url = "http://stub1.com"; 47 insertBookmark(url, "stub 1"); 48 ContentValues values = new ContentValues(); 49 values.put(Images.URL, url); 50 Bitmap bitmap = Bitmap.createBitmap(1, 1, Config.ARGB_8888); 51 ByteArrayOutputStream os = new ByteArrayOutputStream(); 52 bitmap.compress(Bitmap.CompressFormat.PNG, 100, os); 53 values.put(Images.THUMBNAIL, os.toByteArray()); 54 // Use updateBookmarks because the bookmarks URI observer should 55 // be triggered, even though we aren't giving it a bookmarks URI 56 assertTrue(updateBookmark(Images.CONTENT_URI, values)); 57 } 58 59 public void testIsValidParentNullAccount() { 60 doTestIsValidParent(null, null); 61 } 62 63 public void testIsValidParentWithAccount() { 64 doTestIsValidParent("test (at) gmail.com", "com.google"); 65 } 66 67 private void doTestIsValidParent(String accountName, String accountType) { 68 // Create the folder 69 ContentValues values = new ContentValues(); 70 values.put(BrowserContract.Bookmarks.TITLE, "New Folder"); 71 values.put(BrowserContract.Bookmarks.IS_FOLDER, 1); 72 values.put(BrowserContract.Bookmarks.ACCOUNT_NAME, accountName); 73 values.put(BrowserContract.Bookmarks.ACCOUNT_TYPE, accountType); 74 Uri folderUri = insertBookmark(values); 75 assertNotNull(folderUri); 76 long folderId = ContentUris.parseId(folderUri); 77 assertTrue("Failed to parse folder id!", folderId > 0); 78 // Insert a bookmark with the same ACCOUNT_* info as parent 79 values.put(BrowserContract.Bookmarks.TITLE, "google"); 80 values.put(BrowserContract.Bookmarks.URL, "http://google.com"); 81 values.put(BrowserContract.Bookmarks.IS_FOLDER, 0); 82 values.put(BrowserContract.Bookmarks.PARENT, folderId); 83 Uri insertedUri = insertBookmark(values); 84 assertNotNull(insertedUri); 85 Cursor c = getMockContentResolver().query(insertedUri, 86 PROJECTION, null, null, null); 87 try { 88 assertNotNull(c); 89 assertTrue(c.moveToFirst()); 90 long insertedParentId = c.getLong(INDEX_PARENT); 91 String insertedAccountName = c.getString(INDEX_ACCOUNT_NAME); 92 String insertedAccountType = c.getString(INDEX_ACCOUNT_TYPE); 93 assertEquals(folderId, insertedParentId); 94 assertEquals(accountName, insertedAccountName); 95 assertEquals(accountType, insertedAccountType); 96 97 // Insert a bookmark with no ACCOUNT_* set, BUT with a valid parent 98 // The inserted should end up with the ACCOUNT_* of the parent 99 values.remove(BrowserContract.Bookmarks.ACCOUNT_NAME); 100 values.remove(BrowserContract.Bookmarks.ACCOUNT_TYPE); 101 insertedUri = insertBookmark(values); 102 assertNotNull(insertedUri); 103 c.close(); 104 c = getMockContentResolver().query(insertedUri, 105 PROJECTION, null, null, null); 106 assertNotNull(c); 107 assertTrue(c.moveToFirst()); 108 insertedParentId = c.getLong(INDEX_PARENT); 109 insertedAccountName = c.getString(INDEX_ACCOUNT_NAME); 110 insertedAccountType = c.getString(INDEX_ACCOUNT_TYPE); 111 assertEquals(folderId, insertedParentId); 112 assertEquals(accountName, insertedAccountName); 113 assertEquals(accountType, insertedAccountType); 114 115 // Insert a bookmark with a different ACCOUNT_* than it's parent 116 // ACCOUNT_* should override parent 117 accountName = accountName + "@something.else"; 118 accountType = "com.google"; 119 values.put(BrowserContract.Bookmarks.ACCOUNT_NAME, accountName); 120 values.put(BrowserContract.Bookmarks.ACCOUNT_TYPE, accountType); 121 insertedUri = insertBookmark(values); 122 assertNotNull(insertedUri); 123 c.close(); 124 c = getMockContentResolver().query(insertedUri, 125 PROJECTION, null, null, null); 126 assertNotNull(c); 127 assertTrue(c.moveToFirst()); 128 insertedParentId = c.getLong(INDEX_PARENT); 129 insertedAccountName = c.getString(INDEX_ACCOUNT_NAME); 130 insertedAccountType = c.getString(INDEX_ACCOUNT_TYPE); 131 assertNotSame(folderId, insertedParentId); 132 assertEquals(accountName, insertedAccountName); 133 assertEquals(accountType, insertedAccountType); 134 } finally { 135 c.close(); 136 } 137 } 138 } 139