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