File: TestFog.cpp

package info (click to toggle)
firefox 142.0.1-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 4,591,884 kB
  • sloc: cpp: 7,451,570; javascript: 6,392,463; ansic: 3,712,584; python: 1,388,569; xml: 629,223; asm: 426,919; java: 184,857; sh: 63,439; makefile: 19,150; objc: 13,059; perl: 12,983; yacc: 4,583; cs: 3,846; pascal: 3,352; lex: 1,720; ruby: 1,003; exp: 762; php: 436; lisp: 258; awk: 247; sql: 66; sed: 53; csh: 10
file content (732 lines) | stat: -rw-r--r-- 25,817 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "FOGFixture.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include "mozilla/glean/GleanTestsTestMetrics.h"
#include "mozilla/glean/GleanPings.h"
#include "mozilla/glean/fog_ffi_generated.h"
#include "mozilla/Maybe.h"
#include "mozilla/Result.h"
#include "mozilla/ResultVariant.h"
#include "mozilla/TimeStamp.h"
#include "../../bindings/private/Common.h"

#include "nsTArray.h"

#include "mozilla/Preferences.h"
#include "mozilla/Unused.h"
#include "nsString.h"
#include "prtime.h"

using mozilla::Preferences;
using mozilla::TimeDuration;
using namespace mozilla::glean;
using namespace mozilla::glean::impl;

#define DATA_PREF "datareporting.healthreport.uploadEnabled"

extern "C" {
// This function is called by the rust code in test.rs if a non-fatal test
// failure occurs.
void GTest_FOG_ExpectFailure(const char* aMessage) {
  EXPECT_STREQ(aMessage, "");
}
}

TEST_F(FOGFixture, BuiltinPingsRegistered) {
  Preferences::SetInt("telemetry.fog.test.localhost_port", -1);
  nsAutoCString metricsPingName("metrics");
  nsAutoCString baselinePingName("baseline");
  nsAutoCString eventsPingName("events");
  ASSERT_EQ(NS_OK, fog_submit_ping(&metricsPingName));
  ASSERT_EQ(NS_OK, fog_submit_ping(&baselinePingName));
  ASSERT_EQ(NS_OK, fog_submit_ping(&eventsPingName));
}

TEST_F(FOGFixture, TestCppCounterWorks) {
  mozilla::glean::test_only::bad_code.Add(42);

  ASSERT_EQ(42, mozilla::glean::test_only::bad_code.TestGetValue("test-ping"_ns)
                    .unwrap()
                    .value());
  // And test that the ping name's optional, while you're at it:
  ASSERT_EQ(42, test_only::bad_code.TestGetValue().unwrap().value());
}

TEST_F(FOGFixture, TestCppStringWorks) {
  auto kValue = "cheez!"_ns;
  mozilla::glean::test_only::cheesy_string.Set(kValue);

  ASSERT_STREQ(kValue.get(), mozilla::glean::test_only::cheesy_string
                                 .TestGetValue("test-ping"_ns)
                                 .unwrap()
                                 .value()
                                 .get());
}

TEST_F(FOGFixture, TestCppTimespanWorks) {
  mozilla::glean::test_only::can_we_time_it.Start();
  PR_Sleep(PR_MillisecondsToInterval(10));
  mozilla::glean::test_only::can_we_time_it.Stop();

  ASSERT_TRUE(
      mozilla::glean::test_only::can_we_time_it.TestGetValue("test-ping"_ns)
          .unwrap()
          .value() > 0);
}

TEST_F(FOGFixture, TestCppUuidWorks) {
  nsCString kTestUuid("decafdec-afde-cafd-ecaf-decafdecafde");
  test_only::what_id_it.Set(kTestUuid);
  ASSERT_STREQ(kTestUuid.get(),
               test_only::what_id_it.TestGetValue("test-ping"_ns)
                   .unwrap()
                   .value()
                   .get());

  test_only::what_id_it.GenerateAndSet();
  // Since we generate v4 UUIDs, and the first character of the third group
  // isn't 4, this won't ever collide with kTestUuid.
  ASSERT_STRNE(kTestUuid.get(),
               test_only::what_id_it.TestGetValue("test-ping"_ns)
                   .unwrap()
                   .value()
                   .get());
}

