• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 // Copyright (C) 2012 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 #include "shill/metrics.h"
18 
19 #include <string>
20 #include <vector>
21 
22 #if defined(__ANDROID__)
23 #include <dbus/service_constants.h>
24 #else
25 #include <chromeos/dbus/service_constants.h>
26 #endif  // __ANDROID__
27 #include <metrics/metrics_library_mock.h>
28 #include <metrics/timer_mock.h>
29 
30 #include "shill/mock_control.h"
31 #include "shill/mock_event_dispatcher.h"
32 #include "shill/mock_log.h"
33 #include "shill/mock_manager.h"
34 #include "shill/mock_service.h"
35 
36 #if !defined(DISABLE_WIFI)
37 #include "shill/mock_eap_credentials.h"
38 #include "shill/wifi/mock_wifi_service.h"
39 #endif  // DISABLE_WIFI
40 
41 using std::string;
42 
43 using testing::_;
44 using testing::DoAll;
45 using testing::Ge;
46 using testing::Mock;
47 using testing::Return;
48 using testing::SetArgumentPointee;
49 using testing::Test;
50 
51 namespace shill {
52 
53 class MetricsTest : public Test {
54  public:
MetricsTest()55   MetricsTest()
56       : manager_(&control_interface_,
57                  &dispatcher_,
58                  &metrics_),
59         metrics_(&dispatcher_),
60 #if !defined(DISABLE_WIFI)
61         open_wifi_service_(new MockWiFiService(&control_interface_,
62                                                &dispatcher_,
63                                                &metrics_,
64                                                &manager_,
65                                                manager_.wifi_provider(),
66                                                ssid_,
67                                                kModeManaged,
68                                                kSecurityNone,
69                                                false)),
70         wep_wifi_service_(new MockWiFiService(&control_interface_,
71                                               &dispatcher_,
72                                               &metrics_,
73                                               &manager_,
74                                               manager_.wifi_provider(),
75                                               ssid_,
76                                               kModeManaged,
77                                               kSecurityWep,
78                                               false)),
79         eap_wifi_service_(new MockWiFiService(&control_interface_,
80                                               &dispatcher_,
81                                               &metrics_,
82                                               &manager_,
83                                               manager_.wifi_provider(),
84                                               ssid_,
85                                               kModeManaged,
86                                               kSecurity8021x,
87                                               false)),
88         eap_(new MockEapCredentials()),
89 #endif  // DISABLE_WIFI
90         service_(new MockService(&control_interface_,
91                                  &dispatcher_,
92                                  &metrics_,
93                                  &manager_)) {}
94 
~MetricsTest()95   virtual ~MetricsTest() {}
96 
SetUp()97   virtual void SetUp() {
98     metrics_.set_library(&library_);
99 #if !defined(DISABLE_WIFI)
100     eap_wifi_service_->eap_.reset(eap_);  // Passes ownership.
101 #endif  // DISABLE_WIFI
102     metrics_.collect_bootstats_ = false;
103   }
104 
105  protected:
ExpectCommonPostReady(Metrics::WiFiApMode ap_mode,Metrics::WiFiChannel channel,Metrics::WiFiNetworkPhyMode mode,Metrics::WiFiSecurity security,int signal_strength)106   void ExpectCommonPostReady(Metrics::WiFiApMode ap_mode,
107                              Metrics::WiFiChannel channel,
108                              Metrics::WiFiNetworkPhyMode mode,
109                              Metrics::WiFiSecurity security,
110                              int signal_strength) {
111     EXPECT_CALL(library_, SendEnumToUMA("Network.Shill.Wifi.ApMode",
112                                         ap_mode,
113                                         Metrics::kWiFiApModeMax));
114     EXPECT_CALL(library_, SendEnumToUMA("Network.Shill.Wifi.Channel",
115                                         channel,
116                                         Metrics::kMetricNetworkChannelMax));
117     EXPECT_CALL(library_, SendEnumToUMA("Network.Shill.Wifi.PhyMode",
118                                         mode,
119                                         Metrics::kWiFiNetworkPhyModeMax));
120     EXPECT_CALL(library_, SendEnumToUMA("Network.Shill.Wifi.Security",
121                                         security,
122                                         Metrics::kWiFiSecurityMax));
123     EXPECT_CALL(library_,
124                 SendToUMA("Network.Shill.Wifi.SignalStrength",
125                           signal_strength,
126                           Metrics::kMetricNetworkSignalStrengthMin,
127                           Metrics::kMetricNetworkSignalStrengthMax,
128                           Metrics::kMetricNetworkSignalStrengthNumBuckets));
129   }
130 
131   MockControl control_interface_;
132   MockEventDispatcher dispatcher_;
133   MockManager manager_;
134   Metrics metrics_;  // This must be destroyed after all |service_|s.
135   MetricsLibraryMock library_;
136 #if !defined(DISABLE_WIFI)
137   const std::vector<uint8_t> ssid_;
138   scoped_refptr<MockWiFiService> open_wifi_service_;
139   scoped_refptr<MockWiFiService> wep_wifi_service_;
140   scoped_refptr<MockWiFiService> eap_wifi_service_;
141   MockEapCredentials* eap_;  // Owned by |eap_wifi_service_|.
142 #endif  // DISABLE_WIFI
143   scoped_refptr<MockService> service_;
144 };
145 
TEST_F(MetricsTest,TimeToConfig)146 TEST_F(MetricsTest, TimeToConfig) {
147   EXPECT_CALL(library_, SendToUMA("Network.Shill.Unknown.TimeToConfig",
148                                   Ge(0),
149                                   Metrics::kTimerHistogramMillisecondsMin,
150                                   Metrics::kTimerHistogramMillisecondsMax,
151                                   Metrics::kTimerHistogramNumBuckets));
152   metrics_.NotifyServiceStateChanged(*service_, Service::kStateConfiguring);
153   metrics_.NotifyServiceStateChanged(*service_, Service::kStateConnected);
154 }
155 
TEST_F(MetricsTest,TimeToPortal)156 TEST_F(MetricsTest, TimeToPortal) {
157   EXPECT_CALL(library_, SendToUMA("Network.Shill.Unknown.TimeToPortal",
158                                   Ge(0),
159                                   Metrics::kTimerHistogramMillisecondsMin,
160                                   Metrics::kTimerHistogramMillisecondsMax,
161                                   Metrics::kTimerHistogramNumBuckets));
162   metrics_.NotifyServiceStateChanged(*service_, Service::kStateConnected);
163   metrics_.NotifyServiceStateChanged(*service_, Service::kStatePortal);
164 }
165 
TEST_F(MetricsTest,TimeToOnline)166 TEST_F(MetricsTest, TimeToOnline) {
167   EXPECT_CALL(library_, SendToUMA("Network.Shill.Unknown.TimeToOnline",
168                                   Ge(0),
169                                   Metrics::kTimerHistogramMillisecondsMin,
170                                   Metrics::kTimerHistogramMillisecondsMax,
171                                   Metrics::kTimerHistogramNumBuckets));
172   metrics_.NotifyServiceStateChanged(*service_, Service::kStateConnected);
173   metrics_.NotifyServiceStateChanged(*service_, Service::kStateOnline);
174 }
175 
TEST_F(MetricsTest,ServiceFailure)176 TEST_F(MetricsTest, ServiceFailure) {
177   EXPECT_CALL(*service_, failure())
178       .WillRepeatedly(Return(Service::kFailureBadPassphrase));
179   EXPECT_CALL(library_,
180       SendEnumToUMA(Metrics::kMetricNetworkServiceErrors,
181                     Metrics::kNetworkServiceErrorBadPassphrase,
182                     Metrics::kNetworkServiceErrorMax));
183   metrics_.NotifyServiceStateChanged(*service_, Service::kStateFailure);
184 }
185 
186 #if !defined(DISABLE_WIFI)
TEST_F(MetricsTest,WiFiServiceTimeToJoin)187 TEST_F(MetricsTest, WiFiServiceTimeToJoin) {
188   EXPECT_CALL(library_, SendToUMA("Network.Shill.Wifi.TimeToJoin",
189                                   Ge(0),
190                                   Metrics::kTimerHistogramMillisecondsMin,
191                                   Metrics::kTimerHistogramMillisecondsMax,
192                                   Metrics::kTimerHistogramNumBuckets));
193   metrics_.NotifyServiceStateChanged(*open_wifi_service_,
194                                      Service::kStateAssociating);
195   metrics_.NotifyServiceStateChanged(*open_wifi_service_,
196                                      Service::kStateConfiguring);
197 }
198 
TEST_F(MetricsTest,WiFiServicePostReady)199 TEST_F(MetricsTest, WiFiServicePostReady) {
200   base::TimeDelta non_zero_time_delta = base::TimeDelta::FromMilliseconds(1);
201   chromeos_metrics::TimerMock* mock_time_resume_to_ready_timer =
202       new chromeos_metrics::TimerMock;
203   metrics_.set_time_resume_to_ready_timer(mock_time_resume_to_ready_timer);
204 
205   const int kStrength = -42;
206   ExpectCommonPostReady(Metrics::kWiFiApModeManaged,
207                         Metrics::kWiFiChannel2412,
208                         Metrics::kWiFiNetworkPhyMode11a,
209                         Metrics::kWiFiSecurityWep,
210                         -kStrength);
211   EXPECT_CALL(library_, SendToUMA("Network.Shill.Wifi.TimeResumeToReady",
212                                   _, _, _, _)).Times(0);
213   EXPECT_CALL(library_, SendEnumToUMA("Network.Shill.Wifi.EapOuterProtocol",
214                                        _, _)).Times(0);
215   EXPECT_CALL(library_, SendEnumToUMA("Network.Shill.Wifi.EapInnerProtocol",
216                                       _, _)).Times(0);
217   wep_wifi_service_->frequency_ = 2412;
218   wep_wifi_service_->physical_mode_ = Metrics::kWiFiNetworkPhyMode11a;
219   wep_wifi_service_->raw_signal_strength_ = kStrength;
220   metrics_.NotifyServiceStateChanged(*wep_wifi_service_,
221                                      Service::kStateConnected);
222   Mock::VerifyAndClearExpectations(&library_);
223 
224   // Simulate a system suspend, resume and an AP reconnect.
225   ExpectCommonPostReady(Metrics::kWiFiApModeManaged,
226                         Metrics::kWiFiChannel2412,
227                         Metrics::kWiFiNetworkPhyMode11a,
228                         Metrics::kWiFiSecurityWep,
229                         -kStrength);
230   EXPECT_CALL(library_, SendToUMA("Network.Shill.Wifi.TimeResumeToReady",
231                                   Ge(0),
232                                   Metrics::kTimerHistogramMillisecondsMin,
233                                   Metrics::kTimerHistogramMillisecondsMax,
234                                   Metrics::kTimerHistogramNumBuckets));
235   EXPECT_CALL(*mock_time_resume_to_ready_timer, GetElapsedTime(_)).
236       WillOnce(DoAll(SetArgumentPointee<0>(non_zero_time_delta), Return(true)));
237   metrics_.NotifySuspendDone();
238   metrics_.NotifyServiceStateChanged(*wep_wifi_service_,
239                                      Service::kStateConnected);
240   Mock::VerifyAndClearExpectations(&library_);
241   Mock::VerifyAndClearExpectations(mock_time_resume_to_ready_timer);
242 
243   // Make sure subsequent connects do not count towards TimeResumeToReady.
244   ExpectCommonPostReady(Metrics::kWiFiApModeManaged,
245                         Metrics::kWiFiChannel2412,
246                         Metrics::kWiFiNetworkPhyMode11a,
247                         Metrics::kWiFiSecurityWep,
248                         -kStrength);
249   EXPECT_CALL(library_, SendToUMA("Network.Shill.Wifi.TimeResumeToReady",
250                                   _, _, _, _)).Times(0);
251   metrics_.NotifyServiceStateChanged(*wep_wifi_service_,
252                                      Service::kStateConnected);
253 }
254 
TEST_F(MetricsTest,WiFiServicePostReadyEAP)255 TEST_F(MetricsTest, WiFiServicePostReadyEAP) {
256   const int kStrength = -42;
257   ExpectCommonPostReady(Metrics::kWiFiApModeManaged,
258                         Metrics::kWiFiChannel2412,
259                         Metrics::kWiFiNetworkPhyMode11a,
260                         Metrics::kWiFiSecurity8021x,
261                         -kStrength);
262   eap_wifi_service_->frequency_ = 2412;
263   eap_wifi_service_->physical_mode_ = Metrics::kWiFiNetworkPhyMode11a;
264   eap_wifi_service_->raw_signal_strength_ = kStrength;
265   EXPECT_CALL(*eap_, OutputConnectionMetrics(&metrics_, Technology::kWifi));
266   metrics_.NotifyServiceStateChanged(*eap_wifi_service_,
267                                      Service::kStateConnected);
268 }
269 
TEST_F(MetricsTest,WiFiServicePostReadyAdHoc)270 TEST_F(MetricsTest, WiFiServicePostReadyAdHoc) {
271   auto adhoc_wifi_service(
272       make_scoped_refptr(new MockWiFiService(&control_interface_,
273                                              &dispatcher_,
274                                              &metrics_,
275                                              &manager_,
276                                              manager_.wifi_provider(),
277                                              ssid_,
278                                              kModeAdhoc,
279                                              kSecurityNone,
280                                              false)));
281   const int kStrength = -42;
282   ExpectCommonPostReady(Metrics::kWiFiApModeAdHoc,
283                         Metrics::kWiFiChannel2412,
284                         Metrics::kWiFiNetworkPhyMode11b,
285                         Metrics::kWiFiSecurityNone,
286                         -kStrength);
287   adhoc_wifi_service->frequency_ = 2412;
288   adhoc_wifi_service->physical_mode_ = Metrics::kWiFiNetworkPhyMode11b;
289   adhoc_wifi_service->raw_signal_strength_ = kStrength;
290   metrics_.NotifyServiceStateChanged(*adhoc_wifi_service,
291                                      Service::kStateConnected);
292 }
293 #endif  // DISABLE_WIFI
294 
TEST_F(MetricsTest,FrequencyToChannel)295 TEST_F(MetricsTest, FrequencyToChannel) {
296   EXPECT_EQ(Metrics::kWiFiChannelUndef, metrics_.WiFiFrequencyToChannel(2411));
297   EXPECT_EQ(Metrics::kWiFiChannel2412, metrics_.WiFiFrequencyToChannel(2412));
298   EXPECT_EQ(Metrics::kWiFiChannel2472, metrics_.WiFiFrequencyToChannel(2472));
299   EXPECT_EQ(Metrics::kWiFiChannelUndef, metrics_.WiFiFrequencyToChannel(2473));
300   EXPECT_EQ(Metrics::kWiFiChannel2484, metrics_.WiFiFrequencyToChannel(2484));
301   EXPECT_EQ(Metrics::kWiFiChannelUndef, metrics_.WiFiFrequencyToChannel(5169));
302   EXPECT_EQ(Metrics::kWiFiChannel5170, metrics_.WiFiFrequencyToChannel(5170));
303   EXPECT_EQ(Metrics::kWiFiChannel5190, metrics_.WiFiFrequencyToChannel(5190));
304   EXPECT_EQ(Metrics::kWiFiChannel5180, metrics_.WiFiFrequencyToChannel(5180));
305   EXPECT_EQ(Metrics::kWiFiChannel5200, metrics_.WiFiFrequencyToChannel(5200));
306   EXPECT_EQ(Metrics::kWiFiChannel5230, metrics_.WiFiFrequencyToChannel(5230));
307   EXPECT_EQ(Metrics::kWiFiChannelUndef, metrics_.WiFiFrequencyToChannel(5231));
308   EXPECT_EQ(Metrics::kWiFiChannelUndef, metrics_.WiFiFrequencyToChannel(5239));
309   EXPECT_EQ(Metrics::kWiFiChannel5240, metrics_.WiFiFrequencyToChannel(5240));
310   EXPECT_EQ(Metrics::kWiFiChannelUndef, metrics_.WiFiFrequencyToChannel(5241));
311   EXPECT_EQ(Metrics::kWiFiChannel5320, metrics_.WiFiFrequencyToChannel(5320));
312   EXPECT_EQ(Metrics::kWiFiChannelUndef, metrics_.WiFiFrequencyToChannel(5321));
313   EXPECT_EQ(Metrics::kWiFiChannelUndef, metrics_.WiFiFrequencyToChannel(5499));
314   EXPECT_EQ(Metrics::kWiFiChannel5500, metrics_.WiFiFrequencyToChannel(5500));
315   EXPECT_EQ(Metrics::kWiFiChannelUndef, metrics_.WiFiFrequencyToChannel(5501));
316   EXPECT_EQ(Metrics::kWiFiChannel5700, metrics_.WiFiFrequencyToChannel(5700));
317   EXPECT_EQ(Metrics::kWiFiChannelUndef, metrics_.WiFiFrequencyToChannel(5701));
318   EXPECT_EQ(Metrics::kWiFiChannelUndef, metrics_.WiFiFrequencyToChannel(5744));
319   EXPECT_EQ(Metrics::kWiFiChannel5745, metrics_.WiFiFrequencyToChannel(5745));
320   EXPECT_EQ(Metrics::kWiFiChannelUndef, metrics_.WiFiFrequencyToChannel(5746));
321   EXPECT_EQ(Metrics::kWiFiChannel5825, metrics_.WiFiFrequencyToChannel(5825));
322   EXPECT_EQ(Metrics::kWiFiChannelUndef, metrics_.WiFiFrequencyToChannel(5826));
323 }
324 
TEST_F(MetricsTest,TimeOnlineTimeToDrop)325 TEST_F(MetricsTest, TimeOnlineTimeToDrop) {
326   chromeos_metrics::TimerMock* mock_time_online_timer =
327       new chromeos_metrics::TimerMock;
328   metrics_.set_time_online_timer(mock_time_online_timer);
329   chromeos_metrics::TimerMock* mock_time_to_drop_timer =
330       new chromeos_metrics::TimerMock;
331   metrics_.set_time_to_drop_timer(mock_time_to_drop_timer);
332   scoped_refptr<MockService> wifi_service =
333       new MockService(&control_interface_, &dispatcher_, &metrics_, &manager_);
334   EXPECT_CALL(*service_, technology()).
335       WillOnce(Return(Technology::kEthernet));
336   EXPECT_CALL(*wifi_service, technology()).
337       WillOnce(Return(Technology::kWifi));
338   EXPECT_CALL(library_, SendToUMA("Network.Shill.Ethernet.TimeOnline",
339                                   Ge(0),
340                                   Metrics::kMetricTimeOnlineSecondsMin,
341                                   Metrics::kMetricTimeOnlineSecondsMax,
342                                   Metrics::kTimerHistogramNumBuckets));
343   EXPECT_CALL(library_, SendToUMA(Metrics::kMetricTimeToDropSeconds,
344                                   Ge(0),
345                                   Metrics::kMetricTimeToDropSecondsMin,
346                                   Metrics::kMetricTimeToDropSecondsMax,
347                                   Metrics::kTimerHistogramNumBuckets)).Times(0);
348   EXPECT_CALL(*mock_time_online_timer, Start()).Times(2);
349   EXPECT_CALL(*mock_time_to_drop_timer, Start());
350   metrics_.NotifyDefaultServiceChanged(service_.get());
351   metrics_.NotifyDefaultServiceChanged(wifi_service.get());
352 
353   EXPECT_CALL(*mock_time_online_timer, Start());
354   EXPECT_CALL(*mock_time_to_drop_timer, Start()).Times(0);
355   EXPECT_CALL(library_, SendToUMA("Network.Shill.Wifi.TimeOnline",
356                                   Ge(0),
357                                   Metrics::kMetricTimeOnlineSecondsMin,
358                                   Metrics::kMetricTimeOnlineSecondsMax,
359                                   Metrics::kTimerHistogramNumBuckets));
360   EXPECT_CALL(library_, SendToUMA(Metrics::kMetricTimeToDropSeconds,
361                                   Ge(0),
362                                   Metrics::kMetricTimeToDropSecondsMin,
363                                   Metrics::kMetricTimeToDropSecondsMax,
364                                   Metrics::kTimerHistogramNumBuckets));
365   metrics_.NotifyDefaultServiceChanged(nullptr);
366 }
367 
TEST_F(MetricsTest,Disconnect)368 TEST_F(MetricsTest, Disconnect) {
369   EXPECT_CALL(*service_, technology()).
370       WillRepeatedly(Return(Technology::kWifi));
371   EXPECT_CALL(*service_, explicitly_disconnected()).
372       WillOnce(Return(false));
373   EXPECT_CALL(library_, SendToUMA("Network.Shill.Wifi.Disconnect",
374                                   false,
375                                   Metrics::kMetricDisconnectMin,
376                                   Metrics::kMetricDisconnectMax,
377                                   Metrics::kMetricDisconnectNumBuckets));
378   metrics_.NotifyServiceDisconnect(*service_);
379 
380   EXPECT_CALL(*service_, explicitly_disconnected()).
381       WillOnce(Return(true));
382   EXPECT_CALL(library_, SendToUMA("Network.Shill.Wifi.Disconnect",
383                                   true,
384                                   Metrics::kMetricDisconnectMin,
385                                   Metrics::kMetricDisconnectMax,
386                                   Metrics::kMetricDisconnectNumBuckets));
387   metrics_.NotifyServiceDisconnect(*service_);
388 }
389 
TEST_F(MetricsTest,PortalDetectionResultToEnum)390 TEST_F(MetricsTest, PortalDetectionResultToEnum) {
391   ConnectivityTrial::Result trial_result(ConnectivityTrial::kPhaseDNS,
392                                 ConnectivityTrial::kStatusFailure);
393   PortalDetector::Result result(trial_result, 0, true);
394 
395   EXPECT_EQ(Metrics::kPortalResultDNSFailure,
396             Metrics::PortalDetectionResultToEnum(result));
397 
398   result.trial_result.phase = ConnectivityTrial::kPhaseDNS;
399   result.trial_result.status = ConnectivityTrial::kStatusTimeout;
400   EXPECT_EQ(Metrics::kPortalResultDNSTimeout,
401             Metrics::PortalDetectionResultToEnum(result));
402 
403   result.trial_result.phase = ConnectivityTrial::kPhaseConnection;
404   result.trial_result.status = ConnectivityTrial::kStatusFailure;
405   EXPECT_EQ(Metrics::kPortalResultConnectionFailure,
406             Metrics::PortalDetectionResultToEnum(result));
407 
408   result.trial_result.phase = ConnectivityTrial::kPhaseConnection;
409   result.trial_result.status = ConnectivityTrial::kStatusTimeout;
410   EXPECT_EQ(Metrics::kPortalResultConnectionTimeout,
411             Metrics::PortalDetectionResultToEnum(result));
412 
413   result.trial_result.phase = ConnectivityTrial::kPhaseHTTP;
414   result.trial_result.status = ConnectivityTrial::kStatusFailure;
415   EXPECT_EQ(Metrics::kPortalResultHTTPFailure,
416             Metrics::PortalDetectionResultToEnum(result));
417 
418   result.trial_result.phase = ConnectivityTrial::kPhaseHTTP;
419   result.trial_result.status = ConnectivityTrial::kStatusTimeout;
420   EXPECT_EQ(Metrics::kPortalResultHTTPTimeout,
421             Metrics::PortalDetectionResultToEnum(result));
422 
423   result.trial_result.phase = ConnectivityTrial::kPhaseContent;
424   result.trial_result.status = ConnectivityTrial::kStatusSuccess;
425   EXPECT_EQ(Metrics::kPortalResultSuccess,
426             Metrics::PortalDetectionResultToEnum(result));
427 
428   result.trial_result.phase = ConnectivityTrial::kPhaseContent;
429   result.trial_result.status = ConnectivityTrial::kStatusFailure;
430   EXPECT_EQ(Metrics::kPortalResultContentFailure,
431             Metrics::PortalDetectionResultToEnum(result));
432 
433   result.trial_result.phase = ConnectivityTrial::kPhaseContent;
434   result.trial_result.status = ConnectivityTrial::kStatusTimeout;
435   EXPECT_EQ(Metrics::kPortalResultContentTimeout,
436             Metrics::PortalDetectionResultToEnum(result));
437 
438   result.trial_result.phase = ConnectivityTrial::kPhaseUnknown;
439   result.trial_result.status = ConnectivityTrial::kStatusFailure;
440   EXPECT_EQ(Metrics::kPortalResultUnknown,
441             Metrics::PortalDetectionResultToEnum(result));
442 }
443 
TEST_F(MetricsTest,TimeToConnect)444 TEST_F(MetricsTest, TimeToConnect) {
445   EXPECT_CALL(library_,
446       SendToUMA("Network.Shill.Cellular.TimeToConnect",
447                 Ge(0),
448                 Metrics::kMetricTimeToConnectMillisecondsMin,
449                 Metrics::kMetricTimeToConnectMillisecondsMax,
450                 Metrics::kMetricTimeToConnectMillisecondsNumBuckets));
451   const int kInterfaceIndex = 1;
452   metrics_.RegisterDevice(kInterfaceIndex, Technology::kCellular);
453   metrics_.NotifyDeviceConnectStarted(kInterfaceIndex, false);
454   metrics_.NotifyDeviceConnectFinished(kInterfaceIndex);
455 }
456 
TEST_F(MetricsTest,TimeToDisable)457 TEST_F(MetricsTest, TimeToDisable) {
458   EXPECT_CALL(library_,
459       SendToUMA("Network.Shill.Cellular.TimeToDisable",
460                 Ge(0),
461                 Metrics::kMetricTimeToDisableMillisecondsMin,
462                 Metrics::kMetricTimeToDisableMillisecondsMax,
463                 Metrics::kMetricTimeToDisableMillisecondsNumBuckets));
464   const int kInterfaceIndex = 1;
465   metrics_.RegisterDevice(kInterfaceIndex, Technology::kCellular);
466   metrics_.NotifyDeviceDisableStarted(kInterfaceIndex);
467   metrics_.NotifyDeviceDisableFinished(kInterfaceIndex);
468 }
469 
TEST_F(MetricsTest,TimeToEnable)470 TEST_F(MetricsTest, TimeToEnable) {
471   EXPECT_CALL(library_,
472       SendToUMA("Network.Shill.Cellular.TimeToEnable",
473                 Ge(0),
474                 Metrics::kMetricTimeToEnableMillisecondsMin,
475                 Metrics::kMetricTimeToEnableMillisecondsMax,
476                 Metrics::kMetricTimeToEnableMillisecondsNumBuckets));
477   const int kInterfaceIndex = 1;
478   metrics_.RegisterDevice(kInterfaceIndex, Technology::kCellular);
479   metrics_.NotifyDeviceEnableStarted(kInterfaceIndex);
480   metrics_.NotifyDeviceEnableFinished(kInterfaceIndex);
481 }
482 
TEST_F(MetricsTest,TimeToInitialize)483 TEST_F(MetricsTest, TimeToInitialize) {
484   EXPECT_CALL(library_,
485       SendToUMA("Network.Shill.Cellular.TimeToInitialize",
486                 Ge(0),
487                 Metrics::kMetricTimeToInitializeMillisecondsMin,
488                 Metrics::kMetricTimeToInitializeMillisecondsMax,
489                 Metrics::kMetricTimeToInitializeMillisecondsNumBuckets));
490   const int kInterfaceIndex = 1;
491   metrics_.RegisterDevice(kInterfaceIndex, Technology::kCellular);
492   metrics_.NotifyDeviceInitialized(kInterfaceIndex);
493 }
494 
TEST_F(MetricsTest,TimeToScan)495 TEST_F(MetricsTest, TimeToScan) {
496   EXPECT_CALL(library_,
497       SendToUMA("Network.Shill.Cellular.TimeToScan",
498                 Ge(0),
499                 Metrics::kMetricTimeToScanMillisecondsMin,
500                 Metrics::kMetricTimeToScanMillisecondsMax,
501                 Metrics::kMetricTimeToScanMillisecondsNumBuckets));
502   const int kInterfaceIndex = 1;
503   metrics_.RegisterDevice(kInterfaceIndex, Technology::kCellular);
504   metrics_.NotifyDeviceScanStarted(kInterfaceIndex);
505   metrics_.NotifyDeviceScanFinished(kInterfaceIndex);
506 }
507 
TEST_F(MetricsTest,TimeToScanAndConnect)508 TEST_F(MetricsTest, TimeToScanAndConnect) {
509   EXPECT_CALL(library_,
510       SendToUMA("Network.Shill.Wifi.TimeToScan",
511                 Ge(0),
512                 Metrics::kMetricTimeToScanMillisecondsMin,
513                 Metrics::kMetricTimeToScanMillisecondsMax,
514                 Metrics::kMetricTimeToScanMillisecondsNumBuckets));
515   const int kInterfaceIndex = 1;
516   metrics_.RegisterDevice(kInterfaceIndex, Technology::kWifi);
517   metrics_.NotifyDeviceScanStarted(kInterfaceIndex);
518   metrics_.NotifyDeviceScanFinished(kInterfaceIndex);
519 
520   EXPECT_CALL(library_,
521       SendToUMA("Network.Shill.Wifi.TimeToConnect",
522                 Ge(0),
523                 Metrics::kMetricTimeToConnectMillisecondsMin,
524                 Metrics::kMetricTimeToConnectMillisecondsMax,
525                 Metrics::kMetricTimeToConnectMillisecondsNumBuckets));
526   EXPECT_CALL(library_,
527       SendToUMA("Network.Shill.Wifi.TimeToScanAndConnect",
528                 Ge(0),
529                 Metrics::kMetricTimeToScanMillisecondsMin,
530                 Metrics::kMetricTimeToScanMillisecondsMax +
531                     Metrics::kMetricTimeToConnectMillisecondsMax,
532                 Metrics::kMetricTimeToScanMillisecondsNumBuckets +
533                     Metrics::kMetricTimeToConnectMillisecondsNumBuckets));
534   metrics_.NotifyDeviceConnectStarted(kInterfaceIndex, false);
535   metrics_.NotifyDeviceConnectFinished(kInterfaceIndex);
536 }
537 
TEST_F(MetricsTest,SpontaneousConnect)538 TEST_F(MetricsTest, SpontaneousConnect) {
539   const int kInterfaceIndex = 1;
540   metrics_.RegisterDevice(kInterfaceIndex, Technology::kWifi);
541   EXPECT_CALL(library_,
542       SendToUMA("Network.Shill.Wifi.TimeToConnect",
543                 Ge(0),
544                 Metrics::kMetricTimeToConnectMillisecondsMin,
545                 Metrics::kMetricTimeToConnectMillisecondsMax,
546                 Metrics::kMetricTimeToConnectMillisecondsNumBuckets)).Times(0);
547   EXPECT_CALL(library_,
548       SendToUMA("Network.Shill.Wifi.TimeToScanAndConnect",
549                 Ge(0),
550                 Metrics::kMetricTimeToScanMillisecondsMin,
551                 Metrics::kMetricTimeToScanMillisecondsMax +
552                     Metrics::kMetricTimeToConnectMillisecondsMax,
553                 Metrics::kMetricTimeToScanMillisecondsNumBuckets +
554                     Metrics::kMetricTimeToConnectMillisecondsNumBuckets)).
555       Times(0);
556   // This simulates a connection that is not scan-based.
557   metrics_.NotifyDeviceConnectFinished(kInterfaceIndex);
558 }
559 
TEST_F(MetricsTest,ResetConnectTimer)560 TEST_F(MetricsTest, ResetConnectTimer) {
561   const int kInterfaceIndex = 1;
562   metrics_.RegisterDevice(kInterfaceIndex, Technology::kWifi);
563   chromeos_metrics::TimerReporterMock* mock_scan_timer =
564       new chromeos_metrics::TimerReporterMock;
565   metrics_.set_time_to_scan_timer(kInterfaceIndex, mock_scan_timer);
566   chromeos_metrics::TimerReporterMock* mock_connect_timer =
567       new chromeos_metrics::TimerReporterMock;
568   metrics_.set_time_to_connect_timer(kInterfaceIndex, mock_connect_timer);
569   chromeos_metrics::TimerReporterMock* mock_scan_connect_timer =
570       new chromeos_metrics::TimerReporterMock;
571   metrics_.set_time_to_scan_connect_timer(kInterfaceIndex,
572                                           mock_scan_connect_timer);
573   EXPECT_CALL(*mock_scan_timer, Reset()).Times(0);
574   EXPECT_CALL(*mock_connect_timer, Reset());
575   EXPECT_CALL(*mock_scan_connect_timer, Reset());
576   metrics_.ResetConnectTimer(kInterfaceIndex);
577 }
578 
TEST_F(MetricsTest,TimeToScanNoStart)579 TEST_F(MetricsTest, TimeToScanNoStart) {
580   EXPECT_CALL(library_,
581       SendToUMA("Network.Shill.Cellular.TimeToScan", _, _, _, _)).Times(0);
582   const int kInterfaceIndex = 1;
583   metrics_.RegisterDevice(kInterfaceIndex, Technology::kCellular);
584   metrics_.NotifyDeviceScanFinished(kInterfaceIndex);
585 }
586 
TEST_F(MetricsTest,TimeToScanIgnore)587 TEST_F(MetricsTest, TimeToScanIgnore) {
588   // Make sure TimeToScan is not sent if the elapsed time exceeds the max
589   // value.  This simulates the case where the device is in an area with no
590   // service.
591   const int kInterfaceIndex = 1;
592   metrics_.RegisterDevice(kInterfaceIndex, Technology::kCellular);
593   base::TimeDelta large_time_delta =
594       base::TimeDelta::FromMilliseconds(
595           Metrics::kMetricTimeToScanMillisecondsMax + 1);
596   chromeos_metrics::TimerReporterMock* mock_time_to_scan_timer =
597       new chromeos_metrics::TimerReporterMock;
598   metrics_.set_time_to_scan_timer(kInterfaceIndex, mock_time_to_scan_timer);
599   EXPECT_CALL(*mock_time_to_scan_timer, Stop()).WillOnce(Return(true));
600   EXPECT_CALL(*mock_time_to_scan_timer, GetElapsedTime(_)).
601       WillOnce(DoAll(SetArgumentPointee<0>(large_time_delta), Return(true)));
602   EXPECT_CALL(library_, SendToUMA(_, _, _, _, _)).Times(0);
603   metrics_.NotifyDeviceScanStarted(kInterfaceIndex);
604   metrics_.NotifyDeviceScanFinished(kInterfaceIndex);
605 }
606 
TEST_F(MetricsTest,Cellular3GPPRegistrationDelayedDropPosted)607 TEST_F(MetricsTest, Cellular3GPPRegistrationDelayedDropPosted) {
608   EXPECT_CALL(library_,
609       SendEnumToUMA(Metrics::kMetricCellular3GPPRegistrationDelayedDrop,
610                     Metrics::kCellular3GPPRegistrationDelayedDropPosted,
611                     Metrics::kCellular3GPPRegistrationDelayedDropMax));
612   metrics_.Notify3GPPRegistrationDelayedDropPosted();
613   Mock::VerifyAndClearExpectations(&library_);
614 
615   EXPECT_CALL(library_,
616       SendEnumToUMA(Metrics::kMetricCellular3GPPRegistrationDelayedDrop,
617                     Metrics::kCellular3GPPRegistrationDelayedDropCanceled,
618                     Metrics::kCellular3GPPRegistrationDelayedDropMax));
619   metrics_.Notify3GPPRegistrationDelayedDropCanceled();
620 }
621 
TEST_F(MetricsTest,CellularAutoConnect)622 TEST_F(MetricsTest, CellularAutoConnect) {
623   EXPECT_CALL(library_,
624       SendToUMA("Network.Shill.Cellular.TimeToConnect",
625                 Ge(0),
626                 Metrics::kMetricTimeToConnectMillisecondsMin,
627                 Metrics::kMetricTimeToConnectMillisecondsMax,
628                 Metrics::kMetricTimeToConnectMillisecondsNumBuckets));
629   EXPECT_CALL(library_,
630       SendToUMA(Metrics::kMetricCellularAutoConnectTotalTime,
631                 Ge(0),
632                 Metrics::kMetricCellularAutoConnectTotalTimeMin,
633                 Metrics::kMetricCellularAutoConnectTotalTimeMax,
634                 Metrics::kMetricCellularAutoConnectTotalTimeNumBuckets));
635   EXPECT_CALL(library_,
636       SendToUMA(Metrics::kMetricCellularAutoConnectTries,
637                 2,
638                 Metrics::kMetricCellularAutoConnectTriesMin,
639                 Metrics::kMetricCellularAutoConnectTriesMax,
640                 Metrics::kMetricCellularAutoConnectTriesNumBuckets));
641   const int kInterfaceIndex = 1;
642   metrics_.RegisterDevice(kInterfaceIndex, Technology::kCellular);
643   metrics_.NotifyDeviceConnectStarted(kInterfaceIndex, true);
644   metrics_.NotifyDeviceConnectStarted(kInterfaceIndex, true);
645   metrics_.NotifyDeviceConnectFinished(kInterfaceIndex);
646 }
647 
TEST_F(MetricsTest,CellularDrop)648 TEST_F(MetricsTest, CellularDrop) {
649   const char* kUMATechnologyStrings[] = {
650       kNetworkTechnology1Xrtt,
651       kNetworkTechnologyEdge,
652       kNetworkTechnologyEvdo,
653       kNetworkTechnologyGprs,
654       kNetworkTechnologyGsm,
655       kNetworkTechnologyHspa,
656       kNetworkTechnologyHspaPlus,
657       kNetworkTechnologyLte,
658       kNetworkTechnologyUmts,
659       "Unknown" };
660 
661   const uint16_t signal_strength = 100;
662   const int kInterfaceIndex = 1;
663   metrics_.RegisterDevice(kInterfaceIndex, Technology::kCellular);
664   for (size_t index = 0; index < arraysize(kUMATechnologyStrings); ++index) {
665     EXPECT_CALL(library_,
666         SendEnumToUMA(Metrics::kMetricCellularDrop,
667                       index,
668                       Metrics::kCellularDropTechnologyMax));
669     EXPECT_CALL(library_,
670         SendToUMA(Metrics::kMetricCellularSignalStrengthBeforeDrop,
671                   signal_strength,
672                   Metrics::kMetricCellularSignalStrengthBeforeDropMin,
673                   Metrics::kMetricCellularSignalStrengthBeforeDropMax,
674                   Metrics::kMetricCellularSignalStrengthBeforeDropNumBuckets));
675     metrics_.NotifyCellularDeviceDrop(kUMATechnologyStrings[index],
676                                       signal_strength);
677     Mock::VerifyAndClearExpectations(&library_);
678   }
679 }
680 
TEST_F(MetricsTest,CellularDeviceFailure)681 TEST_F(MetricsTest, CellularDeviceFailure) {
682   EXPECT_CALL(library_, SendEnumToUMA(Metrics::kMetricCellularFailure,
683                                       Metrics::kMetricCellularConnectionFailure,
684                                       Metrics::kMetricCellularMaxFailure));
685   metrics_.NotifyCellularDeviceConnectionFailure();
686 }
687 
TEST_F(MetricsTest,CellularOutOfCreditsReason)688 TEST_F(MetricsTest, CellularOutOfCreditsReason) {
689   EXPECT_CALL(library_,
690       SendEnumToUMA(Metrics::kMetricCellularOutOfCreditsReason,
691                     Metrics::kCellularOutOfCreditsReasonConnectDisconnectLoop,
692                     Metrics::kCellularOutOfCreditsReasonMax));
693   metrics_.NotifyCellularOutOfCredits(
694       Metrics::kCellularOutOfCreditsReasonConnectDisconnectLoop);
695   Mock::VerifyAndClearExpectations(&library_);
696 
697   EXPECT_CALL(library_,
698       SendEnumToUMA(Metrics::kMetricCellularOutOfCreditsReason,
699                     Metrics::kCellularOutOfCreditsReasonTxCongested,
700                     Metrics::kCellularOutOfCreditsReasonMax));
701   metrics_.NotifyCellularOutOfCredits(
702       Metrics::kCellularOutOfCreditsReasonTxCongested);
703   Mock::VerifyAndClearExpectations(&library_);
704 
705   EXPECT_CALL(library_,
706       SendEnumToUMA(Metrics::kMetricCellularOutOfCreditsReason,
707                     Metrics::kCellularOutOfCreditsReasonElongatedTimeWait,
708                     Metrics::kCellularOutOfCreditsReasonMax));
709   metrics_.NotifyCellularOutOfCredits(
710       Metrics::kCellularOutOfCreditsReasonElongatedTimeWait);
711 }
712 
TEST_F(MetricsTest,CorruptedProfile)713 TEST_F(MetricsTest, CorruptedProfile) {
714   EXPECT_CALL(library_, SendEnumToUMA(Metrics::kMetricCorruptedProfile,
715                                       Metrics::kCorruptedProfile,
716                                       Metrics::kCorruptedProfileMax));
717   metrics_.NotifyCorruptedProfile();
718 }
719 
TEST_F(MetricsTest,Logging)720 TEST_F(MetricsTest, Logging) {
721   NiceScopedMockLog log;
722   const int kVerboseLevel5 = -5;
723   ScopeLogger::GetInstance()->EnableScopesByName("+metrics");
724   ScopeLogger::GetInstance()->set_verbose_level(-kVerboseLevel5);
725 
726   const string kEnumName("fake-enum");
727   const int kEnumValue = 1;
728   const int kEnumMax = 12;
729   EXPECT_CALL(log, Log(kVerboseLevel5, _,
730                        "(metrics) Sending enum fake-enum with value 1."));
731   EXPECT_CALL(library_, SendEnumToUMA(kEnumName, kEnumValue, kEnumMax));
732   metrics_.SendEnumToUMA(kEnumName, kEnumValue, kEnumMax);
733 
734   const string kMetricName("fake-metric");
735   const int kMetricValue = 2;
736   const int kHistogramMin = 0;
737   const int kHistogramMax = 100;
738   const int kHistogramBuckets = 10;
739   EXPECT_CALL(log, Log(kVerboseLevel5, _,
740                        "(metrics) Sending metric fake-metric with value 2."));
741   EXPECT_CALL(library_, SendToUMA(kMetricName, kMetricValue, kHistogramMin,
742                                   kHistogramMax, kHistogramBuckets));
743   metrics_.SendToUMA(kMetricName, kMetricValue,
744                      kHistogramMin, kHistogramMax, kHistogramBuckets);
745 
746   ScopeLogger::GetInstance()->EnableScopesByName("-metrics");
747   ScopeLogger::GetInstance()->set_verbose_level(0);
748 }
749 
TEST_F(MetricsTest,NotifyServicesOnSameNetwork)750 TEST_F(MetricsTest, NotifyServicesOnSameNetwork) {
751   EXPECT_CALL(library_,
752       SendToUMA(Metrics::kMetricServicesOnSameNetwork,
753                 1,
754                 Metrics::kMetricServicesOnSameNetworkMin,
755                 Metrics::kMetricServicesOnSameNetworkMax,
756                 Metrics::kMetricServicesOnSameNetworkNumBuckets));
757   metrics_.NotifyServicesOnSameNetwork(1);
758 }
759 
TEST_F(MetricsTest,NotifyUserInitiatedEvent)760 TEST_F(MetricsTest, NotifyUserInitiatedEvent) {
761   EXPECT_CALL(library_,
762       SendEnumToUMA(Metrics::kMetricUserInitiatedEvents,
763                     Metrics::kUserInitiatedEventWifiScan,
764                     Metrics::kUserInitiatedEventMax));
765   metrics_.NotifyUserInitiatedEvent(Metrics::kUserInitiatedEventWifiScan);
766 }
767 
TEST_F(MetricsTest,NotifyWifiTxBitrate)768 TEST_F(MetricsTest, NotifyWifiTxBitrate) {
769   EXPECT_CALL(library_,
770       SendToUMA(Metrics::kMetricWifiTxBitrate,
771                 1,
772                 Metrics::kMetricWifiTxBitrateMin,
773                 Metrics::kMetricWifiTxBitrateMax,
774                 Metrics::kMetricWifiTxBitrateNumBuckets));
775   metrics_.NotifyWifiTxBitrate(1);
776 }
777 
TEST_F(MetricsTest,NotifyUserInitiatedConnectionResult)778 TEST_F(MetricsTest, NotifyUserInitiatedConnectionResult) {
779   EXPECT_CALL(library_,
780       SendEnumToUMA(Metrics::kMetricWifiUserInitiatedConnectionResult,
781                     Metrics::kUserInitiatedConnectionResultSuccess,
782                     Metrics::kUserInitiatedConnectionResultMax));
783   metrics_.NotifyUserInitiatedConnectionResult(
784       Metrics::kMetricWifiUserInitiatedConnectionResult,
785       Metrics::kUserInitiatedConnectionResultSuccess);
786 }
787 
TEST_F(MetricsTest,NotifyFallbackDNSTestResult)788 TEST_F(MetricsTest, NotifyFallbackDNSTestResult) {
789   EXPECT_CALL(library_,
790       SendEnumToUMA("Network.Shill.Wifi.FallbackDNSTestResult",
791                     Metrics::kFallbackDNSTestResultSuccess,
792                     Metrics::kFallbackDNSTestResultMax));
793   metrics_.NotifyFallbackDNSTestResult(Technology::kWifi,
794                                        Metrics::kFallbackDNSTestResultSuccess);
795 }
796 
TEST_F(MetricsTest,NotifyNetworkProblemDetected)797 TEST_F(MetricsTest, NotifyNetworkProblemDetected) {
798   EXPECT_CALL(library_,
799       SendEnumToUMA("Network.Shill.Wifi.NetworkProblemDetected",
800                     Metrics::kNetworkProblemDNSFailure,
801                     Metrics::kNetworkProblemMax));
802   metrics_.NotifyNetworkProblemDetected(Technology::kWifi,
803                                         Metrics::kNetworkProblemDNSFailure);
804 }
805 
TEST_F(MetricsTest,NotifyDhcpClientStatus)806 TEST_F(MetricsTest, NotifyDhcpClientStatus) {
807   EXPECT_CALL(library_,
808       SendEnumToUMA("Network.Shill.DHCPClientStatus",
809                     Metrics::kDhcpClientStatusReboot,
810                     Metrics::kDhcpClientStatusMax));
811   metrics_.NotifyDhcpClientStatus(Metrics::kDhcpClientStatusReboot);
812 }
813 
TEST_F(MetricsTest,DeregisterDevice)814 TEST_F(MetricsTest, DeregisterDevice) {
815   const int kInterfaceIndex = 1;
816   metrics_.RegisterDevice(kInterfaceIndex, Technology::kCellular);
817 
818   EXPECT_CALL(library_,
819       SendEnumToUMA("Network.Shill.DeviceRemovedEvent",
820                     Metrics::kDeviceTechnologyTypeCellular,
821                     Metrics::kDeviceTechnologyTypeMax));
822   metrics_.DeregisterDevice(kInterfaceIndex);
823 }
824 
TEST_F(MetricsTest,NotifyWakeOnWiFiFeaturesEnabledState)825 TEST_F(MetricsTest, NotifyWakeOnWiFiFeaturesEnabledState) {
826   const Metrics::WakeOnWiFiFeaturesEnabledState state =
827       Metrics::kWakeOnWiFiFeaturesEnabledStateNone;
828   EXPECT_CALL(
829       library_,
830       SendEnumToUMA("Network.Shill.WiFi.WakeOnWiFiFeaturesEnabledState", state,
831                     Metrics::kWakeOnWiFiFeaturesEnabledStateMax));
832   metrics_.NotifyWakeOnWiFiFeaturesEnabledState(state);
833 }
834 
TEST_F(MetricsTest,NotifyVerifyWakeOnWiFiSettingsResult)835 TEST_F(MetricsTest, NotifyVerifyWakeOnWiFiSettingsResult) {
836   const Metrics::VerifyWakeOnWiFiSettingsResult result =
837       Metrics::kVerifyWakeOnWiFiSettingsResultSuccess;
838   EXPECT_CALL(
839       library_,
840       SendEnumToUMA("Network.Shill.WiFi.VerifyWakeOnWiFiSettingsResult", result,
841                     Metrics::kVerifyWakeOnWiFiSettingsResultMax));
842   metrics_.NotifyVerifyWakeOnWiFiSettingsResult(result);
843 }
844 
TEST_F(MetricsTest,NotifyConnectedToServiceAfterWake)845 TEST_F(MetricsTest, NotifyConnectedToServiceAfterWake) {
846   const Metrics::WiFiConnectionStatusAfterWake status =
847       Metrics::kWiFiConnetionStatusAfterWakeOnWiFiEnabledWakeConnected;
848   EXPECT_CALL(library_,
849               SendEnumToUMA("Network.Shill.WiFi.WiFiConnectionStatusAfterWake",
850                             status, Metrics::kWiFiConnetionStatusAfterWakeMax));
851   metrics_.NotifyConnectedToServiceAfterWake(status);
852 }
853 
TEST_F(MetricsTest,NotifyWakeOnWiFiThrottled)854 TEST_F(MetricsTest, NotifyWakeOnWiFiThrottled) {
855   EXPECT_FALSE(metrics_.wake_on_wifi_throttled_);
856   metrics_.NotifyWakeOnWiFiThrottled();
857   EXPECT_TRUE(metrics_.wake_on_wifi_throttled_);
858 }
859 
TEST_F(MetricsTest,NotifySuspendWithWakeOnWiFiEnabledDone)860 TEST_F(MetricsTest, NotifySuspendWithWakeOnWiFiEnabledDone) {
861   const Metrics::WakeOnWiFiThrottled result_true =
862       Metrics::kWakeOnWiFiThrottledTrue;
863   metrics_.wake_on_wifi_throttled_ = true;
864   EXPECT_CALL(library_,
865               SendEnumToUMA("Network.Shill.WiFi.WakeOnWiFiThrottled",
866                             result_true, Metrics::kWakeOnWiFiThrottledMax));
867   metrics_.NotifySuspendWithWakeOnWiFiEnabledDone();
868 
869   const Metrics::WakeOnWiFiThrottled result_false =
870       Metrics::kWakeOnWiFiThrottledFalse;
871   metrics_.wake_on_wifi_throttled_ = false;
872   EXPECT_CALL(library_,
873               SendEnumToUMA("Network.Shill.WiFi.WakeOnWiFiThrottled",
874                             result_false, Metrics::kWakeOnWiFiThrottledMax));
875   metrics_.NotifySuspendWithWakeOnWiFiEnabledDone();
876 }
877 
TEST_F(MetricsTest,NotifySuspendActionsCompleted_Success)878 TEST_F(MetricsTest, NotifySuspendActionsCompleted_Success) {
879   base::TimeDelta non_zero_time_delta = base::TimeDelta::FromMilliseconds(1);
880   chromeos_metrics::TimerMock* mock_time_suspend_actions_timer =
881       new chromeos_metrics::TimerMock;
882   metrics_.set_time_suspend_actions_timer(mock_time_suspend_actions_timer);
883   metrics_.wake_reason_received_ = true;
884   EXPECT_CALL(*mock_time_suspend_actions_timer, GetElapsedTime(_))
885       .WillOnce(
886           DoAll(SetArgumentPointee<0>(non_zero_time_delta), Return(true)));
887   EXPECT_CALL(*mock_time_suspend_actions_timer, HasStarted())
888       .WillOnce(Return(true));
889   EXPECT_CALL(library_,
890               SendToUMA(Metrics::kMetricSuspendActionTimeTaken,
891                         non_zero_time_delta.InMilliseconds(),
892                         Metrics::kMetricSuspendActionTimeTakenMillisecondsMin,
893                         Metrics::kMetricSuspendActionTimeTakenMillisecondsMax,
894                         Metrics::kTimerHistogramNumBuckets));
895   EXPECT_CALL(library_, SendEnumToUMA(Metrics::kMetricSuspendActionResult,
896                                       Metrics::kSuspendActionResultSuccess,
897                                       Metrics::kSuspendActionResultMax));
898   metrics_.NotifySuspendActionsCompleted(true);
899   EXPECT_FALSE(metrics_.wake_reason_received_);
900 }
901 
TEST_F(MetricsTest,NotifySuspendActionsCompleted_Failure)902 TEST_F(MetricsTest, NotifySuspendActionsCompleted_Failure) {
903   base::TimeDelta non_zero_time_delta = base::TimeDelta::FromMilliseconds(1);
904   chromeos_metrics::TimerMock* mock_time_suspend_actions_timer =
905       new chromeos_metrics::TimerMock;
906   metrics_.set_time_suspend_actions_timer(mock_time_suspend_actions_timer);
907   metrics_.wake_reason_received_ = true;
908   EXPECT_CALL(*mock_time_suspend_actions_timer, GetElapsedTime(_))
909       .WillOnce(
910           DoAll(SetArgumentPointee<0>(non_zero_time_delta), Return(true)));
911   EXPECT_CALL(*mock_time_suspend_actions_timer, HasStarted())
912       .WillOnce(Return(true));
913   EXPECT_CALL(library_,
914               SendToUMA(Metrics::kMetricSuspendActionTimeTaken,
915                         non_zero_time_delta.InMilliseconds(),
916                         Metrics::kMetricSuspendActionTimeTakenMillisecondsMin,
917                         Metrics::kMetricSuspendActionTimeTakenMillisecondsMax,
918                         Metrics::kTimerHistogramNumBuckets));
919   EXPECT_CALL(library_, SendEnumToUMA(Metrics::kMetricSuspendActionResult,
920                                       Metrics::kSuspendActionResultFailure,
921                                       Metrics::kSuspendActionResultMax));
922   metrics_.NotifySuspendActionsCompleted(false);
923   EXPECT_FALSE(metrics_.wake_reason_received_);
924 }
925 
TEST_F(MetricsTest,NotifyDarkResumeActionsCompleted_Success)926 TEST_F(MetricsTest, NotifyDarkResumeActionsCompleted_Success) {
927   metrics_.num_scan_results_expected_in_dark_resume_ = 0;
928   base::TimeDelta non_zero_time_delta = base::TimeDelta::FromMilliseconds(1);
929   chromeos_metrics::TimerMock* mock_time_dark_resume_actions_timer =
930       new chromeos_metrics::TimerMock;
931   metrics_.set_time_dark_resume_actions_timer(
932       mock_time_dark_resume_actions_timer);
933   metrics_.wake_reason_received_ = true;
934   const int non_zero_num_retries = 3;
935   metrics_.dark_resume_scan_retries_ = non_zero_num_retries;
936   EXPECT_CALL(*mock_time_dark_resume_actions_timer, GetElapsedTime(_))
937       .WillOnce(
938           DoAll(SetArgumentPointee<0>(non_zero_time_delta), Return(true)));
939   EXPECT_CALL(*mock_time_dark_resume_actions_timer, HasStarted())
940       .WillOnce(Return(true));
941   EXPECT_CALL(
942       library_,
943       SendToUMA(Metrics::kMetricDarkResumeActionTimeTaken,
944                 non_zero_time_delta.InMilliseconds(),
945                 Metrics::kMetricDarkResumeActionTimeTakenMillisecondsMin,
946                 Metrics::kMetricDarkResumeActionTimeTakenMillisecondsMax,
947                 Metrics::kTimerHistogramNumBuckets));
948   EXPECT_CALL(library_, SendEnumToUMA(Metrics::kMetricDarkResumeActionResult,
949                                       Metrics::kDarkResumeActionResultSuccess,
950                                       Metrics::kDarkResumeActionResultMax));
951   EXPECT_CALL(
952       library_,
953       SendEnumToUMA(Metrics::kMetricDarkResumeUnmatchedScanResultReceived,
954                     Metrics::kDarkResumeUnmatchedScanResultsReceivedFalse,
955                     Metrics::kDarkResumeUnmatchedScanResultsReceivedMax));
956   EXPECT_CALL(library_, SendToUMA(Metrics::kMetricDarkResumeScanNumRetries,
957                                   non_zero_num_retries,
958                                   Metrics::kMetricDarkResumeScanNumRetriesMin,
959                                   Metrics::kMetricDarkResumeScanNumRetriesMax,
960                                   Metrics::kTimerHistogramNumBuckets));
961   metrics_.NotifyDarkResumeActionsCompleted(true);
962   EXPECT_FALSE(metrics_.wake_reason_received_);
963 }
964 
TEST_F(MetricsTest,NotifyDarkResumeActionsCompleted_Failure)965 TEST_F(MetricsTest, NotifyDarkResumeActionsCompleted_Failure) {
966   metrics_.num_scan_results_expected_in_dark_resume_ = 0;
967   base::TimeDelta non_zero_time_delta = base::TimeDelta::FromMilliseconds(1);
968   chromeos_metrics::TimerMock* mock_time_dark_resume_actions_timer =
969       new chromeos_metrics::TimerMock;
970   metrics_.set_time_dark_resume_actions_timer(
971       mock_time_dark_resume_actions_timer);
972   metrics_.wake_reason_received_ = true;
973   const int non_zero_num_retries = 3;
974   metrics_.dark_resume_scan_retries_ = non_zero_num_retries;
975   EXPECT_CALL(*mock_time_dark_resume_actions_timer, GetElapsedTime(_))
976       .WillOnce(
977           DoAll(SetArgumentPointee<0>(non_zero_time_delta), Return(true)));
978   EXPECT_CALL(*mock_time_dark_resume_actions_timer, HasStarted())
979       .WillOnce(Return(true));
980   EXPECT_CALL(
981       library_,
982       SendToUMA(Metrics::kMetricDarkResumeActionTimeTaken,
983                 non_zero_time_delta.InMilliseconds(),
984                 Metrics::kMetricDarkResumeActionTimeTakenMillisecondsMin,
985                 Metrics::kMetricDarkResumeActionTimeTakenMillisecondsMax,
986                 Metrics::kTimerHistogramNumBuckets));
987   EXPECT_CALL(library_, SendEnumToUMA(Metrics::kMetricDarkResumeActionResult,
988                                       Metrics::kDarkResumeActionResultFailure,
989                                       Metrics::kDarkResumeActionResultMax));
990   EXPECT_CALL(
991       library_,
992       SendEnumToUMA(Metrics::kMetricDarkResumeUnmatchedScanResultReceived,
993                     Metrics::kDarkResumeUnmatchedScanResultsReceivedFalse,
994                     Metrics::kDarkResumeUnmatchedScanResultsReceivedMax));
995   EXPECT_CALL(library_, SendToUMA(Metrics::kMetricDarkResumeScanNumRetries,
996                                   non_zero_num_retries,
997                                   Metrics::kMetricDarkResumeScanNumRetriesMin,
998                                   Metrics::kMetricDarkResumeScanNumRetriesMax,
999                                   Metrics::kTimerHistogramNumBuckets));
1000   metrics_.NotifyDarkResumeActionsCompleted(false);
1001   EXPECT_FALSE(metrics_.wake_reason_received_);
1002 }
1003 
TEST_F(MetricsTest,NotifySuspendActionsStarted)1004 TEST_F(MetricsTest, NotifySuspendActionsStarted) {
1005   metrics_.time_suspend_actions_timer->Stop();
1006   metrics_.wake_on_wifi_throttled_ = true;
1007   metrics_.NotifySuspendActionsStarted();
1008   EXPECT_TRUE(metrics_.time_suspend_actions_timer->HasStarted());
1009   EXPECT_FALSE(metrics_.wake_on_wifi_throttled_);
1010 }
1011 
TEST_F(MetricsTest,NotifyDarkResumeActionsStarted)1012 TEST_F(MetricsTest, NotifyDarkResumeActionsStarted) {
1013   metrics_.time_dark_resume_actions_timer->Stop();
1014   metrics_.num_scan_results_expected_in_dark_resume_ = 2;
1015   metrics_.dark_resume_scan_retries_ = 3;
1016   metrics_.NotifyDarkResumeActionsStarted();
1017   EXPECT_TRUE(metrics_.time_dark_resume_actions_timer->HasStarted());
1018   EXPECT_EQ(0, metrics_.num_scan_results_expected_in_dark_resume_);
1019   EXPECT_EQ(0, metrics_.dark_resume_scan_retries_);
1020 }
1021 
TEST_F(MetricsTest,NotifyDarkResumeInitiateScan)1022 TEST_F(MetricsTest, NotifyDarkResumeInitiateScan) {
1023   metrics_.num_scan_results_expected_in_dark_resume_ = 0;
1024   metrics_.NotifyDarkResumeInitiateScan();
1025   EXPECT_EQ(1, metrics_.num_scan_results_expected_in_dark_resume_);
1026 }
1027 
TEST_F(MetricsTest,NotifyDarkResumeScanResultsReceived)1028 TEST_F(MetricsTest, NotifyDarkResumeScanResultsReceived) {
1029   metrics_.num_scan_results_expected_in_dark_resume_ = 1;
1030   metrics_.NotifyDarkResumeScanResultsReceived();
1031   EXPECT_EQ(0, metrics_.num_scan_results_expected_in_dark_resume_);
1032 }
1033 
TEST_F(MetricsTest,NotifyDarkResumeScanRetry)1034 TEST_F(MetricsTest, NotifyDarkResumeScanRetry) {
1035   const int initial_num_retries = 2;
1036   metrics_.dark_resume_scan_retries_ = initial_num_retries;
1037   metrics_.NotifyDarkResumeScanRetry();
1038   EXPECT_EQ(initial_num_retries + 1, metrics_.dark_resume_scan_retries_);
1039 }
1040 
TEST_F(MetricsTest,NotifyBeforeSuspendActions_InDarkResume)1041 TEST_F(MetricsTest, NotifyBeforeSuspendActions_InDarkResume) {
1042   const bool in_dark_resume = true;
1043   bool is_connected;
1044   metrics_.dark_resume_scan_retries_ = 1;
1045 
1046   is_connected = true;
1047   EXPECT_CALL(library_,
1048               SendEnumToUMA(Metrics::kMetricDarkResumeScanRetryResult,
1049                             Metrics::kDarkResumeScanRetryResultConnected,
1050                             Metrics::kDarkResumeScanRetryResultMax));
1051   metrics_.NotifyBeforeSuspendActions(is_connected, in_dark_resume);
1052 
1053   is_connected = false;
1054   EXPECT_CALL(library_,
1055               SendEnumToUMA(Metrics::kMetricDarkResumeScanRetryResult,
1056                             Metrics::kDarkResumeScanRetryResultNotConnected,
1057                             Metrics::kDarkResumeScanRetryResultMax));
1058   metrics_.NotifyBeforeSuspendActions(is_connected, in_dark_resume);
1059 }
1060 
TEST_F(MetricsTest,NotifyBeforeSuspendActions_NotInDarkResume)1061 TEST_F(MetricsTest, NotifyBeforeSuspendActions_NotInDarkResume) {
1062   const bool in_dark_resume = false;
1063   bool is_connected;
1064   metrics_.dark_resume_scan_retries_ = 1;
1065 
1066   is_connected = true;
1067   EXPECT_CALL(library_, SendEnumToUMA(_, _, _)).Times(0);
1068   metrics_.NotifyBeforeSuspendActions(is_connected, in_dark_resume);
1069 
1070   is_connected = false;
1071   EXPECT_CALL(library_, SendEnumToUMA(_, _, _)).Times(0);
1072   metrics_.NotifyBeforeSuspendActions(is_connected, in_dark_resume);
1073 }
1074 
TEST_F(MetricsTest,NotifyConnectionDiagnosticsIssue_Success)1075 TEST_F(MetricsTest, NotifyConnectionDiagnosticsIssue_Success) {
1076   const string& issue = ConnectionDiagnostics::kIssueIPCollision;
1077   EXPECT_CALL(library_,
1078               SendEnumToUMA(Metrics::kMetricConnectionDiagnosticsIssue,
1079                             Metrics::kConnectionDiagnosticsIssueIPCollision,
1080                             Metrics::kConnectionDiagnosticsIssueMax));
1081   metrics_.NotifyConnectionDiagnosticsIssue(issue);
1082 }
1083 
TEST_F(MetricsTest,NotifyConnectionDiagnosticsIssue_Failure)1084 TEST_F(MetricsTest, NotifyConnectionDiagnosticsIssue_Failure) {
1085   const string& invalid_issue = "Invalid issue string.";
1086   EXPECT_CALL(library_, SendEnumToUMA(_, _, _)).Times(0);
1087   metrics_.NotifyConnectionDiagnosticsIssue(invalid_issue);
1088 }
1089 
1090 #ifndef NDEBUG
1091 
1092 typedef MetricsTest MetricsDeathTest;
1093 
TEST_F(MetricsDeathTest,PortalDetectionResultToEnumDNSSuccess)1094 TEST_F(MetricsDeathTest, PortalDetectionResultToEnumDNSSuccess) {
1095   PortalDetector::Result result(
1096       ConnectivityTrial::Result(ConnectivityTrial::kPhaseDNS,
1097                                 ConnectivityTrial::kStatusSuccess),
1098       0, true);
1099   EXPECT_DEATH(Metrics::PortalDetectionResultToEnum(result),
1100                "Final result status 1 is not allowed in the DNS phase");
1101 }
1102 
TEST_F(MetricsDeathTest,PortalDetectionResultToEnumConnectionSuccess)1103 TEST_F(MetricsDeathTest, PortalDetectionResultToEnumConnectionSuccess) {
1104   PortalDetector::Result result(
1105       ConnectivityTrial::Result(ConnectivityTrial::kPhaseConnection,
1106                                 ConnectivityTrial::kStatusSuccess),
1107       0, true);
1108   EXPECT_DEATH(Metrics::PortalDetectionResultToEnum(result),
1109                "Final result status 1 is not allowed in the Connection phase");
1110 }
1111 
TEST_F(MetricsDeathTest,PortalDetectionResultToEnumHTTPSuccess)1112 TEST_F(MetricsDeathTest, PortalDetectionResultToEnumHTTPSuccess) {
1113   PortalDetector::Result result(
1114       ConnectivityTrial::Result(ConnectivityTrial::kPhaseHTTP,
1115                                 ConnectivityTrial::kStatusSuccess),
1116       0, true);
1117   EXPECT_DEATH(Metrics::PortalDetectionResultToEnum(result),
1118                "Final result status 1 is not allowed in the HTTP phase");
1119 }
1120 
1121 #endif  // NDEBUG
1122 
1123 }  // namespace shill
1124