decorators_utils_unittest.cc 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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/decorators/decorators_utils.h"
  5. #include <utility>
  6. #include "base/test/bind.h"
  7. #include "components/performance_manager/graph/node_attached_data_impl.h"
  8. #include "components/performance_manager/test_support/performance_manager_test_harness.h"
  9. #include "content/public/browser/web_contents.h"
  10. #include "testing/gtest/include/gtest/gtest.h"
  11. namespace performance_manager {
  12. namespace {
  13. class FakePageNodeDecoratorData
  14. : public NodeAttachedDataImpl<FakePageNodeDecoratorData> {
  15. public:
  16. struct Traits : public NodeAttachedDataInMap<PageNodeImpl> {};
  17. FakePageNodeDecoratorData() = default;
  18. ~FakePageNodeDecoratorData() override = default;
  19. FakePageNodeDecoratorData(const FakePageNodeDecoratorData& other) = delete;
  20. FakePageNodeDecoratorData& operator=(const FakePageNodeDecoratorData&) =
  21. delete;
  22. void SetOnSetPropertyCalledExpectations(base::OnceClosure closure_to_call,
  23. int expected_value) {
  24. closure_to_call_ = std::move(closure_to_call);
  25. expected_value_ = expected_value;
  26. }
  27. void SetProperty(int value) {
  28. EXPECT_EQ(expected_value_, value);
  29. std::move(closure_to_call_).Run();
  30. }
  31. private:
  32. base::OnceClosure closure_to_call_;
  33. int expected_value_;
  34. friend class ::performance_manager::NodeAttachedDataImpl<
  35. FakePageNodeDecoratorData>;
  36. explicit FakePageNodeDecoratorData(const PageNodeImpl* page_node) {}
  37. };
  38. class DecoratorsUtilsTest : public PerformanceManagerTestHarness {
  39. public:
  40. using Super = PerformanceManagerTestHarness;
  41. void SetUp() override {
  42. Super::SetUp();
  43. SetContents(CreateTestWebContents());
  44. }
  45. void TearDown() override {
  46. DeleteContents();
  47. Super::TearDown();
  48. }
  49. };
  50. } // namespace
  51. // Test that the function parameter for SetPropertyForWebContentsPageNode has
  52. // been called.
  53. TEST_F(DecoratorsUtilsTest, SetPropertyForWebContentsPageNode) {
  54. base::RunLoop run_loop;
  55. constexpr int kFakePropertyValue = 1234;
  56. // Set up and create a dummy PageNode.
  57. base::WeakPtr<PageNode> node =
  58. PerformanceManager::GetPrimaryPageNodeForWebContents(web_contents());
  59. auto quit_closure = run_loop.QuitClosure();
  60. auto call_on_graph_cb = base::BindLambdaForTesting([&]() {
  61. EXPECT_TRUE(node);
  62. FakePageNodeDecoratorData::GetOrCreate(PageNodeImpl::FromNode(node.get()))
  63. ->SetOnSetPropertyCalledExpectations(std::move(quit_closure),
  64. kFakePropertyValue);
  65. });
  66. PerformanceManager::CallOnGraph(FROM_HERE, call_on_graph_cb);
  67. // Call to the tested function with SetProperty passed in as argument.
  68. // SetProperty contains the RunLoop's quit closure.
  69. SetPropertyForWebContentsPageNode(web_contents(),
  70. &FakePageNodeDecoratorData::SetProperty,
  71. kFakePropertyValue);
  72. // This will run until SetProperty calls the closure.
  73. run_loop.Run();
  74. }
  75. } // namespace performance_manager