esim_test_base.cc 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. // Copyright 2020 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. #include "ash/services/cellular_setup/esim_test_base.h"
  5. #include <memory>
  6. #include "ash/services/cellular_setup/esim_manager.h"
  7. #include "ash/services/cellular_setup/esim_test_utils.h"
  8. #include "ash/services/cellular_setup/public/mojom/esim_manager.mojom-forward.h"
  9. #include "chromeos/ash/components/dbus/hermes/hermes_clients.h"
  10. #include "chromeos/ash/components/dbus/hermes/hermes_euicc_client.h"
  11. #include "chromeos/ash/components/dbus/hermes/hermes_manager_client.h"
  12. #include "chromeos/ash/components/dbus/shill/shill_clients.h"
  13. #include "chromeos/ash/components/dbus/shill/shill_manager_client.h"
  14. #include "chromeos/ash/components/network/cellular_connection_handler.h"
  15. #include "chromeos/ash/components/network/cellular_esim_installer.h"
  16. #include "chromeos/ash/components/network/cellular_esim_uninstall_handler.h"
  17. #include "chromeos/ash/components/network/cellular_inhibitor.h"
  18. #include "chromeos/ash/components/network/fake_network_connection_handler.h"
  19. #include "chromeos/ash/components/network/network_configuration_handler.h"
  20. #include "chromeos/ash/components/network/network_device_handler.h"
  21. #include "chromeos/ash/components/network/network_profile_handler.h"
  22. #include "chromeos/ash/components/network/network_state_handler.h"
  23. #include "chromeos/ash/components/network/test_cellular_esim_profile_handler.h"
  24. namespace ash::cellular_setup {
  25. const char* ESimTestBase::kTestEuiccPath = "/org/chromium/Hermes/Euicc/0";
  26. const char* ESimTestBase::kTestEid = "12345678901234567890123456789012";
  27. ESimTestBase::ESimTestBase()
  28. : task_environment_(base::test::TaskEnvironment::TimeSource::MOCK_TIME) {
  29. if (!ShillManagerClient::Get())
  30. shill_clients::InitializeFakes();
  31. if (!HermesManagerClient::Get())
  32. hermes_clients::InitializeFakes();
  33. }
  34. ESimTestBase::~ESimTestBase() = default;
  35. void ESimTestBase::SetUp() {
  36. HermesManagerClient::Get()->GetTestInterface()->ClearEuiccs();
  37. HermesEuiccClient::Get()->GetTestInterface()->SetInteractiveDelay(
  38. base::Seconds(0));
  39. network_state_handler_ = NetworkStateHandler::InitializeForTest();
  40. network_device_handler_ =
  41. NetworkDeviceHandler::InitializeForTesting(network_state_handler_.get());
  42. network_configuration_handler_ =
  43. NetworkConfigurationHandler::InitializeForTest(
  44. network_state_handler_.get(), network_device_handler_.get());
  45. network_connection_handler_ =
  46. std::make_unique<FakeNetworkConnectionHandler>();
  47. network_profile_handler_ = NetworkProfileHandler::InitializeForTesting();
  48. cellular_inhibitor_ = std::make_unique<CellularInhibitor>();
  49. cellular_inhibitor_->Init(network_state_handler_.get(),
  50. network_device_handler_.get());
  51. cellular_esim_profile_handler_ =
  52. std::make_unique<TestCellularESimProfileHandler>();
  53. cellular_esim_profile_handler_->Init(network_state_handler_.get(),
  54. cellular_inhibitor_.get());
  55. cellular_connection_handler_ = std::make_unique<CellularConnectionHandler>();
  56. cellular_connection_handler_->Init(network_state_handler_.get(),
  57. cellular_inhibitor_.get(),
  58. cellular_esim_profile_handler_.get());
  59. cellular_esim_installer_ = std::make_unique<CellularESimInstaller>();
  60. cellular_esim_installer_->Init(
  61. cellular_connection_handler_.get(), cellular_inhibitor_.get(),
  62. network_connection_handler_.get(), network_profile_handler_.get(),
  63. network_state_handler_.get());
  64. cellular_esim_uninstall_handler_ =
  65. std::make_unique<CellularESimUninstallHandler>();
  66. cellular_esim_uninstall_handler_->Init(
  67. cellular_inhibitor_.get(), cellular_esim_profile_handler_.get(),
  68. /*managed_cellular_pref_handler=*/nullptr,
  69. network_configuration_handler_.get(), network_connection_handler_.get(),
  70. network_state_handler_.get());
  71. esim_manager_ = std::make_unique<ESimManager>(
  72. cellular_connection_handler_.get(), cellular_esim_installer_.get(),
  73. cellular_esim_profile_handler_.get(),
  74. cellular_esim_uninstall_handler_.get(), cellular_inhibitor_.get(),
  75. network_connection_handler_.get(), network_state_handler_.get());
  76. observer_ = std::make_unique<ESimManagerTestObserver>();
  77. esim_manager_->AddObserver(observer_->GenerateRemote());
  78. }
  79. void ESimTestBase::TearDown() {
  80. esim_manager_.reset();
  81. observer_.reset();
  82. HermesEuiccClient::Get()->GetTestInterface()->ResetPendingEventsRequested();
  83. }
  84. void ESimTestBase::SetupEuicc() {
  85. HermesManagerClient::Get()->GetTestInterface()->AddEuicc(
  86. dbus::ObjectPath(kTestEuiccPath), kTestEid, /*is_active=*/true,
  87. /*physical_slot=*/0);
  88. base::RunLoop().RunUntilIdle();
  89. }
  90. std::vector<mojo::PendingRemote<mojom::Euicc>>
  91. ESimTestBase::GetAvailableEuiccs() {
  92. std::vector<mojo::PendingRemote<mojom::Euicc>> result;
  93. base::RunLoop run_loop;
  94. esim_manager()->GetAvailableEuiccs(base::BindOnce(
  95. [](std::vector<mojo::PendingRemote<mojom::Euicc>>* result,
  96. base::OnceClosure quit_closure,
  97. std::vector<mojo::PendingRemote<mojom::Euicc>> available_euiccs) {
  98. for (auto& euicc : available_euiccs)
  99. result->push_back(std::move(euicc));
  100. std::move(quit_closure).Run();
  101. },
  102. &result, run_loop.QuitClosure()));
  103. run_loop.Run();
  104. return result;
  105. }
  106. mojo::Remote<mojom::Euicc> ESimTestBase::GetEuiccForEid(
  107. const std::string& eid) {
  108. std::vector<mojo::PendingRemote<mojom::Euicc>> euicc_pending_remotes =
  109. GetAvailableEuiccs();
  110. for (auto& euicc_pending_remote : euicc_pending_remotes) {
  111. mojo::Remote<mojom::Euicc> euicc(std::move(euicc_pending_remote));
  112. mojom::EuiccPropertiesPtr euicc_properties = GetEuiccProperties(euicc);
  113. if (euicc_properties->eid == eid) {
  114. return euicc;
  115. }
  116. }
  117. return mojo::Remote<mojom::Euicc>();
  118. }
  119. void ESimTestBase::FastForwardProfileRefreshDelay() {
  120. const base::TimeDelta kProfileRefreshCallbackDelay = base::Milliseconds(150);
  121. // Connect can result in two profile refresh calls before and after
  122. // enabling profile. Fast forward by delay after refresh.
  123. task_environment()->FastForwardBy(2 * kProfileRefreshCallbackDelay);
  124. }
  125. } // namespace ash::cellular_setup