| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "core/timing/Performance.h" | 5 #include "core/timing/Performance.h" |
| 6 | 6 |
| 7 #include "bindings/core/v8/PerformanceObserverCallback.h" | 7 #include "bindings/core/v8/PerformanceObserverCallback.h" |
| 8 #include "bindings/core/v8/V8BindingForTesting.h" | 8 #include "bindings/core/v8/V8BindingForTesting.h" |
| 9 #include "core/timing/PerformanceBase.h" | 9 #include "core/timing/PerformanceBase.h" |
| 10 #include "core/timing/PerformanceLongTaskTiming.h" | 10 #include "core/timing/PerformanceLongTaskTiming.h" |
| 11 #include "core/timing/PerformanceObserver.h" | 11 #include "core/timing/PerformanceObserver.h" |
| 12 #include "core/timing/PerformanceObserverInit.h" | 12 #include "core/timing/PerformanceObserverInit.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
| 14 | 14 |
| 15 namespace blink { | 15 namespace blink { |
| 16 | 16 |
| 17 class TestPerformanceBase : public PerformanceBase { | 17 class TestPerformanceBase : public PerformanceBase { |
| 18 public: | 18 public: |
| 19 TestPerformanceBase() : PerformanceBase(0) {} | 19 TestPerformanceBase() : PerformanceBase(0) {} |
| 20 ~TestPerformanceBase() {} | 20 ~TestPerformanceBase() {} |
| 21 | 21 |
| 22 ExecutionContext* getExecutionContext() const override { return nullptr; } | 22 ExecutionContext* getExecutionContext() const override { return nullptr; } |
| 23 | 23 |
| 24 int numActiveObservers() { return m_activeObservers.size(); } | 24 int numActiveObservers() { return m_activeObservers.size(); } |
| 25 | 25 |
| 26 int numObservers() { return m_observers.size(); } | 26 int numObservers() { return m_observers.size(); } |
| 27 | 27 |
| 28 int numLongTaskTimingEntries() { return m_longTaskTimingBuffer.size(); } | 28 bool hasPerformanceObserverFor(PerformanceEntry::EntryType entryType) { |
| 29 return hasObserverFor(entryType); |
| 30 } |
| 29 | 31 |
| 30 DEFINE_INLINE_TRACE() { PerformanceBase::trace(visitor); } | 32 DEFINE_INLINE_TRACE() { PerformanceBase::trace(visitor); } |
| 31 }; | 33 }; |
| 32 | 34 |
| 33 class PerformanceBaseTest : public ::testing::Test { | 35 class PerformanceBaseTest : public ::testing::Test { |
| 34 protected: | 36 protected: |
| 35 void initialize(ScriptState* scriptState) { | 37 void initialize(ScriptState* scriptState) { |
| 36 v8::Local<v8::Function> callback = | 38 v8::Local<v8::Function> callback = |
| 37 v8::Function::New(scriptState->context(), nullptr).ToLocalChecked(); | 39 v8::Function::New(scriptState->context(), nullptr).ToLocalChecked(); |
| 38 m_base = new TestPerformanceBase(); | 40 m_base = new TestPerformanceBase(); |
| 39 m_cb = | 41 m_cb = |
| 40 PerformanceObserverCallback::create(scriptState->isolate(), callback); | 42 PerformanceObserverCallback::create(scriptState->isolate(), callback); |
| 41 m_observer = PerformanceObserver::create(scriptState, m_base, m_cb); | 43 m_observer = PerformanceObserver::create(scriptState, m_base, m_cb); |
| 42 } | 44 } |
| 43 | 45 |
| 46 int numPerformanceEntriesInObserver() { |
| 47 return m_observer->m_performanceEntries.size(); |
| 48 } |
| 49 |
| 44 Persistent<TestPerformanceBase> m_base; | 50 Persistent<TestPerformanceBase> m_base; |
| 45 Persistent<PerformanceObserver> m_observer; | 51 Persistent<PerformanceObserver> m_observer; |
| 46 Persistent<PerformanceObserverCallback> m_cb; | 52 Persistent<PerformanceObserverCallback> m_cb; |
| 47 }; | 53 }; |
| 48 | 54 |
| 49 TEST_F(PerformanceBaseTest, Register) { | 55 TEST_F(PerformanceBaseTest, Register) { |
| 50 V8TestingScope scope; | 56 V8TestingScope scope; |
| 51 initialize(scope.getScriptState()); | 57 initialize(scope.getScriptState()); |
| 52 | 58 |
| 53 EXPECT_EQ(0, m_base->numObservers()); | 59 EXPECT_EQ(0, m_base->numObservers()); |
| (...skipping 27 matching lines...) Expand all Loading... |
| 81 EXPECT_EQ(0, m_base->numObservers()); | 87 EXPECT_EQ(0, m_base->numObservers()); |
| 82 EXPECT_EQ(0, m_base->numActiveObservers()); | 88 EXPECT_EQ(0, m_base->numActiveObservers()); |
| 83 } | 89 } |
| 84 | 90 |
| 85 TEST_F(PerformanceBaseTest, AddLongTaskTiming) { | 91 TEST_F(PerformanceBaseTest, AddLongTaskTiming) { |
| 86 V8TestingScope scope; | 92 V8TestingScope scope; |
| 87 initialize(scope.getScriptState()); | 93 initialize(scope.getScriptState()); |
| 88 | 94 |
| 89 // Add a long task entry, but no observer registered. | 95 // Add a long task entry, but no observer registered. |
| 90 m_base->addLongTaskTiming(1234, 5678, "www.foo.com/bar", nullptr); | 96 m_base->addLongTaskTiming(1234, 5678, "www.foo.com/bar", nullptr); |
| 91 EXPECT_EQ(0, m_base->numLongTaskTimingEntries()); // has no effect | 97 EXPECT_FALSE(m_base->hasPerformanceObserverFor(PerformanceEntry::LongTask)); |
| 98 EXPECT_EQ(0, numPerformanceEntriesInObserver()); // has no effect |
| 92 | 99 |
| 93 // Make an observer for longtask | 100 // Make an observer for longtask |
| 94 NonThrowableExceptionState exceptionState; | 101 NonThrowableExceptionState exceptionState; |
| 95 PerformanceObserverInit options; | 102 PerformanceObserverInit options; |
| 96 Vector<String> entryTypeVec; | 103 Vector<String> entryTypeVec; |
| 97 entryTypeVec.append("longtask"); | 104 entryTypeVec.append("longtask"); |
| 98 options.setEntryTypes(entryTypeVec); | 105 options.setEntryTypes(entryTypeVec); |
| 99 m_observer->observe(options, exceptionState); | 106 m_observer->observe(options, exceptionState); |
| 100 | 107 |
| 108 EXPECT_TRUE(m_base->hasPerformanceObserverFor(PerformanceEntry::LongTask)); |
| 101 // Add a long task entry | 109 // Add a long task entry |
| 102 m_base->addLongTaskTiming(1234, 5678, "www.foo.com/bar", nullptr); | 110 m_base->addLongTaskTiming(1234, 5678, "www.foo.com/bar", nullptr); |
| 103 EXPECT_EQ(1, m_base->numLongTaskTimingEntries()); // added an entry | 111 EXPECT_EQ(1, numPerformanceEntriesInObserver()); // added an entry |
| 104 } | 112 } |
| 105 | |
| 106 } // namespace blink | 113 } // namespace blink |
| OLD | NEW |