Home | History | Annotate | Download | only in leaks
      1 /*
      2  * Copyright (C) 2017 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
      5  * except in compliance with the License. You may obtain a copy of the License at
      6  *
      7  *      http://www.apache.org/licenses/LICENSE-2.0
      8  *
      9  * Unless required by applicable law or agreed to in writing, software distributed under the
     10  * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
     11  * KIND, either express or implied. See the License for the specific language governing
     12  * permissions and limitations under the License.
     13  */
     14 
     15 package com.android.systemui.utils.leaks;
     16 
     17 import android.content.Context;
     18 import android.testing.LeakCheck;
     19 import android.testing.LeakCheck.Tracker;
     20 
     21 import com.android.systemui.statusbar.policy.ExtensionController;
     22 
     23 import java.util.function.Consumer;
     24 import java.util.function.Supplier;
     25 
     26 public class FakeExtensionController implements ExtensionController {
     27 
     28     private final Tracker mTracker;
     29 
     30     public FakeExtensionController(LeakCheck test) {
     31         mTracker = test.getTracker("extension");
     32     }
     33 
     34     @Override
     35     public <T> ExtensionBuilder<T> newExtension(Class<T> cls) {
     36         final Object o = new Object();
     37         mTracker.getLeakInfo(o).addAllocation(new Throwable());
     38         return new FakeExtensionBuilder(o);
     39     }
     40 
     41     private class FakeExtensionBuilder<T> implements ExtensionBuilder<T> {
     42         private final Object mAllocation;
     43 
     44         public FakeExtensionBuilder(Object o) {
     45             mAllocation = o;
     46         }
     47 
     48         @Override
     49         public ExtensionBuilder<T> withTunerFactory(TunerFactory<T> factory) {
     50             return this;
     51         }
     52 
     53         @Override
     54         public <P extends T> ExtensionBuilder<T> withPlugin(Class<P> cls) {
     55             return this;
     56         }
     57 
     58         @Override
     59         public <P extends T> ExtensionBuilder<T> withPlugin(Class<P> cls, String action) {
     60             return this;
     61         }
     62 
     63         @Override
     64         public <P> ExtensionBuilder<T> withPlugin(Class<P> cls, String action, PluginConverter<T, P> converter) {
     65             return this;
     66         }
     67 
     68         @Override
     69         public ExtensionBuilder<T> withDefault(Supplier<T> def) {
     70             return this;
     71         }
     72 
     73         @Override
     74         public ExtensionBuilder<T> withCallback(Consumer<T> callback) {
     75             return this;
     76         }
     77 
     78         @Override
     79         public ExtensionBuilder<T> withUiMode(int mode, Supplier<T> def) {
     80             return null;
     81         }
     82 
     83         @Override
     84         public ExtensionBuilder<T> withFeature(String feature, Supplier<T> def) {
     85             return null;
     86         }
     87 
     88         @Override
     89         public Extension build() {
     90             return new FakeExtension(mAllocation);
     91         }
     92     }
     93 
     94     private class FakeExtension<T> implements Extension<T> {
     95         private final Object mAllocation;
     96 
     97         public FakeExtension(Object allocation) {
     98             mAllocation = allocation;
     99         }
    100 
    101         @Override
    102         public T get() {
    103             // TODO: Support defaults or things.
    104             return null;
    105         }
    106 
    107         @Override
    108         public void clearItem(boolean isDestroyed) {
    109 
    110         }
    111 
    112         @Override
    113         public Context getContext() {
    114             return null;
    115         }
    116 
    117         @Override
    118         public void destroy() {
    119             mTracker.getLeakInfo(mAllocation).clearAllocations();
    120         }
    121 
    122         @Override
    123         public void addCallback(Consumer<T> callback) {
    124         }
    125 
    126         public T reload() {
    127             return null;
    128         }
    129     }
    130 }
    131