1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/chromeos/login/error_screens_histogram_helper.h"
7 #include "base/metrics/statistics_recorder.h"
8 #include "base/test/histogram_tester.h"
9 #include "chrome/browser/chromeos/login/screens/network_error.h"
10 #include "content/public/test/test_browser_thread_bundle.h"
11 #include "testing/gtest/include/gtest/gtest.h"
15 class ErrorScreensHistogramHelperTest
: public testing::Test
{
17 void SetUp() override
{
18 helper_
.reset(new ErrorScreensHistogramHelper("TestScreen"));
19 second_helper_
.reset(new ErrorScreensHistogramHelper("TestScreen2"));
22 content::TestBrowserThreadBundle thread_bundle_
;
23 base::HistogramTester histograms_
;
24 scoped_ptr
<ErrorScreensHistogramHelper
> helper_
;
25 scoped_ptr
<ErrorScreensHistogramHelper
> second_helper_
;
28 // No errors when screen was not shown.
29 TEST_F(ErrorScreensHistogramHelperTest
, DoesNotShowScreen
) {
31 histograms_
.ExpectTotalCount("OOBE.NetworkErrorShown.TestScreen", 0);
34 // No errors when screen was shown and error was not.
35 TEST_F(ErrorScreensHistogramHelperTest
, ShowScreenWithoutError
) {
36 helper_
->OnScreenShow();
38 second_helper_
->OnScreenShow();
39 second_helper_
.reset();
40 histograms_
.ExpectUniqueSample("OOBE.NetworkErrorShown.TestScreen",
41 NetworkError::ERROR_STATE_NONE
, 1);
42 histograms_
.ExpectUniqueSample("OOBE.NetworkErrorShown.TestScreen2",
43 NetworkError::ERROR_STATE_NONE
, 1);
46 // Show 3 offline errors and 1 portal error. Make sure in time histograms logged
48 TEST_F(ErrorScreensHistogramHelperTest
, ShowScreenAndError
) {
49 helper_
->OnScreenShow();
50 second_helper_
->OnScreenShow();
51 helper_
->OnErrorShow(NetworkError::ERROR_STATE_OFFLINE
);
52 second_helper_
->OnErrorShow(NetworkError::ERROR_STATE_PORTAL
);
53 helper_
->OnErrorShow(NetworkError::ERROR_STATE_OFFLINE
);
54 helper_
->OnErrorHide();
55 second_helper_
->OnErrorHide();
56 helper_
->OnErrorShow(NetworkError::ERROR_STATE_OFFLINE
);
57 histograms_
.ExpectUniqueSample("OOBE.NetworkErrorShown.TestScreen",
58 NetworkError::ERROR_STATE_OFFLINE
, 3);
59 histograms_
.ExpectUniqueSample("OOBE.NetworkErrorShown.TestScreen2",
60 NetworkError::ERROR_STATE_PORTAL
, 1);
61 helper_
->OnErrorShow(NetworkError::ERROR_STATE_PORTAL
);
62 histograms_
.ExpectBucketCount("OOBE.NetworkErrorShown.TestScreen",
63 NetworkError::ERROR_STATE_PORTAL
, 1);
64 histograms_
.ExpectTotalCount("OOBE.ErrorScreensTime.TestScreen.Portal", 0);
66 histograms_
.ExpectTotalCount("OOBE.ErrorScreensTime.TestScreen.Portal", 1);
69 // Show error and hide it after 1 sec.
70 TEST_F(ErrorScreensHistogramHelperTest
, TestShowHideTime
) {
71 helper_
->OnScreenShow();
72 second_helper_
->OnScreenShow();
73 base::Time now
= base::Time::Now();
74 helper_
->OnErrorShowTime(NetworkError::ERROR_STATE_PORTAL
, now
);
75 now
+= base::TimeDelta::FromMilliseconds(1000);
76 helper_
->OnErrorHideTime(now
);
78 histograms_
.ExpectUniqueSample(
79 "OOBE.ErrorScreensTime.TestScreen.Portal", 1000, 1);
82 // Show, hide, show, hide error with 1 sec interval. Make sure time logged in
83 // histogram is 2 sec.
84 TEST_F(ErrorScreensHistogramHelperTest
, TestShowHideShowHideTime
) {
85 helper_
->OnScreenShow();
86 second_helper_
->OnScreenShow();
87 base::Time now
= base::Time::Now();
88 helper_
->OnErrorShowTime(NetworkError::ERROR_STATE_PROXY
, now
);
89 now
+= base::TimeDelta::FromMilliseconds(1000);
90 helper_
->OnErrorHideTime(now
);
91 now
+= base::TimeDelta::FromMilliseconds(1000);
92 helper_
->OnErrorShowTime(NetworkError::ERROR_STATE_PORTAL
, now
);
93 now
+= base::TimeDelta::FromMilliseconds(1000);
94 helper_
->OnErrorHideTime(now
);
96 histograms_
.ExpectUniqueSample(
97 "OOBE.ErrorScreensTime.TestScreen.Portal", 2000, 1);
100 // Show, show, hide error with 1 sec interval. Make sure time logged in
101 // histogram is 2 sec.
102 TEST_F(ErrorScreensHistogramHelperTest
, TestShowShowHideTime
) {
103 helper_
->OnScreenShow();
104 second_helper_
->OnScreenShow();
105 base::Time now
= base::Time::Now();
106 helper_
->OnErrorShowTime(NetworkError::ERROR_STATE_PROXY
, now
);
107 now
+= base::TimeDelta::FromMilliseconds(1000);
108 helper_
->OnErrorShowTime(NetworkError::ERROR_STATE_PORTAL
, now
);
109 now
+= base::TimeDelta::FromMilliseconds(1000);
110 helper_
->OnErrorHideTime(now
);
112 histograms_
.ExpectUniqueSample(
113 "OOBE.ErrorScreensTime.TestScreen.Portal", 2000, 1);
116 } // namespace chromeos