blob: 03f87713cf4a2ebd4cef4b9b79d1541866060fe6 [file] [log] [blame]
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "core/timing/PerformanceObserver.h"
#include "bindings/core/v8/V8BindingForTesting.h"
#include "bindings/core/v8/v8_performance_observer_callback.h"
#include "core/dom/ExecutionContext.h"
#include "core/timing/Performance.h"
#include "core/timing/PerformanceBase.h"
#include "core/timing/PerformanceMark.h"
#include "core/timing/PerformanceObserverInit.h"
#include "public/platform/TaskType.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace blink {
class MockPerformanceBase : public PerformanceBase {
public:
explicit MockPerformanceBase(ScriptState* script_state)
: PerformanceBase(0,
ExecutionContext::From(script_state)
->GetTaskRunner(TaskType::kPerformanceTimeline)) {}
~MockPerformanceBase() = default;
ExecutionContext* GetExecutionContext() const override { return nullptr; }
};
class PerformanceObserverTest : public ::testing::Test {
protected:
void Initialize(ScriptState* script_state) {
v8::Local<v8::Function> callback =
v8::Function::New(script_state->GetContext(), nullptr).ToLocalChecked();
base_ = new MockPerformanceBase(script_state);
cb_ = V8PerformanceObserverCallback::Create(callback);
observer_ = new PerformanceObserver(ExecutionContext::From(script_state),
base_, cb_);
}
bool IsRegistered() { return observer_->is_registered_; }
int NumPerformanceEntries() { return observer_->performance_entries_.size(); }
void Deliver() { observer_->Deliver(); }
Persistent<MockPerformanceBase> base_;
Persistent<V8PerformanceObserverCallback> cb_;
Persistent<PerformanceObserver> observer_;
};
TEST_F(PerformanceObserverTest, Observe) {
V8TestingScope scope;
Initialize(scope.GetScriptState());
NonThrowableExceptionState exception_state;
PerformanceObserverInit options;
Vector<String> entry_type_vec;
entry_type_vec.push_back("mark");
options.setEntryTypes(entry_type_vec);
observer_->observe(options, exception_state);
EXPECT_TRUE(IsRegistered());
}
TEST_F(PerformanceObserverTest, Enqueue) {
V8TestingScope scope;
Initialize(scope.GetScriptState());
ScriptValue empty_value;
Persistent<PerformanceEntry> entry =
PerformanceMark::Create(scope.GetScriptState(), "m", 1234, empty_value);
EXPECT_EQ(0, NumPerformanceEntries());
observer_->EnqueuePerformanceEntry(*entry);
EXPECT_EQ(1, NumPerformanceEntries());
}
TEST_F(PerformanceObserverTest, Deliver) {
V8TestingScope scope;
Initialize(scope.GetScriptState());
ScriptValue empty_value;
Persistent<PerformanceEntry> entry =
PerformanceMark::Create(scope.GetScriptState(), "m", 1234, empty_value);
EXPECT_EQ(0, NumPerformanceEntries());
observer_->EnqueuePerformanceEntry(*entry);
EXPECT_EQ(1, NumPerformanceEntries());
Deliver();
EXPECT_EQ(0, NumPerformanceEntries());
}
TEST_F(PerformanceObserverTest, Disconnect) {
V8TestingScope scope;
Initialize(scope.GetScriptState());
ScriptValue empty_value;
Persistent<PerformanceEntry> entry =
PerformanceMark::Create(scope.GetScriptState(), "m", 1234, empty_value);
EXPECT_EQ(0, NumPerformanceEntries());
observer_->EnqueuePerformanceEntry(*entry);
EXPECT_EQ(1, NumPerformanceEntries());
observer_->disconnect();
EXPECT_FALSE(IsRegistered());
EXPECT_EQ(0, NumPerformanceEntries());
}
}