chromium/base/scoped_observation_unittest.cc

// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/scoped_observation.h"

#include "base/containers/contains.h"
#include "base/memory/raw_ptr.h"
#include "base/ranges/algorithm.h"
#include "base/scoped_observation_traits.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace base {
namespace {

class TestSourceObserver {};

class TestSource {};

void TestSource::AddObserver(TestSourceObserver* observer) {}

void TestSource::RemoveObserver(TestSourceObserver* observer) {}

bool TestSource::HasObserver(TestSourceObserver* observer) const {}

TestScopedObservation;

}  // namespace

TEST(ScopedObservationTest, RemovesObservationOnDestruction) {}

TEST(ScopedObservationTest, Reset) {}

TEST(ScopedObservationTest, IsObserving) {}

TEST(ScopedObservationTest, IsObservingSource) {}

namespace {

// A test source with oddly named Add/Remove functions.
class TestSourceWithNonDefaultNames {};

TestScopedObservationWithNonDefaultNames;

}  // namespace

template <>
struct ScopedObservationTraits<TestSourceWithNonDefaultNames,
                               TestSourceObserver> {};

TEST(ScopedObservationTest, NonDefaultNames) {}

namespace {

// A forward-declared test source.

class TestSourceFwd;

class ObservationHolder : public TestSourceObserver {};

// TestSourceFwd gets an actual definition!
class TestSourceFwd : public TestSource {};

// Calling ScopedObservation::Observe() requires an actual definition rather
// than just a forward declaration; make sure it compiles now that there is a
// definition.
ObservationHolder::ObservationHolder(TestSourceFwd* source) {}

}  // namespace

TEST(ScopedObservationTest, ForwardDeclaredSource) {}

namespace {

class TestSourceWithNonDefaultNamesFwd;

class ObservationWithNonDefaultNamesHolder : public TestSourceObserver {};

// TestSourceWithNonDefaultNamesFwd gets an actual definition!
class TestSourceWithNonDefaultNamesFwd : public TestSourceWithNonDefaultNames {};

}  // namespace

// Now we define the corresponding traits. ScopedObservationTraits
// specializations must be defined in base::, since that is where the primary
// template definition lives.
template <>
struct ScopedObservationTraits<TestSourceWithNonDefaultNamesFwd,
                               TestSourceObserver> {};

namespace {

// Calling ScopedObservation::Observe() requires an actual definition rather
// than just a forward declaration; make sure it compiles now that there is
// a definition.
ObservationWithNonDefaultNamesHolder::ObservationWithNonDefaultNamesHolder(
    TestSourceWithNonDefaultNamesFwd* source) {}

}  // namespace

TEST(ScopedObservationTest, ForwardDeclaredSourceWithNonDefaultNames) {}

}  // namespace base