123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134 |
- // Copyright 2019 The Chromium Authors. All rights reserved.
- // Use of this source code is governed by a BSD-style license that can be
- // found in the LICENSE file.
- #include "ash/services/cellular_setup/cellular_setup_impl.h"
- #include <memory>
- #include <utility>
- #include <vector>
- #include "ash/services/cellular_setup/cellular_setup_base.h"
- #include "ash/services/cellular_setup/fake_ota_activator.h"
- #include "ash/services/cellular_setup/ota_activator_impl.h"
- #include "ash/services/cellular_setup/public/cpp/fake_activation_delegate.h"
- #include "base/run_loop.h"
- #include "base/test/task_environment.h"
- #include "chromeos/ash/components/network/network_handler_test_helper.h"
- #include "mojo/public/cpp/bindings/pending_remote.h"
- #include "testing/gtest/include/gtest/gtest.h"
- namespace ash::cellular_setup {
- namespace {
- class FakeOtaActivatorFactory : public OtaActivatorImpl::Factory {
- public:
- FakeOtaActivatorFactory() = default;
- FakeOtaActivatorFactory(const FakeOtaActivatorFactory&) = delete;
- FakeOtaActivatorFactory& operator=(const FakeOtaActivatorFactory&) = delete;
- ~FakeOtaActivatorFactory() override = default;
- std::vector<FakeOtaActivator*>& created_instances() {
- return created_instances_;
- }
- private:
- // OtaActivatorImpl::Factory:
- std::unique_ptr<OtaActivator> CreateInstance(
- mojo::PendingRemote<mojom::ActivationDelegate> activation_delegate,
- base::OnceClosure on_finished_callback,
- NetworkStateHandler* network_state_handler,
- NetworkConnectionHandler* network_connection_handler,
- NetworkActivationHandler* network_activation_handler,
- scoped_refptr<base::TaskRunner> task_runner) override {
- EXPECT_TRUE(activation_delegate);
- EXPECT_TRUE(network_state_handler);
- EXPECT_TRUE(network_connection_handler);
- EXPECT_TRUE(network_activation_handler);
- EXPECT_TRUE(task_runner);
- auto fake_ota_activator =
- std::make_unique<FakeOtaActivator>(std::move(on_finished_callback));
- created_instances_.push_back(fake_ota_activator.get());
- return fake_ota_activator;
- }
- std::vector<FakeOtaActivator*> created_instances_;
- };
- } // namespace
- class CellularSetupImplTest : public testing::Test {
- public:
- CellularSetupImplTest(const CellularSetupImplTest&) = delete;
- CellularSetupImplTest& operator=(const CellularSetupImplTest&) = delete;
- protected:
- CellularSetupImplTest() = default;
- ~CellularSetupImplTest() override = default;
- // testing::Test:
- void SetUp() override {
- OtaActivatorImpl::Factory::SetFactoryForTesting(
- &fake_ota_activator_factory_);
- }
- void TearDown() override {
- OtaActivatorImpl::Factory::SetFactoryForTesting(nullptr);
- }
- void CallStartActivation(FakeActivationDelegate* fake_activation_delegate) {
- size_t num_before_call = num_carrier_portal_handlers_received_;
- EXPECT_EQ(num_before_call,
- fake_ota_activator_factory_.created_instances().size());
- base::RunLoop run_loop;
- cellular_setup_.StartActivation(
- fake_activation_delegate->GenerateRemote(),
- base::BindOnce(&CellularSetupImplTest::OnCarrierPortalHandlerReceived,
- base::Unretained(this), run_loop.QuitClosure()));
- run_loop.Run();
- EXPECT_EQ(num_before_call + 1u, num_carrier_portal_handlers_received_);
- EXPECT_EQ(num_before_call + 1u,
- fake_ota_activator_factory_.created_instances().size());
- fake_ota_activator_factory_.created_instances()[num_before_call]
- ->InvokeOnFinishedCallback();
- }
- private:
- void OnCarrierPortalHandlerReceived(
- base::OnceClosure quit_closure,
- mojo::PendingRemote<mojom::CarrierPortalHandler> carrier_portal_handler) {
- ++num_carrier_portal_handlers_received_;
- std::move(quit_closure).Run();
- }
- base::test::TaskEnvironment task_environment_;
- NetworkHandlerTestHelper network_handler_test_helper_;
- FakeOtaActivatorFactory fake_ota_activator_factory_;
- CellularSetupImpl cellular_setup_;
- size_t num_carrier_portal_handlers_received_ = 0u;
- };
- TEST_F(CellularSetupImplTest, StartActivation_SingleAttempt) {
- auto fake_activation_delegate = std::make_unique<FakeActivationDelegate>();
- CallStartActivation(fake_activation_delegate.get());
- }
- TEST_F(CellularSetupImplTest, StartActivation_MultipleAttempts) {
- auto fake_activation_delegate_1 = std::make_unique<FakeActivationDelegate>();
- CallStartActivation(fake_activation_delegate_1.get());
- auto fake_activation_delegate_2 = std::make_unique<FakeActivationDelegate>();
- CallStartActivation(fake_activation_delegate_2.get());
- }
- } // namespace ash::cellular_setup
|