1 // Copyright (c) 2012 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.
5 #include "chrome/test/base/tracing.h"
7 #include "base/message_loop/message_loop.h"
8 #include "base/run_loop.h"
9 #include "base/trace_event/trace_event.h"
10 #include "chrome/browser/ui/browser.h"
11 #include "chrome/browser/ui/tabs/tab_strip_model.h"
12 #include "chrome/test/base/in_process_browser_test.h"
13 #include "chrome/test/base/ui_test_utils.h"
14 #include "content/public/browser/render_view_host.h"
15 #include "content/public/browser/web_contents.h"
16 #include "content/public/test/browser_test_utils.h"
17 #include "testing/gtest/include/gtest/gtest.h"
21 using tracing::BeginTracingWithWatch
;
22 using tracing::WaitForWatchEvent
;
23 using tracing::EndTracing
;
25 const char g_category
[] = "test_tracing";
26 const char g_event
[] = "TheEvent";
28 class TracingBrowserTest
: public InProcessBrowserTest
{
30 // Execute some no-op javascript on the current tab - this triggers a trace
31 // event in RenderFrameImpl::OnJavaScriptExecuteRequestForTests (from the
33 void ExecuteJavascriptOnCurrentTab() {
34 content::RenderViewHost
* rvh
= browser()->tab_strip_model()->
35 GetActiveWebContents()->GetRenderViewHost();
37 ASSERT_TRUE(content::ExecuteScript(rvh
, ";"));
41 void AddEvents(int num
) {
42 for (int i
= 0; i
< num
; ++i
)
43 TRACE_EVENT_INSTANT0(g_category
, g_event
, TRACE_EVENT_SCOPE_THREAD
);
46 IN_PROC_BROWSER_TEST_F(TracingBrowserTest
, BeginTracingWithWatch
) {
47 base::TimeDelta no_timeout
;
48 base::TimeDelta short_timeout
= base::TimeDelta::FromMilliseconds(5);
49 std::string json_events
;
51 // One event before wait.
52 ASSERT_TRUE(BeginTracingWithWatch(g_category
, g_category
, g_event
, 1));
54 EXPECT_TRUE(WaitForWatchEvent(no_timeout
));
55 ASSERT_TRUE(EndTracing(&json_events
));
57 // One event after wait.
58 ASSERT_TRUE(BeginTracingWithWatch(g_category
, g_category
, g_event
, 1));
59 base::MessageLoop::current()->PostTask(FROM_HERE
, base::Bind(&AddEvents
, 1));
60 EXPECT_TRUE(WaitForWatchEvent(no_timeout
));
61 ASSERT_TRUE(EndTracing(&json_events
));
63 // Not enough events timeout.
64 ASSERT_TRUE(BeginTracingWithWatch(g_category
, g_category
, g_event
, 2));
66 EXPECT_FALSE(WaitForWatchEvent(short_timeout
));
67 ASSERT_TRUE(EndTracing(&json_events
));
69 // Multi event before wait.
70 ASSERT_TRUE(BeginTracingWithWatch(g_category
, g_category
, g_event
, 5));
72 EXPECT_TRUE(WaitForWatchEvent(no_timeout
));
73 ASSERT_TRUE(EndTracing(&json_events
));
75 // Multi event after wait.
76 ASSERT_TRUE(BeginTracingWithWatch(g_category
, g_category
, g_event
, 5));
77 base::MessageLoop::current()->PostTask(FROM_HERE
, base::Bind(&AddEvents
, 5));
78 EXPECT_TRUE(WaitForWatchEvent(no_timeout
));
79 ASSERT_TRUE(EndTracing(&json_events
));
81 // Child process events from same process.
82 ASSERT_TRUE(BeginTracingWithWatch(g_category
, g_category
,
83 "OnJavaScriptExecuteRequestForTests", 2));
84 ASSERT_NO_FATAL_FAILURE(ExecuteJavascriptOnCurrentTab());
85 ASSERT_NO_FATAL_FAILURE(ExecuteJavascriptOnCurrentTab());
86 EXPECT_TRUE(WaitForWatchEvent(no_timeout
));
87 ASSERT_TRUE(EndTracing(&json_events
));
89 // Child process events from different processes.
90 GURL
url1("chrome://tracing/");
91 GURL
url2("chrome://credits/");
92 ASSERT_TRUE(BeginTracingWithWatch(g_category
, g_category
,
93 "OnJavaScriptExecuteRequestForTests", 2));
94 // Open two tabs to different URLs to encourage two separate renderer
95 // processes. Each will fire an event that will be counted towards the total.
96 ui_test_utils::NavigateToURLWithDisposition(browser(), url1
,
97 NEW_FOREGROUND_TAB
, ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION
);
98 ASSERT_NO_FATAL_FAILURE(ExecuteJavascriptOnCurrentTab());
99 ui_test_utils::NavigateToURLWithDisposition(browser(), url2
,
100 NEW_FOREGROUND_TAB
, ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION
);
101 ASSERT_NO_FATAL_FAILURE(ExecuteJavascriptOnCurrentTab());
102 EXPECT_TRUE(WaitForWatchEvent(no_timeout
));
103 ASSERT_TRUE(EndTracing(&json_events
));