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 <memory>
6 
7 #include "base/base64.h"
8 #include "base/json/json_reader.h"
9 #include "base/json/json_writer.h"
10 #include "base/trace_event/memory_dump_manager.h"
11 #include "base/trace_event/trace_config.h"
12 #include "base/values.h"
13 #include "content/browser/tracing/tracing_ui.h"
14 #include "testing/gtest/include/gtest/gtest.h"
15 
16 namespace content {
17 
18 class TracingUITest : public testing::Test {
19  public:
TracingUITest()20   TracingUITest() {}
21 };
22 
GetConfig()23 std::string GetConfig() {
24   std::unique_ptr<base::DictionaryValue> dict(new base::DictionaryValue());
25   std::unique_ptr<base::Value> filter1(
26       new base::Value(base::trace_event::MemoryDumpManager::kTraceCategory));
27   std::unique_ptr<base::Value> filter2(new base::Value("filter2"));
28   std::unique_ptr<base::ListValue> included(new base::ListValue);
29   included->Append(std::move(filter1));
30   std::unique_ptr<base::ListValue> excluded(new base::ListValue);
31   excluded->Append(std::move(filter2));
32 
33   dict->SetList("included_categories", std::move(included));
34   dict->SetList("excluded_categories", std::move(excluded));
35   dict->SetString("record_mode", "record-continuously");
36   dict->SetBoolean("enable_systrace", true);
37   dict->SetString("stream_format", "protobuf");
38 
39   std::unique_ptr<base::DictionaryValue> memory_config(
40       new base::DictionaryValue());
41   std::unique_ptr<base::DictionaryValue> trigger(new base::DictionaryValue());
42   trigger->SetString("mode", "detailed");
43   trigger->SetInteger("periodic_interval_ms", 10000);
44   std::unique_ptr<base::ListValue> triggers(new base::ListValue);
45   triggers->Append(std::move(trigger));
46   memory_config->SetList("triggers", std::move(triggers));
47   dict->SetDictionary("memory_dump_config", std::move(memory_config));
48 
49   std::string results;
50   if (!base::JSONWriter::Write(*dict.get(), &results))
51     return "";
52 
53   std::string data;
54   base::Base64Encode(results, &data);
55   return data;
56 }
57 
TEST_F(TracingUITest,ConfigParsing)58 TEST_F(TracingUITest, ConfigParsing) {
59   base::trace_event::TraceConfig config;
60   std::string stream_format;
61   ASSERT_TRUE(TracingUI::GetTracingOptions(GetConfig(), config, stream_format));
62   EXPECT_EQ(config.GetTraceRecordMode(),
63             base::trace_event::RECORD_CONTINUOUSLY);
64   std::string expected(base::trace_event::MemoryDumpManager::kTraceCategory);
65   expected += ",-filter2";
66   EXPECT_EQ(config.ToCategoryFilterString(), expected);
67   EXPECT_EQ(stream_format, "protobuf");
68   EXPECT_TRUE(config.IsSystraceEnabled());
69 
70   ASSERT_EQ(config.memory_dump_config().triggers.size(), 1u);
71   EXPECT_EQ(config.memory_dump_config().triggers[0].min_time_between_dumps_ms,
72             10000u);
73   EXPECT_EQ(config.memory_dump_config().triggers[0].level_of_detail,
74             base::trace_event::MemoryDumpLevelOfDetail::DETAILED);
75 }
76 
77 }  // namespace content
78