| Index: components/background_task_scheduler/android/junit/src/org/chromium/components/background_task_scheduler/BackgroundTaskSchedulerUmaTest.java
|
| diff --git a/components/background_task_scheduler/android/junit/src/org/chromium/components/background_task_scheduler/BackgroundTaskSchedulerUmaTest.java b/components/background_task_scheduler/android/junit/src/org/chromium/components/background_task_scheduler/BackgroundTaskSchedulerUmaTest.java
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..bb8dec2152a4196bbad36557650afd19822b077f
|
| --- /dev/null
|
| +++ b/components/background_task_scheduler/android/junit/src/org/chromium/components/background_task_scheduler/BackgroundTaskSchedulerUmaTest.java
|
| @@ -0,0 +1,184 @@
|
| +// 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.components.background_task_scheduler;
|
| +
|
| +import static org.junit.Assert.assertEquals;
|
| +import static org.junit.Assert.assertTrue;
|
| +import static org.mockito.Mockito.anyInt;
|
| +import static org.mockito.Mockito.anyString;
|
| +import static org.mockito.Mockito.doNothing;
|
| +import static org.mockito.Mockito.eq;
|
| +import static org.mockito.Mockito.times;
|
| +import static org.mockito.Mockito.verify;
|
| +
|
| +import org.junit.Before;
|
| +import org.junit.Test;
|
| +import org.junit.runner.RunWith;
|
| +import org.mockito.MockitoAnnotations;
|
| +import org.mockito.Spy;
|
| +import org.robolectric.RuntimeEnvironment;
|
| +import org.robolectric.annotation.Config;
|
| +
|
| +import org.chromium.base.ContextUtils;
|
| +import org.chromium.base.test.util.Feature;
|
| +import org.chromium.testing.local.LocalRobolectricTestRunner;
|
| +
|
| +import java.util.Set;
|
| +
|
| +/** Unit tests for {@link BackgroundTaskSchedulerUma}. */
|
| +@RunWith(LocalRobolectricTestRunner.class)
|
| +@Config(manifest = Config.NONE)
|
| +public class BackgroundTaskSchedulerUmaTest {
|
| + @Spy
|
| + private BackgroundTaskSchedulerUma mUmaSpy;
|
| +
|
| + @Before
|
| + public void setUp() {
|
| + MockitoAnnotations.initMocks(this);
|
| + ContextUtils.initApplicationContextForTests(RuntimeEnvironment.application);
|
| + BackgroundTaskSchedulerUma.setInstanceForTesting(mUmaSpy);
|
| + doNothing().when(mUmaSpy).assertNativeIsLoaded();
|
| + }
|
| +
|
| + @Test
|
| + @Feature({"BackgroundTaskScheduler"})
|
| + public void testToUmaEnumValueFromTaskId() {
|
| + assertEquals(BackgroundTaskSchedulerUma.BACKGROUND_TASK_TEST,
|
| + BackgroundTaskSchedulerUma.toUmaEnumValueFromTaskId(TaskIds.TEST));
|
| + assertEquals(BackgroundTaskSchedulerUma.BACKGROUND_TASK_OMAHA,
|
| + BackgroundTaskSchedulerUma.toUmaEnumValueFromTaskId(TaskIds.OMAHA_JOB_ID));
|
| + assertEquals(BackgroundTaskSchedulerUma.BACKGROUND_TASK_GCM,
|
| + BackgroundTaskSchedulerUma.toUmaEnumValueFromTaskId(
|
| + TaskIds.GCM_BACKGROUND_TASK_JOB_ID));
|
| + assertEquals(BackgroundTaskSchedulerUma.BACKGROUND_TASK_NOTIFICATIONS,
|
| + BackgroundTaskSchedulerUma.toUmaEnumValueFromTaskId(
|
| + TaskIds.NOTIFICATION_SERVICE_JOB_ID));
|
| + assertEquals(BackgroundTaskSchedulerUma.BACKGROUND_TASK_WEBVIEW_MINIDUMP,
|
| + BackgroundTaskSchedulerUma.toUmaEnumValueFromTaskId(
|
| + TaskIds.WEBVIEW_MINIDUMP_UPLOADING_JOB_ID));
|
| + assertEquals(BackgroundTaskSchedulerUma.BACKGROUND_TASK_CHROME_MINIDUMP,
|
| + BackgroundTaskSchedulerUma.toUmaEnumValueFromTaskId(
|
| + TaskIds.CHROME_MINIDUMP_UPLOADING_JOB_ID));
|
| + assertEquals(BackgroundTaskSchedulerUma.BACKGROUND_TASK_OFFLINE_PAGES,
|
| + BackgroundTaskSchedulerUma.toUmaEnumValueFromTaskId(
|
| + TaskIds.OFFLINE_PAGES_BACKGROUND_JOB_ID));
|
| + assertEquals(BackgroundTaskSchedulerUma.BACKGROUND_TASK_OFFLINE_PREFETCH,
|
| + BackgroundTaskSchedulerUma.toUmaEnumValueFromTaskId(
|
| + TaskIds.OFFLINE_PAGES_PREFETCH_JOB_ID));
|
| + assertEquals(BackgroundTaskSchedulerUma.BACKGROUND_TASK_COUNT, 8);
|
| + }
|
| +
|
| + @Test
|
| + @Feature({"BackgroundTaskScheduler"})
|
| + public void testCacheEvent() {
|
| + String eventName = "event";
|
| + int eventValue = 77;
|
| + mUmaSpy.cacheEvent(eventName, eventValue);
|
| +
|
| + Set<String> cachedUmaEntries = BackgroundTaskSchedulerUma.getCachedUmaEntries(
|
| + ContextUtils.getAppSharedPreferences());
|
| + assertTrue(cachedUmaEntries.contains("event:77:1"));
|
| + assertEquals(1, cachedUmaEntries.size());
|
| +
|
| + mUmaSpy.cacheEvent(eventName, eventValue);
|
| + mUmaSpy.cacheEvent(eventName, eventValue);
|
| +
|
| + cachedUmaEntries = BackgroundTaskSchedulerUma.getCachedUmaEntries(
|
| + ContextUtils.getAppSharedPreferences());
|
| + assertTrue(cachedUmaEntries.contains("event:77:3"));
|
| + assertEquals(1, cachedUmaEntries.size());
|
| +
|
| + int eventValue2 = 50;
|
| + mUmaSpy.cacheEvent(eventName, eventValue2);
|
| +
|
| + cachedUmaEntries = BackgroundTaskSchedulerUma.getCachedUmaEntries(
|
| + ContextUtils.getAppSharedPreferences());
|
| + assertTrue(cachedUmaEntries.contains("event:77:3"));
|
| + assertTrue(cachedUmaEntries.contains("event:50:1"));
|
| + assertEquals(2, cachedUmaEntries.size());
|
| + }
|
| +
|
| + @Test
|
| + @Feature({"BackgroundTaskScheduler"})
|
| + public void testFlushStats() {
|
| + doNothing().when(mUmaSpy).recordEnumeratedHistogram(anyString(), anyInt(), anyInt());
|
| +
|
| + BackgroundTaskSchedulerUma.getInstance().flushStats();
|
| + verify(mUmaSpy, times(0)).recordEnumeratedHistogram(anyString(), anyInt(), anyInt());
|
| +
|
| + String eventName = "event";
|
| + int eventValue = 77;
|
| + int eventValue2 = 50;
|
| + mUmaSpy.cacheEvent(eventName, eventValue);
|
| + mUmaSpy.cacheEvent(eventName, eventValue);
|
| + mUmaSpy.cacheEvent(eventName, eventValue);
|
| + mUmaSpy.cacheEvent(eventName, eventValue2);
|
| +
|
| + BackgroundTaskSchedulerUma.getInstance().flushStats();
|
| +
|
| + verify(mUmaSpy, times(3))
|
| + .recordEnumeratedHistogram(eq(eventName), eq(eventValue),
|
| + eq(BackgroundTaskSchedulerUma.BACKGROUND_TASK_COUNT));
|
| + verify(mUmaSpy, times(1))
|
| + .recordEnumeratedHistogram(eq(eventName), eq(eventValue2),
|
| + eq(BackgroundTaskSchedulerUma.BACKGROUND_TASK_COUNT));
|
| + Set<String> cachedUmaEntries = BackgroundTaskSchedulerUma.getCachedUmaEntries(
|
| + ContextUtils.getAppSharedPreferences());
|
| + assertTrue(cachedUmaEntries.isEmpty());
|
| + }
|
| +
|
| + @Test
|
| + @Feature({"BackgroundTaskScheduler"})
|
| + public void testReportTaskScheduledSuccess() {
|
| + doNothing().when(mUmaSpy).cacheEvent(anyString(), anyInt());
|
| + BackgroundTaskSchedulerUma.getInstance().reportTaskScheduled(TaskIds.TEST, true);
|
| + verify(mUmaSpy, times(1))
|
| + .cacheEvent(eq("BackgroundTaskScheduler.TaskScheduled.Success"),
|
| + eq(BackgroundTaskSchedulerUma.BACKGROUND_TASK_TEST));
|
| + }
|
| +
|
| + @Test
|
| + @Feature({"BackgroundTaskScheduler"})
|
| + public void testReportTaskScheduledFailure() {
|
| + doNothing().when(mUmaSpy).cacheEvent(anyString(), anyInt());
|
| + BackgroundTaskSchedulerUma.getInstance().reportTaskScheduled(
|
| + TaskIds.OFFLINE_PAGES_BACKGROUND_JOB_ID, false);
|
| + verify(mUmaSpy, times(1))
|
| + .cacheEvent(eq("BackgroundTaskScheduler.TaskScheduled.Failure"),
|
| + eq(BackgroundTaskSchedulerUma.BACKGROUND_TASK_OFFLINE_PAGES));
|
| + }
|
| +
|
| + @Test
|
| + @Feature({"BackgroundTaskScheduler"})
|
| + public void testReportTaskCanceled() {
|
| + doNothing().when(mUmaSpy).cacheEvent(anyString(), anyInt());
|
| + BackgroundTaskSchedulerUma.getInstance().reportTaskCanceled(
|
| + TaskIds.OFFLINE_PAGES_PREFETCH_JOB_ID);
|
| + verify(mUmaSpy, times(1))
|
| + .cacheEvent(eq("BackgroundTaskScheduler.TaskCanceled"),
|
| + eq(BackgroundTaskSchedulerUma.BACKGROUND_TASK_OFFLINE_PREFETCH));
|
| + }
|
| +
|
| + @Test
|
| + @Feature({"BackgroundTaskScheduler"})
|
| + public void testReportTaskStarted() {
|
| + doNothing().when(mUmaSpy).cacheEvent(anyString(), anyInt());
|
| + BackgroundTaskSchedulerUma.getInstance().reportTaskStarted(TaskIds.OMAHA_JOB_ID);
|
| + verify(mUmaSpy, times(1))
|
| + .cacheEvent(eq("BackgroundTaskScheduler.TaskStarted"),
|
| + eq(BackgroundTaskSchedulerUma.BACKGROUND_TASK_OMAHA));
|
| + }
|
| +
|
| + @Test
|
| + @Feature({"BackgroundTaskScheduler"})
|
| + public void testReportTaskStopped() {
|
| + doNothing().when(mUmaSpy).cacheEvent(anyString(), anyInt());
|
| + BackgroundTaskSchedulerUma.getInstance().reportTaskStopped(
|
| + TaskIds.GCM_BACKGROUND_TASK_JOB_ID);
|
| + verify(mUmaSpy, times(1))
|
| + .cacheEvent(eq("BackgroundTaskScheduler.TaskStopped"),
|
| + eq(BackgroundTaskSchedulerUma.BACKGROUND_TASK_GCM));
|
| + }
|
| +}
|
|
|