blob: b9d188a3871a3bac3e21920f7dc973fd3891c826 [file] [log] [blame]
Jason Monk1d9632d2017-02-09 13:20:04 -08001/*
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
15package com.android.systemui.utils.leaks;
16
Jason Monk340b0e52017-03-08 14:57:56 -050017import android.testing.LeakCheck;
18import android.testing.LeakCheck.Tracker;
Jason Monk1d9632d2017-02-09 13:20:04 -080019
20import com.android.systemui.statusbar.policy.ExtensionController;
21
22import java.util.function.Consumer;
23import java.util.function.Supplier;
24
25public class FakeExtensionController implements ExtensionController {
26
27 private final Tracker mTracker;
28
Jason Monk340b0e52017-03-08 14:57:56 -050029 public FakeExtensionController(LeakCheck test) {
Jason Monk1d9632d2017-02-09 13:20:04 -080030 mTracker = test.getTracker("extension");
31 }
32
33 @Override
34 public <T> ExtensionBuilder<T> newExtension(Class<T> cls) {
35 final Object o = new Object();
36 mTracker.getLeakInfo(o).addAllocation(new Throwable());
37 return new FakeExtensionBuilder(o);
38 }
39
40 private class FakeExtensionBuilder<T> implements ExtensionBuilder<T> {
41 private final Object mAllocation;
42
43 public FakeExtensionBuilder(Object o) {
44 mAllocation = o;
45 }
46
47 @Override
48 public ExtensionBuilder<T> withTunerFactory(TunerFactory<T> factory) {
49 return this;
50 }
51
52 @Override
53 public <P extends T> ExtensionBuilder<T> withPlugin(Class<P> cls) {
54 return this;
55 }
56
57 @Override
58 public <P extends T> ExtensionBuilder<T> withPlugin(Class<P> cls, String action) {
59 return this;
60 }
61
62 @Override
63 public <P> ExtensionBuilder<T> withPlugin(Class<P> cls, String action, PluginConverter<T, P> converter) {
64 return this;
65 }
66
67 @Override
68 public ExtensionBuilder<T> withDefault(Supplier<T> def) {
69 return this;
70 }
71
72 @Override
73 public ExtensionBuilder<T> withCallback(Consumer<T> callback) {
74 return this;
75 }
76
77 @Override
78 public Extension build() {
79 return new FakeExtension(mAllocation);
80 }
81 }
82
83 private class FakeExtension<T> implements Extension<T> {
84 private final Object mAllocation;
85
86 public FakeExtension(Object allocation) {
87 mAllocation = allocation;
88 }
89
90 @Override
91 public T get() {
92 // TODO: Support defaults or things.
93 return null;
94 }
95
96 @Override
97 public void destroy() {
98 mTracker.getLeakInfo(mAllocation).clearAllocations();
99 }
100 }
101}