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

content / browser / interest_group / test_interest_group_observer.h [blame]

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

#ifndef CONTENT_BROWSER_INTEREST_GROUP_TEST_INTEREST_GROUP_OBSERVER_H_
#define CONTENT_BROWSER_INTEREST_GROUP_TEST_INTEREST_GROUP_OBSERVER_H_

#include <map>
#include <memory>
#include <optional>
#include <ostream>
#include <string>
#include <tuple>
#include <vector>

#include "base/run_loop.h"
#include "base/time/time.h"
#include "content/browser/interest_group/interest_group_manager_impl.h"
#include "url/origin.h"

namespace content {

// Reusable InterestGroupObserver that logs all events that occur and can wait
// until an expected set of events is observed.
class TestInterestGroupObserver
    : public InterestGroupManagerImpl::InterestGroupObserver {
 public:
  struct Entry {
    Entry(
        std::string devtools_auction_id,
        InterestGroupManagerImpl::InterestGroupObserver::AccessType access_type,
        url::Origin owner_origin,
        std::string ig_name,
        std::optional<double> bid = std::nullopt,
        std::optional<std::string> bid_currency = std::nullopt,
        std::optional<url::Origin> component_seller_origin = std::nullopt);
    Entry(const Entry&);
    ~Entry();

    Entry& operator=(const Entry&);
    bool operator==(const Entry&) const;

    // The auction ID is replaced by either "global" or a number starting from
    // 1, strinfigied.
    std::string devtools_auction_id;
    InterestGroupManagerImpl::InterestGroupObserver::AccessType access_type;
    url::Origin owner_origin;
    std::string ig_name;
    std::optional<double> bid;
    std::optional<std::string> bid_currency;
    std::optional<url::Origin> component_seller_origin;
  };

  TestInterestGroupObserver();
  ~TestInterestGroupObserver() override;

  TestInterestGroupObserver(const TestInterestGroupObserver&) = delete;
  TestInterestGroupObserver& operator=(const TestInterestGroupObserver&) =
      delete;

  // InterestGroupManagerImpl::InterestGroupObserver implementation:
  void OnInterestGroupAccessed(
      base::optional_ref<const std::string> devtools_auction_id,
      base::Time access_time,
      InterestGroupManagerImpl::InterestGroupObserver::AccessType type,
      const url::Origin& owner_origin,
      const std::string& name,
      base::optional_ref<const url::Origin> component_seller_origin,
      std::optional<double> bid,
      base::optional_ref<const std::string> bid_currency) override;

  // Waits until exactly the expected events have seen, in the passed in order.
  // Once they have been observed, clears the log of the previous events. Note
  // that the vector includes events that happened before WaitForAccesses() was
  // invoked.
  void WaitForAccesses(const std::vector<Entry>& expected);

  // Just like WaitForAccesses(), but expects entries to be in the exact order
  // provided. Order of events logged on auction completion / using an auction
  // result (e.g., kBid, kWin) is not guaranteed. This should only be used for
  // events with a guaranteed order.
  void WaitForAccessesInOrder(const std::vector<Entry>& expected);

 private:
  std::map<std::string, std::string> seen_auction_ids_;
  std::vector<Entry> accesses_;
  std::vector<Entry> expected_;
  std::unique_ptr<base::RunLoop> run_loop_;
};

// Printer for gtest.
void PrintTo(const TestInterestGroupObserver::Entry& e, std::ostream* os);

}  // namespace content

#endif  // CONTENT_BROWSER_INTEREST_GROUP_TEST_INTEREST_GROUP_OBSERVER_H_