OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 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 |
| 5 #include "base/bind.h" |
| 6 #include "content/browser/tracing/background_tracing_endpoint.h" |
| 7 #include "content/browser/tracing/background_tracing_manager.h" |
| 8 #include "content/browser/tracing/background_tracing_scenario.h" |
| 9 #include "content/public/test/content_browser_test.h" |
| 10 #include "content/public/test/content_browser_test_utils.h" |
| 11 #include "content/public/test/test_utils.h" |
| 12 |
| 13 namespace content { |
| 14 |
| 15 class BackgroundTracingManagerBrowserTest : public ContentBrowserTest { |
| 16 public: |
| 17 BackgroundTracingManagerBrowserTest() {} |
| 18 |
| 19 private: |
| 20 DISALLOW_COPY_AND_ASSIGN(BackgroundTracingManagerBrowserTest); |
| 21 }; |
| 22 |
| 23 class BackgroundTracingManagerBrowserTestEndpoint |
| 24 : public BackgroundTracingEndpoint { |
| 25 public: |
| 26 BackgroundTracingManagerBrowserTestEndpoint(const base::Closure& callback) |
| 27 : callback_(callback), receive_count_(0) {} |
| 28 |
| 29 void ReceiveTraceFinalContents(const std::string& file_contents) override { |
| 30 receive_count_ += 1; |
| 31 done_callback_.Run(); |
| 32 callback_.Run(); |
| 33 } |
| 34 |
| 35 int get_receive_count() const { return receive_count_; } |
| 36 |
| 37 private: |
| 38 ~BackgroundTracingManagerBrowserTestEndpoint() override {} |
| 39 |
| 40 base::Closure callback_; |
| 41 int receive_count_; |
| 42 }; |
| 43 |
| 44 class BackgroundTracingManagerBrowserTestScenario |
| 45 : public BackgroundTracingScenario { |
| 46 public: |
| 47 BackgroundTracingManagerBrowserTestScenario( |
| 48 scoped_refptr<BackgroundTracingManagerBrowserTestEndpoint> e) |
| 49 : BackgroundTracingScenario(e) {} |
| 50 |
| 51 protected: |
| 52 ~BackgroundTracingManagerBrowserTestScenario() override {} |
| 53 }; |
| 54 |
| 55 void StartedFinalizingCallback(base::Closure callback, |
| 56 bool expected, |
| 57 bool value) { |
| 58 EXPECT_EQ(expected, value); |
| 59 if (!callback.is_null()) |
| 60 callback.Run(); |
| 61 } |
| 62 |
| 63 // This tests that the endpoint receives the final trace data. |
| 64 IN_PROC_BROWSER_TEST_F(BackgroundTracingManagerBrowserTest, |
| 65 ReceiveTraceFinalContentsOnTrigger) { |
| 66 { |
| 67 base::RunLoop run_loop; |
| 68 scoped_refptr<BackgroundTracingManagerBrowserTestEndpoint> endpoint = |
| 69 new BackgroundTracingManagerBrowserTestEndpoint(run_loop.QuitClosure()); |
| 70 scoped_refptr<BackgroundTracingManagerBrowserTestScenario> scenario = |
| 71 new BackgroundTracingManagerBrowserTestScenario(endpoint); |
| 72 |
| 73 EXPECT_FALSE( |
| 74 BackgroundTracingManager::GetInstance()->is_recording_for_testing()); |
| 75 |
| 76 BackgroundTracingManager::GetInstance()->SetActiveScenario(scenario); |
| 77 |
| 78 EXPECT_TRUE( |
| 79 BackgroundTracingManager::GetInstance()->is_recording_for_testing()); |
| 80 |
| 81 BackgroundTracingManager::GetInstance()->TryFinalizingBackgroundTracing( |
| 82 base::Bind(&StartedFinalizingCallback, base::Closure(), true)); |
| 83 |
| 84 EXPECT_TRUE( |
| 85 BackgroundTracingManager::GetInstance()->is_gathering_for_testing()); |
| 86 |
| 87 run_loop.Run(); |
| 88 |
| 89 EXPECT_FALSE( |
| 90 BackgroundTracingManager::GetInstance()->is_gathering_for_testing()); |
| 91 EXPECT_TRUE(endpoint->get_receive_count() == 1); |
| 92 } |
| 93 } |
| 94 |
| 95 // This tests trigger more than once still only gathers once. |
| 96 IN_PROC_BROWSER_TEST_F(BackgroundTracingManagerBrowserTest, |
| 97 MultipleTriggersOnlyGatherOnce) { |
| 98 { |
| 99 base::RunLoop run_loop; |
| 100 scoped_refptr<BackgroundTracingManagerBrowserTestEndpoint> endpoint = |
| 101 new BackgroundTracingManagerBrowserTestEndpoint(run_loop.QuitClosure()); |
| 102 scoped_refptr<BackgroundTracingManagerBrowserTestScenario> scenario = |
| 103 new BackgroundTracingManagerBrowserTestScenario(endpoint); |
| 104 |
| 105 EXPECT_FALSE( |
| 106 BackgroundTracingManager::GetInstance()->is_recording_for_testing()); |
| 107 |
| 108 BackgroundTracingManager::GetInstance()->SetActiveScenario(scenario); |
| 109 |
| 110 EXPECT_TRUE( |
| 111 BackgroundTracingManager::GetInstance()->is_recording_for_testing()); |
| 112 |
| 113 BackgroundTracingManager::GetInstance()->TryFinalizingBackgroundTracing( |
| 114 base::Bind(&StartedFinalizingCallback, base::Closure(), true)); |
| 115 |
| 116 EXPECT_TRUE( |
| 117 BackgroundTracingManager::GetInstance()->is_gathering_for_testing()); |
| 118 |
| 119 BackgroundTracingManager::GetInstance()->TryFinalizingBackgroundTracing( |
| 120 base::Bind(&StartedFinalizingCallback, base::Closure(), false)); |
| 121 |
| 122 run_loop.Run(); |
| 123 |
| 124 EXPECT_FALSE( |
| 125 BackgroundTracingManager::GetInstance()->is_gathering_for_testing()); |
| 126 EXPECT_TRUE(endpoint->get_receive_count() == 1); |
| 127 } |
| 128 } |
| 129 |
| 130 // This tests that you can't trigger without a scenario set. |
| 131 IN_PROC_BROWSER_TEST_F(BackgroundTracingManagerBrowserTest, |
| 132 CannotTriggerWithoutScenarioSet) { |
| 133 { |
| 134 base::RunLoop run_loop; |
| 135 |
| 136 EXPECT_FALSE( |
| 137 BackgroundTracingManager::GetInstance()->is_recording_for_testing()); |
| 138 EXPECT_FALSE( |
| 139 BackgroundTracingManager::GetInstance()->is_gathering_for_testing()); |
| 140 |
| 141 BackgroundTracingManager::GetInstance()->TryFinalizingBackgroundTracing( |
| 142 base::Bind(&StartedFinalizingCallback, run_loop.QuitClosure(), false)); |
| 143 |
| 144 run_loop.Run(); |
| 145 |
| 146 EXPECT_FALSE( |
| 147 BackgroundTracingManager::GetInstance()->is_recording_for_testing()); |
| 148 EXPECT_FALSE( |
| 149 BackgroundTracingManager::GetInstance()->is_gathering_for_testing()); |
| 150 } |
| 151 } |
| 152 |
| 153 // This tests triggering with a null callback. |
| 154 IN_PROC_BROWSER_TEST_F(BackgroundTracingManagerBrowserTest, |
| 155 TriggerWithNullCallback) { |
| 156 BackgroundTracingManager::GetInstance()->TryFinalizingBackgroundTracing( |
| 157 BackgroundTracingManager::StartedFinalizingCallback()); |
| 158 } |
| 159 |
| 160 } // namespace content |
OLD | NEW |