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
   97
   98
   99
  100
  101
  102
  103
  104
  105
  106
  107
  108
  109
  110
  111
  112
  113
  114
  115
  116
  117
  118
  119
  120
  121
  122
  123
  124
  125
  126
  127
  128
  129
  130
  131
  132
  133
  134
  135
  136
  137
  138
  139
  140
  141
  142
  143
  144
  145
  146
  147
  148
  149
  150
  151
  152
  153
  154
  155
  156
  157
  158
  159
  160
  161
  162
  163
  164
  165
  166
  167
  168
  169
  170
  171

base / trace_event / traced_value_support_unittest.cc [blame]

// Copyright 2021 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/trace_event/traced_value_support.h"

#include <optional>
#include <string_view>

#include "base/memory/ref_counted.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/perfetto/include/perfetto/test/traced_value_test_support.h"

namespace base {
namespace trace_event {

namespace {

struct RefCountedData : RefCounted<RefCountedData> {
 public:
  explicit RefCountedData(std::string data) : data_(data) {}

  void WriteIntoTrace(perfetto::TracedValue context) const {
    std::move(context).WriteString(data_);
  }

 private:
  ~RefCountedData() = default;
  friend class RefCounted<RefCountedData>;

  std::string data_;
};

struct WeakData {
 public:
  explicit WeakData(std::string data) : data_(data) {}

  void WriteIntoTrace(perfetto::TracedValue context) const {
    std::move(context).WriteString(data_);
  }

  base::WeakPtr<const WeakData> GetWeakPtr() const {
    return weak_ptr_factory_.GetWeakPtr();
  }

 private:
  std::string data_;

  base::WeakPtrFactory<WeakData> weak_ptr_factory_{this};
};

}  // namespace

TEST(TracedValueSupportTest, ScopedRefPtr) {
  EXPECT_EQ(
      perfetto::TracedValueToString(scoped_refptr<RefCountedData>(nullptr)),
      "0x0");
  scoped_refptr<RefCountedData> data =
      base::MakeRefCounted<RefCountedData>("refcounted");
  EXPECT_EQ(perfetto::TracedValueToString(data), "refcounted");
}

TEST(TracedValueSupportTest, Optional) {
  EXPECT_EQ(perfetto::TracedValueToString(std::optional<int>()), "0x0");
  EXPECT_EQ(perfetto::TracedValueToString(std::optional<const int>(42)), "42");
}

TEST(TracedValueSupportTest, WeakPtr) {
  std::unique_ptr<WeakData> data = std::make_unique<WeakData>("weak");
  base::WeakPtr<const WeakData> weak_ptr = data->GetWeakPtr();
  EXPECT_EQ(perfetto::TracedValueToString(weak_ptr), "weak");
  data.reset();
  EXPECT_EQ(perfetto::TracedValueToString(weak_ptr), "0x0");
}

TEST(TracedValueSupportTest, Time) {
  EXPECT_EQ(perfetto::TracedValueToString(base::Microseconds(42)), "42");
  EXPECT_EQ(
      perfetto::TracedValueToString(base::Time() + base::Microseconds(42)),
      "42");
  EXPECT_EQ(
      perfetto::TracedValueToString(base::TimeTicks() + base::Microseconds(42)),
      "42");
}

TEST(TracedValueSupportTest, UnguessableToken) {
  auto token = UnguessableToken::Create();
  EXPECT_EQ(perfetto::TracedValueToString(token), token.ToString());
}

TEST(TracedValueSupportTest, UTF16String) {
  EXPECT_EQ(perfetto::TracedValueToString(u"utf-16"), "utf-16");
  EXPECT_EQ(
      perfetto::TracedValueToString(static_cast<const char16_t*>(u"utf-16")),
      "utf-16");
  EXPECT_EQ(perfetto::TracedValueToString(std::u16string(u"utf-16")), "utf-16");
}

TEST(TracedValueSupportTest, WideString) {
  EXPECT_EQ(perfetto::TracedValueToString(L"wide"), "wide");
  EXPECT_EQ(perfetto::TracedValueToString(static_cast<const wchar_t*>(L"wide")),
            "wide");
  EXPECT_EQ(perfetto::TracedValueToString(std::wstring(L"wide")), "wide");
}

TEST(TracedValueSupportTest, StdString) {
  EXPECT_EQ(perfetto::TracedValueToString(std::string_view("string")),
            "string");
  EXPECT_EQ(perfetto::TracedValueToString(std::u16string_view(u"utf-16")),
            "utf-16");
  EXPECT_EQ(perfetto::TracedValueToString(std::wstring_view(L"wide")), "wide");
}

TEST(TracedValueSupportTest, RawPtr) {
  // Serialise nullptr.
  EXPECT_EQ(perfetto::TracedValueToString(raw_ptr<int>()), "0x0");

  {
    // If the pointer is non-null, its dereferenced value will be serialised.
    int value = 42;
    raw_ptr<int> value_simple(&value);
    raw_ptr<int, AllowPtrArithmetic> value_with_traits(&value);

    EXPECT_EQ(perfetto::TracedValueToString(value), "42");
    EXPECT_EQ(perfetto::TracedValueToString(value_with_traits), "42");
  }

  struct WithTraceSupport {
    void WriteIntoTrace(perfetto::TracedValue ctx) const {
      std::move(ctx).WriteString("result");
    }
  };

  {
    WithTraceSupport value;
    raw_ptr<WithTraceSupport> value_simple(&value);
    raw_ptr<WithTraceSupport, AllowPtrArithmetic> value_with_traits(&value);

    EXPECT_EQ(perfetto::TracedValueToString(value_simple), "result");
    EXPECT_EQ(perfetto::TracedValueToString(value_with_traits), "result");
  }
}

TEST(TracedValueSupportTest, RawRef) {
  {
    int value = 42;
    raw_ref<int> value_simple(value);
    raw_ref<int, AllowPtrArithmetic> value_with_traits(value);

    EXPECT_EQ(perfetto::TracedValueToString(value), "42");
    EXPECT_EQ(perfetto::TracedValueToString(value_with_traits), "42");
  }

  struct WithTraceSupport {
    void WriteIntoTrace(perfetto::TracedValue ctx) const {
      std::move(ctx).WriteString("result");
    }
  };

  {
    WithTraceSupport value;
    raw_ref<WithTraceSupport> value_simple(value);
    raw_ref<WithTraceSupport, AllowPtrArithmetic> value_with_traits(value);

    EXPECT_EQ(perfetto::TracedValueToString(value_simple), "result");
    EXPECT_EQ(perfetto::TracedValueToString(value_with_traits), "result");
  }
}

}  // namespace trace_event
}  // namespace base