1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64
   65
   66
   67
   68
   69
   70
   71
   72
   73
   74
   75
   76
   77
   78
   79
   80
   81
   82
   83
   84
   85
   86
   87
   88
   89
   90
   91
   92
   93
   94
   95
   96
   97
   98
   99
  100
  101

ash / ambient / resources / ambient_dlc_background_installer_unittest.cc [blame]

// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ash/ambient/resources/ambient_dlc_background_installer.h"

#include <string>

#include "ash/constants/ambient_time_of_day_constants.h"
#include "ash/public/cpp/personalization_app/time_of_day_test_utils.h"
#include "base/test/task_environment.h"
#include "base/test/test_future.h"
#include "chromeos/ash/components/dbus/dlcservice/dlcservice.pb.h"
#include "chromeos/ash/components/dbus/dlcservice/fake_dlcservice_client.h"
#include "chromeos/ash/components/network/network_state_test_helper.h"
#include "chromeos/ash/services/network_config/public/cpp/cros_network_config_test_helper.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/cros_system_api/dbus/shill/dbus-constants.h"

namespace ash {
namespace {

class AmbientDlcBackgroundInstallerTest : public ::testing::Test {
 protected:
  AmbientDlcBackgroundInstallerTest() {
    feature_list_.InitWithFeatures(personalization_app::GetTimeOfDayFeatures(),
                                   {});
    wifi_device_path_ =
        cros_network_config_helper_.network_state_helper().ConfigureWiFi(
            shill::kStateIdle);
  }

  size_t GetNumTimeOfDayInstallRequests() {
    base::test::TestFuture<std::string_view, const dlcservice::DlcsWithContent&>
        future;
    dlc_service_client_.GetExistingDlcs(future.GetCallback());
    size_t num_installs = 0;
    for (const auto& dlc_info : future.Get<1>().dlc_infos()) {
      if (dlc_info.id() == kTimeOfDayDlcId) {
        ++num_installs;
      }
    }
    return num_installs;
  }

  bool IsTimeOfDayDlcInstalled() {
    return GetNumTimeOfDayInstallRequests() >= 1u;
  }

  base::test::ScopedFeatureList feature_list_;
  base::test::TaskEnvironment task_environment_;
  network_config::CrosNetworkConfigTestHelper cros_network_config_helper_;
  FakeDlcserviceClient dlc_service_client_;
  std::string wifi_device_path_;
};

TEST_F(AmbientDlcBackgroundInstallerTest, InstallsWhenNetworkUp) {
  AmbientBackgroundDlcInstaller installer;
  ASSERT_FALSE(IsTimeOfDayDlcInstalled());

  cros_network_config_helper_.network_state_helper().SetServiceProperty(
      wifi_device_path_, shill::kStateProperty,
      base::Value(shill::kStateOnline));
  task_environment_.RunUntilIdle();
  EXPECT_TRUE(IsTimeOfDayDlcInstalled());
}

TEST_F(AmbientDlcBackgroundInstallerTest, InstallsWhenNetworkAlreadyUp) {
  cros_network_config_helper_.network_state_helper().SetServiceProperty(
      wifi_device_path_, shill::kStateProperty,
      base::Value(shill::kStateOnline));

  AmbientBackgroundDlcInstaller installer;
  task_environment_.RunUntilIdle();
  EXPECT_TRUE(IsTimeOfDayDlcInstalled());
}

TEST_F(AmbientDlcBackgroundInstallerTest, InstallsOnlyOnce) {
  AmbientBackgroundDlcInstaller installer;
  ASSERT_FALSE(IsTimeOfDayDlcInstalled());

  cros_network_config_helper_.network_state_helper().SetServiceProperty(
      wifi_device_path_, shill::kStateProperty,
      base::Value(shill::kStateOnline));
  task_environment_.RunUntilIdle();

  cros_network_config_helper_.network_state_helper().SetServiceProperty(
      wifi_device_path_, shill::kStateProperty, base::Value(shill::kStateIdle));
  task_environment_.RunUntilIdle();

  cros_network_config_helper_.network_state_helper().SetServiceProperty(
      wifi_device_path_, shill::kStateProperty,
      base::Value(shill::kStateOnline));
  task_environment_.RunUntilIdle();

  EXPECT_EQ(GetNumTimeOfDayInstallRequests(), 1u);
}

}  // namespace
}  // namespace ash