update_engine: Make SystemState accessible from everywhere

SystemState is supposed to be a global context and is used lamost
everywhere. So instead of passing it to functions and keeping multiple
pointers to it, its better to do what we did in dlcservice and make it a
singleton class with a getter that can be get from everywhere.

BUG=b:171829801
TEST=unittests

Change-Id: I3b2de9394b7769b3911195ca52d61dbe49afd4dd
Reviewed-on: https://chromium-review.googlesource.com/c/aosp/platform/system/update_engine/+/2521792
Commit-Queue: Amin Hassani <ahassani@chromium.org>
Tested-by: Amin Hassani <ahassani@chromium.org>
Reviewed-by: Jae Hoon Kim <kimjae@chromium.org>
diff --git a/cros/common_service_unittest.cc b/cros/common_service_unittest.cc
index 733ec0a..a6b0014 100644
--- a/cros/common_service_unittest.cc
+++ b/cros/common_service_unittest.cc
@@ -39,17 +39,14 @@
 
 class UpdateEngineServiceTest : public ::testing::Test {
  protected:
-  UpdateEngineServiceTest()
-      : mock_update_attempter_(fake_system_state_.mock_update_attempter()),
-        common_service_(&fake_system_state_) {}
+  UpdateEngineServiceTest() = default;
 
-  void SetUp() override { fake_system_state_.set_device_policy(nullptr); }
+  void SetUp() override {
+    FakeSystemState::CreateInstance();
+    FakeSystemState::Get()->set_device_policy(nullptr);
+    mock_update_attempter_ = FakeSystemState::Get()->mock_update_attempter();
+  }
 
-  // Fake/mock infrastructure.
-  FakeSystemState fake_system_state_;
-  policy::MockDevicePolicy mock_device_policy_;
-
-  // Shortcut for fake_system_state_.mock_update_attempter().
   MockUpdateAttempter* mock_update_attempter_;
 
   brillo::ErrorPtr error_;
@@ -119,7 +116,7 @@
 TEST_F(UpdateEngineServiceTest, SetChannelWithNoPolicy) {
   EXPECT_CALL(*mock_update_attempter_, RefreshDevicePolicy());
   // If SetTargetChannel is called it means the policy check passed.
-  EXPECT_CALL(*fake_system_state_.mock_request_params(),
+  EXPECT_CALL(*FakeSystemState::Get()->mock_request_params(),
               SetTargetChannel("stable-channel", true, _))
       .WillOnce(Return(true));
   EXPECT_TRUE(common_service_.SetChannel(&error_, "stable-channel", true));
@@ -129,10 +126,10 @@
 // When the policy is present, the delegated value should be checked.
 TEST_F(UpdateEngineServiceTest, SetChannelWithDelegatedPolicy) {
   policy::MockDevicePolicy mock_device_policy;
-  fake_system_state_.set_device_policy(&mock_device_policy);
+  FakeSystemState::Get()->set_device_policy(&mock_device_policy);
   EXPECT_CALL(mock_device_policy, GetReleaseChannelDelegated(_))
       .WillOnce(DoAll(SetArgPointee<0>(true), Return(true)));
-  EXPECT_CALL(*fake_system_state_.mock_request_params(),
+  EXPECT_CALL(*FakeSystemState::Get()->mock_request_params(),
               SetTargetChannel("beta-channel", true, _))
       .WillOnce(Return(true));
 
@@ -144,7 +141,7 @@
 // raised.
 TEST_F(UpdateEngineServiceTest, SetChannelWithInvalidChannel) {
   EXPECT_CALL(*mock_update_attempter_, RefreshDevicePolicy());
-  EXPECT_CALL(*fake_system_state_.mock_request_params(),
+  EXPECT_CALL(*FakeSystemState::Get()->mock_request_params(),
               SetTargetChannel("foo-channel", true, _))
       .WillOnce(Return(false));
 
@@ -155,8 +152,8 @@
 }
 
 TEST_F(UpdateEngineServiceTest, GetChannel) {
-  fake_system_state_.mock_request_params()->set_current_channel("current");
-  fake_system_state_.mock_request_params()->set_target_channel("target");
+  FakeSystemState::Get()->mock_request_params()->set_current_channel("current");
+  FakeSystemState::Get()->mock_request_params()->set_target_channel("target");
   string channel;
   EXPECT_TRUE(common_service_.GetChannel(
       &error_, true /* get_current_channel */, &channel));