Bernardo Rufino | 3184cc9 | 2018-01-20 16:54:58 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 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.testing.shadows; |
| 18 | |
| 19 | import dalvik.system.CloseGuard; |
| 20 | |
| 21 | import org.robolectric.annotation.Implements; |
| 22 | |
| 23 | @Implements(CloseGuard.class) |
| 24 | public class ShadowCloseGuard { |
| 25 | private static final Reporter REPORTER = new Reporter(); |
| 26 | |
| 27 | public static boolean hasReported() { |
| 28 | return REPORTER.mReports > 0; |
| 29 | } |
| 30 | |
| 31 | public static void setUp() { |
| 32 | // Can't do this in static {} block because shadow initialization is part of real class |
| 33 | // initialization and it happens right in the beginning. When the shadow is being |
| 34 | // initialized the class hasn't been initialized yet and it will be after the shadow. So, |
| 35 | // REPORTER field (inside CloseGuard) will be assigned *after* setReporter() is called. |
| 36 | CloseGuard.setReporter(REPORTER); |
| 37 | REPORTER.mReports = 0; |
| 38 | } |
| 39 | |
| 40 | private static class Reporter implements CloseGuard.Reporter { |
| 41 | private int mReports = 0; |
| 42 | |
| 43 | @Override |
| 44 | public void report(String message, Throwable allocationSite) { |
| 45 | mReports += 1; |
| 46 | } |
| 47 | } |
| 48 | } |