blob: 4b0c049e4cf5006ec90e5a01c3bd3d630d7ed9e0 [file] [log] [blame]
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package org.chromium.base.metrics.test;
import android.util.Pair;
import org.robolectric.annotation.Implementation;
import org.robolectric.annotation.Implements;
import org.robolectric.annotation.Resetter;
import org.chromium.base.metrics.RecordHistogram;
import java.util.HashMap;
import java.util.concurrent.TimeUnit;
/**
* Implementation of RecordHistogram which does not rely on native and still enables testing of
* histogram counts.
*/
@Implements(RecordHistogram.class)
public class ShadowRecordHistogram {
private static HashMap<Pair<String, Integer>, Integer> sSamples =
new HashMap<Pair<String, Integer>, Integer>();
@Resetter
public static void reset() {
sSamples.clear();
}
@Implementation
public static void recordCountHistogram(String name, int sample) {
Pair<String, Integer> key = Pair.create(name, sample);
incrementSampleCount(key);
}
@Implementation
public static void recordCount100Histogram(String name, int sample) {
Pair<String, Integer> key = Pair.create(name, sample);
incrementSampleCount(key);
}
@Implementation
public static void recordEnumeratedHistogram(String name, int sample, int boundary) {
assert sample < boundary : "Sample " + sample + " is not within boundary " + boundary + "!";
incrementSampleCount(Pair.create(name, sample));
}
@Implementation
public static void recordLongTimesHistogram100(String name, long duration, TimeUnit timeUnit) {
Pair<String, Integer> key = Pair.create(name, (int) timeUnit.toMillis(duration));
incrementSampleCount(key);
}
@Implementation
public static int getHistogramValueCountForTesting(String name, int sample) {
Integer i = sSamples.get(Pair.create(name, sample));
return (i != null) ? i : 0;
}
private static void incrementSampleCount(Pair<String, Integer> key) {
Integer i = sSamples.get(key);
if (i == null) {
i = 0;
}
sSamples.put(key, i + 1);
}
}