Track SchedulerConfiguration and update system

Adds SchedulerConfigurationManager, which tracks the preference that
determines which scheduler configuration to use on Chrome OS and
updates the system side on configuration changes.

BUG=chromium:927840
TEST=Unit test.

Change-Id: I2991b08a6c53cd8af9fc5146691749f51ce77018
Reviewed-on: https://chromium-review.googlesource.com/c/1456067
Reviewed-by: Dominic Battré <[email protected]>
Reviewed-by: Dan Erat <[email protected]>
Commit-Queue: Mattias Nissler <[email protected]>
Cr-Commit-Position: refs/heads/master@{#631666}
diff --git a/chrome/browser/chromeos/scheduler_configuration_manager_unittest.cc b/chrome/browser/chromeos/scheduler_configuration_manager_unittest.cc
new file mode 100644
index 0000000..e1507d6
--- /dev/null
+++ b/chrome/browser/chromeos/scheduler_configuration_manager_unittest.cc
@@ -0,0 +1,82 @@
+// 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/chromeos/scheduler_configuration_manager.h"
+
+#include <memory>
+
+#include "base/test/scoped_task_environment.h"
+#include "chrome/common/pref_names.h"
+#include "chromeos/dbus/fake_debug_daemon_client.h"
+#include "components/prefs/testing_pref_service.h"
+#include "testing/gtest/include/gtest/gtest.h"
+#include "third_party/cros_system_api/dbus/debugd/dbus-constants.h"
+
+namespace chromeos {
+
+class SchedulerConfigurationManagerTest : public testing::Test {
+ public:
+  SchedulerConfigurationManagerTest() {
+    SchedulerConfigurationManager::RegisterLocalStatePrefs(
+        local_state_.registry());
+  }
+
+  base::test::ScopedTaskEnvironment scoped_task_environment_;
+
+  FakeDebugDaemonClient debug_daemon_client_;
+  TestingPrefServiceSimple local_state_;
+};
+
+TEST_F(SchedulerConfigurationManagerTest, Startup) {
+  local_state_.SetString(prefs::kSchedulerConfiguration, "initial");
+  debug_daemon_client_.SetServiceIsAvailable(false);
+
+  // Manager waits on initialization for service to be available.
+  SchedulerConfigurationManager manager(&debug_daemon_client_, &local_state_);
+  scoped_task_environment_.RunUntilIdle();
+  EXPECT_EQ("", debug_daemon_client_.scheduler_configuration_name());
+
+  // Config changes don't lead to updates while debugd isn't ready.
+  local_state_.SetString(prefs::kSchedulerConfiguration, "config");
+  scoped_task_environment_.RunUntilIdle();
+  EXPECT_EQ("", debug_daemon_client_.scheduler_configuration_name());
+
+  // Once the debugd service becomes available, the config gets set.
+  debug_daemon_client_.SetServiceIsAvailable(true);
+  scoped_task_environment_.RunUntilIdle();
+  EXPECT_EQ("config", debug_daemon_client_.scheduler_configuration_name());
+}
+
+TEST_F(SchedulerConfigurationManagerTest, ConfigChange) {
+  // Correct default is used when there is no configured value.
+  SchedulerConfigurationManager manager(&debug_daemon_client_, &local_state_);
+  scoped_task_environment_.RunUntilIdle();
+  EXPECT_EQ(debugd::scheduler_configuration::kPerformanceScheduler,
+            debug_daemon_client_.scheduler_configuration_name());
+
+  // Change user pref, which should trigger a config change.
+  local_state_.SetUserPref(prefs::kSchedulerConfiguration,
+                           std::make_unique<base::Value>("user"));
+  scoped_task_environment_.RunUntilIdle();
+  EXPECT_EQ("user", debug_daemon_client_.scheduler_configuration_name());
+
+  // Set a policy, which should override the user setting
+  local_state_.SetManagedPref(prefs::kSchedulerConfiguration,
+                              std::make_unique<base::Value>("policy"));
+  scoped_task_environment_.RunUntilIdle();
+  EXPECT_EQ("policy", debug_daemon_client_.scheduler_configuration_name());
+
+  // Dropping the user pref doesn't change anything.
+  local_state_.RemoveUserPref(prefs::kSchedulerConfiguration);
+  scoped_task_environment_.RunUntilIdle();
+  EXPECT_EQ("policy", debug_daemon_client_.scheduler_configuration_name());
+
+  // Dropping the policy as well reverts to the default configuration.
+  local_state_.RemoveManagedPref(prefs::kSchedulerConfiguration);
+  scoped_task_environment_.RunUntilIdle();
+  EXPECT_EQ(debugd::scheduler_configuration::kPerformanceScheduler,
+            debug_daemon_client_.scheduler_configuration_name());
+}
+
+}  // namespace chromeos