Mattias Nissler | 980f2aa5 | 2019-02-13 13:21:49 | [diff] [blame^] | 1 | // Copyright 2019 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 | |
| 5 | #include "chrome/browser/chromeos/scheduler_configuration_manager.h" |
| 6 | |
| 7 | #include <memory> |
| 8 | |
| 9 | #include "base/test/scoped_task_environment.h" |
| 10 | #include "chrome/common/pref_names.h" |
| 11 | #include "chromeos/dbus/fake_debug_daemon_client.h" |
| 12 | #include "components/prefs/testing_pref_service.h" |
| 13 | #include "testing/gtest/include/gtest/gtest.h" |
| 14 | #include "third_party/cros_system_api/dbus/debugd/dbus-constants.h" |
| 15 | |
| 16 | namespace chromeos { |
| 17 | |
| 18 | class SchedulerConfigurationManagerTest : public testing::Test { |
| 19 | public: |
| 20 | SchedulerConfigurationManagerTest() { |
| 21 | SchedulerConfigurationManager::RegisterLocalStatePrefs( |
| 22 | local_state_.registry()); |
| 23 | } |
| 24 | |
| 25 | base::test::ScopedTaskEnvironment scoped_task_environment_; |
| 26 | |
| 27 | FakeDebugDaemonClient debug_daemon_client_; |
| 28 | TestingPrefServiceSimple local_state_; |
| 29 | }; |
| 30 | |
| 31 | TEST_F(SchedulerConfigurationManagerTest, Startup) { |
| 32 | local_state_.SetString(prefs::kSchedulerConfiguration, "initial"); |
| 33 | debug_daemon_client_.SetServiceIsAvailable(false); |
| 34 | |
| 35 | // Manager waits on initialization for service to be available. |
| 36 | SchedulerConfigurationManager manager(&debug_daemon_client_, &local_state_); |
| 37 | scoped_task_environment_.RunUntilIdle(); |
| 38 | EXPECT_EQ("", debug_daemon_client_.scheduler_configuration_name()); |
| 39 | |
| 40 | // Config changes don't lead to updates while debugd isn't ready. |
| 41 | local_state_.SetString(prefs::kSchedulerConfiguration, "config"); |
| 42 | scoped_task_environment_.RunUntilIdle(); |
| 43 | EXPECT_EQ("", debug_daemon_client_.scheduler_configuration_name()); |
| 44 | |
| 45 | // Once the debugd service becomes available, the config gets set. |
| 46 | debug_daemon_client_.SetServiceIsAvailable(true); |
| 47 | scoped_task_environment_.RunUntilIdle(); |
| 48 | EXPECT_EQ("config", debug_daemon_client_.scheduler_configuration_name()); |
| 49 | } |
| 50 | |
| 51 | TEST_F(SchedulerConfigurationManagerTest, ConfigChange) { |
| 52 | // Correct default is used when there is no configured value. |
| 53 | SchedulerConfigurationManager manager(&debug_daemon_client_, &local_state_); |
| 54 | scoped_task_environment_.RunUntilIdle(); |
| 55 | EXPECT_EQ(debugd::scheduler_configuration::kPerformanceScheduler, |
| 56 | debug_daemon_client_.scheduler_configuration_name()); |
| 57 | |
| 58 | // Change user pref, which should trigger a config change. |
| 59 | local_state_.SetUserPref(prefs::kSchedulerConfiguration, |
| 60 | std::make_unique<base::Value>("user")); |
| 61 | scoped_task_environment_.RunUntilIdle(); |
| 62 | EXPECT_EQ("user", debug_daemon_client_.scheduler_configuration_name()); |
| 63 | |
| 64 | // Set a policy, which should override the user setting |
| 65 | local_state_.SetManagedPref(prefs::kSchedulerConfiguration, |
| 66 | std::make_unique<base::Value>("policy")); |
| 67 | scoped_task_environment_.RunUntilIdle(); |
| 68 | EXPECT_EQ("policy", debug_daemon_client_.scheduler_configuration_name()); |
| 69 | |
| 70 | // Dropping the user pref doesn't change anything. |
| 71 | local_state_.RemoveUserPref(prefs::kSchedulerConfiguration); |
| 72 | scoped_task_environment_.RunUntilIdle(); |
| 73 | EXPECT_EQ("policy", debug_daemon_client_.scheduler_configuration_name()); |
| 74 | |
| 75 | // Dropping the policy as well reverts to the default configuration. |
| 76 | local_state_.RemoveManagedPref(prefs::kSchedulerConfiguration); |
| 77 | scoped_task_environment_.RunUntilIdle(); |
| 78 | EXPECT_EQ(debugd::scheduler_configuration::kPerformanceScheduler, |
| 79 | debug_daemon_client_.scheduler_configuration_name()); |
| 80 | } |
| 81 | |
| 82 | } // namespace chromeos |