Bug 1355498 - Refactor commonly used actions in functions within TestHistograms.cpp. r=Dexter
authorAvikalpa Kundu <avikalpakundu@gmail.com>
Thu, 25 May 2017 10:45:00 -0400
changeset 585313 12e45fe24d0f2bc8aebab20a63c4a5aaaa4636ff
parent 585312 e5c93b74884bac69b5c97994bb2b2e94999a5204
child 585314 1bf1d62654f54a93ff38701ca6a17d4def4998a6
push id61093
push userdgottwald@mozilla.com
push dateFri, 26 May 2017 20:16:26 +0000
reviewersDexter
bugs1355498
milestone55.0a1
Bug 1355498 - Refactor commonly used actions in functions within TestHistograms.cpp. r=Dexter
toolkit/components/telemetry/tests/gtest/TestHistograms.cpp
--- a/toolkit/components/telemetry/tests/gtest/TestHistograms.cpp
+++ b/toolkit/components/telemetry/tests/gtest/TestHistograms.cpp
@@ -4,104 +4,116 @@
  */
 
 #include "gtest/gtest.h"
 #include "js/Conversions.h"
 #include "nsITelemetry.h"
 #include "Telemetry.h"
 #include "TelemetryFixture.h"
 
+using namespace mozilla;
+
+namespace {
+
+void
+GetAndClearHistogram(JSContext* cx, nsCOMPtr<nsITelemetry> mTelemetry,
+                     const nsACString &name, bool is_keyed)
+{
+  JS::RootedValue testHistogram(cx);
+  nsresult rv = is_keyed ? mTelemetry->GetKeyedHistogramById(name, cx, &testHistogram)
+                         : mTelemetry->GetHistogramById(name, cx, &testHistogram);
+
+  ASSERT_EQ(rv, NS_OK) << "Cannot fetch histogram";
+
+  // Clear the stored value
+  JS::RootedObject testHistogramObj(cx, &testHistogram.toObject());
+  JS::RootedValue rval(cx);
+  ASSERT_TRUE(JS_CallFunctionName(cx, testHistogramObj, "clear",
+                  JS::HandleValueArray::empty(), &rval)) << "Cannot clear histogram";
+}
+
+void
+GetSnapshots(JSContext* cx, nsCOMPtr<nsITelemetry> mTelemetry,
+             const char* name, JS::MutableHandleValue valueOut, bool is_keyed)
+{
+  JS::RootedValue snapshot(cx);
+  nsresult rv = is_keyed ? mTelemetry->GetKeyedHistogramSnapshots(cx, &snapshot)
+                         : mTelemetry->GetHistogramSnapshots(cx, &snapshot);
+
+  ASSERT_EQ(rv, NS_OK) << "Cannot call histogram snapshots";
+  valueOut.set(snapshot);
+}
+
+void
+GetProperty(JSContext* cx, const char* name, JS::HandleValue valueIn,
+                             JS::MutableHandleValue valueOut)
+{
+    JS::RootedValue property(cx);
+    JS::RootedObject valueInObj(cx, &valueIn.toObject());
+    ASSERT_TRUE(JS_GetProperty(cx, valueInObj, name, &property))
+      << "Cannot get property '" << name << "'";
+    valueOut.set(property);
+}
+
+}
+
 TEST_F(TelemetryTestFixture, AccumulateCountHistogram)
 {
   const uint32_t kExpectedValue = 100;
   AutoJSContextWithGlobal cx(mCleanGlobal);
 
-  JS::RootedValue testHistogram(cx.GetJSContext());
-  JS::RootedValue rval(cx.GetJSContext());
-
-  // Get the histogram
-  nsresult rv = mTelemetry->GetHistogramById(
-      NS_LITERAL_CSTRING("TELEMETRY_TEST_COUNT"),
-      cx.GetJSContext(),
-      &testHistogram);
-  ASSERT_EQ(rv, NS_OK) << "Cannot fetch histogram";
-
-  // Clear the stored value
-  JS::RootedObject testHistogramObj(cx.GetJSContext(), &testHistogram.toObject());
-  ASSERT_TRUE(JS_CallFunctionName(cx.GetJSContext(), testHistogramObj, "clear",
-                  JS::HandleValueArray::empty(), &rval)) << "Cannot clear histogram";
+  GetAndClearHistogram(cx.GetJSContext(), mTelemetry, NS_LITERAL_CSTRING("TELEMETRY_TEST_COUNT"),
+                       false);
 
   // Accumulate in the histogram
   Telemetry::Accumulate(Telemetry::TELEMETRY_TEST_COUNT, kExpectedValue);
 
   // Get a snapshot for all the histograms
   JS::RootedValue snapshot(cx.GetJSContext());
-  rv = mTelemetry->GetHistogramSnapshots(cx.GetJSContext(), &snapshot);
-  ASSERT_EQ(rv, NS_OK) << "Cannot call histogram snapshots";
+  GetSnapshots(cx.GetJSContext(), mTelemetry, "TELEMETRY_TEST_COUNT", &snapshot, false);
 
   // Get the histogram from the snapshot
   JS::RootedValue histogram(cx.GetJSContext());
-  JS::RootedObject snapshotObj(cx.GetJSContext(), &snapshot.toObject());
-  JS_GetProperty(cx.GetJSContext(), snapshotObj, "TELEMETRY_TEST_COUNT", &histogram);
+  GetProperty(cx.GetJSContext(), "TELEMETRY_TEST_COUNT", snapshot, &histogram);
 
-  // Get the snapshot for the test histogram
+  // Get "sum" property from histogram
   JS::RootedValue sum(cx.GetJSContext());
-  JS::RootedObject histogramObj(cx.GetJSContext(), &histogram.toObject());
-  JS_GetProperty(cx.GetJSContext(), histogramObj, "sum", &sum);
+  GetProperty(cx.GetJSContext(), "sum", histogram,  &sum);
 
-  // Check that the value stored in the histogram matches with |kExpectedValue|
+  // Check that the "sum" stored in the histogram matches with |kExpectedValue|
   uint32_t uSum = 0;
   JS::ToUint32(cx.GetJSContext(), sum, &uSum);
   ASSERT_EQ(uSum, kExpectedValue) << "The histogram is not returning expected value";
 }
 
 TEST_F(TelemetryTestFixture, AccumulateKeyedCountHistogram)
 {
   const uint32_t kExpectedValue = 100;
   AutoJSContextWithGlobal cx(mCleanGlobal);
 
-  JS::RootedValue testHistogram(cx.GetJSContext());
-  JS::RootedValue rval(cx.GetJSContext());
-
-  // Get the histogram
-  nsresult rv = mTelemetry->GetKeyedHistogramById(
-      NS_LITERAL_CSTRING("TELEMETRY_TEST_KEYED_COUNT"),
-      cx.GetJSContext(),
-      &testHistogram);
-  ASSERT_EQ(rv, NS_OK) << "Cannot fetch histogram";
-
-  // Clear the stored value
-  JS::RootedObject testHistogramObj(cx.GetJSContext(), &testHistogram.toObject());
-  ASSERT_TRUE(JS_CallFunctionName(cx.GetJSContext(), testHistogramObj, "clear",
-    JS::HandleValueArray::empty(), &rval)) << "Cannot clear histogram";
+  GetAndClearHistogram(cx.GetJSContext(), mTelemetry,
+                       NS_LITERAL_CSTRING("TELEMETRY_TEST_KEYED_COUNT"), true);
 
   // Accumulate data in the provided key within the histogram
   Telemetry::Accumulate(Telemetry::TELEMETRY_TEST_KEYED_COUNT, NS_LITERAL_CSTRING("sample"),
                         kExpectedValue);
 
   // Get a snapshot for all the histograms
   JS::RootedValue snapshot(cx.GetJSContext());
-  rv = mTelemetry->GetKeyedHistogramSnapshots(cx.GetJSContext(), &snapshot);
-  ASSERT_EQ(rv, NS_OK) << "Cannot call histogram snapshots";
+  GetSnapshots(cx.GetJSContext(), mTelemetry, "TELEMETRY_TEST_KEYED_COUNT", &snapshot, true);
 
   // Get the histogram from the snapshot
   JS::RootedValue histogram(cx.GetJSContext());
-  JS::RootedObject snapshotObj(cx.GetJSContext(), &snapshot.toObject());
-  ASSERT_TRUE(JS_GetProperty(cx.GetJSContext(), snapshotObj, "TELEMETRY_TEST_KEYED_COUNT",
-                             &histogram)) << "Cannot find the expected histogram";
+  GetProperty(cx.GetJSContext(), "TELEMETRY_TEST_KEYED_COUNT", snapshot, &histogram);
 
-  // Get histogram from keyed data
-  JS::RootedObject histogramObj(cx.GetJSContext(), &histogram.toObject());
+  // Get "sample" property from histogram
   JS::RootedValue expectedKeyData(cx.GetJSContext());
-  ASSERT_TRUE(JS_GetProperty(cx.GetJSContext(), histogramObj, "sample", &expectedKeyData))
-    << "Cannot find the expected key in the histogram data";
+  GetProperty(cx.GetJSContext(), "sample", histogram,  &expectedKeyData);
 
-  // Get sum from keyed data
-  JS::RootedObject expectedKeyDataObj(cx.GetJSContext(), &expectedKeyData.toObject());
+  // Get "sum" property from keyed data
   JS::RootedValue sum(cx.GetJSContext());
-  ASSERT_TRUE(JS_GetProperty(cx.GetJSContext(), expectedKeyDataObj, "sum", &sum))
-    << "Cannot find the 'sum' property in the data for this key";
+  GetProperty(cx.GetJSContext(), "sum", expectedKeyData,  &sum);
 
   // Check that the sum stored in the histogram matches with |kExpectedValue|
   uint32_t uSum = 0;
   JS::ToUint32(cx.GetJSContext(), sum, &uSum);
   ASSERT_EQ(uSum, kExpectedValue) << "The histogram is not returning expected sum";
 }