Commit 0839c2de authored by Ted Choc's avatar Ted Choc Committed by Commit Bot

Fix main intent background duration startup metrics.

Accidentally broken by this refactoring:
https://chromium-review.googlesource.com/582338

Added tests to ensure the metric does not regress in the future.

BUG=800625

Change-Id: Iae1e29d11f62f177cea1200fa24befa64b77f559
Reviewed-on: https://chromium-review.googlesource.com/879433
Commit-Queue: Ted Choc <tedchoc@chromium.org>
Reviewed-by: default avatarMaria Khomenko <mariakhomenko@chromium.org>
Cr-Commit-Position: refs/heads/master@{#531708}
parent 85812812
......@@ -214,7 +214,8 @@ public class ChromeTabbedActivity
private static final String ACTION_CLOSE_TABS =
"com.google.android.apps.chrome.ACTION_CLOSE_TABS";
private static final String LAST_BACKGROUNDED_TIME_MS_PREF =
@VisibleForTesting
public static final String LAST_BACKGROUNDED_TIME_MS_PREF =
"ChromeTabbedActivity.BackgroundTimeMs";
private static final String NTP_LAUNCH_DELAY_IN_MINS_PARAM = "delay_in_mins";
......@@ -982,8 +983,7 @@ public class ChromeTabbedActivity
private void logMainIntentBehavior(Intent intent) {
assert isMainIntentFromLauncher(intent);
long currentTime = System.currentTimeMillis();
mMainIntentMetrics.onMainIntentWithNative(currentTime - getTimeSinceLastBackgroundedMs());
mMainIntentMetrics.onMainIntentWithNative(getTimeSinceLastBackgroundedMs());
}
/** Access the main intent metrics for test validation. */
......@@ -1110,9 +1110,13 @@ public class ChromeTabbedActivity
* Returns the number of milliseconds since Chrome was last backgrounded.
*/
private long getTimeSinceLastBackgroundedMs() {
long lastBackgroundedTimeMs =
ContextUtils.getAppSharedPreferences().getLong(LAST_BACKGROUNDED_TIME_MS_PREF, -1);
return System.currentTimeMillis() - lastBackgroundedTimeMs;
// TODO(tedchoc): We should cache the last visible time and reuse it to avoid different
// values of this depending on when it is called after the activity was
// shown.
long currentTime = System.currentTimeMillis();
long lastBackgroundedTimeMs = ContextUtils.getAppSharedPreferences().getLong(
LAST_BACKGROUNDED_TIME_MS_PREF, currentTime);
return currentTime - lastBackgroundedTimeMs;
}
@Override
......
......@@ -4,18 +4,25 @@
package org.chromium.chrome.browser.metrics;
import static org.junit.Assert.assertThat;
import android.annotation.SuppressLint;
import android.content.ComponentName;
import android.content.Intent;
import android.support.test.InstrumentationRegistry;
import android.support.test.filters.MediumTest;
import org.hamcrest.Matchers;
import org.junit.After;
import org.junit.Assert;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.chromium.base.ContextUtils;
import org.chromium.base.ThreadUtils;
import org.chromium.base.test.util.CommandLineFlags;
import org.chromium.base.test.util.UserActionTester;
import org.chromium.chrome.R;
import org.chromium.chrome.browser.ChromeSwitches;
import org.chromium.chrome.browser.ChromeTabbedActivity;
......@@ -37,11 +44,21 @@ import java.util.concurrent.Callable;
*/
@RunWith(ChromeJUnit4ClassRunner.class)
@CommandLineFlags.Add({ChromeSwitches.DISABLE_FIRST_RUN_EXPERIENCE})
@SuppressLint({"ApplySharedPref", "CommitPrefEdits"})
public class MainIntentBehaviorMetricsIntegrationTest {
private static final long HOURS_IN_MS = 60 * 60 * 1000L;
@Rule
public ChromeActivityTestRule<ChromeTabbedActivity> mActivityTestRule =
new ChromeActivityTestRule<>(ChromeTabbedActivity.class);
private UserActionTester mActionTester;
@After
public void tearDown() {
if (mActionTester != null) mActionTester.tearDown();
}
@MediumTest
@Test
public void testFocusOmnibox() {
......@@ -137,6 +154,66 @@ public class MainIntentBehaviorMetricsIntegrationTest {
.getPendingActionRecordForMainIntent());
}
@MediumTest
@Test
public void testBackgroundDuration_24hrs() {
assertBackgroundDurationLogged(
24 * HOURS_IN_MS, "MobileStartup.MainIntentReceived.After24Hours");
}
@MediumTest
@Test
public void testBackgroundDuration_12hrs() {
assertBackgroundDurationLogged(
12 * HOURS_IN_MS, "MobileStartup.MainIntentReceived.After12Hours");
}
@MediumTest
@Test
public void testBackgroundDuration_6hrs() {
assertBackgroundDurationLogged(
6 * HOURS_IN_MS, "MobileStartup.MainIntentReceived.After6Hours");
}
@MediumTest
@Test
public void testBackgroundDuration_1hr() {
assertBackgroundDurationLogged(HOURS_IN_MS, "MobileStartup.MainIntentReceived.After1Hour");
}
@MediumTest
@Test
public void testBackgroundDuration_0hr() {
assertBackgroundDurationLogged(0, null);
for (String action : mActionTester.getActions()) {
if (action.startsWith("MobileStartup.MainIntentReceived.After")) {
Assert.fail("Unexpected background duration logged: " + action);
}
}
}
private void assertBackgroundDurationLogged(long duration, String expectedMetric) {
startActivity(false);
mActionTester = new UserActionTester();
ContextUtils.getAppSharedPreferences()
.edit()
.putLong(ChromeTabbedActivity.LAST_BACKGROUNDED_TIME_MS_PREF,
System.currentTimeMillis() - duration)
.commit();
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
ThreadUtils.runOnUiThreadBlocking(
() -> { mActivityTestRule.getActivity().onNewIntent(intent); });
assertThat(mActionTester.toString(), mActionTester.getActions(),
Matchers.hasItem("MobileStartup.MainIntentReceived"));
if (expectedMetric != null) {
assertThat(mActionTester.toString(), mActionTester.getActions(),
Matchers.hasItem(expectedMetric));
}
}
private void startActivity(boolean addLauncherCategory) {
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment