Commit 6bf5b850 authored by Marc Grimme's avatar Marc Grimme Committed by Commit Bot

Unittests for ManagementUIHandler::AddDeviceReportingInfo()

Currently no unit tests are covering the logic behind
ManagementUIHandler::AddDeviceReportingInfo().
This CL adds the missing unit tests and does some basic refactoring in
the test class itself.

R=marcgrimme

Bug: 1044954
Change-Id: I1767deec4d6754e9bb55ad544fb6f0a59ca697cd
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2016640
Commit-Queue: Marc Grimme <marcgrimme@chromium.org>
Reviewed-by: default avatarLei Zhang <thestig@chromium.org>
Reviewed-by: default avatarRoman Aleksandrov <raleksandrov@google.com>
Cr-Commit-Position: refs/heads/master@{#762800}
parent 3ab6b3bc
......@@ -46,6 +46,7 @@
#include "chrome/browser/chromeos/policy/policy_cert_service.h"
#include "chrome/browser/chromeos/policy/policy_cert_service_factory.h"
#include "chrome/browser/chromeos/policy/status_collector/device_status_collector.h"
#include "chrome/browser/chromeos/policy/status_collector/status_collector.h"
#include "chrome/browser/chromeos/policy/status_uploader.h"
#include "chrome/browser/chromeos/policy/system_log_uploader.h"
#include "chrome/browser/chromeos/profiles/profile_helper.h"
......@@ -228,85 +229,6 @@ void AddDeviceReportingElement(base::Value* report_sources,
data.SetKey("reportingType", base::Value(ToJSDeviceReportingType(type)));
report_sources->Append(std::move(data));
}
void AddDeviceReportingInfo(base::Value* report_sources, Profile* profile) {
policy::BrowserPolicyConnectorChromeOS* connector =
g_browser_process->platform_part()->browser_policy_connector_chromeos();
// Only check for report status in managed environment.
if (!connector->IsEnterpriseManaged())
return;
policy::DeviceCloudPolicyManagerChromeOS* manager =
connector->GetDeviceCloudPolicyManager();
if (!manager)
return;
const policy::StatusCollector* collector =
manager->GetStatusUploader()->status_collector();
// Elements appear on the page in the order they are added.
if (collector->ShouldReportActivityTimes()) {
AddDeviceReportingElement(report_sources, kManagementReportActivityTimes,
DeviceReportingType::kDeviceActivity);
} else {
if (collector->ShouldReportUsers()) {
AddDeviceReportingElement(report_sources, kManagementReportUsers,
DeviceReportingType::kSupervisedUser);
}
}
if (collector->ShouldReportHardwareStatus()) {
AddDeviceReportingElement(report_sources, kManagementReportHardwareStatus,
DeviceReportingType::kDeviceStatistics);
}
if (collector->ShouldReportNetworkInterfaces()) {
AddDeviceReportingElement(report_sources,
kManagementReportNetworkInterfaces,
DeviceReportingType::kDevice);
}
if (collector->ShouldReportCrashReportInfo()) {
AddDeviceReportingElement(report_sources, kManagementReportCrashReports,
DeviceReportingType::kCrashReport);
}
if (manager->GetSystemLogUploader()->upload_enabled()) {
AddDeviceReportingElement(report_sources, kManagementLogUploadEnabled,
DeviceReportingType::kLogs);
}
if (profile->GetPrefs()->GetBoolean(
prefs::kPrintingSendUsernameAndFilenameEnabled)) {
AddDeviceReportingElement(report_sources, kManagementPrinting,
DeviceReportingType::kPrint);
}
if (crostini::CrostiniFeatures::Get()->IsAllowed(profile)) {
if (!profile->GetPrefs()
->GetFilePath(crostini::prefs::kCrostiniAnsiblePlaybookFilePath)
.empty()) {
AddDeviceReportingElement(report_sources,
kManagementCrostiniContainerConfiguration,
DeviceReportingType::kCrostini);
} else if (profile->GetPrefs()->GetBoolean(
crostini::prefs::kReportCrostiniUsageEnabled)) {
AddDeviceReportingElement(report_sources, kManagementCrostini,
DeviceReportingType::kCrostini);
}
}
if (g_browser_process->local_state()->GetBoolean(
prefs::kCloudReportingEnabled) &&
base::FeatureList::IsEnabled(features::kEnterpriseReportingInChromeOS)) {
AddDeviceReportingElement(report_sources,
kManagementExtensionReportUsername,
DeviceReportingType::kUsername);
AddDeviceReportingElement(report_sources, kManagementReportExtensions,
DeviceReportingType::kExtensions);
AddDeviceReportingElement(report_sources,
kManagementReportAndroidApplications,
DeviceReportingType::kAndroidApplication);
}
}
#endif // defined(OS_CHROMEOS)
std::vector<base::Value> GetPermissionsForExtension(
......@@ -566,6 +488,64 @@ void ManagementUIHandler::AddReportingInfo(base::Value* report_sources) {
}
}
#if defined(OS_CHROMEOS)
const policy::DeviceCloudPolicyManagerChromeOS*
ManagementUIHandler::GetDeviceCloudPolicyManager() const {
// Only check for report status in managed environment.
if (!device_managed_)
return nullptr;
const policy::BrowserPolicyConnectorChromeOS* connector =
g_browser_process->platform_part()->browser_policy_connector_chromeos();
return connector->GetDeviceCloudPolicyManager();
}
void ManagementUIHandler::AddDeviceReportingInfo(
base::Value* report_sources,
const policy::StatusCollector* collector,
const policy::SystemLogUploader* uploader,
const Profile* profile) const {
if (!collector || !profile || !uploader)
return;
// Elements appear on the page in the order they are added.
if (collector->ShouldReportActivityTimes()) {
AddDeviceReportingElement(report_sources, kManagementReportActivityTimes,
DeviceReportingType::kDeviceActivity);
} else {
if (collector->ShouldReportUsers()) {
AddDeviceReportingElement(report_sources, kManagementReportUsers,
DeviceReportingType::kSupervisedUser);
}
}
if (collector->ShouldReportHardwareStatus()) {
AddDeviceReportingElement(report_sources, kManagementReportHardwareStatus,
DeviceReportingType::kDeviceStatistics);
}
if (collector->ShouldReportNetworkInterfaces()) {
AddDeviceReportingElement(report_sources,
kManagementReportNetworkInterfaces,
DeviceReportingType::kDevice);
}
if (uploader->upload_enabled()) {
AddDeviceReportingElement(report_sources, kManagementLogUploadEnabled,
DeviceReportingType::kLogs);
}
if (profile->GetPrefs()->GetBoolean(
prefs::kPrintingSendUsernameAndFilenameEnabled)) {
AddDeviceReportingElement(report_sources, kManagementPrinting,
DeviceReportingType::kPrint);
}
if (profile->GetPrefs()->GetBoolean(
crostini::prefs::kReportCrostiniUsageEnabled)) {
AddDeviceReportingElement(report_sources, kManagementCrostini,
DeviceReportingType::kCrostini);
}
}
#endif
base::Value ManagementUIHandler::GetContextualManagedData(Profile* profile) {
base::Value response(base::Value::Type::DICTIONARY);
#if defined(OS_CHROMEOS)
......@@ -861,7 +841,19 @@ void ManagementUIHandler::HandleGetDeviceReportingInfo(
base::Value report_sources(base::Value::Type::LIST);
AllowJavascript();
AddDeviceReportingInfo(&report_sources, Profile::FromWebUI(web_ui()));
const policy::DeviceCloudPolicyManagerChromeOS* manager =
GetDeviceCloudPolicyManager();
policy::StatusUploader* uploader = nullptr;
policy::SystemLogUploader* syslog_uploader = nullptr;
policy::StatusCollector* collector = nullptr;
if (manager) {
uploader = manager->GetStatusUploader();
syslog_uploader = manager->GetSystemLogUploader();
if (uploader)
collector = uploader->status_collector();
}
AddDeviceReportingInfo(&report_sources, collector, syslog_uploader,
Profile::FromWebUI(web_ui()));
ResolveJavascriptCallback(args->GetList()[0] /* callback_id */,
report_sources);
......
......@@ -85,7 +85,10 @@ class Extension;
} // namespace extensions
namespace policy {
class DeviceCloudPolicyManagerChromeOS;
class PolicyService;
class StatusCollector;
class SystemLogUploader;
} // namespace policy
class Profile;
......@@ -129,6 +132,12 @@ class ManagementUIHandler : public content::WebUIMessageHandler,
#if defined(OS_CHROMEOS)
// Protected for testing.
virtual const std::string GetDeviceDomain() const;
virtual const policy::DeviceCloudPolicyManagerChromeOS*
GetDeviceCloudPolicyManager() const;
void AddDeviceReportingInfo(base::Value* report_sources,
const policy::StatusCollector* collector,
const policy::SystemLogUploader* uploader,
const Profile* profile) const;
#endif // defined(OS_CHROMEOS)
private:
void GetManagementStatus(Profile* profile, base::Value* status) const;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment