chromium/chrome/browser/media/router/discovery/access_code/access_code_cast_sink_service_unittest.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 "chrome/browser/media/router/discovery/access_code/access_code_cast_sink_service.h"

#include <string>
#include <utility>
#include <vector>

#include "base/json/values_util.h"
#include "base/run_loop.h"
#include "base/test/bind.h"
#include "base/test/gmock_callback_support.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/test/mock_callback.h"
#include "base/test/scoped_feature_list.h"
#include "base/test/test_future.h"
#include "chrome/browser/media/router/chrome_media_router_factory.h"
#include "chrome/browser/media/router/discovery/access_code/access_code_cast_feature.h"
#include "chrome/browser/media/router/discovery/access_code/access_code_cast_pref_updater.h"
#include "chrome/browser/media/router/discovery/access_code/access_code_media_sink_util.h"
#include "chrome/browser/media/router/discovery/access_code/access_code_test_util.h"
#include "chrome/browser/media/router/discovery/discovery_network_monitor.h"
#include "chrome/browser/media/router/discovery/mdns/cast_media_sink_service_impl.h"
#include "chrome/browser/media/router/discovery/mdns/cast_media_sink_service_test_helpers.h"
#include "chrome/browser/media/router/discovery/mdns/media_sink_util.h"
#include "chrome/browser/media/router/discovery/media_sink_discovery_metrics.h"
#include "chrome/browser/media/router/media_router_feature.h"
#include "chrome/browser/media/router/test/provider_test_helpers.h"
#include "chrome/browser/prefs/browser_prefs.h"
#include "chrome/common/pref_names.h"
#include "chrome/test/base/testing_browser_process.h"
#include "chrome/test/base/testing_profile.h"
#include "components/media_router/browser/media_router_factory.h"
#include "components/media_router/browser/test/mock_media_router.h"
#include "components/media_router/common/discovery/media_sink_service_base.h"
#include "components/media_router/common/providers/cast/channel/cast_socket.h"
#include "components/media_router/common/providers/cast/channel/cast_socket_service.h"
#include "components/media_router/common/providers/cast/channel/cast_test_util.h"
#include "components/media_router/common/test/test_helper.h"
#include "components/sync_preferences/testing_pref_service_syncable.h"
#include "content/public/test/browser_task_environment.h"
#include "content/public/test/test_utils.h"
#include "services/network/test/test_network_connection_tracker.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

_;
Eq;
Return;

