1 // Copyright 2020 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. 4 5 #ifndef THIRD_PARTY_BLINK_PUBLIC_COMMON_PRIVACY_BUDGET_SCOPED_SWITCH_SAMPLE_COLLECTOR_H_ 6 #define THIRD_PARTY_BLINK_PUBLIC_COMMON_PRIVACY_BUDGET_SCOPED_SWITCH_SAMPLE_COLLECTOR_H_ 7 8 #include "base/component_export.h" 9 #include "third_party/blink/public/common/common_export.h" 10 #include "third_party/blink/public/common/privacy_budget/identifiability_sample_collector.h" 11 12 namespace blink { 13 namespace test { 14 15 // ***NOTE *** 16 // If you are looking for an intropspectable `IdentifiabilitySampleCollector` 17 // for testing, you are probably looking for 18 // `ScopedIdentifiabilityTestSampleCollector` instead. 19 // 20 // `IdentifiabilitySampleCollector` is a per-process singleton meant to be 21 // accessible from anywhere and from any thread. For testing purposes, however, 22 // it would be convenient to swap out the default collector and instead use 23 // a test stand-in. 24 // 25 // `ScopedSwitchSampleCollector` sets the default sample collector to 26 // a replacement object for the duration of the `ScopedSwitchSampleCollector`'s 27 // lifetime. 28 // 29 // Example usage: 30 // 31 // TEST(MyTest, Something) { 32 // MyFakeSampleCollector collector; 33 // ScopedSwitchSampleCollector scoped_default(&collector); 34 // ... 35 // } 36 // 37 // `ScopedSwitchSampleCollector` does not nest. Instantiating a new object while 38 // there's an active `ScopedSwitchSampleCollector` isn't allowed and will 39 // `DCHECK` on debug builds. COMPONENT_EXPORT(PRIVACY_BUDGET_TEST_SUPPORT)40class COMPONENT_EXPORT(PRIVACY_BUDGET_TEST_SUPPORT) 41 ScopedSwitchSampleCollector { 42 public: 43 // Sets `replacement` as the default `IdentifiabilitySampleCollector` for the 44 // duration of this object's lifetime. `replacement` must outlive this object. 45 explicit ScopedSwitchSampleCollector( 46 IdentifiabilitySampleCollector* replacement); 47 48 ~ScopedSwitchSampleCollector(); 49 }; 50 51 } // namespace test 52 } // namespace blink 53 54 #endif // THIRD_PARTY_BLINK_PUBLIC_COMMON_PRIVACY_BUDGET_SCOPED_SWITCH_SAMPLE_COLLECTOR_H_ 55