tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

TestTelemetry.cpp (5555B)


      1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
      2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
      3 /* This Source Code Form is subject to the terms of the Mozilla Public
      4 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
      5 * You can obtain one at http://mozilla.org/MPL/2.0/. */
      6 
      7 #include "QuotaManagerDependencyFixture.h"
      8 #include "mozilla/dom/ScriptSettings.h"
      9 #include "mozilla/dom/SimpleGlobalObject.h"
     10 #include "mozilla/gtest/MozAssertions.h"
     11 #include "nsITelemetry.h"
     12 
     13 namespace mozilla::dom::quota::test {
     14 
     15 namespace {
     16 
     17 namespace js_helpers {
     18 
     19 void CallFunction(JSContext* aCx, JS::Handle<JS::Value> aValue,
     20                  const char* aName, JS::MutableHandle<JS::Value> aResult) {
     21  JS::Rooted<JSObject*> obj(aCx, &aValue.toObject());
     22 
     23  JS::Rooted<JS::Value> result(aCx);
     24  ASSERT_TRUE(JS_CallFunctionName(aCx, obj, aName,
     25                                  JS::HandleValueArray::empty(), &result));
     26 
     27  aResult.set(result);
     28 }
     29 
     30 void HasProperty(JSContext* aCx, JS::Handle<JS::Value> aValue,
     31                 const char* aName, bool* aResult) {
     32  JS::Rooted<JSObject*> obj(aCx, &aValue.toObject());
     33 
     34  bool result;
     35  ASSERT_TRUE(JS_HasProperty(aCx, obj, aName, &result));
     36 
     37  *aResult = result;
     38 }
     39 
     40 void GetProperty(JSContext* aCx, JS::Handle<JS::Value> aValue,
     41                 const char* aName, JS::MutableHandle<JS::Value> aResult) {
     42  JS::Rooted<JSObject*> obj(aCx, &aValue.toObject());
     43 
     44  JS::Rooted<JS::Value> result(aCx);
     45  ASSERT_TRUE(JS_GetProperty(aCx, obj, aName, &result));
     46 
     47  aResult.set(result);
     48 }
     49 
     50 void Enumerate(JSContext* aCx, JS::Handle<JS::Value> aValue,
     51               JS::MutableHandle<JS::IdVector> aResult) {
     52  JS::Rooted<JSObject*> obj(aCx, &aValue.toObject());
     53  ASSERT_TRUE(JS_Enumerate(aCx, obj, aResult));
     54 }
     55 
     56 void GetElement(JSContext* aCx, JS::Handle<JS::Value> aValue, uint32_t aIndex,
     57                JS::MutableHandle<JS::Value> aResult) {
     58  JS::Rooted<JSObject*> obj(aCx, &aValue.toObject());
     59 
     60  JS::Rooted<JS::Value> result(aCx);
     61  ASSERT_TRUE(JS_GetElement(aCx, obj, aIndex, &result));
     62 
     63  aResult.set(result);
     64 }
     65 
     66 void GetIndexValue(JSContext* aCx, JS::Handle<JS::Value> aValues,
     67                   JS::PropertyKey aIndexId, uint32_t* aResult) {
     68  nsAutoJSString indexId;
     69  ASSERT_TRUE(indexId.init(aCx, aIndexId));
     70 
     71  nsresult rv;
     72  const auto index = indexId.ToInteger64(&rv);
     73  ASSERT_NS_SUCCEEDED(rv);
     74 
     75  JS::Rooted<JS::Value> element(aCx);
     76  GetElement(aCx, aValues, index, &element);
     77 
     78  uint32_t value = 0;
     79  ASSERT_TRUE(JS::ToUint32(aCx, element, &value));
     80 
     81  *aResult = value;
     82 }
     83 
     84 }  // namespace js_helpers
     85 
     86 }  // namespace
     87 
     88 TEST(DOM_Quota_Telemetry, ShutdownTime)
     89 {
     90  nsCOMPtr<nsITelemetry> telemetry =
     91      do_GetService("@mozilla.org/base/telemetry;1");
     92 
     93  JSObject* simpleGlobal = dom::SimpleGlobalObject::Create(
     94      dom::SimpleGlobalObject::GlobalType::BindingDetail);
     95 
     96  JS::Rooted<JSObject*> global(dom::RootingCx(), simpleGlobal);
     97 
     98  AutoJSAPI jsapi;
     99  ASSERT_TRUE(jsapi.Init(global));
    100 
    101  JSContext* cx = jsapi.cx();
    102 
    103  JS::Rooted<JS::Value> histogram(cx);
    104  ASSERT_NS_SUCCEEDED(telemetry->GetKeyedHistogramById("QM_SHUTDOWN_TIME_V0"_ns,
    105                                                       cx, &histogram));
    106 
    107  JS::Rooted<JS::Value> dummy(cx);
    108  ASSERT_NO_FATAL_FAILURE(
    109      js_helpers::CallFunction(cx, histogram, "clear", &dummy));
    110 
    111  ASSERT_NO_FATAL_FAILURE(QuotaManagerDependencyFixture::InitializeFixture());
    112  ASSERT_NO_FATAL_FAILURE(QuotaManagerDependencyFixture::ShutdownFixture());
    113 
    114  JS::Rooted<JS::Value> snapshot(cx);
    115  ASSERT_NO_FATAL_FAILURE(
    116      js_helpers::CallFunction(cx, histogram, "snapshot", &snapshot));
    117 
    118  {
    119    bool hasKey = false;
    120    ASSERT_NO_FATAL_FAILURE(
    121        js_helpers::HasProperty(cx, snapshot, "Normal", &hasKey));
    122    ASSERT_TRUE(hasKey);
    123 
    124    JS::Rooted<JS::Value> key(cx);
    125    ASSERT_NO_FATAL_FAILURE(
    126        js_helpers::GetProperty(cx, snapshot, "Normal", &key));
    127 
    128    JS::Rooted<JS::Value> values(cx);
    129    ASSERT_NO_FATAL_FAILURE(
    130        js_helpers::GetProperty(cx, key, "values", &values));
    131 
    132    JS::Rooted<JS::IdVector> indexIds(cx, JS::IdVector(cx));
    133    ASSERT_NO_FATAL_FAILURE(js_helpers::Enumerate(cx, values, &indexIds));
    134    ASSERT_TRUE(indexIds.length() == 2u || indexIds.length() == 3u);
    135 
    136    if (indexIds.length() == 2) {
    137      uint32_t value = 0u;
    138 
    139      ASSERT_NO_FATAL_FAILURE(
    140          js_helpers::GetIndexValue(cx, values, indexIds[0], &value));
    141      ASSERT_EQ(value, 1u);
    142 
    143      ASSERT_NO_FATAL_FAILURE(
    144          js_helpers::GetIndexValue(cx, values, indexIds[1], &value));
    145      ASSERT_EQ(value, 0u);
    146    } else {
    147      uint32_t value = 1u;
    148 
    149      ASSERT_NO_FATAL_FAILURE(
    150          js_helpers::GetIndexValue(cx, values, indexIds[0], &value));
    151      ASSERT_EQ(value, 0u);
    152 
    153      ASSERT_NO_FATAL_FAILURE(
    154          js_helpers::GetIndexValue(cx, values, indexIds[1], &value));
    155      ASSERT_EQ(value, 1u);
    156 
    157      ASSERT_NO_FATAL_FAILURE(
    158          js_helpers::GetIndexValue(cx, values, indexIds[2], &value));
    159      ASSERT_EQ(value, 0u);
    160    }
    161  }
    162 
    163  {
    164    bool hasKey = true;
    165    ASSERT_NO_FATAL_FAILURE(
    166        js_helpers::HasProperty(cx, snapshot, "WasSuspended", &hasKey));
    167    ASSERT_FALSE(hasKey);
    168  }
    169 
    170  {
    171    bool hasKey = true;
    172    ASSERT_NO_FATAL_FAILURE(
    173        js_helpers::HasProperty(cx, snapshot, "TimeStampErr1", &hasKey));
    174    ASSERT_FALSE(hasKey);
    175  }
    176 
    177  {
    178    bool hasKey = true;
    179    ASSERT_NO_FATAL_FAILURE(
    180        js_helpers::HasProperty(cx, snapshot, "TimeStampErr2", &hasKey));
    181    ASSERT_FALSE(hasKey);
    182  }
    183 }
    184 
    185 }  // namespace mozilla::dom::quota::test