namespace media_router {

SinkSource;
MockAddSinkResultCallback;
DiscoveryDevice;

// This delay is needed because MediaNotificationService waits for
// `kExpirationDelay` before it calls the CastMediaSinkServiceImpl to
// disconnect/remove sinks. `kExpirationDelay` is not used here because
// sometimes there are two delayed tasks posted.
static constexpr base::TimeDelta kRemoveRouteDelay =;

class AccessCodeCastSinkServiceTest : public testing::Test {};

// static
const std::vector<DiscoveryNetworkInfo>
    AccessCodeCastSinkServiceTest::fake_ethernet_info_ =;
// static
const std::vector<DiscoveryNetworkInfo>
    AccessCodeCastSinkServiceTest::fake_wifi_info_ =;
// static
const std::vector<DiscoveryNetworkInfo>
    AccessCodeCastSinkServiceTest::fake_unknown_info_ =;

// static
std::vector<DiscoveryNetworkInfo>
    AccessCodeCastSinkServiceTest::fake_network_info_ =;

TEST_F(AccessCodeCastSinkServiceTest,
       AccessCodeCastDeviceRemovedAfterRouteEndsExpirationEnabled) {}

TEST_F(AccessCodeCastSinkServiceTest, AddExistingSinkToMediaRouter) {}

TEST_F(AccessCodeCastSinkServiceTest, AddNewSinkToMediaRouter) {}

TEST_F(AccessCodeCastSinkServiceTest, DiscoveryDeviceMissingWithOk) {}

TEST_F(AccessCodeCastSinkServiceTest, ValidDiscoveryDeviceAndCode) {}

TEST_F(AccessCodeCastSinkServiceTest, InvalidDiscoveryDevice) {}

TEST_F(AccessCodeCastSinkServiceTest, NonOKResultCode) {}

TEST_F(AccessCodeCastSinkServiceTest, OnChannelOpenedSuccess) {}

TEST_F(AccessCodeCastSinkServiceTest, OnChannelOpenedFailure) {}

TEST_F(AccessCodeCastSinkServiceTest, SinkDoesntExistForPrefs) {}

TEST_F(AccessCodeCastSinkServiceTest, TestFetchAndAddStoredDevices) {}

TEST_F(AccessCodeCastSinkServiceTest, TestChangeNetworksExpiration) {}

TEST_F(AccessCodeCastSinkServiceTest, TestChangeNetworksNoExpiration) {}

TEST_F(AccessCodeCastSinkServiceTest,
       TestAddInvalidDevicesNoMediaSinkInternal) {}

TEST_F(AccessCodeCastSinkServiceTest, TestCalculateDurationTillExpiration) {}

TEST_F(AccessCodeCastSinkServiceTest, TestSetExpirationTimer) {}

TEST_F(AccessCodeCastSinkServiceTest, TestResetExpirationTimersNetworkChange) {}

TEST_F(AccessCodeCastSinkServiceTest, TestResetExpirationTimersShutdown) {}

TEST_F(AccessCodeCastSinkServiceTest, TestChangeEnabledPref) {}

TEST_F(AccessCodeCastSinkServiceTest, TestChangeDurationPref) {}

TEST_F(AccessCodeCastSinkServiceTest, TestChangeNetworkWithRouteActive) {}

TEST_F(AccessCodeCastSinkServiceTest,
       TestChangeNetworkWithRouteActiveExpiration) {}

TEST_F(AccessCodeCastSinkServiceTest, DiscoverSinkWithNoMediaRouter) {}

TEST_F(AccessCodeCastSinkServiceTest,
       TestCheckMediaSinkForExpirationNoExpiration) {}

TEST_F(AccessCodeCastSinkServiceTest,
       TestCheckMediaSinkForExpirationBeforeDelay) {}

TEST_F(AccessCodeCastSinkServiceTest,
       TestCheckMediaSinkForExpirationAfterDelay) {}

TEST_F(AccessCodeCastSinkServiceTest, TestOfflineDiscoverSink) {}

TEST_F(AccessCodeCastSinkServiceTest, RefreshStoredDeviceInfo) {}

TEST_F(AccessCodeCastSinkServiceTest, RefreshExistingDeviceName) {}

TEST_F(AccessCodeCastSinkServiceTest, RefreshStoredDeviceTimer) {}

TEST_F(AccessCodeCastSinkServiceTest, HandleMediaRouteAdded) {}

TEST_F(AccessCodeCastSinkServiceTest, RecordRouteDuration) {}

TEST_F(AccessCodeCastSinkServiceTest, RecordRouteDurationNonAccessCodeDevice) {}

TEST_F(AccessCodeCastSinkServiceTest, RestartExpirationTimerDoesntResetTimer) {}

TEST_F(AccessCodeCastSinkServiceTest, AddRouteCallsHandleMediaRoute) {}

TEST_F(AccessCodeCastSinkServiceTest, InitializePrefUpdater) {}

#if BUILDFLAG(IS_CHROMEOS_ASH)
TEST_F(AccessCodeCastSinkServiceTest, OnDevicesPrefChange) {
  auto cast_sink = CreateCastSink(1);
  base::Value::Dict devices_dict;
  devices_dict.Set(cast_sink.id(),
                   CreateValueDictFromMediaSinkInternal(cast_sink));
  static_cast<MockAccessCodeCastPrefUpdater*>(pref_updater())
      ->set_devices_dict(std::move(devices_dict));

  ExpectOpenChannels({cast_sink}, 1);
  ExpectHasSink({cast_sink}, 1);

  // We don't need to actually store the devices in the pref service
  // since we are using MockAccessCodeCastPrefUpdater, which does not use the
  // pref service. We only need to modify the pref service so that
  // AccessCodeCastSinkService is notified.
  GetTestingPrefs()->SetDict(prefs::kAccessCodeCastDevices,
                             base::Value::Dict());
  task_environment_.RunUntilIdle();

  // if there's no new device in the pref service, the access code cast sink
  // service shouldn't attempt to open channels to existing sinks.
  ExpectOpenChannels({cast_sink}, 0);
  ExpectHasSink({cast_sink}, 0);
  GetTestingPrefs()->SetDict(prefs::kAccessCodeCastDevices,
                             base::Value::Dict());
  task_environment_.RunUntilIdle();
}
#endif

}  // namespace media_router