• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2024 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.server.appsearch.contactsindexer;
18 
19 import android.annotation.NonNull;
20 import android.app.job.JobScheduler;
21 import android.content.ContentResolver;
22 import android.content.Context;
23 import android.content.ContextWrapper;
24 import android.content.pm.ProviderInfo;
25 import android.os.UserHandle;
26 import android.test.mock.MockContentProvider;
27 import android.test.mock.MockContentResolver;
28 
29 import androidx.test.core.app.ApplicationProvider;
30 
31 import org.junit.After;
32 import org.junit.Before;
33 
34 import javax.annotation.Nullable;
35 
36 /**
37  * Test base that sets up a test context that directs CP2 queries to a {@link MockContentResolver}
38  * and {@link FakeContactsProvider}. Note that notifications are not broadcast, so Contacts Indexer
39  * updates will not occur automatically in response to inserts and deletes.
40  *
41  * <p>The test context also allows setting a {@link JobScheduler}.
42  */
43 abstract class FakeContactsProviderTestBase {
44     protected TestContext mContext;
45     protected FakeContactsProvider mFakeContactsProvider;
46     protected MockContentResolver mMockContentResolver;
47 
48     @Before
setUp()49     public void setUp() throws Exception {
50         Context context = ApplicationProvider.getApplicationContext();
51         mContext = new TestContext(context);
52         mMockContentResolver = new MockContentResolver(context);
53         mFakeContactsProvider = new FakeContactsProvider();
54         ProviderInfo providerInfo = new ProviderInfo();
55         providerInfo.authority = FakeContactsProvider.AUTHORITY;
56         MockContentProvider.attachInfoForTesting(mFakeContactsProvider, context, providerInfo);
57         mMockContentResolver.addProvider(FakeContactsProvider.AUTHORITY,
58                 mFakeContactsProvider);
59     }
60 
61     @After
tearDown()62     public void tearDown() throws Exception {
63         mFakeContactsProvider.shutdown();
64     }
65 
66     class TestContext extends ContextWrapper {
67         @Nullable
68         JobScheduler mJobScheduler;
69 
TestContext(Context base)70         TestContext(Context base) {
71             super(base);
72         }
73 
74         @Override
getContentResolver()75         public ContentResolver getContentResolver() {
76             return mMockContentResolver;
77         }
78 
79         @Override
80         @Nullable
getSystemService(String name)81         public Object getSystemService(String name) {
82             if (mJobScheduler != null && Context.JOB_SCHEDULER_SERVICE.equals(name)) {
83                 return mJobScheduler;
84             }
85             return getBaseContext().getSystemService(name);
86         }
87 
setJobScheduler(@ullable JobScheduler jobScheduler)88         public void setJobScheduler(@Nullable JobScheduler jobScheduler) {
89             mJobScheduler = jobScheduler;
90         }
91 
92         @Override
getApplicationContext()93         public Context getApplicationContext() {
94             return this;
95         }
96 
97         @Override
98         @NonNull
createContextAsUser(UserHandle user, int flags)99         public Context createContextAsUser(UserHandle user, int flags) {
100             return this;
101         }
102     }
103 }
104