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
17package com.android.exchange.utility;
18
19import com.android.emailcommon.provider.Account;
20import com.android.exchange.provider.EmailContentSetupUtils;
21
22import android.content.ContentResolver;
23import android.content.ContentUris;
24import android.content.Context;
25import android.test.AndroidTestCase;
26
27import java.util.ArrayList;
28
29public abstract class ExchangeTestCase extends AndroidTestCase {
30    private final ArrayList<Long> mCreatedAccountIds = new ArrayList<Long>();
31    protected Context mProviderContext;
32
33    @Override
34    public void setUp() throws Exception {
35        super.setUp();
36        mContext = getContext();
37        // Could use MockContext here if we switch over
38        mProviderContext = mContext;
39    }
40
41    @Override
42    public void tearDown() throws Exception {
43        super.tearDown();
44        ContentResolver resolver = mProviderContext.getContentResolver();
45        for (Long accountId: mCreatedAccountIds) {
46            resolver.delete(ContentUris.withAppendedId(Account.CONTENT_URI, accountId), null,
47                    null);
48        }
49    }
50
51    /**
52     * Add an account to our list of test accounts; we'll delete it automatically in tearDown()
53     * @param account the account to be added to our list of test accounts
54     */
55    protected void addTestAccount(Account account) {
56        if (account.mId > 0) {
57            mCreatedAccountIds.add(account.mId);
58        }
59    }
60
61    /**
62     * Create a test account that will be automatically deleted when the test is finished
63     * @param name the name of the account
64     * @param saveIt whether or not to save the account in EmailProvider
65     * @return the account created
66     */
67    protected Account setupTestAccount(String name, boolean saveIt) {
68        Account account = EmailContentSetupUtils.setupAccount(name, saveIt, mProviderContext);
69        addTestAccount(account);
70        return account;
71    }
72}
73