TEST_F(FOGFixture, TestCppBooleanWorks) {
  mozilla::glean::test_only::can_we_flag_it.Set(false);

  ASSERT_EQ(false, mozilla::glean::test_only::can_we_flag_it
                       .TestGetValue("test-ping"_ns)
                       .unwrap()
                       .value());
}

MATCHER_P(BitEq, x, "bit equal") {
  static_assert(sizeof(x) == sizeof(arg));
  return std::memcmp(&arg, &x, sizeof(x)) == 0;
}

TEST_F(FOGFixture, TestCppDatetimeWorks) {
  PRExplodedTime date{0, 35, 10, 12, 6, 10, 2020, 0, 0, {5 * 60 * 60, 0}};
  test_only::what_a_date.Set(&date);

  auto received = test_only::what_a_date.TestGetValue("test-ping"_ns).unwrap();
  ASSERT_THAT(received.value(), BitEq(date));
}

using mozilla::Some;
using mozilla::glean::test_only_ipc::AnEventExtra;
using mozilla::glean::test_only_ipc::EventWithExtraExtra;
using std::tuple;

TEST_F(FOGFixture, TestCppEventWorks) {
  test_only_ipc::no_extra_event.Record();
  ASSERT_TRUE(test_only_ipc::no_extra_event.TestGetValue("test-ping"_ns)
                  .unwrap()
                  .isSome());

  AnEventExtra extra = {.extra1 = Some("can set extras"_ns)};
  test_only_ipc::an_event.Record(Some(extra));
  auto optEvents =
      test_only_ipc::an_event.TestGetValue("test-ping"_ns).unwrap();
  ASSERT_TRUE(optEvents.isSome());

  auto events = optEvents.extract();
  ASSERT_EQ(1UL, events.Length());
  ASSERT_STREQ("test_only.ipc", events[0].mCategory.get());
  ASSERT_STREQ("an_event", events[0].mName.get());
  ASSERT_EQ(1UL, events[0].mExtra.Length());
  ASSERT_STREQ("extra1", std::get<0>(events[0].mExtra[0]).get());
  ASSERT_STREQ("can set extras", std::get<1>(events[0].mExtra[0]).get());
}

TEST_F(FOGFixture, TestCppEventsWithDifferentExtraTypes) {
  EventWithExtraExtra extra = {.extra1 = Some("can set extras"_ns),
                               .extra2 = Some(37),
                               .extra3LongerName = Some(false)};
  test_only_ipc::event_with_extra.Record(Some(extra));
  auto optEvents =
      test_only_ipc::event_with_extra.TestGetValue("test-ping"_ns).unwrap();
  ASSERT_TRUE(optEvents.isSome());

  auto events = optEvents.extract();
  ASSERT_EQ(1UL, events.Length());

  // The list of extra key/value pairs can be in any order.
  ASSERT_EQ(3UL, events[0].mExtra.Length());
  for (auto extra : events[0].mExtra) {
    auto key = std::get<0>(extra);
    auto value = std::get<1>(extra);

    if (key == "extra1"_ns) {
      ASSERT_STREQ("can set extras", value.get());
    } else if (key == "extra2"_ns) {
      ASSERT_STREQ("37", value.get());
    } else if (key == "extra3_longer_name"_ns) {
      ASSERT_STREQ("false", value.get());
    } else {
      ASSERT_TRUE(false)
      << "Invalid extra item recorded.";
    }
  }
}

