chromium/content/browser/network/sandboxed_nqe_browsertest.cc

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

#include <vector>

#include "base/containers/contains.h"
#include "base/run_loop.h"
#include "base/test/scoped_feature_list.h"
#include "build/build_config.h"
#include "content/browser/network/network_service_util_internal.h"
#include "content/public/browser/network_service_instance.h"
#include "content/public/browser/network_service_util.h"
#include "content/public/common/content_features.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/content_browser_test.h"
#include "mojo/public/cpp/bindings/sync_call_restrictions.h"
#include "sandbox/policy/features.h"
#include "services/network/public/cpp/network_quality_tracker.h"
#include "services/network/public/mojom/network_service.mojom.h"
#include "services/network/public/mojom/network_service_test.mojom.h"

#if BUILDFLAG(IS_ANDROID)
#include "net/android/network_change_notifier_delegate_android.h"
#include "net/android/network_library.h"
#endif

namespace content {
namespace {

std::ostream& operator<<(
    std::ostream& os,
    const std::vector<net::EffectiveConnectionType>& types) {}

class TestNetworkQualityObserver
    : public network::NetworkQualityTracker::EffectiveConnectionTypeObserver {};

class SandboxedNQEBrowserTest : public ContentBrowserTest {};

// NetworkQualityEstimater used to call android syscall to gather device info
// in the constructor, which has been removed. This test confirms that.
IN_PROC_BROWSER_TEST_F(SandboxedNQEBrowserTest, GetNetworkService) {}

#if BUILDFLAG(IS_WIN)
#define MAYBE_NetworkQualityTracker
#else
#define MAYBE_NetworkQualityTracker
#endif
// Simulate EffectiveConnectionType change in NetworkQualityEstimator and
// reports it to mojo client.
IN_PROC_BROWSER_TEST_F(SandboxedNQEBrowserTest, MAYBE_NetworkQualityTracker) {}

#if BUILDFLAG(IS_ANDROID)
// Turn on/off Wifi on Android and listen it in the network service.
IN_PROC_BROWSER_TEST_F(SandboxedNQEBrowserTest, TurnWifiEnabled) {
  const std::string wifi_ssid = net::android::GetWifiSSID();
  if (wifi_ssid.empty()) {
    GTEST_SKIP() << "This test requires wifi network.";
  }
  // Let NetworkQualityEstimator reports NetworkChangeNotifier::CONNECTION_WIFI
  // as EFFECTIVE_CONNECTION_TYPE_SLOW_2G since EffectiveConnectionType and
  // the production receivers doesn't notice Wifi.
  ForceNetworkQualityEstimatorReportWifiAsSlow2G();
  net::NetworkChangeNotifierDelegateAndroid::
      EnableNetworkChangeNotifierAutoDetectForTest();

  std::unique_ptr<network::NetworkQualityTracker> tracker =
      std::make_unique<network::NetworkQualityTracker>(
          base::BindRepeating(&GetNetworkService));
  TestNetworkQualityObserver network_quality_observer;
  tracker->AddEffectiveConnectionTypeObserver(&network_quality_observer);

  net::android::SetWifiEnabledForTesting(true);
  network_quality_observer.WaitForNotification(
      net::EFFECTIVE_CONNECTION_TYPE_SLOW_2G);

  net::android::SetWifiEnabledForTesting(false);
  network_quality_observer.WaitForNotification(
      net::EFFECTIVE_CONNECTION_TYPE_4G);

  net::android::SetWifiEnabledForTesting(true);
  network_quality_observer.WaitForNotification(
      net::EFFECTIVE_CONNECTION_TYPE_SLOW_2G);
}
#endif

}  // namespace
}  // namespace content