| Index: chrome/browser/tracing/chrome_tracing_delegate_browsertest.cc
|
| diff --git a/chrome/browser/tracing/chrome_tracing_delegate_browsertest.cc b/chrome/browser/tracing/chrome_tracing_delegate_browsertest.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..8437c39821c3c3acfdc87bd2609df8b0efa36a13
|
| --- /dev/null
|
| +++ b/chrome/browser/tracing/chrome_tracing_delegate_browsertest.cc
|
| @@ -0,0 +1,182 @@
|
| +// Copyright 2015 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 "base/bind.h"
|
| +#include "base/prefs/pref_service.h"
|
| +#include "chrome/app/chrome_command_ids.h"
|
| +#include "chrome/browser/browser_process.h"
|
| +#include "chrome/browser/ui/browser_commands.h"
|
| +#include "chrome/browser/ui/browser_list.h"
|
| +#include "chrome/common/pref_names.h"
|
| +#include "chrome/test/base/in_process_browser_test.h"
|
| +#include "content/public/browser/background_tracing_manager.h"
|
| +#include "content/public/browser/background_tracing_preemptive_config.h"
|
| +#include "content/public/browser/background_tracing_reactive_config.h"
|
| +#include "content/public/browser/browser_thread.h"
|
| +#include "content/public/test/test_utils.h"
|
| +
|
| +namespace {
|
| +
|
| +class ChromeTracingDelegateBrowserTest : public InProcessBrowserTest {
|
| + public:
|
| + ChromeTracingDelegateBrowserTest()
|
| + : receive_count_(0),
|
| + started_finalizations_count_(0),
|
| + last_on_started_finalizing_success_(false) {}
|
| +
|
| + bool StartPreemptiveScenario(const base::Closure& on_upload_callback,
|
| + bool require_anonymized_data) {
|
| + on_upload_callback_ = on_upload_callback;
|
| +
|
| + scoped_ptr<content::BackgroundTracingPreemptiveConfig> config(
|
| + new content::BackgroundTracingPreemptiveConfig());
|
| +
|
| + content::BackgroundTracingPreemptiveConfig::MonitoringRule rule;
|
| + rule.type = content::BackgroundTracingPreemptiveConfig::
|
| + MONITOR_AND_DUMP_WHEN_TRIGGER_NAMED;
|
| + rule.named_trigger_info.trigger_name = "test";
|
| +
|
| + config->configs.push_back(rule);
|
| +
|
| + content::BackgroundTracingManager::ReceiveCallback receive_callback =
|
| + base::Bind(&ChromeTracingDelegateBrowserTest::OnUpload,
|
| + base::Unretained(this));
|
| +
|
| + return content::BackgroundTracingManager::GetInstance()->SetActiveScenario(
|
| + config.Pass(), receive_callback, require_anonymized_data);
|
| + }
|
| +
|
| + void TriggerReactiveScenario(
|
| + const base::Closure& on_started_finalization_callback) {
|
| + on_started_finalization_callback_ = on_started_finalization_callback;
|
| + trigger_handle_ =
|
| + content::BackgroundTracingManager::GetInstance()->RegisterTriggerType(
|
| + "test");
|
| +
|
| + content::BackgroundTracingManager::StartedFinalizingCallback
|
| + started_finalizing_callback =
|
| + base::Bind(&ChromeTracingDelegateBrowserTest::OnStartedFinalizing,
|
| + base::Unretained(this));
|
| + content::BackgroundTracingManager::GetInstance()->TriggerNamedEvent(
|
| + trigger_handle_, started_finalizing_callback);
|
| + }
|
| +
|
| + int get_receive_count() const { return receive_count_; }
|
| + bool get_started_finalizations() const {
|
| + return started_finalizations_count_;
|
| + }
|
| + bool get_last_started_finalization_success() const {
|
| + return last_on_started_finalizing_success_;
|
| + }
|
| +
|
| + private:
|
| + void OnUpload(const base::RefCountedString* file_contents,
|
| + base::Callback<void()> done_callback) {
|
| + receive_count_ += 1;
|
| +
|
| + content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE,
|
| + base::Bind(done_callback));
|
| + content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE,
|
| + base::Bind(on_upload_callback_));
|
| + }
|
| +
|
| + void OnStartedFinalizing(bool success) {
|
| + started_finalizations_count_++;
|
| + last_on_started_finalizing_success_ = success;
|
| +
|
| + if (!on_started_finalization_callback_.is_null()) {
|
| + content::BrowserThread::PostTask(
|
| + content::BrowserThread::UI, FROM_HERE,
|
| + base::Bind(on_started_finalization_callback_));
|
| + }
|
| + }
|
| +
|
| + base::Closure on_upload_callback_;
|
| + base::Closure on_started_finalization_callback_;
|
| + int receive_count_;
|
| + int started_finalizations_count_;
|
| + content::BackgroundTracingManager::TriggerHandle trigger_handle_;
|
| + bool last_on_started_finalizing_success_;
|
| +};
|
| +
|
| +IN_PROC_BROWSER_TEST_F(ChromeTracingDelegateBrowserTest,
|
| + BackgroundTracingTimeThrottled) {
|
| + base::RunLoop wait_for_upload;
|
| +
|
| + EXPECT_TRUE(StartPreemptiveScenario(wait_for_upload.QuitClosure(), false));
|
| +
|
| + TriggerReactiveScenario(base::Closure());
|
| +
|
| + wait_for_upload.Run();
|
| +
|
| + EXPECT_TRUE(get_receive_count() == 1);
|
| +
|
| + PrefService* local_state = g_browser_process->local_state();
|
| + DCHECK(local_state);
|
| + const base::Time last_upload_time = base::Time::FromInternalValue(
|
| + local_state->GetInt64(prefs::kBackgroundTracingLastUpload));
|
| + EXPECT_FALSE(last_upload_time.is_null());
|
| +
|
| + // We should not be able to start a new reactive scenario immediately after
|
| + // a previous one gets uploaded.
|
| + EXPECT_FALSE(StartPreemptiveScenario(base::Closure(), false));
|
| +}
|
| +
|
| +IN_PROC_BROWSER_TEST_F(ChromeTracingDelegateBrowserTest,
|
| + BackgroundTracingThrottleTimeElapsed) {
|
| + base::RunLoop wait_for_upload;
|
| +
|
| + EXPECT_TRUE(StartPreemptiveScenario(wait_for_upload.QuitClosure(), false));
|
| +
|
| + TriggerReactiveScenario(base::Closure());
|
| +
|
| + wait_for_upload.Run();
|
| +
|
| + EXPECT_TRUE(get_receive_count() == 1);
|
| +
|
| + PrefService* local_state = g_browser_process->local_state();
|
| + DCHECK(local_state);
|
| + const base::Time last_upload_time = base::Time::FromInternalValue(
|
| + local_state->GetInt64(prefs::kBackgroundTracingLastUpload));
|
| + EXPECT_FALSE(last_upload_time.is_null());
|
| +
|
| + // We move the last upload time to eight days in the past,
|
| + // and at that point should be able to start a scenario again.
|
| + base::Time new_upload_time = last_upload_time - base::TimeDelta::FromDays(8);
|
| + local_state->SetInt64(prefs::kBackgroundTracingLastUpload,
|
| + new_upload_time.ToInternalValue());
|
| + EXPECT_TRUE(StartPreemptiveScenario(base::Closure(), false));
|
| +}
|
| +
|
| +// If we need a PII-stripped trace, any existing OTR session should block the
|
| +// trace.
|
| +IN_PROC_BROWSER_TEST_F(ChromeTracingDelegateBrowserTest,
|
| + ExistingIncognitoSessionBlockingTraceStart) {
|
| + EXPECT_TRUE(chrome::ExecuteCommand(browser(), IDC_NEW_INCOGNITO_WINDOW));
|
| + EXPECT_TRUE(BrowserList::IsOffTheRecordSessionActive());
|
| + const bool require_anonymized_data = true;
|
| + EXPECT_FALSE(
|
| + StartPreemptiveScenario(base::Closure(), require_anonymized_data));
|
| +}
|
| +
|
| +// If we need a PII-stripped trace, any new OTR session during tracing should
|
| +// block the finalization of the trace.
|
| +IN_PROC_BROWSER_TEST_F(ChromeTracingDelegateBrowserTest,
|
| + NewIncognitoSessionBlockingTraceFinalization) {
|
| + const bool require_anonymized_data = true;
|
| + EXPECT_TRUE(
|
| + StartPreemptiveScenario(base::Closure(), require_anonymized_data));
|
| +
|
| + EXPECT_TRUE(chrome::ExecuteCommand(browser(), IDC_NEW_INCOGNITO_WINDOW));
|
| + EXPECT_TRUE(BrowserList::IsOffTheRecordSessionActive());
|
| +
|
| + base::RunLoop wait_for_finalization_start;
|
| + TriggerReactiveScenario(wait_for_finalization_start.QuitClosure());
|
| + wait_for_finalization_start.Run();
|
| +
|
| + EXPECT_TRUE(get_started_finalizations() == 1);
|
| + EXPECT_FALSE(get_last_started_finalization_success());
|
| +}
|
| +
|
| +} // namespace
|
|
|