chromium/content/browser/media/system_media_controls_notifier_unittest.cc

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

#include "content/browser/media/system_media_controls_notifier.h"

#include <memory>
#include <utility>

#include "base/strings/utf_string_conversions.h"
#include "build/build_config.h"
#include "components/system_media_controls/mock_system_media_controls.h"
#include "content/public/test/browser_task_environment.h"
#include "content/public/test/test_media_session_client.h"
#include "services/media_session/public/mojom/media_session.mojom.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

#if BUILDFLAG(IS_WIN)
#include "ui/base/idle/scoped_set_idle_state.h"
#endif  // BUILDFLAG(IS_WIN)

namespace {
std::u16string hidden_metadata_placeholder_title =;
std::u16string hidden_metadata_placeholder_artist =;
std::u16string hidden_metadata_placeholder_album =;
int hidden_metadata_placeholder_thumbnail_size =;
}  // namespace

namespace content {

MediaPlaybackState;
MediaSessionInfo;
MediaSessionInfoPtr;
PlaybackStatus;
_;
Expectation;
WithArg;

class SystemMediaControlsNotifierTest : public testing::Test {};

TEST_F(SystemMediaControlsNotifierTest, ProperlyUpdatesPlaybackState) {}

TEST_F(SystemMediaControlsNotifierTest, ProperlyDebouncesPlaybackState) {}

TEST_F(SystemMediaControlsNotifierTest, StopClearsPendingPlaybackState) {}

TEST_F(SystemMediaControlsNotifierTest, ProperlyUpdatesMetadata) {}

TEST_F(SystemMediaControlsNotifierTest, ProperlyUpdatesNullMetadata) {}

TEST_F(SystemMediaControlsNotifierTest, ProperlyDebouncesMetadataUpdates) {}

TEST_F(SystemMediaControlsNotifierTest, ProperlyUpdatesMetadaBetweenDebounces) {}

TEST_F(SystemMediaControlsNotifierTest, EmptyMetadataClearsPendingMetadata) {}

TEST_F(SystemMediaControlsNotifierTest, ProperlyUpdatesPosition) {}

TEST_F(SystemMediaControlsNotifierTest, ProperlyHandlesNullPosition) {}

TEST_F(SystemMediaControlsNotifierTest, ProperlyDebouncesPositionUpdates) {}

TEST_F(SystemMediaControlsNotifierTest,
       ProperlyUpdatesPositionBetweenDebounces) {}

TEST_F(SystemMediaControlsNotifierTest, NullPositionClearsPendingPosition) {}

TEST_F(SystemMediaControlsNotifierTest, ProperlyUpdatesImage) {}

TEST_F(SystemMediaControlsNotifierTest, ProperlyDebouncesImage) {}

TEST_F(SystemMediaControlsNotifierTest, ProperlyUpdatesIsSeekTooEnabled) {}

TEST_F(SystemMediaControlsNotifierTest, ProperlyDebouncesIsSeekTooEnabled) {}

TEST_F(SystemMediaControlsNotifierTest, ProperlyUpdatesID) {}

TEST_F(SystemMediaControlsNotifierTest, DontHideMediaMetadataIfNotNeeded) {}

TEST_F(SystemMediaControlsNotifierTest, HideMediaMetadataIfNeeded) {}

TEST_F(SystemMediaControlsNotifierTest, HideMediaImageIfNeeded) {}

#if BUILDFLAG(IS_WIN)
TEST_F(SystemMediaControlsNotifierTest, DisablesOnLockAndEnablesOnUnlock) {
  EXPECT_CALL(mock_system_media_controls(), SetEnabled(false));

  {
    // Lock the screen.
    ui::ScopedSetIdleState locked(ui::IDLE_STATE_LOCKED);

    // Make sure that the lock polling timer is running and then force it to
    // fire so that we don't need to wait. This should disable the service.
    EXPECT_TRUE(lock_polling_timer().IsRunning());
    lock_polling_timer().user_task().Run();
  }

  // Ensure that the service was disabled.
  testing::Mock::VerifyAndClearExpectations(&mock_system_media_controls());

  // The service should be reenabled on unlock.
  EXPECT_CALL(mock_system_media_controls(), SetEnabled(true));

  {
    // Unlock the screen.
    ui::ScopedSetIdleState unlocked(ui::IDLE_STATE_ACTIVE);

    // Make sure that the lock polling timer is running and then force it to
    // fire so that we don't need to wait. This should enable the service.
    EXPECT_TRUE(lock_polling_timer().IsRunning());
    lock_polling_timer().user_task().Run();
  }
}

TEST_F(SystemMediaControlsNotifierTest, DoesNotDisableOnLockWhenPlaying) {
  EXPECT_CALL(mock_system_media_controls(), SetEnabled(_)).Times(0);

  SimulatePlaying();

  // Lock the screen.
  ui::ScopedSetIdleState locked(ui::IDLE_STATE_LOCKED);

  // Make sure that the lock polling timer is running and then force it to
  // fire so that we don't need to wait. This should not disable the service.
  EXPECT_TRUE(lock_polling_timer().IsRunning());
  lock_polling_timer().user_task().Run();
}

TEST_F(SystemMediaControlsNotifierTest, DisablesAfterPausingOnLockScreen) {
  Expectation playing =
      EXPECT_CALL(mock_system_media_controls(),
                  SetPlaybackStatus(PlaybackStatus::kPlaying));
  Expectation paused = EXPECT_CALL(mock_system_media_controls(),
                                   SetPlaybackStatus(PlaybackStatus::kPaused))
                           .After(playing);
  EXPECT_CALL(mock_system_media_controls(), SetEnabled(false)).After(paused);

  SimulatePlaying();
  metadata_update_timer().FireNow();

  // Lock the screen.
  ui::ScopedSetIdleState locked(ui::IDLE_STATE_LOCKED);

  // Make sure that the lock polling timer is running and then force it to
  // fire so that we don't need to wait. This should not disable the service.
  EXPECT_TRUE(lock_polling_timer().IsRunning());
  lock_polling_timer().user_task().Run();

  // Since we're playing, the timer to hide the SMTC should not be running.
  EXPECT_FALSE(hide_smtc_timer().IsRunning());

  SimulatePaused();
  metadata_update_timer().FireNow();

  // Now that we're paused, the timer to hide the SMTC should be running.
  EXPECT_TRUE(hide_smtc_timer().IsRunning());

  // Force the timer to fire now. This should disable the service.
  hide_smtc_timer().FireNow();
}
#endif  // BUILDFLAG(IS_WIN)

}  // namespace content