TestWebRTCCodecInfo and TestAudioFocusManager each carried their own RAII helper to set a boolean pref for the duration of a test and restore it afterwards. Move that into mozilla/gtest/ScopedPrefSetter.h so gtests share a single copy. Differential Revision: https://phabricator.services.mozilla.com/D310214
46 lines
1.5 KiB
C++
46 lines
1.5 KiB
C++
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
/* vim: set ts=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/. */
|
|
|
|
#ifndef mozilla_gtest_ScopedPrefSetter_h
|
|
#define mozilla_gtest_ScopedPrefSetter_h
|
|
|
|
#include "mozilla/Assertions.h"
|
|
#include "mozilla/Attributes.h"
|
|
#include "mozilla/Preferences.h"
|
|
#include "nsThreadUtils.h"
|
|
|
|
namespace mozilla {
|
|
|
|
// Sets a boolean preference for the lifetime of the instance and restores the
|
|
// previous value on destruction, so a test does not leak its pref change into
|
|
// later tests. Must be constructed and destroyed on the main thread.
|
|
class MOZ_RAII ScopedPrefSetter {
|
|
public:
|
|
ScopedPrefSetter(const char* aPrefName, bool aValue)
|
|
: mPrefName(aPrefName),
|
|
mOriginalValue(Preferences::GetBool(aPrefName, false)) {
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
Preferences::SetBool(mPrefName, aValue);
|
|
}
|
|
~ScopedPrefSetter() {
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
Preferences::SetBool(mPrefName, mOriginalValue);
|
|
}
|
|
|
|
ScopedPrefSetter(const ScopedPrefSetter&) = delete;
|
|
ScopedPrefSetter& operator=(const ScopedPrefSetter&) = delete;
|
|
ScopedPrefSetter(ScopedPrefSetter&&) = delete;
|
|
ScopedPrefSetter& operator=(ScopedPrefSetter&&) = delete;
|
|
|
|
private:
|
|
const char* mPrefName;
|
|
const bool mOriginalValue;
|
|
};
|
|
|
|
} // namespace mozilla
|
|
|
|
#endif // mozilla_gtest_ScopedPrefSetter_h
|