aw_tracing_delegate_unittest.cc 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. // Copyright 2022 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. #include "android_webview/browser/tracing/aw_tracing_delegate.h"
  5. #include "android_webview/browser/aw_browser_process.h"
  6. #include "android_webview/browser/aw_feature_list_creator.h"
  7. #include "base/values.h"
  8. #include "components/metrics/metrics_pref_names.h"
  9. #include "components/prefs/pref_registry_simple.h"
  10. #include "components/prefs/testing_pref_service.h"
  11. #include "components/tracing/common/background_tracing_state_manager.h"
  12. #include "components/tracing/common/pref_names.h"
  13. #include "content/public/browser/background_tracing_config.h"
  14. #include "content/public/test/browser_task_environment.h"
  15. #include "testing/gtest/include/gtest/gtest.h"
  16. namespace android_webview {
  17. class AwTracingDelegateTest : public testing::Test {
  18. public:
  19. void SetUp() override {
  20. AwFeatureListCreator* aw_feature_list_creator = new AwFeatureListCreator();
  21. aw_feature_list_creator->CreateLocalState();
  22. browser_process_ =
  23. new android_webview::AwBrowserProcess(aw_feature_list_creator);
  24. pref_service_ = std::make_unique<TestingPrefServiceSimple>();
  25. pref_service_->registry()->RegisterBooleanPref(
  26. metrics::prefs::kMetricsReportingEnabled, false);
  27. pref_service_->SetBoolean(metrics::prefs::kMetricsReportingEnabled, true);
  28. tracing::RegisterPrefs(pref_service_->registry());
  29. tracing::BackgroundTracingStateManager::GetInstance()
  30. .SetPrefServiceForTesting(pref_service_.get());
  31. }
  32. void TearDown() override {
  33. delete browser_process_;
  34. tracing::BackgroundTracingStateManager::GetInstance().Reset();
  35. }
  36. android_webview::AwTracingDelegate delegate_;
  37. private:
  38. content::BrowserTaskEnvironment task_environment_;
  39. raw_ptr<android_webview::AwBrowserProcess> browser_process_;
  40. std::unique_ptr<TestingPrefServiceSimple> pref_service_;
  41. };
  42. std::unique_ptr<content::BackgroundTracingConfig> CreateValidConfig() {
  43. base::Value::Dict dict;
  44. dict.Set("scenario_name", "TestScenario");
  45. dict.Set("mode", "PREEMPTIVE_TRACING_MODE");
  46. dict.Set("custom_categories", "toplevel");
  47. base::Value::List rules_list;
  48. {
  49. base::Value::Dict rules_dict;
  50. rules_dict.Set("rule", "MONITOR_AND_DUMP_WHEN_TRIGGER_NAMED");
  51. rules_dict.Set("trigger_name", "test");
  52. rules_list.Append(std::move(rules_dict));
  53. }
  54. dict.Set("configs", std::move(rules_list));
  55. return content::BackgroundTracingConfig::FromDict(std::move(dict));
  56. }
  57. TEST_F(AwTracingDelegateTest, IsAllowedToBegin) {
  58. auto config = CreateValidConfig();
  59. EXPECT_TRUE(delegate_.IsAllowedToBeginBackgroundScenario(
  60. *config, /*requires_anonymized_data=*/false));
  61. EXPECT_TRUE(delegate_.IsAllowedToEndBackgroundScenario(
  62. *config, /*requires_anonymized_data=*/false,
  63. /*is_crash_scenario=*/false));
  64. }
  65. TEST_F(AwTracingDelegateTest, IsAllowedToBeginSessionEndedUnexpectedly) {
  66. tracing::BackgroundTracingStateManager::GetInstance().SaveState(
  67. {}, tracing::BackgroundTracingState::STARTED);
  68. base::Value dict(base::Value::Type::DICTIONARY);
  69. tracing::BackgroundTracingStateManager::GetInstance().Initialize(nullptr);
  70. auto config = CreateValidConfig();
  71. EXPECT_FALSE(delegate_.IsAllowedToBeginBackgroundScenario(
  72. *config, /*requires_anonymized_data=*/false));
  73. }
  74. } // namespace android_webview