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
  113
  114
  115
  116
  117
  118
  119
  120
  121
  122
  123
  124
  125
  126
  127
  128
  129
  130
  131
  132
  133
  134
  135
  136

ash / system / network / vpn_detailed_view_pixeltest.cc [blame]

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

#include <utility>
#include <vector>

#include "ash/system/network/tray_network_state_model.h"
#include "ash/system/network/vpn_detailed_view.h"
#include "ash/system/unified/quick_settings_view.h"
#include "ash/system/unified/unified_system_tray.h"
#include "ash/system/unified/unified_system_tray_bubble.h"
#include "ash/test/ash_test_base.h"
#include "ash/test/pixel/ash_pixel_differ.h"
#include "ash/test/pixel/ash_pixel_test_init_params.h"
#include "base/memory/raw_ptr.h"
#include "chromeos/services/network_config/public/mojom/cros_network_config.mojom.h"
#include "chromeos/services/network_config/public/mojom/network_types.mojom.h"
#include "ui/views/view.h"
#include "ui/views/view_utils.h"

namespace ash {

using chromeos::network_config::mojom::ConnectionStateType;
using chromeos::network_config::mojom::NetworkStateProperties;
using chromeos::network_config::mojom::NetworkStatePropertiesPtr;
using chromeos::network_config::mojom::NetworkType;
using chromeos::network_config::mojom::NetworkTypeStateProperties;
using chromeos::network_config::mojom::VpnProvider;
using chromeos::network_config::mojom::VpnProviderPtr;
using chromeos::network_config::mojom::VPNStateProperties;
using chromeos::network_config::mojom::VPNStatePropertiesPtr;
using chromeos::network_config::mojom::VpnType;

constexpr char kArcProviderAppId[] = "arc_provider_app_id";
constexpr char kArcProviderId[] = "arc_provider_id";
constexpr char kArcProviderName[] = "arc_provider_name";
constexpr char kExtensionProviderAppId[] = "extension_provider_app_id";
constexpr char kExtensionProviderId[] = "extension_provider_id";
constexpr char kExtensionProviderName[] = "extension_provider_name";

// Pixel test for the VPN list that is shown in the quick settings VPN sub-page.
class VpnDetailedViewPixelTest : public AshTestBase {
 public:
  VpnDetailedViewPixelTest() = default;

  // AshTestBase:
  void SetUp() override {
    AshTestBase::SetUp();

    // Show the detailed view.
    UnifiedSystemTray* system_tray = GetPrimaryUnifiedSystemTray();
    system_tray->ShowBubble();
    ASSERT_TRUE(system_tray->bubble());
    system_tray->bubble()
        ->unified_system_tray_controller()
        ->ShowVPNDetailedView();

    TrayDetailedView* detailed_view =
        system_tray->bubble()
            ->quick_settings_view()
            ->GetDetailedViewForTest<TrayDetailedView>();
    ASSERT_TRUE(detailed_view);
    ASSERT_TRUE(views::IsViewClass<VpnDetailedView>(detailed_view));
    vpn_detailed_view_ = static_cast<VpnDetailedView*>(detailed_view);
  }

  std::optional<pixel_test::InitParams> CreatePixelTestInitParams()
      const override {
    return pixel_test::InitParams();
  }

  void AddBuiltInProvider() {
    // Updating the list of third-party providers adds the built-in provider,
    // even if that list is empty.
    vpn_detailed_view_->model()->vpn_list()->SetVpnProvidersForTest({});
    vpn_detailed_view_->OnGetNetworkStateList({});
  }

  void AddMultipleProvidersAndNetwork() {
    std::vector<VpnProviderPtr> providers;
    // Add an extension provider.
    VpnProviderPtr provider = VpnProvider::New();
    provider->type = VpnType::kExtension;
    provider->provider_name = kExtensionProviderName;
    provider->provider_id = kExtensionProviderId;
    provider->app_id = kExtensionProviderAppId;
    providers.push_back(std::move(provider));
    // Add an ARC provider.
    provider = VpnProvider::New();
    provider->type = VpnType::kArc;
    provider->provider_name = kArcProviderName;
    provider->provider_id = kArcProviderId;
    provider->app_id = kArcProviderAppId;
    providers.push_back(std::move(provider));
    vpn_detailed_view_->model()->vpn_list()->SetVpnProvidersForTest(
        std::move(providers));

    // Add a network just for the extension provider.
    NetworkStatePropertiesPtr network = NetworkStateProperties::New();
    network->guid = "vpn_id";
    network->name = "vpn_name";
    network->type = NetworkType::kVPN;
    network->connection_state = ConnectionStateType::kNotConnected;
    VPNStatePropertiesPtr vpn = VPNStateProperties::New();
    vpn->type = VpnType::kExtension;
    vpn->provider_name = kExtensionProviderName;
    vpn->provider_id = kExtensionProviderId;
    network->type_state = NetworkTypeStateProperties::NewVpn(std::move(vpn));
    std::vector<NetworkStatePropertiesPtr> networks;
    networks.push_back(std::move(network));
    vpn_detailed_view_->OnGetNetworkStateList(std::move(networks));
  }

  raw_ptr<VpnDetailedView, DanglingUntriaged> vpn_detailed_view_ = nullptr;
};

TEST_F(VpnDetailedViewPixelTest, OnlyBuiltInVpn) {
  AddBuiltInProvider();

  // Compare pixels.
  EXPECT_TRUE(GetPixelDiffer()->CompareUiComponentsOnPrimaryScreen(
      "check_view",
      /*revision_number=*/11, vpn_detailed_view_));
}

TEST_F(VpnDetailedViewPixelTest, MultipleVpns) {
  AddMultipleProvidersAndNetwork();

  // Compare pixels.
  EXPECT_TRUE(GetPixelDiffer()->CompareUiComponentsOnPrimaryScreen(
      "check_view",
      /*revision_number=*/11, vpn_detailed_view_));
}

}  // namespace ash