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.contacts;
18 
19 import android.content.ContentValues;
20 import android.content.Context;
21 import android.database.Cursor;
22 import android.util.Log;
23 
24 import junit.framework.Assert;
25 
26 import java.io.File;
27 import java.io.FileOutputStream;
28 import java.io.IOException;
29 
30 public class TestUtils {
TestUtils()31     private TestUtils() {
32     }
33 
34     /** Convenient method to create a ContentValues */
cv(Object... namesAndValues)35     public static ContentValues cv(Object... namesAndValues) {
36         Assert.assertTrue((namesAndValues.length % 2) == 0);
37 
38         final ContentValues ret = new ContentValues();
39         for (int i = 1; i < namesAndValues.length; i += 2) {
40             final String name = namesAndValues[i - 1].toString();
41             final Object value =  namesAndValues[i];
42             if (value == null) {
43                 ret.putNull(name);
44             } else if (value instanceof String) {
45                 ret.put(name, (String) value);
46             } else if (value instanceof Integer) {
47                 ret.put(name, (Integer) value);
48             } else if (value instanceof Long) {
49                 ret.put(name, (Long) value);
50             } else {
51                 Assert.fail("Unsupported type: " + value.getClass().getSimpleName());
52             }
53         }
54         return ret;
55     }
56 
57     /**
58      * Writes the content of a cursor to the log.
59      */
dumpCursor(Cursor c)60     public static final void dumpCursor(Cursor c) {
61         final String TAG = "contacts";
62 
63         final StringBuilder sb = new StringBuilder();
64         for (int i = 0; i < c.getColumnCount(); i++) {
65             if (sb.length() > 0) sb.append("|");
66             sb.append(c.getColumnName(i));
67         }
68         Log.i(TAG, sb.toString());
69 
70         c.moveToPosition(-1);
71         while (c.moveToNext()) {
72             sb.setLength(0);
73             for (int i = 0; i < c.getColumnCount(); i++) {
74                 if (sb.length() > 0) sb.append("|");
75 
76                 if (c.getType(i) == Cursor.FIELD_TYPE_BLOB) {
77                     byte[] blob = c.getBlob(i);
78                     sb.append("([blob] ");
79                     sb.append(blob == null ? "null" : blob.length + "b");
80                     sb.append(")");
81                 } else {
82                     sb.append(c.getString(i));
83                 }
84             }
85             Log.i(TAG, sb.toString());
86         }
87     }
88 
89     /**
90      * Writes an arbitrary byte array to the test apk's cache directory.
91      */
dumpToCacheDir(Context context, String prefix, String suffix, byte[] data)92     public static final String dumpToCacheDir(Context context, String prefix, String suffix,
93             byte[] data) {
94         try {
95             File file = File.createTempFile(prefix, suffix, context.getCacheDir());
96             FileOutputStream fos = new FileOutputStream(file);
97             fos.write(data);
98             fos.close();
99             return file.getAbsolutePath();
100         } catch (IOException e) {
101             return "[Failed to write to file: " + e.getMessage() + "]";
102         }
103     }
104 }
105