TEST_F(FOGFixture, TestCppMemoryDistWorks) {
  test_only::do_you_remember.Accumulate(7);
  test_only::do_you_remember.Accumulate(17);

  DistributionData data =
      test_only::do_you_remember.TestGetValue("test-ping"_ns).unwrap().ref();
  // Sum is in bytes, test_only::do_you_remember is in megabytes. So
  // multiplication ahoy!
  ASSERT_EQ(data.sum, 24UL * 1024 * 1024);
  ASSERT_EQ(data.count, 2UL);
  for (const auto& entry : data.values) {
    const uint64_t bucket = entry.GetKey();
    const uint64_t count = entry.GetData();
    ASSERT_TRUE(count == 0 ||
                (count == 1 && (bucket == 17520006 || bucket == 7053950)))
    << "Only two occupied buckets";
  }
}

TEST_F(FOGFixture, TestCppCustomDistWorks) {
  test_only_ipc::a_custom_dist.AccumulateSamples({7, 268435458});

  DistributionData data =
      test_only_ipc::a_custom_dist.TestGetValue("test-ping"_ns).unwrap().ref();
  ASSERT_EQ(data.sum, 7UL + 268435458);
  ASSERT_EQ(data.count, 2UL);
  for (const auto& entry : data.values) {
    const uint64_t bucket = entry.GetKey();
    const uint64_t count = entry.GetData();
    ASSERT_TRUE(count == 0 ||
                (count == 1 && (bucket == 1 || bucket == 268435456)))
    << "Only two occupied buckets";
  }
}

TEST_F(FOGFixture, TestCppPings) {
  const auto& ping = mozilla::glean_pings::OnePingOnly;

  test_only::one_ping_one_bool.Set(false);

  {
    bool submitted = false;

    ping.TestBeforeNextSubmit([&submitted](const nsACString& aReason) {
      submitted = true;
      ASSERT_EQ(false,
                test_only::one_ping_one_bool.TestGetValue().unwrap().ref());
    });
    ping.Submit();

    ASSERT_TRUE(submitted)
    << "Must have actually called the lambda.";
  }

  test_only::one_ping_one_bool.Set(false);

  ASSERT_TRUE(ping.TestSubmission(
      [](const nsACString& aReason) {
        ASSERT_EQ(false,
                  test_only::one_ping_one_bool.TestGetValue().unwrap().ref());
      },
      [&]() { ping.Submit(); }))
  << "Must submit ping";
}

TEST_F(FOGFixture, TestCppStringLists) {
  auto kValue = "cheez!"_ns;
  auto kValue2 = "cheezier!"_ns;
  auto kValue3 = "cheeziest."_ns;

  nsTArray<nsCString> cheezList;
  cheezList.EmplaceBack(kValue);
  cheezList.EmplaceBack(kValue2);

  test_only::cheesy_string_list.Set(cheezList);

  auto val = test_only::cheesy_string_list.TestGetValue().unwrap().value();
  // Note: This is fragile if the order is ever not preserved.
  ASSERT_STREQ(kValue.get(), val[0].get());
  ASSERT_STREQ(kValue2.get(), val[1].get());

  test_only::cheesy_string_list.Add(kValue3);

  val = test_only::cheesy_string_list.TestGetValue().unwrap().value();
  ASSERT_STREQ(kValue3.get(), val[2].get());
}

TEST_F(FOGFixture, TestCppTimingDistWorks) {
  auto id1 = test_only::what_time_is_it.Start();
  auto id2 = test_only::what_time_is_it.Start();
  PR_Sleep(PR_MillisecondsToInterval(5));
  auto id3 = test_only::what_time_is_it.Start();
  test_only::what_time_is_it.Cancel(std::move(id1));
  PR_Sleep(PR_MillisecondsToInterval(5));
  test_only::what_time_is_it.StopAndAccumulate(std::move(id2));
  test_only::what_time_is_it.StopAndAccumulate(std::move(id3));

  DistributionData data =
      test_only::what_time_is_it.TestGetValue().unwrap().ref();

  // Cancelled timers should not increase count.
  ASSERT_EQ(data.count, 2UL);

  const uint64_t NANOS_IN_MILLIS = 1e6;

  // bug 1701847 - Sleeps don't necessarily round up as you'd expect.
  // Give ourselves a 200000ns (0.2ms) window to be off on fast machines.
  const uint64_t EPSILON = 200000;

  // We don't know exactly how long those sleeps took, only that it was at
  // least 15ms total.
  ASSERT_GT(data.sum, (uint64_t)(15 * NANOS_IN_MILLIS) - EPSILON);

  // We also can't guarantee the buckets, but we can guarantee two samples.
  uint64_t sampleCount = 0;
  for (const auto& value : data.values.Values()) {
    sampleCount += value;
  }
  ASSERT_EQ(sampleCount, (uint64_t)2);
}

