summaryrefslogtreecommitdiffstats
path: root/toolkit/components/nimbus/test/gtest
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-19 00:47:55 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-19 00:47:55 +0000
commit26a029d407be480d791972afb5975cf62c9360a6 (patch)
treef435a8308119effd964b339f76abb83a57c29483 /toolkit/components/nimbus/test/gtest
parentInitial commit. (diff)
downloadfirefox-26a029d407be480d791972afb5975cf62c9360a6.tar.xz
firefox-26a029d407be480d791972afb5975cf62c9360a6.zip
Adding upstream version 124.0.1.upstream/124.0.1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'toolkit/components/nimbus/test/gtest')
-rw-r--r--toolkit/components/nimbus/test/gtest/NimbusFeatures_GetTest.cpp187
-rw-r--r--toolkit/components/nimbus/test/gtest/NimbusFeatures_RecordExposure.cpp43
-rw-r--r--toolkit/components/nimbus/test/gtest/moz.build16
3 files changed, 246 insertions, 0 deletions
diff --git a/toolkit/components/nimbus/test/gtest/NimbusFeatures_GetTest.cpp b/toolkit/components/nimbus/test/gtest/NimbusFeatures_GetTest.cpp
new file mode 100644
index 0000000000..0880ad3ae7
--- /dev/null
+++ b/toolkit/components/nimbus/test/gtest/NimbusFeatures_GetTest.cpp
@@ -0,0 +1,187 @@
+/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/* vim: set ts=8 sts=2 et sw=2 tw=80: */
+/* 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 "gtest/gtest.h"
+#include "mozilla/Preferences.h"
+#include "mozilla/browser/NimbusFeatures.h"
+
+using namespace mozilla;
+
+static bool gPrefUpdate = false;
+
+TEST(NimbusFeaturesGet, Errors)
+{
+ ASSERT_EQ(Preferences::SetInt("nimbus.syncdatastore.foo.value", 42,
+ PrefValueKind::User),
+ NS_OK);
+ ASSERT_EQ(NimbusFeatures::GetInt("foo"_ns, "value"_ns, 0), 42);
+ ASSERT_EQ(Preferences::SetBool("nimbus.syncdatastore.foo.enabled", true,
+ PrefValueKind::User),
+ NS_OK);
+ ASSERT_TRUE(NimbusFeatures::GetBool("foo"_ns, "enabled"_ns, false));
+
+ ASSERT_EQ(Preferences::ClearUser("nimbus.syncdatastore.foo.value"), NS_OK);
+}
+
+TEST(NimbusFeaturesGetRollout, Errors)
+{
+ ASSERT_EQ(Preferences::SetInt("nimbus.syncdefaultsstore.rollout.value", 7,
+ PrefValueKind::User),
+ NS_OK);
+ ASSERT_EQ(NimbusFeatures::GetInt("rollout"_ns, "value"_ns, 0), 7);
+ ASSERT_EQ(Preferences::SetBool("nimbus.syncdefaultsstore.rollout.enabled",
+ true, PrefValueKind::User),
+ NS_OK);
+ ASSERT_TRUE(NimbusFeatures::GetBool("rollout"_ns, "enabled"_ns, false));
+}
+
+TEST(NimbusFeaturesExperimentPriorityOverRollouts, Errors)
+{
+ ASSERT_EQ(Preferences::SetInt("nimbus.syncdatastore.feature.value", 12,
+ PrefValueKind::User),
+ NS_OK);
+ ASSERT_EQ(Preferences::SetInt("nimbus.syncdefaultsstore.feature.value", 22,
+ PrefValueKind::User),
+ NS_OK);
+ ASSERT_EQ(NimbusFeatures::GetInt("feature"_ns, "value"_ns, 0), 12);
+ ASSERT_EQ(Preferences::SetBool("nimbus.syncdatastore.feature.enabled", true,
+ PrefValueKind::User),
+ NS_OK);
+ ASSERT_EQ(Preferences::SetBool("nimbus.syncdefaultsstore.feature.enabled",
+ false, PrefValueKind::User),
+ NS_OK);
+ ASSERT_TRUE(NimbusFeatures::GetBool("feature"_ns, "enabled"_ns, false));
+}
+
+TEST(NimbusFeaturesDataSourcePrecedence, Errors)
+{
+ const auto FALLBACK_VALUE = 1;
+ const auto EXPERIMENT_VALUE = 2;
+ const auto ROLLOUT_VALUE = 3;
+
+ ASSERT_EQ(Preferences::SetInt("nimbus.testing.testInt", FALLBACK_VALUE,
+ PrefValueKind::User),
+ NS_OK);
+
+ // If there is no experiment or rollout, the fallback value should be
+ // returned.
+ ASSERT_EQ(NimbusFeatures::GetInt("testFeature"_ns, "testInt"_ns, 0),
+ FALLBACK_VALUE);
+
+ // Enroll in an experiment.
+ ASSERT_EQ(Preferences::SetInt("nimbus.syncdatastore.testFeature.testInt",
+ EXPERIMENT_VALUE, PrefValueKind::User),
+ NS_OK);
+
+ // Enroll in a rollout.
+ ASSERT_EQ(Preferences::SetInt("nimbus.syncdefaultsstore.testFeature.testInt",
+ ROLLOUT_VALUE, PrefValueKind::User),
+ NS_OK);
+
+ // Experiment value should take precedence.
+ ASSERT_EQ(NimbusFeatures::GetInt("testFeature"_ns, "testInt"_ns, 0),
+ EXPERIMENT_VALUE);
+
+ // After experiments it should default to rollouts.
+ Preferences::ClearUser("nimbus.syncdatastore.testFeature.testInt");
+ ASSERT_EQ(NimbusFeatures::GetInt("testFeature"_ns, "testInt"_ns, 0),
+ ROLLOUT_VALUE);
+
+ // Cleanup
+ Preferences::ClearUser("nimbus.syncdefaultsstore.testFeature.testInt");
+ Preferences::ClearUser("nimbus.testing.testInt");
+}
+
+static void FooValueUpdated(const char* aPref, void* aUserData) {
+ ASSERT_STREQ(aPref, "nimbus.syncdatastore.foo.value");
+ ASSERT_EQ(aUserData, reinterpret_cast<void*>(13));
+
+ ASSERT_FALSE(gPrefUpdate);
+ gPrefUpdate = true;
+
+ ASSERT_EQ(NimbusFeatures::GetInt("foo"_ns, "value"_ns, 0), 24);
+}
+
+static void BarRolloutValueUpdated(const char* aPref, void* aUserData) {
+ ASSERT_STREQ(aPref, "nimbus.syncdefaultsstore.bar.value");
+
+ ASSERT_FALSE(gPrefUpdate);
+ gPrefUpdate = true;
+}
+
+TEST(NimbusFeaturesGetFallback, Errors)
+{
+ // No experiment is set and we expect to return fallback pref values
+
+ // As defined by fallbackPref browser.aboutwelcome.enabled
+ // in FeatureManifest.yaml
+ Preferences::SetBool("browser.aboutwelcome.enabled", true,
+ PrefValueKind::Default);
+ ASSERT_EQ(NimbusFeatures::GetBool("aboutwelcome"_ns, "enabled"_ns, false),
+ true);
+ Preferences::SetBool("browser.aboutwelcome.enabled", false,
+ PrefValueKind::User);
+ ASSERT_EQ(NimbusFeatures::GetBool("aboutwelcome"_ns, "enabled"_ns, true),
+ false);
+ Preferences::ClearUser("browser.aboutwelcome.enabled");
+
+ const auto FALLBACK_VALUE = 5;
+ const auto DEFAULT_VALUE = 42;
+
+ Preferences::SetInt("nimbus.testing.testInt", FALLBACK_VALUE,
+ PrefValueKind::Default);
+ ASSERT_EQ(
+ NimbusFeatures::GetInt("testFeature"_ns, "testInt"_ns, DEFAULT_VALUE),
+ FALLBACK_VALUE);
+
+ Preferences::ClearUser("nimbus.testing.testInt");
+}
+
+TEST(NimbusFeaturesUpdate, Errors)
+{
+ // Verify updating foo.value calls FooValueUpdated.
+ ASSERT_EQ(NimbusFeatures::OnUpdate("foo"_ns, "value"_ns, FooValueUpdated,
+ reinterpret_cast<void*>(13)),
+ NS_OK);
+ ASSERT_EQ(
+ NimbusFeatures::OnUpdate("bar"_ns, "value"_ns, BarRolloutValueUpdated,
+ reinterpret_cast<void*>(13)),
+ NS_OK);
+ ASSERT_EQ(Preferences::SetInt("nimbus.syncdatastore.foo.value", 24,
+ PrefValueKind::User),
+ NS_OK);
+ ASSERT_TRUE(gPrefUpdate);
+ ASSERT_EQ(NimbusFeatures::GetInt("foo"_ns, "value"_ns, 0), 24);
+
+ // Verify updating foo.enabled doesn't call FooValueUpdated.
+ ASSERT_TRUE(NimbusFeatures::GetBool("foo"_ns, "enabled"_ns, false));
+ ASSERT_EQ(Preferences::SetBool("nimbus.syncdatastore.foo.enabled", false,
+ PrefValueKind::User),
+ NS_OK);
+ ASSERT_FALSE(NimbusFeatures::GetBool("foo"_ns, "enabled"_ns, true));
+ gPrefUpdate = false;
+
+ ASSERT_EQ(Preferences::SetInt("nimbus.syncdefaultsstore.bar.value", 25,
+ PrefValueKind::User),
+ NS_OK);
+ ASSERT_TRUE(gPrefUpdate);
+ gPrefUpdate = false;
+
+ // Verify OffUpdate requires a matching user data pointer to unregister.
+ ASSERT_EQ(NimbusFeatures::OffUpdate("foo"_ns, "value"_ns, FooValueUpdated,
+ reinterpret_cast<void*>(14)),
+ NS_ERROR_FAILURE);
+
+ // Verify updating foo.value no longer calls FooValueUpdated after it has
+ // been unregistered.
+ ASSERT_EQ(NimbusFeatures::OffUpdate("foo"_ns, "value"_ns, FooValueUpdated,
+ reinterpret_cast<void*>(13)),
+ NS_OK);
+ ASSERT_EQ(Preferences::SetInt("nimbus.syncdatastore.foo.value", 25,
+ PrefValueKind::User),
+ NS_OK);
+ ASSERT_EQ(NimbusFeatures::GetInt("foo"_ns, "value"_ns, 0), 25);
+}
diff --git a/toolkit/components/nimbus/test/gtest/NimbusFeatures_RecordExposure.cpp b/toolkit/components/nimbus/test/gtest/NimbusFeatures_RecordExposure.cpp
new file mode 100644
index 0000000000..7b0d1e0e7b
--- /dev/null
+++ b/toolkit/components/nimbus/test/gtest/NimbusFeatures_RecordExposure.cpp
@@ -0,0 +1,43 @@
+/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/* vim: set ts=8 sts=2 et sw=2 tw=80: */
+/* 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 "gtest/gtest.h"
+#include "mozilla/Preferences.h"
+#include "mozilla/browser/NimbusFeatures.h"
+#include "js/Array.h"
+#include "js/PropertyAndElement.h"
+#include "js/TypeDecls.h"
+#include "TelemetryFixture.h"
+#include "TelemetryTestHelpers.h"
+
+using namespace mozilla;
+using namespace TelemetryTestHelpers;
+
+class NimbusTelemetryFixture : public TelemetryTestFixture {};
+
+TEST_F(NimbusTelemetryFixture, NimbusFeaturesTelemetry) {
+ constexpr auto prefName = "nimbus.syncdatastore.foo"_ns;
+ constexpr auto prefValue =
+ R"({"slug":"experiment-slug","branch":{"slug":"branch-slug"}})";
+ AutoJSContextWithGlobal cx(mCleanGlobal);
+ Unused << mTelemetry->ClearEvents();
+
+ ASSERT_EQ(NimbusFeatures::RecordExposureEvent("foo"_ns), NS_ERROR_UNEXPECTED)
+ << "Should fail because not enrolled in experiment";
+ // Set the experiment info for `foo`
+ Preferences::SetCString(prefName.get(), prefValue);
+ ASSERT_EQ(NimbusFeatures::RecordExposureEvent("foo"_ns), NS_OK)
+ << "Should work for the 2nd call";
+ ASSERT_EQ(NimbusFeatures::RecordExposureEvent("foo"_ns, true), NS_ERROR_ABORT)
+ << "Should abort because we've sent exposure and aOnce is true";
+ ASSERT_EQ(NimbusFeatures::RecordExposureEvent("bar"_ns), NS_ERROR_UNEXPECTED)
+ << "Should fail because we don't have an experiment for bar";
+
+ JS::Rooted<JS::Value> eventsSnapshot(cx.GetJSContext());
+ GetEventSnapshot(cx.GetJSContext(), &eventsSnapshot);
+ ASSERT_TRUE(EventPresent(cx.GetJSContext(), eventsSnapshot, "normandy"_ns,
+ "expose"_ns, "nimbus_experiment"_ns));
+}
diff --git a/toolkit/components/nimbus/test/gtest/moz.build b/toolkit/components/nimbus/test/gtest/moz.build
new file mode 100644
index 0000000000..41befbcd46
--- /dev/null
+++ b/toolkit/components/nimbus/test/gtest/moz.build
@@ -0,0 +1,16 @@
+# -*- Mode: python; indent-tabs-mode: nil; tab-width: 40 -*-
+# vim: set filetype=python:
+# 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/.
+
+UNIFIED_SOURCES += [
+ "NimbusFeatures_GetTest.cpp",
+ "NimbusFeatures_RecordExposure.cpp",
+]
+
+LOCAL_INCLUDES += [
+ "/toolkit/components/telemetry/tests/gtest",
+]
+
+FINAL_LIBRARY = "xul-gtest"