blob: 7ef8bc5b6f7fb967d90f3d177902968d75829d16 [file] [log] [blame]
// Copyright 2019 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 "chrome/browser/tracing/background_tracing_metrics_provider.h"
#include "base/bind.h"
#include "content/public/browser/background_tracing_manager.h"
#include "content/public/test/test_browser_thread_bundle.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/metrics_proto/chrome_user_metrics_extension.pb.h"
#include "third_party/metrics_proto/trace_log.pb.h"
namespace tracing {
namespace {
const char kDummyTrace[] = "Trace bytes as serialized proto";
} // namespace
class BackgroundTracingMetricsProviderTest : public testing::Test {
public:
BackgroundTracingMetricsProviderTest() = default;
private:
content::TestBrowserThreadBundle test_browser_thread_bundle_;
};
TEST_F(BackgroundTracingMetricsProviderTest, NoTraceData) {
BackgroundTracingMetricsProvider provider;
ASSERT_FALSE(provider.HasIndependentMetrics());
}
TEST_F(BackgroundTracingMetricsProviderTest, UploadsTraceLog) {
BackgroundTracingMetricsProvider provider;
EXPECT_FALSE(provider.HasIndependentMetrics());
content::BackgroundTracingManager::GetInstance()->SetTraceToUploadForTesting(
kDummyTrace);
EXPECT_TRUE(provider.HasIndependentMetrics());
metrics::ChromeUserMetricsExtension uma_proto;
uma_proto.set_client_id(100);
uma_proto.set_session_id(15);
provider.ProvideIndependentMetrics(
base::BindOnce([](bool success) { EXPECT_TRUE(success); }), &uma_proto,
/* snapshot_manager=*/nullptr);
EXPECT_EQ(100u, uma_proto.client_id());
EXPECT_EQ(15, uma_proto.session_id());
ASSERT_EQ(1, uma_proto.trace_log_size());
EXPECT_EQ(kDummyTrace, uma_proto.trace_log(0).raw_data());
EXPECT_FALSE(provider.HasIndependentMetrics());
}
TEST_F(BackgroundTracingMetricsProviderTest, HandleMissingTrace) {
BackgroundTracingMetricsProvider provider;
EXPECT_FALSE(provider.HasIndependentMetrics());
content::BackgroundTracingManager::GetInstance()->SetTraceToUploadForTesting(
kDummyTrace);
EXPECT_TRUE(provider.HasIndependentMetrics());
content::BackgroundTracingManager::GetInstance()->SetTraceToUploadForTesting(
"");
metrics::ChromeUserMetricsExtension uma_proto;
uma_proto.set_client_id(100);
uma_proto.set_session_id(15);
provider.ProvideIndependentMetrics(
base::BindOnce([](bool success) { EXPECT_FALSE(success); }), &uma_proto,
/* snapshot_manager=*/nullptr);
EXPECT_EQ(100u, uma_proto.client_id());
EXPECT_EQ(15, uma_proto.session_id());
EXPECT_EQ(0, uma_proto.trace_log_size());
EXPECT_FALSE(provider.HasIndependentMetrics());
}
} // namespace tracing