cellular_setup_impl_unittest.cc 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. #include "ash/services/cellular_setup/cellular_setup_impl.h"
  5. #include <memory>
  6. #include <utility>
  7. #include <vector>
  8. #include "ash/services/cellular_setup/cellular_setup_base.h"
  9. #include "ash/services/cellular_setup/fake_ota_activator.h"
  10. #include "ash/services/cellular_setup/ota_activator_impl.h"
  11. #include "ash/services/cellular_setup/public/cpp/fake_activation_delegate.h"
  12. #include "base/run_loop.h"
  13. #include "base/test/task_environment.h"
  14. #include "chromeos/ash/components/network/network_handler_test_helper.h"
  15. #include "mojo/public/cpp/bindings/pending_remote.h"
  16. #include "testing/gtest/include/gtest/gtest.h"
  17. namespace ash::cellular_setup {
  18. namespace {
  19. class FakeOtaActivatorFactory : public OtaActivatorImpl::Factory {
  20. public:
  21. FakeOtaActivatorFactory() = default;
  22. FakeOtaActivatorFactory(const FakeOtaActivatorFactory&) = delete;
  23. FakeOtaActivatorFactory& operator=(const FakeOtaActivatorFactory&) = delete;
  24. ~FakeOtaActivatorFactory() override = default;
  25. std::vector<FakeOtaActivator*>& created_instances() {
  26. return created_instances_;
  27. }
  28. private:
  29. // OtaActivatorImpl::Factory:
  30. std::unique_ptr<OtaActivator> CreateInstance(
  31. mojo::PendingRemote<mojom::ActivationDelegate> activation_delegate,
  32. base::OnceClosure on_finished_callback,
  33. NetworkStateHandler* network_state_handler,
  34. NetworkConnectionHandler* network_connection_handler,
  35. NetworkActivationHandler* network_activation_handler,
  36. scoped_refptr<base::TaskRunner> task_runner) override {
  37. EXPECT_TRUE(activation_delegate);
  38. EXPECT_TRUE(network_state_handler);
  39. EXPECT_TRUE(network_connection_handler);
  40. EXPECT_TRUE(network_activation_handler);
  41. EXPECT_TRUE(task_runner);
  42. auto fake_ota_activator =
  43. std::make_unique<FakeOtaActivator>(std::move(on_finished_callback));
  44. created_instances_.push_back(fake_ota_activator.get());
  45. return fake_ota_activator;
  46. }
  47. std::vector<FakeOtaActivator*> created_instances_;
  48. };
  49. } // namespace
  50. class CellularSetupImplTest : public testing::Test {
  51. public:
  52. CellularSetupImplTest(const CellularSetupImplTest&) = delete;
  53. CellularSetupImplTest& operator=(const CellularSetupImplTest&) = delete;
  54. protected:
  55. CellularSetupImplTest() = default;
  56. ~CellularSetupImplTest() override = default;
  57. // testing::Test:
  58. void SetUp() override {
  59. OtaActivatorImpl::Factory::SetFactoryForTesting(
  60. &fake_ota_activator_factory_);
  61. }
  62. void TearDown() override {
  63. OtaActivatorImpl::Factory::SetFactoryForTesting(nullptr);
  64. }
  65. void CallStartActivation(FakeActivationDelegate* fake_activation_delegate) {
  66. size_t num_before_call = num_carrier_portal_handlers_received_;
  67. EXPECT_EQ(num_before_call,
  68. fake_ota_activator_factory_.created_instances().size());
  69. base::RunLoop run_loop;
  70. cellular_setup_.StartActivation(
  71. fake_activation_delegate->GenerateRemote(),
  72. base::BindOnce(&CellularSetupImplTest::OnCarrierPortalHandlerReceived,
  73. base::Unretained(this), run_loop.QuitClosure()));
  74. run_loop.Run();
  75. EXPECT_EQ(num_before_call + 1u, num_carrier_portal_handlers_received_);
  76. EXPECT_EQ(num_before_call + 1u,
  77. fake_ota_activator_factory_.created_instances().size());
  78. fake_ota_activator_factory_.created_instances()[num_before_call]
  79. ->InvokeOnFinishedCallback();
  80. }
  81. private:
  82. void OnCarrierPortalHandlerReceived(
  83. base::OnceClosure quit_closure,
  84. mojo::PendingRemote<mojom::CarrierPortalHandler> carrier_portal_handler) {
  85. ++num_carrier_portal_handlers_received_;
  86. std::move(quit_closure).Run();
  87. }
  88. base::test::TaskEnvironment task_environment_;
  89. NetworkHandlerTestHelper network_handler_test_helper_;
  90. FakeOtaActivatorFactory fake_ota_activator_factory_;
  91. CellularSetupImpl cellular_setup_;
  92. size_t num_carrier_portal_handlers_received_ = 0u;
  93. };
  94. TEST_F(CellularSetupImplTest, StartActivation_SingleAttempt) {
  95. auto fake_activation_delegate = std::make_unique<FakeActivationDelegate>();
  96. CallStartActivation(fake_activation_delegate.get());
  97. }
  98. TEST_F(CellularSetupImplTest, StartActivation_MultipleAttempts) {
  99. auto fake_activation_delegate_1 = std::make_unique<FakeActivationDelegate>();
  100. CallStartActivation(fake_activation_delegate_1.get());
  101. auto fake_activation_delegate_2 = std::make_unique<FakeActivationDelegate>();
  102. CallStartActivation(fake_activation_delegate_2.get());
  103. }
  104. } // namespace ash::cellular_setup