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/public/browser/background_tracing_manager.h" |
| 7 #include "content/public/test/content_browser_test.h" |
| 8 #include "content/public/test/content_browser_test_utils.h" |
| 9 #include "content/public/test/test_utils.h" |
| 10 |
| 11 namespace content { |
| 12 |
| 13 class BackgroundTracingManagerBrowserTest : public ContentBrowserTest { |
| 14 public: |
| 15 BackgroundTracingManagerBrowserTest() {} |
| 16 |
| 17 private: |
| 18 DISALLOW_COPY_AND_ASSIGN(BackgroundTracingManagerBrowserTest); |
| 19 }; |
| 20 |
| 21 class BackgroundTracingManagerTestSink |
| 22 : public BackgroundTracingManager::UploadSink { |
| 23 public: |
| 24 BackgroundTracingManagerTestSink(const base::Closure& callback) |
| 25 : callback_(callback), receive_count_(0) {} |
| 26 |
| 27 void Upload( |
| 28 const std::string& file_contents, |
| 29 base::Callback<void()> done_callback) override { |
| 30 printf("Size: %lu\n", file_contents.size()); |
| 31 printf("CrashBackendEndpoint: Uploading.\n"); |
| 32 receive_count_ += 1; |
| 33 done_callback.Run(); |
| 34 callback_.Run(); |
| 35 } |
| 36 |
| 37 bool RequiresAnonymizedData() const override { |
| 38 return true; |
| 39 } |
| 40 |
| 41 int get_receive_count() const { return receive_count_; } |
| 42 |
| 43 private: |
| 44 ~BackgroundTracingManagerTestSink() override {} |
| 45 |
| 46 base::Closure callback_; |
| 47 int receive_count_; |
| 48 }; |
| 49 |
| 50 void StartedFinalizingCallback(base::Closure callback, |
| 51 bool expected, |
| 52 bool value) { |
| 53 EXPECT_EQ(expected, value); |
| 54 if (!callback.is_null()) |
| 55 callback.Run(); |
| 56 } |
| 57 |
| 58 scoped_refptr<content::BackgroundTracingPreemptiveConfig> |
| 59 CreatePreemptiveConfig() { |
| 60 scoped_refptr<content::BackgroundTracingPreemptiveConfig> config = |
| 61 new content::BackgroundTracingPreemptiveConfig(); |
| 62 |
| 63 content::BackgroundTracingPreemptiveConfig::MonitoringRule rule; |
| 64 rule.type = content::BackgroundTracingPreemptiveConfig::MONITOR_AND_DUMP_WHEN_
TRIGGER_NAMED; |
| 65 rule.named_trigger_info.trigger_name = "test"; |
| 66 config->configs.push_back(rule); |
| 67 |
| 68 return config; |
| 69 } |
| 70 |
| 71 void SetupBackgroundTracingManager() { |
| 72 content::BackgroundTracingManager::GetInstance()->InvalidateTriggerHandlesForT
esting(); |
| 73 } |
| 74 |
| 75 void DisableScenarioWhenIdle() { |
| 76 BackgroundTracingManager::GetInstance()->SetActiveScenario(NULL, NULL); |
| 77 } |
| 78 |
| 79 // This tests that the endpoint receives the final trace data. |
| 80 IN_PROC_BROWSER_TEST_F(BackgroundTracingManagerBrowserTest, |
| 81 ReceiveTraceFinalContentsOnTrigger) { |
| 82 { |
| 83 SetupBackgroundTracingManager(); |
| 84 |
| 85 base::RunLoop run_loop; |
| 86 scoped_refptr<BackgroundTracingManagerTestSink> endpoint = |
| 87 new BackgroundTracingManagerTestSink(run_loop.QuitClosure()); |
| 88 |
| 89 scoped_refptr<content::BackgroundTracingPreemptiveConfig> config = |
| 90 CreatePreemptiveConfig(); |
| 91 |
| 92 content::BackgroundTracingManager::TriggerHandle handle = |
| 93 content::BackgroundTracingManager::GetInstance()->RegisterTriggerType( |
| 94 "test"); |
| 95 |
| 96 BackgroundTracingManager::GetInstance()->SetActiveScenario( |
| 97 config, endpoint); |
| 98 |
| 99 BackgroundTracingManager::GetInstance()->WhenIdle( |
| 100 base::Bind(&DisableScenarioWhenIdle)); |
| 101 |
| 102 BackgroundTracingManager::GetInstance()->DidTriggerHappen( |
| 103 handle, |
| 104 base::Bind(&StartedFinalizingCallback, base::Closure(), true)); |
| 105 |
| 106 run_loop.Run(); |
| 107 |
| 108 EXPECT_TRUE(endpoint->get_receive_count() == 1); |
| 109 } |
| 110 } |
| 111 |
| 112 // This tests trigger more than once still only gathers once. |
| 113 IN_PROC_BROWSER_TEST_F(BackgroundTracingManagerBrowserTest, |
| 114 MultipleTriggersOnlyGatherOnce) { |
| 115 { |
| 116 SetupBackgroundTracingManager(); |
| 117 |
| 118 base::RunLoop run_loop; |
| 119 scoped_refptr<BackgroundTracingManagerTestSink> endpoint = |
| 120 new BackgroundTracingManagerTestSink(run_loop.QuitClosure()); |
| 121 |
| 122 scoped_refptr<content::BackgroundTracingPreemptiveConfig> config = |
| 123 CreatePreemptiveConfig(); |
| 124 |
| 125 content::BackgroundTracingManager::TriggerHandle handle = |
| 126 content::BackgroundTracingManager::GetInstance()->RegisterTriggerType( |
| 127 "test"); |
| 128 |
| 129 BackgroundTracingManager::GetInstance()->SetActiveScenario( |
| 130 config, endpoint); |
| 131 |
| 132 BackgroundTracingManager::GetInstance()->WhenIdle( |
| 133 base::Bind(&DisableScenarioWhenIdle)); |
| 134 |
| 135 BackgroundTracingManager::GetInstance()->DidTriggerHappen( |
| 136 handle, |
| 137 base::Bind(&StartedFinalizingCallback, base::Closure(), true)); |
| 138 |
| 139 run_loop.Run(); |
| 140 |
| 141 EXPECT_TRUE(endpoint->get_receive_count() == 1); |
| 142 } |
| 143 } |
| 144 |
| 145 // This tests that you can't trigger without a scenario set. |
| 146 IN_PROC_BROWSER_TEST_F(BackgroundTracingManagerBrowserTest, |
| 147 CannotTriggerWithoutScenarioSet) { |
| 148 { |
| 149 SetupBackgroundTracingManager(); |
| 150 |
| 151 base::RunLoop run_loop; |
| 152 scoped_refptr<BackgroundTracingManagerTestSink> endpoint = |
| 153 new BackgroundTracingManagerTestSink(base::Closure()); |
| 154 |
| 155 scoped_refptr<content::BackgroundTracingPreemptiveConfig> config = |
| 156 CreatePreemptiveConfig(); |
| 157 |
| 158 content::BackgroundTracingManager::TriggerHandle handle = |
| 159 content::BackgroundTracingManager::GetInstance()->RegisterTriggerType( |
| 160 "test"); |
| 161 |
| 162 BackgroundTracingManager::GetInstance()->DidTriggerHappen( |
| 163 handle, |
| 164 base::Bind(&StartedFinalizingCallback, run_loop.QuitClosure(), false)); |
| 165 |
| 166 run_loop.Run(); |
| 167 |
| 168 EXPECT_TRUE(endpoint->get_receive_count() == 0); |
| 169 } |
| 170 } |
| 171 |
| 172 // This tests that no trace is triggered with a handle that isn't specified |
| 173 // in the config. |
| 174 IN_PROC_BROWSER_TEST_F(BackgroundTracingManagerBrowserTest, |
| 175 DoesNotTriggerWithWrongHandle) { |
| 176 { |
| 177 SetupBackgroundTracingManager(); |
| 178 |
| 179 base::RunLoop run_loop; |
| 180 scoped_refptr<BackgroundTracingManagerTestSink> endpoint = |
| 181 new BackgroundTracingManagerTestSink(base::Closure()); |
| 182 |
| 183 scoped_refptr<content::BackgroundTracingPreemptiveConfig> config = |
| 184 CreatePreemptiveConfig(); |
| 185 |
| 186 content::BackgroundTracingManager::TriggerHandle handle = |
| 187 content::BackgroundTracingManager::GetInstance()->RegisterTriggerType( |
| 188 "does_not_exist"); |
| 189 |
| 190 BackgroundTracingManager::GetInstance()->SetActiveScenario( |
| 191 config, endpoint); |
| 192 |
| 193 BackgroundTracingManager::GetInstance()->WhenIdle( |
| 194 base::Bind(&DisableScenarioWhenIdle)); |
| 195 |
| 196 BackgroundTracingManager::GetInstance()->DidTriggerHappen( |
| 197 handle, |
| 198 base::Bind(&StartedFinalizingCallback, run_loop.QuitClosure(), false)); |
| 199 |
| 200 run_loop.Run(); |
| 201 |
| 202 EXPECT_TRUE(endpoint->get_receive_count() == 0); |
| 203 } |
| 204 } |
| 205 |
| 206 // This tests that no trace is triggered with an invalid handle. |
| 207 IN_PROC_BROWSER_TEST_F(BackgroundTracingManagerBrowserTest, |
| 208 DoesNotTriggerWithInvalidHandle) { |
| 209 { |
| 210 SetupBackgroundTracingManager(); |
| 211 |
| 212 base::RunLoop run_loop; |
| 213 scoped_refptr<BackgroundTracingManagerTestSink> endpoint = |
| 214 new BackgroundTracingManagerTestSink(base::Closure()); |
| 215 |
| 216 scoped_refptr<content::BackgroundTracingPreemptiveConfig> config = |
| 217 CreatePreemptiveConfig(); |
| 218 |
| 219 content::BackgroundTracingManager::TriggerHandle handle = |
| 220 content::BackgroundTracingManager::GetInstance()->RegisterTriggerType( |
| 221 "test"); |
| 222 |
| 223 content::BackgroundTracingManager::GetInstance()->InvalidateTriggerHandlesFo
rTesting(); |
| 224 |
| 225 BackgroundTracingManager::GetInstance()->SetActiveScenario( |
| 226 config, endpoint); |
| 227 |
| 228 BackgroundTracingManager::GetInstance()->WhenIdle( |
| 229 base::Bind(&DisableScenarioWhenIdle)); |
| 230 |
| 231 BackgroundTracingManager::GetInstance()->DidTriggerHappen( |
| 232 handle, |
| 233 base::Bind(&StartedFinalizingCallback, run_loop.QuitClosure(), false)); |
| 234 |
| 235 run_loop.Run(); |
| 236 |
| 237 EXPECT_TRUE(endpoint->get_receive_count() == 0); |
| 238 } |
| 239 } |
| 240 |
| 241 // This tests that reactive mode configs will fail. |
| 242 IN_PROC_BROWSER_TEST_F(BackgroundTracingManagerBrowserTest, |
| 243 DoesNotAllowReactiveConfig) { |
| 244 { |
| 245 SetupBackgroundTracingManager(); |
| 246 |
| 247 scoped_refptr<BackgroundTracingManagerTestSink> endpoint = |
| 248 new BackgroundTracingManagerTestSink(base::Closure()); |
| 249 |
| 250 scoped_refptr<content::BackgroundTracingReactiveConfig> config = |
| 251 new content::BackgroundTracingReactiveConfig(); |
| 252 |
| 253 bool result = BackgroundTracingManager::GetInstance()->SetActiveScenario( |
| 254 config, endpoint); |
| 255 |
| 256 EXPECT_FALSE(result); |
| 257 } |
| 258 } |
| 259 |
| 260 } // namespace content |
OLD | NEW |