execution_context_attached_data_unittest.cc 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. #include "components/performance_manager/public/execution_context/execution_context_attached_data.h"
  5. #include "base/memory/raw_ptr.h"
  6. #include "components/performance_manager/execution_context/execution_context_registry_impl.h"
  7. #include "components/performance_manager/test_support/graph_test_harness.h"
  8. #include "components/performance_manager/test_support/mock_graphs.h"
  9. #include "testing/gtest/include/gtest/gtest.h"
  10. namespace performance_manager {
  11. namespace execution_context {
  12. namespace {
  13. class FakeData : public ExecutionContextAttachedData<FakeData> {
  14. public:
  15. FakeData() = default;
  16. explicit FakeData(const ExecutionContext* ec) {}
  17. ~FakeData() override = default;
  18. };
  19. class ExecutionContextAttachedDataTest : public GraphTestHarness {
  20. public:
  21. using Super = GraphTestHarness;
  22. ExecutionContextAttachedDataTest() = default;
  23. ExecutionContextAttachedDataTest(const ExecutionContextAttachedDataTest&) =
  24. delete;
  25. ExecutionContextAttachedDataTest& operator=(
  26. const ExecutionContextAttachedDataTest&) = delete;
  27. ~ExecutionContextAttachedDataTest() override = default;
  28. void SetUp() override {
  29. Super::SetUp();
  30. registry_ =
  31. graph()->PassToGraph(std::make_unique<ExecutionContextRegistryImpl>());
  32. }
  33. protected:
  34. raw_ptr<ExecutionContextRegistryImpl> registry_ = nullptr;
  35. };
  36. } // namespace
  37. TEST_F(ExecutionContextAttachedDataTest, AdapterWorks) {
  38. MockMultiplePagesAndWorkersWithMultipleProcessesGraph mock_graph(graph());
  39. auto* ec1 =
  40. registry_->GetExecutionContextForFrameNode(mock_graph.frame.get());
  41. auto* ec2 =
  42. registry_->GetExecutionContextForWorkerNode(mock_graph.worker.get());
  43. EXPECT_FALSE(FakeData::Destroy(ec1));
  44. FakeData* fd1 = FakeData::Get(ec1);
  45. EXPECT_FALSE(fd1);
  46. fd1 = FakeData::GetOrCreate(ec1);
  47. EXPECT_TRUE(fd1);
  48. EXPECT_EQ(fd1, FakeData::Get(ec1));
  49. EXPECT_TRUE(FakeData::Destroy(ec1));
  50. EXPECT_FALSE(FakeData::Get(ec1));
  51. EXPECT_FALSE(FakeData::Destroy(ec2));
  52. FakeData* fd2 = FakeData::Get(ec2);
  53. EXPECT_FALSE(fd2);
  54. fd2 = FakeData::GetOrCreate(ec2);
  55. EXPECT_TRUE(fd2);
  56. EXPECT_EQ(fd2, FakeData::Get(ec2));
  57. EXPECT_TRUE(FakeData::Destroy(ec2));
  58. EXPECT_FALSE(FakeData::Get(ec2));
  59. }
  60. } // namespace execution_context
  61. } // namespace performance_manager