• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
17 package com.android.providers.applications;
18 
19 import android.content.pm.PackageManager;
20 import android.os.Handler;
21 import android.os.Looper;
22 import android.os.Message;
23 
24 import com.android.internal.os.PkgUsageStats;
25 
26 import java.util.ArrayList;
27 import java.util.HashMap;
28 import java.util.List;
29 import java.util.Map;
30 
31 /**
32  * An extension of {@link ApplicationsProvider} that makes its testing easier.
33  */
34 public class ApplicationsProviderForTesting extends ApplicationsProvider {
35 
36     private PackageManager mMockPackageManager;
37 
38     private MockActivityManager mMockActivityManager;
39 
40     private boolean mHasGlobalSearchPermission;
41 
42     private MockUpdateHandler mMockUpdateHandler;
43 
44     @Override
getPackageManager()45     protected PackageManager getPackageManager() {
46         return mMockPackageManager;
47     }
48 
setMockPackageManager(PackageManager mockPackageManager)49     protected void setMockPackageManager(PackageManager mockPackageManager) {
50         mMockPackageManager = mockPackageManager;
51     }
52 
53     @Override
fetchUsageStats()54     protected Map<String, PkgUsageStats> fetchUsageStats() {
55         Map<String, PkgUsageStats> stats = new HashMap<String, PkgUsageStats>();
56         for (PkgUsageStats pus : mMockActivityManager.getAllPackageUsageStats()) {
57             stats.put(pus.packageName, pus);
58         }
59         return stats;
60     }
61 
setMockActivityManager(MockActivityManager mockActivityManager)62     protected void setMockActivityManager(MockActivityManager mockActivityManager) {
63         mMockActivityManager = mockActivityManager;
64     }
65 
setHasGlobalSearchPermission(boolean hasGlobalSearchPermission)66     protected void setHasGlobalSearchPermission(boolean hasGlobalSearchPermission) {
67         mHasGlobalSearchPermission = hasGlobalSearchPermission;
68     }
69 
70     @Override
hasGlobalSearchPermission()71     protected boolean hasGlobalSearchPermission() {
72         return mHasGlobalSearchPermission;
73     }
74 
75     @Override
createHandler(Looper looper)76     Handler createHandler(Looper looper) {
77         mMockUpdateHandler = new MockUpdateHandler(looper);
78         return mMockUpdateHandler;
79     }
80 
dispatchNextMessage()81     public boolean dispatchNextMessage() {
82         return mMockUpdateHandler.dispatchNextMessage();
83     }
84 
85     private class MockUpdateHandler extends UpdateHandler {
86 
87         List<Message> mMessages = new ArrayList<Message>();
88 
MockUpdateHandler(Looper looper)89         MockUpdateHandler(Looper looper) {
90             super(looper);
91         }
92 
93         @Override
sendMessageAtTime(Message msg, long uptimeMillis)94         public boolean sendMessageAtTime(Message msg, long uptimeMillis) {
95             mMessages.add(msg);
96             return true;
97         }
98 
dispatchNextMessage()99         public boolean dispatchNextMessage() {
100             if (!mMessages.isEmpty()) {
101                 dispatchMessage(mMessages.remove(0));
102                 return true;
103             } else {
104                 return false;
105             }
106         }
107     }
108 }
109