TEST_F(FOGFixture, TestCppTimingDistNegativeDuration) {
  // Intentionally a negative duration to test the error case.
  auto negDuration = TimeDuration::FromSeconds(-1);
  test_only::what_time_is_it.AccumulateRawDuration(negDuration);

  ASSERT_EQ(mozilla::Nothing(),
            test_only::what_time_is_it.TestGetValue().unwrap());
}

TEST_F(FOGFixture, TestCppTimingDistMeasure) {
  // Let's test Measure and its RAII AutoTimer
  {
    auto t1 = test_only::what_time_is_it.Measure();
    auto t2 = test_only::what_time_is_it.Measure();
    PR_Sleep(PR_MillisecondsToInterval(5));
    {
      auto t3 = test_only::what_time_is_it.Measure();
      t1.Cancel();
      PR_Sleep(PR_MillisecondsToInterval(5));
    }
  }
  DistributionData data =
      test_only::what_time_is_it.TestGetValue().unwrap().ref();

  // Cancelled timers should not increase count.
  ASSERT_EQ(data.count, 2UL);

  const uint64_t NANOS_IN_MILLIS = 1e6;

  // bug 1701847 - Sleeps don't necessarily round up as you'd expect.
  // Give ourselves a 200000ns (0.2ms) window to be off on fast machines.
  const uint64_t EPSILON = 200000;

  // We don't know exactly how long those sleeps took, only that it was at
  // least 15ms total.
  ASSERT_GT(data.sum, (uint64_t)(15 * NANOS_IN_MILLIS) - EPSILON);

  // We also can't guarantee the buckets, but we can guarantee two samples.
  uint64_t sampleCount = 0;
  for (const auto& value : data.values.Values()) {
    sampleCount += value;
  }
  ASSERT_EQ(sampleCount, (uint64_t)2);
}

TEST_F(FOGFixture, TestLabeledBooleanWorks) {
  ASSERT_EQ(mozilla::Nothing(),
            test_only::mabels_like_balloons.Get("hot_air"_ns)
                .TestGetValue()
                .unwrap());
  test_only::mabels_like_balloons.Get("hot_air"_ns).Set(true);
  test_only::mabels_like_balloons.Get("helium"_ns).Set(false);
  ASSERT_EQ(true, test_only::mabels_like_balloons.Get("hot_air"_ns)
                      .TestGetValue()
                      .unwrap()
                      .ref());
  ASSERT_EQ(false, test_only::mabels_like_balloons.Get("helium"_ns)
                       .TestGetValue()
                       .unwrap()
                       .ref());
}

TEST_F(FOGFixture, TestLabeledBooleanWithLabelsWorks) {
  ASSERT_EQ(mozilla::Nothing(),
            test_only::mabels_like_labeled_balloons
                .EnumGet(test_only::MabelsLikeLabeledBalloonsLabel::eWater)
                .TestGetValue()
                .unwrap());
  test_only::mabels_like_labeled_balloons
      .EnumGet(test_only::MabelsLikeLabeledBalloonsLabel::eWater)
      .Set(true);
  test_only::mabels_like_labeled_balloons
      .EnumGet(test_only::MabelsLikeLabeledBalloonsLabel::eBirthdayParty)
      .Set(false);
  ASSERT_EQ(true,
            test_only::mabels_like_labeled_balloons
                .EnumGet(test_only::MabelsLikeLabeledBalloonsLabel::eWater)
                .TestGetValue()
                .unwrap()
                .ref());
  ASSERT_EQ(
      false,
      test_only::mabels_like_labeled_balloons
          .EnumGet(test_only::MabelsLikeLabeledBalloonsLabel::eBirthdayParty)
          .TestGetValue()
          .unwrap()
          .ref());
}

