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

content / browser / screenlock_monitor / screenlock_monitor_device_source_win_unittest.cc [blame]

// Copyright 2021 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/screenlock_monitor/screenlock_monitor_device_source.h"

#include "base/test/task_environment.h"
#include "content/browser/screenlock_monitor/screenlock_monitor.h"
#include "content/browser/screenlock_monitor/screenlock_monitor_source.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/test/browser_task_environment.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace content {
namespace {

// TODO(crbug.com/40164163): These global state variables will likely lead to
// issues if multiple tests are run in parallel. Use caution if adding more
// tests to this file until crbug.com/1166275 is resolved.
static DWORD g_flag;
static HWND g_hwnd;

// These functions mock the Windows OS component that notifies subscribers
// when a session changes state (i.e. when it locks or unlocks).
// This is the mock for the WTSRegisterSessionNotification API.
bool FakeRegister(HWND hwnd, DWORD flag) {
  // Ensure only valid flags for WTSRegisterSessionNotification are used.
  if (flag != NOTIFY_FOR_ALL_SESSIONS && flag != NOTIFY_FOR_THIS_SESSION)
    return false;

  g_flag = flag;
  g_hwnd = hwnd;
  return true;
}

// Mocks the WTSUnRegisterSessionNotification API.
bool FakeUnregister(HWND hwnd) {
  if (hwnd != g_hwnd)
    return false;

  return true;
}

// If the recipient registered with the |NOTIFY_FOR_THIS_SESSION| flag, then
// we should only send events from this session.
bool ShouldSendEvent(DWORD session_id) {
  if (g_flag == NOTIFY_FOR_ALL_SESSIONS)
    return true;

  DWORD current_session_id;
  EXPECT_TRUE(ProcessIdToSessionId(GetCurrentProcessId(), ¤t_session_id));
  return session_id == current_session_id;
}

void GenerateFakeLockEvent(DWORD session_id) {
  if (!ShouldSendEvent(session_id))
    return;

  SendMessage(g_hwnd, WM_WTSSESSION_CHANGE, WTS_SESSION_LOCK, session_id);
}

void GenerateFakeUnlockEvent(DWORD session_id) {
  if (!ShouldSendEvent(session_id))
    return;

  SendMessage(g_hwnd, WM_WTSSESSION_CHANGE, WTS_SESSION_UNLOCK, session_id);
}
}  // namespace

class ScreenlockMonitorTestObserver : public ScreenlockObserver {
 public:
  ScreenlockMonitorTestObserver() : is_screen_locked_(false) {}
  ~ScreenlockMonitorTestObserver() override = default;

  // ScreenlockObserver callbacks.
  void OnScreenLocked() override { is_screen_locked_ = true; }
  void OnScreenUnlocked() override { is_screen_locked_ = false; }

  bool IsScreenLocked() { return is_screen_locked_; }

 private:
  bool is_screen_locked_;
};

TEST(ScreenlockMonitorDeviceSourceWinTest, FakeSessionNotifications) {
  content::BrowserTaskEnvironment task_environment;
  ASSERT_TRUE(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));

  ScreenlockMonitorDeviceSource::SetFakeNotificationAPIsForTesting(
      &FakeRegister, &FakeUnregister);
  ScreenlockMonitorDeviceSource* screenlock_monitor_source =
      new ScreenlockMonitorDeviceSource();
  auto screenlock_monitor = std::make_unique<ScreenlockMonitor>(
      std::unique_ptr<ScreenlockMonitorSource>(screenlock_monitor_source));
  ScreenlockMonitorTestObserver observer;
  screenlock_monitor->AddObserver(&observer);

  DWORD session_id;
  EXPECT_TRUE(ProcessIdToSessionId(GetCurrentProcessId(), &session_id));

  // |screenlock_monitor_source_| should ignore events from other sessions.
  GenerateFakeLockEvent(session_id + 1);
  base::RunLoop().RunUntilIdle();
  EXPECT_FALSE(observer.IsScreenLocked());

  GenerateFakeLockEvent(session_id);
  base::RunLoop().RunUntilIdle();
  EXPECT_TRUE(observer.IsScreenLocked());

  GenerateFakeUnlockEvent(session_id + 1);
  base::RunLoop().RunUntilIdle();
  EXPECT_TRUE(observer.IsScreenLocked());

  GenerateFakeUnlockEvent(session_id);
  base::RunLoop().RunUntilIdle();
  EXPECT_FALSE(observer.IsScreenLocked());
}

// CAUTION: adding another test here will likely cause issues. See the comment
// at the top of this file, or crbug.com/1166275

}  // namespace content