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
17package com.android.exchange.provider;
18
19import android.content.ContentProviderOperation;
20import android.content.ContentProviderResult;
21import android.content.ContentUris;
22import android.content.ContentValues;
23import android.content.Context;
24import android.content.OperationApplicationException;
25import android.database.Cursor;
26import android.net.Uri;
27import android.os.RemoteException;
28import android.test.ProviderTestCase2;
29import android.test.mock.MockContentResolver;
30import android.test.suitebuilder.annotation.SmallTest;
31
32import java.util.ArrayList;
33
34/**
35 * You can run this entire test case with:
36 *   runtest -c com.android.exchange.provider.MockProviderTests exchange
37 */
38
39// These tests won't run because MockProvider can't be built under the SDK
40
41@SmallTest
42public class MockProviderTests extends ProviderTestCase2<MockProvider> {
43    Context mMockContext;
44    //MockContentResolver mMockResolver;
45
46    private static final String CANHAZ_AUTHORITY = "com.android.canhaz";
47    private static final String PONY_TABLE = "pony";
48    private static final String PONY_COLUMN_NAME = "name";
49    private static final String PONY_COLUMN_TYPE = "type";
50    private static final String PONY_COLUMN_LEGS= "legs";
51    private static final String PONY_COLUMN_CAN_RIDE = "canRide";
52    private static final String[] PONY_PROJECTION = {MockProvider.ID_COLUMN, PONY_COLUMN_NAME,
53        PONY_COLUMN_TYPE, PONY_COLUMN_LEGS, PONY_COLUMN_CAN_RIDE};
54    private static final int PONY_ID = 0;
55    private static final int PONY_NAME = 1;
56    private static final int PONY_TYPE = 2;
57    private static final int PONY_LEGS = 3;
58    private static final int PONY_CAN_RIDE = 4;
59
60    public MockProviderTests() {
61        super(MockProvider.class, MockProvider.AUTHORITY);
62    }
63
64    @Override
65    public void setUp() throws Exception {
66        super.setUp();
67        mMockContext = getMockContext();
68        //mMockResolver = (MockContentResolver)mMockContext.getContentResolver();
69        //mMockResolver.addProvider(CANHAZ_AUTHORITY, new MockProvider(mMockContext));
70    }
71
72    @Override
73    public void tearDown() throws Exception {
74        super.tearDown();
75    }
76
77    private ContentValues ponyValues(String name, String type, int legs, boolean canRide) {
78        ContentValues cv = new ContentValues();
79        cv.put(PONY_COLUMN_NAME, name);
80        cv.put(PONY_COLUMN_TYPE, type);
81        cv.put(PONY_COLUMN_LEGS, legs);
82        cv.put(PONY_COLUMN_CAN_RIDE, canRide ? 1 : 0);
83        return cv;
84    }
85
86//    private ContentProviderResult[] setupPonies() throws RemoteException,
87//            OperationApplicationException {
88//        // The Uri is content://com.android.canhaz/pony
89//        Uri uri = new Uri.Builder().scheme("content").authority(CANHAZ_AUTHORITY)
90//            .path(PONY_TABLE).build();
91//        // Our array of CPO's to be used with applyBatch
92//        ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
93//
94//        // Insert two ponies
95//        ContentValues pony1 = ponyValues("Flicka", "wayward", 4, true);
96//        ops.add(ContentProviderOperation.newInsert(uri).withValues(pony1).build());
97//        ContentValues pony2 = ponyValues("Elise", "dastardly", 3, false);
98//        ops.add(ContentProviderOperation.newInsert(uri).withValues(pony2).build());
99//        // Apply the batch with one insert operation
100//        return mMockResolver.applyBatch(MockProvider.AUTHORITY, ops);
101//    }
102
103    private Uri getPonyUri() {
104        return new Uri.Builder().scheme("content").authority(CANHAZ_AUTHORITY)
105            .path(PONY_TABLE).build();
106    }
107
108//    public void testInsertQueryandDelete() throws RemoteException, OperationApplicationException {
109//        // The Uri is content://com.android.canhaz/pony
110//        ContentProviderResult[] results = setupPonies();
111//        Uri uri = getPonyUri();
112//
113//        // Check the results
114//        assertNotNull(results);
115//        assertEquals(2, results.length);
116//        // Make sure that we've created matcher entries for pony and pony/#
117//        assertEquals(MockProvider.TABLE, MockProvider.sURIMatcher.match(MockProvider.uri(uri)));
118//        assertEquals(MockProvider.RECORD,
119//                MockProvider.sURIMatcher.match(MockProvider.uri(results[0].uri)));
120//        Cursor c = mMockResolver.query(MockProvider.uri(uri), PONY_PROJECTION, null, null, null);
121//        assertNotNull(c);
122//        assertEquals(2, c.getCount());
123//        long eliseId = -1;
124//        long flickaId = -1;
125//        while (c.moveToNext()) {
126//            String name = c.getString(PONY_NAME);
127//            if ("Flicka".equals(name)) {
128//                assertEquals("Flicka", c.getString(PONY_NAME));
129//                assertEquals("wayward", c.getString(PONY_TYPE));
130//                assertEquals(4, c.getInt(PONY_LEGS));
131//                assertEquals(1, c.getInt(PONY_CAN_RIDE));
132//                flickaId = c.getLong(PONY_ID);
133//            } else if ("Elise".equals(name)) {
134//                assertEquals("dastardly", c.getString(PONY_TYPE));
135//                assertEquals(3, c.getInt(PONY_LEGS));
136//                assertEquals(0, c.getInt(PONY_CAN_RIDE));
137//                eliseId = c.getLong(PONY_ID);
138//            } else {
139//                fail("Wrong record: " + name);
140//            }
141//        }
142//
143//        // eliseId and flickaId should have been set
144//        assertNotSame(-1, eliseId);
145//        assertNotSame(-1, flickaId);
146//        // Delete the elise record
147//        assertEquals(1, mMockResolver.delete(ContentUris.withAppendedId(MockProvider.uri(uri),
148//                eliseId), null, null));
149//        c = mMockResolver.query(MockProvider.uri(uri), PONY_PROJECTION, null, null, null);
150//        assertNotNull(c);
151//        // There should be one left (Flicka)
152//        assertEquals(1, c.getCount());
153//        assertTrue(c.moveToNext());
154//        assertEquals("Flicka", c.getString(PONY_NAME));
155//    }
156//
157//    public void testUpdate() throws RemoteException, OperationApplicationException {
158//        // The Uri is content://com.android.canhaz/pony
159//        Uri uri = getPonyUri();
160//        setupPonies();
161//        Cursor c = mMockResolver.query(MockProvider.uri(uri), PONY_PROJECTION, null, null, null);
162//        assertNotNull(c);
163//        assertEquals(2, c.getCount());
164//        // Give all the ponies 5 legs
165//        ContentValues cv = new ContentValues();
166//        cv.put(PONY_COLUMN_LEGS, 5);
167//        assertEquals(2, mMockResolver.update(MockProvider.uri(uri), cv, null, null));
168//        c = mMockResolver.query(MockProvider.uri(uri), PONY_PROJECTION, null, null, null);
169//        assertNotNull(c);
170//        // We should still have two records, and each should have 5 legs, but otherwise be the same
171//        assertEquals(2, c.getCount());
172//        long eliseId = -1;
173//        long flickaId = -1;
174//        while (c.moveToNext()) {
175//            String name = c.getString(PONY_NAME);
176//            if ("Flicka".equals(name)) {
177//                assertEquals("Flicka", c.getString(PONY_NAME));
178//                assertEquals("wayward", c.getString(PONY_TYPE));
179//                assertEquals(5, c.getInt(PONY_LEGS));
180//                assertEquals(1, c.getInt(PONY_CAN_RIDE));
181//                flickaId = c.getLong(PONY_ID);
182//            } else if ("Elise".equals(name)) {
183//                assertEquals("dastardly", c.getString(PONY_TYPE));
184//                assertEquals(5, c.getInt(PONY_LEGS));
185//                assertEquals(0, c.getInt(PONY_CAN_RIDE));
186//                eliseId = c.getLong(PONY_ID);
187//            } else {
188//                fail("Wrong record: " + name);
189//            }
190//        }
191//        // eliseId and flickaId should have been set
192//        assertNotSame(-1, eliseId);
193//        assertNotSame(-1, flickaId);
194//    }
195}
196