TEST_F(FOGFixture, TestLabeledCounterWorks) {
  ASSERT_EQ(mozilla::Nothing(),
            test_only::mabels_kitchen_counters.Get("marble"_ns)
                .TestGetValue()
                .unwrap());
  test_only::mabels_kitchen_counters.Get("marble"_ns).Add(1);
  test_only::mabels_kitchen_counters.Get("laminate"_ns).Add(2);
  ASSERT_EQ(1, test_only::mabels_kitchen_counters.Get("marble"_ns)
                   .TestGetValue()
                   .unwrap()
                   .ref());
  ASSERT_EQ(2, test_only::mabels_kitchen_counters.Get("laminate"_ns)
                   .TestGetValue()
                   .unwrap()
                   .ref());
}

TEST_F(FOGFixture, TestLabeledCounterWithLabelsWorks) {
  ASSERT_EQ(
      mozilla::Nothing(),
      test_only::mabels_labeled_counters
          .EnumGet(test_only::MabelsLabeledCountersLabel::eNextToTheFridge)
          .TestGetValue()
          .unwrap());
  test_only::mabels_labeled_counters
      .EnumGet(test_only::MabelsLabeledCountersLabel::eNextToTheFridge)
      .Add(1);
  test_only::mabels_labeled_counters
      .EnumGet(test_only::MabelsLabeledCountersLabel::eClean)
      .Add(2);
  test_only::mabels_labeled_counters
      .EnumGet(test_only::MabelsLabeledCountersLabel::e1stCounter)
      .Add(3);
  ASSERT_EQ(
      1, test_only::mabels_labeled_counters
             .EnumGet(test_only::MabelsLabeledCountersLabel::eNextToTheFridge)
             .TestGetValue()
             .unwrap()
             .ref());
  ASSERT_EQ(2, test_only::mabels_labeled_counters
                   .EnumGet(test_only::MabelsLabeledCountersLabel::eClean)
                   .TestGetValue()
                   .unwrap()
                   .ref());
  ASSERT_EQ(3, test_only::mabels_labeled_counters
                   .EnumGet(test_only::MabelsLabeledCountersLabel::e1stCounter)
                   .TestGetValue()
                   .unwrap()
                   .ref());
}

TEST_F(FOGFixture, TestLabeledStringWorks) {
  ASSERT_EQ(mozilla::Nothing(),
            test_only::mabels_balloon_strings.Get("twine"_ns)
                .TestGetValue()
                .unwrap());
  test_only::mabels_balloon_strings.Get("twine"_ns).Set("seems acceptable"_ns);
  test_only::mabels_balloon_strings.Get("parachute_cord"_ns)
      .Set("preferred"_ns);
  ASSERT_STREQ("seems acceptable",
               test_only::mabels_balloon_strings.Get("twine"_ns)
                   .TestGetValue()
                   .unwrap()
                   .ref()
                   .get());
  ASSERT_STREQ("preferred",
               test_only::mabels_balloon_strings.Get("parachute_cord"_ns)
                   .TestGetValue()
                   .unwrap()
                   .ref()
                   .get());
}

