ExtensionSyncService: listen for relevant changes instead of being explicitly called...
[chromium-blink-merge.git] / chrome / browser / chrome_browser_application_mac_unittest.mm
blob7b2adc9cdb854908a9aa6b97aa98430ac8e5aa25
1 // Copyright (c) 2012 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 #import <Cocoa/Cocoa.h>
7 #include "base/memory/scoped_ptr.h"
8 #include "base/metrics/histogram.h"
9 #include "base/metrics/histogram_samples.h"
10 #include "base/metrics/statistics_recorder.h"
11 #import "chrome/browser/chrome_browser_application_mac.h"
12 #include "testing/gtest/include/gtest/gtest.h"
14 using base::HistogramBase;
15 using base::HistogramSamples;
16 using base::StatisticsRecorder;
18 namespace chrome_browser_application_mac {
20 // Generate an NSException with the given name.
21 NSException* ExceptionNamed(NSString* name) {
22   return [NSException exceptionWithName:name
23                                  reason:@"No reason given"
24                                userInfo:nil];
27 // Helper to keep binning expectations readible.
28 size_t BinForExceptionNamed(NSString* name) {
29   return BinForException(ExceptionNamed(name));
32 TEST(ChromeApplicationMacTest, ExceptionBinning) {
33   // These exceptions must be in this order.
34   EXPECT_EQ(BinForExceptionNamed(NSGenericException), 0U);
35   EXPECT_EQ(BinForExceptionNamed(NSRangeException), 1U);
36   EXPECT_EQ(BinForExceptionNamed(NSInvalidArgumentException), 2U);
37   EXPECT_EQ(BinForExceptionNamed(NSMallocException), 3U);
39   // Random other exceptions map to |kUnknownNSException|.
40   EXPECT_EQ(BinForExceptionNamed(@"CustomName"), kUnknownNSException);
41   EXPECT_EQ(BinForExceptionNamed(@"Custom Name"), kUnknownNSException);
42   EXPECT_EQ(BinForExceptionNamed(@""), kUnknownNSException);
43   EXPECT_EQ(BinForException(nil), kUnknownNSException);
46 TEST(ChromeApplicationMacTest, RecordException) {
47   // Start up a histogram recorder.
48   // TODO(rtenneti): Leaks StatisticsRecorder and will update suppressions.
49   base::StatisticsRecorder::Initialize();
51   StatisticsRecorder::Histograms histograms;
52   StatisticsRecorder::GetSnapshot("OSX.NSException", &histograms);
53   EXPECT_EQ(0U, histograms.size());
55   // Record some known exceptions.
56   RecordExceptionWithUma(ExceptionNamed(NSGenericException));
57   RecordExceptionWithUma(ExceptionNamed(NSGenericException));
58   RecordExceptionWithUma(ExceptionNamed(NSGenericException));
59   RecordExceptionWithUma(ExceptionNamed(NSGenericException));
60   RecordExceptionWithUma(ExceptionNamed(NSRangeException));
61   RecordExceptionWithUma(ExceptionNamed(NSInvalidArgumentException));
62   RecordExceptionWithUma(ExceptionNamed(NSInvalidArgumentException));
63   RecordExceptionWithUma(ExceptionNamed(NSInvalidArgumentException));
64   RecordExceptionWithUma(ExceptionNamed(NSMallocException));
65   RecordExceptionWithUma(ExceptionNamed(NSMallocException));
67   // Record some unknown exceptions.
68   RecordExceptionWithUma(ExceptionNamed(@"CustomName"));
69   RecordExceptionWithUma(ExceptionNamed(@"Custom Name"));
70   RecordExceptionWithUma(ExceptionNamed(@""));
71   RecordExceptionWithUma(nil);
73   // We should have exactly the right number of exceptions.
74   StatisticsRecorder::GetSnapshot("OSX.NSException", &histograms);
75   EXPECT_EQ(1U, histograms.size());
76   EXPECT_EQ(HistogramBase::kUmaTargetedHistogramFlag, histograms[0]->flags());
78   scoped_ptr<HistogramSamples> samples(histograms[0]->SnapshotSamples());
79   EXPECT_EQ(4, samples->GetCount(0));
80   EXPECT_EQ(1, samples->GetCount(1));
81   EXPECT_EQ(3, samples->GetCount(2));
82   EXPECT_EQ(2, samples->GetCount(3));
84   // The unknown exceptions should end up in the overflow bucket.
85   EXPECT_TRUE(histograms[0]->HasConstructionArguments(1,
86                                                       kUnknownNSException,
87                                                       kUnknownNSException + 1));
88   EXPECT_EQ(4, samples->GetCount(kUnknownNSException));
91 }  // chrome_browser_application_mac