// Copyright 2018 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/scheduler/responsiveness/native_event_observer.h"

#include "base/functional/callback_helpers.h"
#include "base/memory/raw_ptr.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/content_browser_test.h"
#include "ui/events/test/cocoa_test_event_utils.h"

#import <Carbon/Carbon.h>

namespace content::responsiveness {

namespace {

class FakeNativeEventObserver : public BrowserUINativeEventObserver {
 public:
  FakeNativeEventObserver()
      : BrowserUINativeEventObserver(base::DoNothing(), base::DoNothing()) {}
  ~FakeNativeEventObserver() override = default;

  void WillRunNativeEvent(uintptr_t opaque_identifier) override {
    ASSERT_FALSE(will_run_id_);
    will_run_id_ = opaque_identifier;
  }
  void DidRunNativeEvent(uintptr_t opaque_identifier) override {
    ASSERT_FALSE(did_run_id_);
    did_run_id_ = opaque_identifier;
  }

  uintptr_t will_run_id() { return will_run_id_; }
  uintptr_t did_run_id() { return did_run_id_; }

 private:
  uintptr_t will_run_id_ = 0;
  uintptr_t did_run_id_ = 0;
};

}  // namespace

class ResponsivenessNativeEventObserverBrowserTest : public ContentBrowserTest {
};

IN_PROC_BROWSER_TEST_F(ResponsivenessNativeEventObserverBrowserTest,
                       EventForwarding) {
  FakeNativeEventObserver observer;

  EXPECT_FALSE(observer.will_run_id());
  EXPECT_FALSE(observer.did_run_id());
  NSEvent* event = cocoa_test_event_utils::KeyEventWithKeyCode(
      kVK_Return, '\r', NSEventTypeKeyDown, 0);
  [NSApp sendEvent:event];

  EXPECT_EQ(observer.will_run_id(), reinterpret_cast<uintptr_t>((__bridge void*)event));
  EXPECT_EQ(observer.did_run_id(), reinterpret_cast<uintptr_t>((__bridge void*)event));
}

}  // namespace content::responsiveness
