Home | History | Annotate | Download | only in carrierdefaultapp
      1 /*
      2  * Copyright (C) 2016 Google Inc.
      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 package com.android.carrierdefaultapp;
     17 
     18 import android.content.Context;
     19 import android.content.ContextWrapper;
     20 import android.util.Log;
     21 
     22 import java.util.HashMap;
     23 
     24 public class TestContext extends ContextWrapper {
     25 
     26     private final String TAG = this.getClass().getSimpleName();
     27 
     28     private HashMap<String, Object> mInjectedSystemServices = new HashMap<>();
     29 
     30     public TestContext(Context base) {
     31         super(base);
     32     }
     33 
     34     public <S> void injectSystemService(Class<S> cls, S service) {
     35         final String name = getSystemServiceName(cls);
     36         mInjectedSystemServices.put(name, service);
     37     }
     38 
     39     @Override
     40     public Context getApplicationContext() {
     41         return this;
     42     }
     43 
     44     @Override
     45     public Object getSystemService(String name) {
     46         if (mInjectedSystemServices.containsKey(name)) {
     47             Log.d(TAG, "return mocked system service for " + name);
     48             return mInjectedSystemServices.get(name);
     49         }
     50         Log.d(TAG, "return real system service for " + name);
     51         return super.getSystemService(name);
     52     }
     53 
     54     public static void waitForMs(long ms) {
     55         try {
     56             Thread.sleep(ms);
     57         } catch (InterruptedException e) {
     58         }
     59     }
     60 }
     61