TEST_F(FOGFixture, TestLabeledStringWithLabelsWorks) {
  ASSERT_EQ(mozilla::Nothing(),
            test_only::mabels_balloon_labels
                .EnumGet(test_only::MabelsBalloonLabelsLabel::eCelebratory)
                .TestGetValue()
                .unwrap());
  test_only::mabels_balloon_labels
      .EnumGet(test_only::MabelsBalloonLabelsLabel::eCelebratory)
      .Set("for birthdays, etc."_ns);
  test_only::mabels_balloon_labels
      .EnumGet(test_only::MabelsBalloonLabelsLabel::eCelebratoryAndSnarky)
      .Set("for retirements and bridal showers"_ns);
  ASSERT_STREQ("for birthdays, etc.",
               test_only::mabels_balloon_labels
                   .EnumGet(test_only::MabelsBalloonLabelsLabel::eCelebratory)
                   .TestGetValue()
                   .unwrap()
                   .ref()
                   .get());
  ASSERT_STREQ(
      "for retirements and bridal showers",
      test_only::mabels_balloon_labels
          .EnumGet(test_only::MabelsBalloonLabelsLabel::eCelebratoryAndSnarky)
          .TestGetValue()
          .unwrap()
          .ref()
          .get());
}

TEST_F(FOGFixture, TestCppQuantityWorks) {
  // This joke only works in base 13.
  const uint32_t kValue = 6 * 9;
  mozilla::glean::test_only::meaning_of_life.Set(kValue);

  ASSERT_EQ(kValue, mozilla::glean::test_only::meaning_of_life.TestGetValue()
                        .unwrap()
                        .value());
}

TEST_F(FOGFixture, TestCppRateWorks) {
  // 1) Standard rate with internal denominator
  const int32_t kNum = 22;
  const int32_t kDen = 7;  // because I like pi, even just approximately.

  test_only_ipc::irate.AddToNumerator(kNum);
  test_only_ipc::irate.AddToDenominator(kDen);
  auto value = test_only_ipc::irate.TestGetValue().unwrap();
  ASSERT_EQ(kNum, value.ref().first);
  ASSERT_EQ(kDen, value.ref().second);

  // 2) Rate with external denominator
  test_only_ipc::rate_with_external_denominator.AddToNumerator(kNum);
  test_only_ipc::an_external_denominator.Add(kDen);
  value = test_only_ipc::rate_with_external_denominator.TestGetValue().unwrap();
  ASSERT_EQ(kNum, value.ref().first);
  ASSERT_EQ(kDen, value.ref().second);
  ASSERT_EQ(
      kDen,
      test_only_ipc::an_external_denominator.TestGetValue().unwrap().extract());
}

TEST_F(FOGFixture, TestCppUrlWorks) {
  auto kValue = "https://example.com/fog/gtest"_ns;
  mozilla::glean::test_only_ipc::a_url.Set(kValue);

  ASSERT_STREQ(kValue.get(),
               mozilla::glean::test_only_ipc::a_url.TestGetValue("test-ping"_ns)
                   .unwrap()
                   .value()
                   .get());
}

TEST_F(FOGFixture, TestCppTextWorks) {
  auto kValue =
      "République is a French-inspired bakery, café, bar and formal dining room located in the Mid-city neighborhood of Los Angeles, set in a historic 1928 space ..."_ns;
  mozilla::glean::test_only_ipc::a_text.Set(kValue);
  ASSERT_STREQ(kValue.get(),
               mozilla::glean::test_only_ipc::a_text.TestGetValue()
                   .unwrap()
                   .value()
                   .get());
}

TEST_F(FOGFixture, TestLabeledCustomDistributionWorks) {
  ASSERT_EQ(mozilla::Nothing(),
            test_only::mabels_custom_label_lengths.Get("officeSupplies"_ns)
                .TestGetValue()
                .unwrap());
  test_only::mabels_custom_label_lengths.Get("officeSupplies"_ns)
      .AccumulateSamples({7, 268435458});

  DistributionData data =
      test_only::mabels_custom_label_lengths.Get("officeSupplies"_ns)
          .TestGetValue()
          .unwrap()
          .ref();
  ASSERT_EQ(data.sum, 7UL + 268435458);
  ASSERT_EQ(data.count, 2UL);
  for (const auto& entry : data.values) {
    const uint64_t bucket = entry.GetKey();
    const uint64_t count = entry.GetData();
    ASSERT_TRUE(count == 0 ||
                (count == 1 && (bucket == 1 || bucket == 268435456)))
    << "Only two occupied buckets";
  }
}

