blob: 331f13a9ff93e6f134b09cb9522a4171f20c9ae2 [file] [log] [blame]
Mattias Nissler980f2aa52019-02-13 13:21:491// 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
Mattias Nissler76bd3a002019-02-14 08:24:359#include "base/test/scoped_feature_list.h"
Mattias Nissler980f2aa52019-02-13 13:21:4910#include "base/test/scoped_task_environment.h"
Mattias Nissler76bd3a002019-02-14 08:24:3511#include "chrome/common/chrome_features.h"
Mattias Nissler980f2aa52019-02-13 13:21:4912#include "chrome/common/pref_names.h"
13#include "chromeos/dbus/fake_debug_daemon_client.h"
14#include "components/prefs/testing_pref_service.h"
15#include "testing/gtest/include/gtest/gtest.h"
16#include "third_party/cros_system_api/dbus/debugd/dbus-constants.h"
17
18namespace chromeos {
19
20class SchedulerConfigurationManagerTest : public testing::Test {
21 public:
22 SchedulerConfigurationManagerTest() {
23 SchedulerConfigurationManager::RegisterLocalStatePrefs(
24 local_state_.registry());
25 }
26
27 base::test::ScopedTaskEnvironment scoped_task_environment_;
28
29 FakeDebugDaemonClient debug_daemon_client_;
30 TestingPrefServiceSimple local_state_;
31};
32
33TEST_F(SchedulerConfigurationManagerTest, Startup) {
34 local_state_.SetString(prefs::kSchedulerConfiguration, "initial");
35 debug_daemon_client_.SetServiceIsAvailable(false);
36
37 // Manager waits on initialization for service to be available.
38 SchedulerConfigurationManager manager(&debug_daemon_client_, &local_state_);
39 scoped_task_environment_.RunUntilIdle();
40 EXPECT_EQ("", debug_daemon_client_.scheduler_configuration_name());
41
42 // Config changes don't lead to updates while debugd isn't ready.
43 local_state_.SetString(prefs::kSchedulerConfiguration, "config");
44 scoped_task_environment_.RunUntilIdle();
45 EXPECT_EQ("", debug_daemon_client_.scheduler_configuration_name());
46
47 // Once the debugd service becomes available, the config gets set.
48 debug_daemon_client_.SetServiceIsAvailable(true);
49 scoped_task_environment_.RunUntilIdle();
50 EXPECT_EQ("config", debug_daemon_client_.scheduler_configuration_name());
51}
52
53TEST_F(SchedulerConfigurationManagerTest, ConfigChange) {
54 // Correct default is used when there is no configured value.
55 SchedulerConfigurationManager manager(&debug_daemon_client_, &local_state_);
56 scoped_task_environment_.RunUntilIdle();
Mattias Nissler4943da6d2019-06-13 16:23:0257 EXPECT_EQ(debugd::scheduler_configuration::kConservativeScheduler,
Mattias Nissler980f2aa52019-02-13 13:21:4958 debug_daemon_client_.scheduler_configuration_name());
59
60 // Change user pref, which should trigger a config change.
61 local_state_.SetUserPref(prefs::kSchedulerConfiguration,
62 std::make_unique<base::Value>("user"));
63 scoped_task_environment_.RunUntilIdle();
64 EXPECT_EQ("user", debug_daemon_client_.scheduler_configuration_name());
65
66 // Set a policy, which should override the user setting
67 local_state_.SetManagedPref(prefs::kSchedulerConfiguration,
68 std::make_unique<base::Value>("policy"));
69 scoped_task_environment_.RunUntilIdle();
70 EXPECT_EQ("policy", debug_daemon_client_.scheduler_configuration_name());
71
72 // Dropping the user pref doesn't change anything.
73 local_state_.RemoveUserPref(prefs::kSchedulerConfiguration);
74 scoped_task_environment_.RunUntilIdle();
75 EXPECT_EQ("policy", debug_daemon_client_.scheduler_configuration_name());
76
77 // Dropping the policy as well reverts to the default configuration.
78 local_state_.RemoveManagedPref(prefs::kSchedulerConfiguration);
79 scoped_task_environment_.RunUntilIdle();
Mattias Nissler4943da6d2019-06-13 16:23:0280 EXPECT_EQ(debugd::scheduler_configuration::kConservativeScheduler,
Mattias Nissler980f2aa52019-02-13 13:21:4981 debug_daemon_client_.scheduler_configuration_name());
82}
83
Mattias Nissler76bd3a002019-02-14 08:24:3584TEST_F(SchedulerConfigurationManagerTest, FinchDefault) {
85 auto feature_list = std::make_unique<base::test::ScopedFeatureList>();
86 feature_list->InitAndEnableFeatureWithParameters(
87 features::kSchedulerConfiguration, {{"config", "finch"}});
88
89 // Finch parameter selects the default.
90 SchedulerConfigurationManager manager(&debug_daemon_client_, &local_state_);
91 scoped_task_environment_.RunUntilIdle();
92 EXPECT_EQ("finch", debug_daemon_client_.scheduler_configuration_name());
93
94 // Config values override finch default.
95 local_state_.SetString(prefs::kSchedulerConfiguration, "config");
96 scoped_task_environment_.RunUntilIdle();
97 EXPECT_EQ("config", debug_daemon_client_.scheduler_configuration_name());
98}
99
Mattias Nissler980f2aa52019-02-13 13:21:49100} // namespace chromeos