OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 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 <vector> |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/json/json_reader.h" |
| 9 #include "base/message_loop/message_loop.h" |
| 10 #include "base/run_loop.h" |
| 11 #include "content/browser/renderer_host/input/synthetic_gesture.h" |
| 12 #include "content/browser/renderer_host/input/synthetic_gesture_controller.h" |
| 13 #include "content/browser/renderer_host/input/synthetic_gesture_target.h" |
| 14 #include "content/browser/renderer_host/input/synthetic_tap_gesture.h" |
| 15 #include "content/browser/renderer_host/render_widget_host_impl.h" |
| 16 #include "content/browser/web_contents/web_contents_impl.h" |
| 17 #include "content/common/input/synthetic_gesture_params.h" |
| 18 #include "content/common/input/synthetic_smooth_scroll_gesture_params.h" |
| 19 #include "content/public/browser/render_view_host.h" |
| 20 #include "content/public/browser/render_widget_host_view.h" |
| 21 #include "content/public/browser/tracing_controller.h" |
| 22 #include "content/public/test/content_browser_test.h" |
| 23 #include "content/public/test/content_browser_test_utils.h" |
| 24 #include "content/shell/browser/shell.h" |
| 25 #include "testing/gmock/include/gmock/gmock.h" |
| 26 |
| 27 namespace { |
| 28 |
| 29 const char kMouseUpDownDataURL[] = |
| 30 "data:text/html;charset=utf-8," |
| 31 "<!DOCTYPE html>" |
| 32 "<html>" |
| 33 "<head>" |
| 34 "<title>Mouse event trace events reported.</title>" |
| 35 "<script src=\"../../resources/testharness.js\"></script>" |
| 36 "<script src=\"../../resources/testharnessreport.js\"></script>" |
| 37 "<style>" |
| 38 "body {" |
| 39 " height:3000px;" |
| 40 "}" |
| 41 "</style>" |
| 42 "</head>" |
| 43 "<body>" |
| 44 "</body>" |
| 45 "</html>"; |
| 46 |
| 47 } // namespace |
| 48 |
| 49 namespace content { |
| 50 |
| 51 class MouseLatencyBrowserTest : public ContentBrowserTest { |
| 52 public: |
| 53 MouseLatencyBrowserTest() : loop_(base::MessageLoop::TYPE_UI) {} |
| 54 ~MouseLatencyBrowserTest() override {} |
| 55 |
| 56 RenderWidgetHostImpl* GetWidgetHost() { |
| 57 return RenderWidgetHostImpl::From( |
| 58 shell()->web_contents()->GetRenderViewHost()->GetWidget()); |
| 59 } |
| 60 |
| 61 void OnSyntheticGestureCompleted(SyntheticGesture::Result result) { |
| 62 EXPECT_EQ(SyntheticGesture::GESTURE_FINISHED, result); |
| 63 runner_->Quit(); |
| 64 } |
| 65 |
| 66 void OnTraceDataCollected( |
| 67 std::unique_ptr<const base::DictionaryValue> metadata, |
| 68 base::RefCountedString* trace_data_string) { |
| 69 std::unique_ptr<base::Value> trace_data = |
| 70 base::JSONReader::Read(trace_data_string->data()); |
| 71 ASSERT_TRUE(trace_data); |
| 72 trace_data_ = *trace_data; |
| 73 runner_->Quit(); |
| 74 } |
| 75 |
| 76 protected: |
| 77 void LoadURL() { |
| 78 const GURL data_url(kMouseUpDownDataURL); |
| 79 NavigateToURL(shell(), data_url); |
| 80 |
| 81 RenderWidgetHostImpl* host = GetWidgetHost(); |
| 82 host->GetView()->SetSize(gfx::Size(400, 400)); |
| 83 } |
| 84 |
| 85 // Generate mouse events for a synthetic click at |point|. |
| 86 void DoSyncClick(const gfx::PointF& position) { |
| 87 SyntheticTapGestureParams params; |
| 88 params.gesture_source_type = SyntheticGestureParams::MOUSE_INPUT; |
| 89 params.position = position; |
| 90 params.duration_ms = 100; |
| 91 std::unique_ptr<SyntheticTapGesture> gesture( |
| 92 new SyntheticTapGesture(params)); |
| 93 |
| 94 GetWidgetHost()->QueueSyntheticGesture( |
| 95 std::move(gesture), |
| 96 base::Bind(&MouseLatencyBrowserTest::OnSyntheticGestureCompleted, |
| 97 base::Unretained(this))); |
| 98 |
| 99 // Runs until we get the OnSyntheticGestureCompleted callback |
| 100 runner_ = base::MakeUnique<base::RunLoop>(); |
| 101 runner_->Run(); |
| 102 } |
| 103 |
| 104 void StartTracing() { |
| 105 base::trace_event::TraceConfig trace_config( |
| 106 "{" |
| 107 "\"enable_argument_filter\":false," |
| 108 "\"enable_systrace\":false," |
| 109 "\"included_categories\":[" |
| 110 "\"latencyInfo\"" |
| 111 "]," |
| 112 "\"record_mode\":\"record-until-full\"" |
| 113 "}"); |
| 114 |
| 115 base::RunLoop run_loop; |
| 116 ASSERT_TRUE(TracingController::GetInstance()->StartTracing( |
| 117 trace_config, run_loop.QuitClosure())); |
| 118 } |
| 119 |
| 120 const base::Value& StopTracing() { |
| 121 bool success = TracingController::GetInstance()->StopTracing( |
| 122 TracingController::CreateStringSink( |
| 123 base::Bind(&MouseLatencyBrowserTest::OnTraceDataCollected, |
| 124 base::Unretained(this)))); |
| 125 EXPECT_TRUE(success); |
| 126 |
| 127 // Runs until we get the OnTraceDataCollected callback, which populates |
| 128 // trace_data_; |
| 129 runner_ = base::MakeUnique<base::RunLoop>(); |
| 130 runner_->Run(); |
| 131 return trace_data_; |
| 132 } |
| 133 |
| 134 private: |
| 135 base::MessageLoop loop_; |
| 136 std::unique_ptr<base::RunLoop> runner_; |
| 137 base::Value trace_data_; |
| 138 |
| 139 DISALLOW_COPY_AND_ASSIGN(MouseLatencyBrowserTest); |
| 140 }; |
| 141 |
| 142 // Ensures that LatencyInfo async slices are reported correctly for MouseUp and |
| 143 // MouseDown events in the case where no swap is generated. |
| 144 // Disabled on Android because we don't support synthetic mouse input on |
| 145 // Android (crbug.com/723618). |
| 146 #if defined(OS_ANDROID) |
| 147 #define MAYBE_MouseDownAndUpRecordedWithoutSwap \ |
| 148 DISABLED_MouseDownAndUpRecordedWithoutSwap |
| 149 #else |
| 150 #define MAYBE_MouseDownAndUpRecordedWithoutSwap \ |
| 151 MouseDownAndUpRecordedWithoutSwap |
| 152 #endif |
| 153 IN_PROC_BROWSER_TEST_F(MouseLatencyBrowserTest, |
| 154 MAYBE_MouseDownAndUpRecordedWithoutSwap) { |
| 155 LoadURL(); |
| 156 |
| 157 StartTracing(); |
| 158 DoSyncClick(gfx::PointF(100, 100)); |
| 159 const base::Value trace_data = StopTracing(); |
| 160 |
| 161 const base::DictionaryValue* trace_data_dict; |
| 162 trace_data.GetAsDictionary(&trace_data_dict); |
| 163 ASSERT_TRUE(trace_data.GetAsDictionary(&trace_data_dict)); |
| 164 |
| 165 const base::ListValue* traceEvents; |
| 166 ASSERT_TRUE(trace_data_dict->GetList("traceEvents", &traceEvents)); |
| 167 |
| 168 std::vector<std::string> trace_event_names; |
| 169 |
| 170 for (size_t i = 0; i < traceEvents->GetSize(); ++i) { |
| 171 const base::DictionaryValue* traceEvent; |
| 172 ASSERT_TRUE(traceEvents->GetDictionary(i, &traceEvent)); |
| 173 |
| 174 std::string name; |
| 175 ASSERT_TRUE(traceEvent->GetString("name", &name)); |
| 176 |
| 177 if (name != "InputLatency::MouseUp" && name != "InputLatency::MouseDown") |
| 178 continue; |
| 179 trace_event_names.push_back(name); |
| 180 } |
| 181 |
| 182 // We see two events per async slice, a begin and an end. |
| 183 EXPECT_THAT( |
| 184 trace_event_names, |
| 185 testing::ElementsAre("InputLatency::MouseDown", "InputLatency::MouseDown", |
| 186 "InputLatency::MouseUp", "InputLatency::MouseUp")); |
| 187 } |
| 188 |
| 189 } // namespace content |
OLD | NEW |