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
  102
  103
  104
  105
  106
  107
  108
  109
  110
  111
  112

ash / components / arc / net / arc_wifi_host_impl_unittests.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/components/arc/net/arc_wifi_host_impl.h"

#include <string>

#include "ash/components/arc/session/arc_bridge_service.h"
#include "ash/components/arc/session/arc_service_manager.h"
#include "base/test/task_environment.h"
#include "base/test/test_future.h"
#include "chromeos/ash/components/dbus/hermes/hermes_euicc_client.h"
#include "chromeos/ash/components/dbus/hermes/hermes_manager_client.h"
#include "chromeos/ash/components/dbus/hermes/hermes_profile_client.h"
#include "chromeos/ash/components/dbus/shill/shill_clients.h"
#include "chromeos/ash/components/dbus/shill/shill_manager_client.h"
#include "chromeos/ash/components/network/fake_network_state_handler.h"
#include "chromeos/ash/components/network/network_handler.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/cros_system_api/dbus/service_constants.h"

namespace arc {
namespace {
class ArcWifiHostImplTest : public testing::Test {
 public:
  ArcWifiHostImplTest(const ArcWifiHostImplTest&) = delete;
  ArcWifiHostImplTest& operator=(const ArcWifiHostImplTest&) = delete;

  ash::FakeNetworkStateHandler* GetStateHandler() {
    return static_cast<ash::FakeNetworkStateHandler*>(
        ash::NetworkHandler::Get()->network_state_handler());
  }

 protected:
  ArcWifiHostImplTest()
      : task_environment_(base::test::TaskEnvironment::TimeSource::MOCK_TIME) {}

  ~ArcWifiHostImplTest() override = default;

  void SetUp() override {
    // Required for initializing NetworkHandler.
    ash::HermesProfileClient::InitializeFake();
    ash::HermesManagerClient::InitializeFake();
    ash::HermesEuiccClient::InitializeFake();

    // Required for initializingFakeShillManagerClient.
    ash::shill_clients::InitializeFakes();
    ash::ShillManagerClient::Get()
        ->GetTestInterface()
        ->SetupDefaultEnvironment();

    ash::NetworkHandler::Initialize();

    bridge_service_ = std::make_unique<ArcBridgeService>();
    service_ =
        std::make_unique<ArcWifiHostImpl>(nullptr, bridge_service_.get());
    base::RunLoop().RunUntilIdle();
  }

  void TearDown() override {
    service_->Shutdown();
    ash::NetworkHandler::Shutdown();
    ash::HermesEuiccClient::Shutdown();
    ash::HermesManagerClient::Shutdown();
    ash::HermesProfileClient::Shutdown();
    ash::shill_clients::Shutdown();
  }

  ArcWifiHostImpl* service() { return service_.get(); }

 private:
  base::test::TaskEnvironment task_environment_;
  std::unique_ptr<ArcBridgeService> bridge_service_;
  std::unique_ptr<ArcWifiHostImpl> service_;
};

TEST_F(ArcWifiHostImplTest, ToggleWifiEnabledState) {
  base::test::TestFuture<bool> future;
  // Enable WiFi and get WiFi state.
  service()->SetWifiEnabledState(true, future.GetCallback());
  ASSERT_TRUE(future.Take());
  service()->GetWifiEnabledState(future.GetCallback());
  ASSERT_TRUE(future.Take());

  // Disable WiFi and get WiFi state.
  service()->SetWifiEnabledState(false, future.GetCallback());
  ASSERT_TRUE(future.Take());
  service()->GetWifiEnabledState(future.GetCallback());
  ASSERT_FALSE(future.Take());
}

TEST_F(ArcWifiHostImplTest, OnConnectionReadyAndClosed) {
  GetStateHandler()->ClearObserverList();
  EXPECT_TRUE(GetStateHandler()->ObserverListEmpty());
  service()->OnConnectionReady();
  EXPECT_FALSE(GetStateHandler()->ObserverListEmpty());
  service()->OnConnectionClosed();
  EXPECT_TRUE(GetStateHandler()->ObserverListEmpty());
}

TEST_F(ArcWifiHostImplTest, OnShuttingDown) {
  GetStateHandler()->ClearObserverList();
  EXPECT_TRUE(GetStateHandler()->ObserverListEmpty());
  service()->OnConnectionReady();
  EXPECT_FALSE(GetStateHandler()->ObserverListEmpty());
  service()->OnShuttingDown();
  EXPECT_TRUE(GetStateHandler()->ObserverListEmpty());
}
}  // namespace
}  // namespace arc