chromium/chrome/browser/enterprise/connectors/reporting/crash_reporting_context_unittest.cc

// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/enterprise/connectors/reporting/crash_reporting_context.h"

#include "base/command_line.h"
#include "base/files/scoped_temp_dir.h"
#include "chrome/browser/enterprise/connectors/reporting/realtime_reporting_client.h"
#include "chrome/browser/enterprise/connectors/reporting/realtime_reporting_client_factory.h"
#include "chrome/browser/policy/dm_token_utils.h"
#include "chrome/common/channel_info.h"
#include "chrome/test/base/testing_browser_process.h"
#include "chrome/test/base/testing_profile_manager.h"
#include "components/enterprise/connectors/core/connectors_prefs.h"
#include "components/enterprise/connectors/core/reporting_service_settings.h"
#include "components/prefs/pref_registry_simple.h"
#include "components/prefs/testing_pref_service.h"
#include "components/version_info/version_info.h"
#include "content/public/test/browser_task_environment.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

#if BUILDFLAG(GOOGLE_CHROME_BRANDING)
#include "chrome/test/base/scoped_channel_override.h"
#endif

_;
ByMove;
Eq;
Return;

namespace enterprise_connectors {

#if !BUILDFLAG(IS_CHROMEOS)

namespace {

void CreateCrashReport(crashpad::CrashReportDatabase* database,
                       crashpad::CrashReportDatabase::Report* report) {}

#if BUILDFLAG(GOOGLE_CHROME_BRANDING)
// Duplicating the definition of these variables here to ensure that changes to
// those values in the source file are deliberate and caught by tests otherwise.
constexpr char kCrashpadPollingIntervalFlag[] = "crashpad-polling-interval";
constexpr int kDefaultCrashpadPollingIntervalSeconds = 3600;
#endif

}  // namespace

class MockRealtimeCrashReportingClient : public RealtimeReportingClient {};

std::unique_ptr<KeyedService> CreateMockRealtimeCrashReportingClient(
    content::BrowserContext* profile) {}

class CrashReportingContextTest : public testing::Test {};

TEST_F(CrashReportingContextTest, GetNewReportsFromDB) {}

TEST_F(CrashReportingContextTest, GetAndSetLatestCrashReportingTime) {}

TEST_F(CrashReportingContextTest, UploadToReportingServer) {}

#if BUILDFLAG(GOOGLE_CHROME_BRANDING)

struct PollingIntervalParams {
  PollingIntervalParams(chrome::ScopedChannelOverride::Channel channel,
                        const std::string cmd_flag,
                        int expected_interval)
      : channel(channel),
        cmd_flag(cmd_flag),
        expected_interval(expected_interval) {}

  chrome::ScopedChannelOverride::Channel channel;
  std::string cmd_flag;
  int expected_interval;
};

class CrashpadPollingIntervalTest
    : public testing::TestWithParam<PollingIntervalParams> {};

TEST_P(CrashpadPollingIntervalTest, GetCrashpadPollingInterval) {
  chrome::ScopedChannelOverride scoped_channel(GetParam().channel);
  base::CommandLine* commandLine = base::CommandLine::ForCurrentProcess();
  commandLine->AppendSwitchASCII(kCrashpadPollingIntervalFlag,
                                 GetParam().cmd_flag);
  EXPECT_EQ(GetCrashpadPollingInterval(),
            base::Seconds(GetParam().expected_interval));
}

INSTANTIATE_TEST_SUITE_P(
    CrashpadPollingIntervalTest,
    CrashpadPollingIntervalTest,
    testing::Values(
        PollingIntervalParams(chrome::ScopedChannelOverride::Channel::kBeta,
                              "-10",
                              kDefaultCrashpadPollingIntervalSeconds),
        PollingIntervalParams(chrome::ScopedChannelOverride::Channel::kBeta,
                              "10",
                              10),
        PollingIntervalParams(chrome::ScopedChannelOverride::Channel::kStable,
                              "10",
                              kDefaultCrashpadPollingIntervalSeconds)));

#endif  // BUILDFLAG(GOOGLE_CHROME_BRANDING)

#endif  // !BUILDFLAG(IS_CHROMEOS)

}  // namespace enterprise_connectors