TEST_F(FOGFixture, TestLabeledMemoryDistWorks) {
  test_only::what_do_you_remember.Get("bittersweet"_ns).Accumulate(7);
  test_only::what_do_you_remember.Get("bittersweet"_ns).Accumulate(17);

  DistributionData data = test_only::what_do_you_remember.Get("bittersweet"_ns)
                              .TestGetValue()
                              .unwrap()
                              .ref();
  // Sum is in bytes, test_only::what_do_you_remember is in megabytes. So
  // multiplication ahoy!
  ASSERT_EQ(data.sum, 24UL * 1024 * 1024);
  ASSERT_EQ(data.count, 2UL);
  for (const auto& entry : data.values) {
    const uint64_t bucket = entry.GetKey();
    const uint64_t count = entry.GetData();
    ASSERT_TRUE(count == 0 ||
                (count == 1 && (bucket == 17520006 || bucket == 7053950)))
    << "Only two occupied buckets";
  }
}

TEST_F(FOGFixture, TestLabeledTimingDistWorks) {
  auto id1 = test_only::where_has_the_time_gone.Get("UTC"_ns).Start();
  auto id2 = test_only::where_has_the_time_gone.Get("UTC"_ns).Start();
  PR_Sleep(PR_MillisecondsToInterval(5));
  auto id3 = test_only::where_has_the_time_gone.Get("UTC"_ns).Start();
  test_only::where_has_the_time_gone.Get("UTC"_ns).Cancel(std::move(id1));
  PR_Sleep(PR_MillisecondsToInterval(5));
  test_only::where_has_the_time_gone.Get("UTC"_ns).StopAndAccumulate(
      std::move(id2));
  test_only::where_has_the_time_gone.Get("UTC"_ns).StopAndAccumulate(
      std::move(id3));

  DistributionData data = test_only::where_has_the_time_gone.Get("UTC"_ns)
                              .TestGetValue()
                              .unwrap()
                              .ref();

  // Cancelled timers should not increase count.
  ASSERT_EQ(data.count, 2UL);

  const uint64_t NANOS_IN_MILLIS = 1e6;

  // bug 1701847 - Sleeps don't necessarily round up as you'd expect.
  // Give ourselves a 200000ns (0.2ms) window to be off on fast machines.
  const uint64_t EPSILON = 200000;

  // We don't know exactly how long those sleeps took, only that it was at
  // least 15ms total.
  ASSERT_GT(data.sum, (uint64_t)(15 * NANOS_IN_MILLIS) - EPSILON);

  // We also can't guarantee the buckets, but we can guarantee two samples.
  uint64_t sampleCount = 0;
  for (const auto& value : data.values.Values()) {
    sampleCount += value;
  }
  ASSERT_EQ(sampleCount, (uint64_t)2);
}

TEST_F(FOGFixture, TestLabeledTimingDistTruncateGet) {
  auto longKey =
      "this is a label that is longer than the new label limit of 111 characters introduced in bug 1959696 in April of 2025."_ns;

  auto sec = TimeDuration::FromMilliseconds(1);
  test_only::where_has_the_time_gone.MaybeTruncateAndGet(longKey)
      .AccumulateRawDuration(sec);

  DistributionData data =
      test_only::where_has_the_time_gone.MaybeTruncateAndGet(longKey)
          .TestGetValue()
          .unwrap()
          .ref();

  const uint64_t NANOS_IN_MILLIS = 1e6;
  ASSERT_EQ(data.sum, (uint64_t)(1 * NANOS_IN_MILLIS));

  // Double-check that short labels aren't transformed.
  auto shortKey = "some key"_ns;
  test_only::where_has_the_time_gone.MaybeTruncateAndGet(shortKey)
      .AccumulateRawDuration(sec);

  data = test_only::where_has_the_time_gone.Get(shortKey)
             .TestGetValue()
             .unwrap()
             .ref();
  ASSERT_EQ(data.sum, (uint64_t)(1 * NANOS_IN_MILLIS));

  // Let's make sure the long key correctly errors.
  test_only::where_has_the_time_gone.Get(longKey).AccumulateRawDuration(sec);
  ASSERT_TRUE(test_only::where_has_the_time_gone.MaybeTruncateAndGet(longKey)
                  .TestGetValue()
                  .isErr());
}

