| Index: content/browser/frame_host/frame_tree_node_blame_context_unittest.cc
|
| diff --git a/content/browser/frame_host/frame_tree_node_blame_context_unittest.cc b/content/browser/frame_host/frame_tree_node_blame_context_unittest.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..cea3cae97e727df7634c8ca5743340f6dc3234f6
|
| --- /dev/null
|
| +++ b/content/browser/frame_host/frame_tree_node_blame_context_unittest.cc
|
| @@ -0,0 +1,300 @@
|
| +// Copyright 2016 The Chromium Authors. All rights reserved.
|
| +// Use of this source code is governed by a BSD-style license that can be
|
| +// found in the LICENSE file.
|
| +
|
| +#include "content/browser/frame_host/frame_tree_node_blame_context.h"
|
| +
|
| +#include "base/memory/ptr_util.h"
|
| +#include "base/run_loop.h"
|
| +#include "base/test/trace_event_analyzer.h"
|
| +#include "base/trace_event/trace_buffer.h"
|
| +#include "base/trace_event/trace_event_argument.h"
|
| +#include "content/browser/frame_host/frame_tree.h"
|
| +#include "content/browser/frame_host/frame_tree_node.h"
|
| +#include "content/test/test_render_view_host.h"
|
| +#include "content/test/test_web_contents.h"
|
| +#include "testing/gtest/include/gtest/gtest.h"
|
| +#include "third_party/WebKit/public/web/WebSandboxFlags.h"
|
| +
|
| +namespace content {
|
| +
|
| +namespace {
|
| +
|
| +bool PointerCompare(const trace_analyzer::TraceEvent* lhs,
|
| + const trace_analyzer::TraceEvent* rhs) {
|
| + CHECK(lhs);
|
| + CHECK(rhs);
|
| + return *lhs < *rhs;
|
| +}
|
| +
|
| +void OnTraceDataCollected(base::Closure quit_closure,
|
| + base::trace_event::TraceResultBuffer* buffer,
|
| + const scoped_refptr<base::RefCountedString>& json,
|
| + bool has_more_events) {
|
| + buffer->AddFragment(json->data());
|
| + if (!has_more_events)
|
| + quit_closure.Run();
|
| +}
|
| +
|
| +void ExpectFrameTreeNodeObject(const trace_analyzer::TraceEvent* event) {
|
| + EXPECT_EQ("navigation", event->category);
|
| + EXPECT_EQ("Frame", event->name);
|
| +}
|
| +
|
| +void ExpectFrameTreeNodeSnapshot(const trace_analyzer::TraceEvent* event) {
|
| + ExpectFrameTreeNodeObject(event);
|
| + EXPECT_TRUE(event->HasArg("snapshot"));
|
| + EXPECT_TRUE(event->arg_values.at("snapshot")
|
| + ->IsType(base::Value::Type::TYPE_DICTIONARY));
|
| +}
|
| +
|
| +std::string GetParentNodeID(const trace_analyzer::TraceEvent* event) {
|
| + const base::Value* arg_snapshot = event->arg_values.at("snapshot").get();
|
| + const base::DictionaryValue* snapshot;
|
| + EXPECT_TRUE(arg_snapshot->GetAsDictionary(&snapshot));
|
| + if (!snapshot->HasKey("parent"))
|
| + return std::string();
|
| + const base::DictionaryValue* parent;
|
| + EXPECT_TRUE(snapshot->GetDictionary("parent", &parent));
|
| + std::string parent_id;
|
| + EXPECT_TRUE(parent->GetString("id_ref", &parent_id));
|
| + return parent_id;
|
| +}
|
| +
|
| +std::string GetSnapshotURL(const trace_analyzer::TraceEvent* event) {
|
| + const base::Value* arg_snapshot = event->arg_values.at("snapshot").get();
|
| + const base::DictionaryValue* snapshot;
|
| + EXPECT_TRUE(arg_snapshot->GetAsDictionary(&snapshot));
|
| + if (!snapshot->HasKey("url"))
|
| + return std::string();
|
| + std::string url;
|
| + EXPECT_TRUE(snapshot->GetString("url", &url));
|
| + return url;
|
| +}
|
| +
|
| +} // namespace
|
| +
|
| +class FrameTreeNodeBlameContextTest : public RenderViewHostImplTestHarness {
|
| + public:
|
| + FrameTree* tree() { return contents()->GetFrameTree(); }
|
| + FrameTreeNode* root() { return tree()->root(); }
|
| + int process_id() {
|
| + return root()->current_frame_host()->GetProcess()->GetID();
|
| + }
|
| +
|
| + // Create a frame tree specified by shape, which is a string of paired
|
| + // parentheses. Each pair of parentheses represents a FrameTreeNode, and the
|
| + // nesting of parentheses represents the parent-child relation between nodes.
|
| + // Nodes represented by outer-most parentheses are children of the root node.
|
| + // See the test cases for sample usage.
|
| + void CreateFrameTree(const char* shape) {
|
| + main_test_rfh()->InitializeRenderFrameIfNeeded();
|
| + CreateSubframes(root(), 1, shape);
|
| + }
|
| +
|
| + void RemoveAllNonRootFrames() {
|
| + RemoveAllSubFramesRecursively(root());
|
| + }
|
| +
|
| + void StartTracing() {
|
| + base::trace_event::TraceLog::GetInstance()->SetEnabled(
|
| + base::trace_event::TraceConfig("*"),
|
| + base::trace_event::TraceLog::RECORDING_MODE);
|
| + }
|
| +
|
| + void StopTracing() {
|
| + base::trace_event::TraceLog::GetInstance()->SetDisabled();
|
| + }
|
| +
|
| + std::unique_ptr<trace_analyzer::TraceAnalyzer> CreateTraceAnalyzer() {
|
| + base::trace_event::TraceResultBuffer buffer;
|
| + base::trace_event::TraceResultBuffer::SimpleOutput trace_output;
|
| + buffer.SetOutputCallback(trace_output.GetCallback());
|
| + base::RunLoop run_loop;
|
| + buffer.Start();
|
| + base::trace_event::TraceLog::GetInstance()->Flush(
|
| + base::Bind(&OnTraceDataCollected, run_loop.QuitClosure(),
|
| + base::Unretained(&buffer)));
|
| + run_loop.Run();
|
| + buffer.Finish();
|
| +
|
| + return base::WrapUnique(
|
| + trace_analyzer::TraceAnalyzer::Create(trace_output.json_output));
|
| + }
|
| +
|
| + private:
|
| + int CreateSubframes(FrameTreeNode* node, int self_id, const char* shape) {
|
| + int consumption = 0;
|
| + for (int child_num = 1; ; ++child_num) {
|
| + char ch = shape[consumption++];
|
| + if (!ch || ch == ')')
|
| + break;
|
| + int child_id = self_id * 10 + child_num;
|
| + tree()->AddFrame(node, process_id(), child_id,
|
| + blink::WebTreeScopeType::Document, std::string(),
|
| + base::StringPrintf("uniqueName%d", child_id),
|
| + blink::WebSandboxFlags::None,
|
| + blink::WebFrameOwnerProperties());
|
| + FrameTreeNode* child = node->child_at(child_num - 1);
|
| + consumption += CreateSubframes(child, child_id, shape + consumption);
|
| + }
|
| + return consumption;
|
| + }
|
| +
|
| + void RemoveAllSubFramesRecursively(FrameTreeNode* node) {
|
| + while (node->child_count()) {
|
| + FrameTreeNode* child = node->child_at(0);
|
| + RemoveAllSubFramesRecursively(child);
|
| + tree()->RemoveFrame(child);
|
| + }
|
| + }
|
| +};
|
| +
|
| +// Create a frame tree with tracing off, and then enable tracing.
|
| +// Test if the frame tree is dumped with correct topology.
|
| +TEST_F(FrameTreeNodeBlameContextTest, OnTracingEnabled) {
|
| + /* Shape of the frame tree to be created:
|
| + * ()
|
| + * / \
|
| + * () ()
|
| + * / \ |
|
| + * () () ()
|
| + * |
|
| + * ()
|
| + */
|
| + const char* tree_shape = "(()())((()))";
|
| +
|
| + CreateFrameTree(tree_shape);
|
| + StartTracing();
|
| + StopTracing();
|
| +
|
| + std::unique_ptr<trace_analyzer::TraceAnalyzer> analyzer =
|
| + CreateTraceAnalyzer();
|
| + trace_analyzer::TraceEventVector events;
|
| + trace_analyzer::Query q = trace_analyzer::Query::EventPhaseIs(
|
| + TRACE_EVENT_PHASE_SNAPSHOT_OBJECT);
|
| + analyzer->FindEvents(q, &events);
|
| +
|
| + EXPECT_EQ(7u, events.size());
|
| + for (int i = 0; i < 7; ++i) {
|
| + ExpectFrameTreeNodeSnapshot(events[i]);
|
| + FrameTreeNode* node = tree()->FindByID(strtol(events[i]->id.c_str(),
|
| + nullptr, 16));
|
| + EXPECT_NE(nullptr, node);
|
| + std::string parent_id = GetParentNodeID(events[i]);
|
| + if (parent_id.empty())
|
| + EXPECT_EQ(nullptr, node->parent());
|
| + else {
|
| + FrameTreeNode* parent = tree()->FindByID(strtol(parent_id.c_str(),
|
| + nullptr, 16));
|
| + EXPECT_EQ(node->parent(), parent);
|
| + }
|
| + }
|
| +}
|
| +
|
| +// Create a frame tree with tracing on.
|
| +// Test if the creation of each frame is correctly traced.
|
| +TEST_F(FrameTreeNodeBlameContextTest, OnNewFrameCreation) {
|
| + /* Shape of the frame tree to be created:
|
| + * ()
|
| + * / \
|
| + * () ()
|
| + * / \ |
|
| + * () () ()
|
| + * |
|
| + * ()
|
| + */
|
| + const char* tree_shape = "(()())((()))";
|
| +
|
| + StartTracing();
|
| + CreateFrameTree(tree_shape);
|
| + StopTracing();
|
| +
|
| + std::unique_ptr<trace_analyzer::TraceAnalyzer> analyzer =
|
| + CreateTraceAnalyzer();
|
| + trace_analyzer::TraceEventVector events;
|
| + trace_analyzer::Query q = trace_analyzer::Query::EventPhaseIs(
|
| + TRACE_EVENT_PHASE_CREATE_OBJECT);
|
| + analyzer->FindEvents(q, &events);
|
| +
|
| + // The creation of all non-root nodes should be traced.
|
| + EXPECT_EQ(6u, events.size());
|
| + for (int i = 0; i < 6; i++) {
|
| + ExpectFrameTreeNodeObject(events[i]);
|
| + FrameTreeNode* node = tree()->FindByID(strtol(events[i]->id.c_str(),
|
| + nullptr, 16));
|
| + EXPECT_NE(nullptr, node);
|
| + }
|
| +}
|
| +
|
| +// Delete frames from a frame tree with tracing on.
|
| +// Test if the destruction of each blame context is correctly traced.
|
| +TEST_F(FrameTreeNodeBlameContextTest, OnFrameDeletion) {
|
| + /* Shape of the frame tree to be created:
|
| + * ()
|
| + * / \
|
| + * () ()
|
| + * / \ |
|
| + * () () ()
|
| + * |
|
| + * ()
|
| + */
|
| + const char* tree_shape = "(()())((()))";
|
| +
|
| + CreateFrameTree(tree_shape);
|
| + std::set<int> node_ids;
|
| + {
|
| + FrameTree::NodeRange nodes = tree()->Nodes();
|
| + for (auto it = nodes.begin(); it != nodes.end(); ++it)
|
| + node_ids.insert((*it)->frame_tree_node_id());
|
| + }
|
| +
|
| + StartTracing();
|
| + RemoveAllNonRootFrames();
|
| + StopTracing();
|
| +
|
| + std::unique_ptr<trace_analyzer::TraceAnalyzer> analyzer =
|
| + CreateTraceAnalyzer();
|
| + trace_analyzer::TraceEventVector events;
|
| + trace_analyzer::Query q = trace_analyzer::Query::EventPhaseIs(
|
| + TRACE_EVENT_PHASE_DELETE_OBJECT);
|
| + analyzer->FindEvents(q, &events);
|
| +
|
| + // The removal of all non-root nodes should be traced.
|
| + EXPECT_EQ(6u, events.size());
|
| + for (int i = 0; i < 6; i++) {
|
| + ExpectFrameTreeNodeObject(events[i]);
|
| + int id = strtol(events[i]->id.c_str(), nullptr, 16);
|
| + EXPECT_NE(node_ids.end(), node_ids.find(id));
|
| + }
|
| +}
|
| +
|
| +// Change URL of the root node. Test if URL change is correctly traced.
|
| +TEST_F(FrameTreeNodeBlameContextTest, OnURLChange) {
|
| + main_test_rfh()->InitializeRenderFrameIfNeeded();
|
| +
|
| + GURL url1("http://a.com/");
|
| + GURL url2("https://b.net/");
|
| + StartTracing();
|
| + root()->SetCurrentURL(url1);
|
| + root()->SetCurrentURL(url2);
|
| + root()->ResetForNewProcess();
|
| + StopTracing();
|
| +
|
| + std::unique_ptr<trace_analyzer::TraceAnalyzer> analyzer =
|
| + CreateTraceAnalyzer();
|
| + trace_analyzer::TraceEventVector events;
|
| + trace_analyzer::Query q = trace_analyzer::Query::EventPhaseIs(
|
| + TRACE_EVENT_PHASE_SNAPSHOT_OBJECT);
|
| + analyzer->FindEvents(q, &events);
|
| + std::sort(events.begin(), events.end(), PointerCompare);
|
| +
|
| + // Four snapshots are traced: one at startup and one for each URL change.
|
| + EXPECT_EQ(4u, events.size());
|
| + EXPECT_EQ("", GetSnapshotURL(events[0]));
|
| + EXPECT_EQ(url1.spec(), GetSnapshotURL(events[1]));
|
| + EXPECT_EQ(url2.spec(), GetSnapshotURL(events[2]));
|
| + EXPECT_EQ("", GetSnapshotURL(events[3]));
|
| +}
|
| +
|
| +} // namespace content
|
|
|