1 /*
2  * Copyright (C) 2015 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.messaging;
18 
19 import android.content.ComponentName;
20 import android.content.ContentProvider;
21 import android.content.ContentResolver;
22 import android.content.Context;
23 import android.content.Intent;
24 import android.net.Uri;
25 import android.test.RenamingDelegatingContext;
26 import android.test.mock.MockContentResolver;
27 import android.util.Log;
28 
29 import java.util.ArrayList;
30 
31 public class FakeContext extends RenamingDelegatingContext {
32     private static final String TAG = "FakeContext";
33 
34     public interface FakeContextHost {
getServiceClassName()35         public String getServiceClassName();
startServiceForStub(Intent intent)36         public void startServiceForStub(Intent intent);
onStartCommandForStub(Intent intent, int flags, int startid)37         public void onStartCommandForStub(Intent intent, int flags, int startid);
38     }
39 
40     ArrayList<Intent> mStartedIntents;
41     boolean mServiceStarted = false;
42     private final FakeContextHost mService;
43     private final MockContentResolver mContentResolver;
44 
45 
FakeContext(final Context context, final FakeContextHost service)46     public FakeContext(final Context context, final FakeContextHost service) {
47         super(context, "test_");
48         mService = service;
49         mStartedIntents = new ArrayList<Intent>();
50         mContentResolver = new MockContentResolver();
51     }
52 
FakeContext(final Context context)53     public FakeContext(final Context context) {
54         this(context, null);
55     }
56 
extractIntents()57     public ArrayList<Intent> extractIntents() {
58         final ArrayList<Intent> intents = mStartedIntents;
59         mStartedIntents = new ArrayList<Intent>();
60         return intents;
61     }
62 
63     @Override
startService(final Intent intent)64     public ComponentName startService(final Intent intent) {
65         // Record that a startService occurred with the intent that was passed.
66         Log.d(TAG, "MockContext receiving startService. intent=" + intent.toString());
67         mStartedIntents.add(intent);
68         if (mService == null) {
69             return super.startService(intent);
70         } else if (intent.getComponent() != null &&
71                 intent.getComponent().getClassName().equals(mService.getServiceClassName())) {
72             if (!mServiceStarted) {
73                 Log.d(TAG, "MockContext first start service.");
74                 mService.startServiceForStub(intent);
75             } else {
76                 Log.d(TAG, "MockContext not first start service. Calling onStartCommand.");
77                 mService.onStartCommandForStub(intent, 0, 0);
78             }
79             mServiceStarted = true;
80             return new ComponentName(this, intent.getComponent().getClassName());
81         }
82         return null;
83     }
84 
85     @Override
getContentResolver()86     public ContentResolver getContentResolver() {
87         // If you want to use a content provider in your test, then you need to add it
88         // explicitly.
89         return mContentResolver;
90     }
91 
addContentProvider(final String name, final ContentProvider provider)92     public void addContentProvider(final String name, final ContentProvider provider) {
93         mContentResolver.addProvider(name, provider);
94     }
95 
addDefaultProvider(final Context context, final Uri uri)96     public void addDefaultProvider(final Context context, final Uri uri) {
97         final FakeContentProvider provider = new FakeContentProvider(context, uri, true);
98         mContentResolver.addProvider(uri.getAuthority(), provider);
99     }
100 
101 }
102