TEST_F(FOGFixture, TestLabeledQuantityWorks) {
  ASSERT_EQ(mozilla::Nothing(),
            test_only::button_jars.Get("shirt"_ns).TestGetValue().unwrap());
  test_only::button_jars.Get("shirt"_ns).Set(42);
  test_only::button_jars.Get("push"_ns).Set(0);
  ASSERT_EQ(
      42, test_only::button_jars.Get("shirt"_ns).TestGetValue().unwrap().ref());
  ASSERT_EQ(
      0, test_only::button_jars.Get("push"_ns).TestGetValue().unwrap().ref());
}

TEST_F(FOGFixture, TestObjectWorks) {
  ASSERT_EQ(mozilla::Nothing(),
            test_only::balloons.TestGetValueAsJSONString().unwrap());
  test_only::BalloonsObject balloons;
  balloons.EmplaceBack(test_only::BalloonsObjectItem{
      .colour = Some("blorange"_ns),
      .diameter = Some(42),
  });
  test_only::balloons.Set(balloons);

  // TODO(bug 1881023): Check the full obj, not just JSON substr.
  nsCString json =
      test_only::balloons.TestGetValueAsJSONString().unwrap().ref();
  ASSERT_THAT(json.get(), testing::HasSubstr("blorange"));
}

TEST_F(FOGFixture, TestComplexObjectWorks) {
  ASSERT_EQ(mozilla::Nothing(),
            test_only::crash_stack.TestGetValueAsJSONString().unwrap());
  test_only::CrashStackObject crash_obj{
      .status = Some("failure"_ns),
      .main_module = Some(17),
      .crash_info = mozilla::Nothing(),
      .modules = mozilla::Nothing(),
  };

  test_only::crash_stack.Set(crash_obj);

  // TODO(bug 1881023): Check the full obj, not just JSON substr.
  nsCString json =
      test_only::crash_stack.TestGetValueAsJSONString().unwrap().ref();
  ASSERT_THAT(json.get(), testing::HasSubstr("failure"));
}

TEST_F(FOGFixture, TestDualLabeledCounterWorks) {
  ASSERT_EQ(mozilla::Nothing(),
            test_only_ipc::a_dual_labeled_counter.Get("key"_ns, "category"_ns)
                .TestGetValue()
                .unwrap());
}

extern "C" void Rust_TestRustInGTest();
TEST_F(FOGFixture, TestRustInGTest) { Rust_TestRustInGTest(); }

extern "C" void Rust_TestJogfile();
TEST_F(FOGFixture, TestJogfile) { Rust_TestJogfile(); }

TEST_F(FOGFixture, IsCamelCaseWorks) {
  ASSERT_TRUE(IsCamelCase(u"someName"_ns));
  ASSERT_TRUE(IsCamelCase(u"s1234"_ns));
  ASSERT_TRUE(IsCamelCase(u"some"_ns));

  ASSERT_FALSE(IsCamelCase(u""_ns));
  ASSERT_FALSE(IsCamelCase(u"SomeName"_ns));
  ASSERT_FALSE(IsCamelCase(u"some_name"_ns));
  ASSERT_FALSE(IsCamelCase(u"SOMENAME"_ns));
  ASSERT_FALSE(IsCamelCase(u"1234"_ns));
  ASSERT_FALSE(IsCamelCase(u"some#Name"_ns));
}