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/debug/trace_event.h"
8 #include "base/message_loop/message_loop.h"
9 #include "base/run_loop.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 RenderViewImpl::OnScriptEvalRequest (from the renderer process).
32 void ExecuteJavascriptOnCurrentTab() {
33 content::RenderViewHost
* rvh
= browser()->tab_strip_model()->
34 GetActiveWebContents()->GetRenderViewHost();
36 ASSERT_TRUE(content::ExecuteScript(rvh
, ";"));
40 void AddEvents(int num
) {
41 for (int i
= 0; i
< num
; ++i
)
42 TRACE_EVENT_INSTANT0(g_category
, g_event
, TRACE_EVENT_SCOPE_THREAD
);
45 IN_PROC_BROWSER_TEST_F(TracingBrowserTest
, BeginTracingWithWatch
) {
46 base::TimeDelta no_timeout
;
47 base::TimeDelta short_timeout
= base::TimeDelta::FromMilliseconds(5);
48 std::string json_events
;
50 // One event before wait.
51 ASSERT_TRUE(BeginTracingWithWatch(g_category
, g_category
, g_event
, 1));
53 EXPECT_TRUE(WaitForWatchEvent(no_timeout
));
54 ASSERT_TRUE(EndTracing(&json_events
));
56 // One event after wait.
57 ASSERT_TRUE(BeginTracingWithWatch(g_category
, g_category
, g_event
, 1));
58 base::MessageLoop::current()->PostTask(FROM_HERE
, base::Bind(&AddEvents
, 1));
59 EXPECT_TRUE(WaitForWatchEvent(no_timeout
));
60 ASSERT_TRUE(EndTracing(&json_events
));
62 // Not enough events timeout.
63 ASSERT_TRUE(BeginTracingWithWatch(g_category
, g_category
, g_event
, 2));
65 EXPECT_FALSE(WaitForWatchEvent(short_timeout
));
66 ASSERT_TRUE(EndTracing(&json_events
));
68 // Multi event before wait.
69 ASSERT_TRUE(BeginTracingWithWatch(g_category
, g_category
, g_event
, 5));
71 EXPECT_TRUE(WaitForWatchEvent(no_timeout
));
72 ASSERT_TRUE(EndTracing(&json_events
));
74 // Multi event after wait.
75 ASSERT_TRUE(BeginTracingWithWatch(g_category
, g_category
, g_event
, 5));
76 base::MessageLoop::current()->PostTask(FROM_HERE
, base::Bind(&AddEvents
, 5));
77 EXPECT_TRUE(WaitForWatchEvent(no_timeout
));
78 ASSERT_TRUE(EndTracing(&json_events
));
80 // Child process events from same process.
81 ASSERT_TRUE(BeginTracingWithWatch(g_category
, g_category
,
82 "OnScriptEvalRequest", 2));
83 ASSERT_NO_FATAL_FAILURE(ExecuteJavascriptOnCurrentTab());
84 ASSERT_NO_FATAL_FAILURE(ExecuteJavascriptOnCurrentTab());
85 EXPECT_TRUE(WaitForWatchEvent(no_timeout
));
86 ASSERT_TRUE(EndTracing(&json_events
));
88 // Child process events from different processes.
89 GURL
url1("chrome://tracing/");
90 GURL
url2("chrome://credits/");
91 ASSERT_TRUE(BeginTracingWithWatch(g_category
, g_category
,
92 "OnScriptEvalRequest", 2));
93 // Open two tabs to different URLs to encourage two separate renderer
94 // processes. Each will fire an event that will be counted towards the total.
95 ui_test_utils::NavigateToURLWithDisposition(browser(), url1
,
96 NEW_FOREGROUND_TAB
, ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION
);
97 ASSERT_NO_FATAL_FAILURE(ExecuteJavascriptOnCurrentTab());
98 ui_test_utils::NavigateToURLWithDisposition(browser(), url2
,
99 NEW_FOREGROUND_TAB
, ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION
);
100 ASSERT_NO_FATAL_FAILURE(ExecuteJavascriptOnCurrentTab());
101 EXPECT_TRUE(WaitForWatchEvent(no_timeout
));
102 ASSERT_TRUE(EndTracing(&json_events
));