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/testing/DummyPageHolder.h" | 9 #include "core/testing/DummyPageHolder.h" |
| 10 #include "core/testing/NullExecutionContext.h" |
10 #include "core/timing/PerformanceBase.h" | 11 #include "core/timing/PerformanceBase.h" |
11 #include "core/timing/PerformanceLongTaskTiming.h" | 12 #include "core/timing/PerformanceLongTaskTiming.h" |
12 #include "core/timing/PerformanceObserver.h" | 13 #include "core/timing/PerformanceObserver.h" |
13 #include "core/timing/PerformanceObserverInit.h" | 14 #include "core/timing/PerformanceObserverInit.h" |
| 15 #include "platform/network/ResourceResponse.h" |
14 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
15 | 17 |
16 namespace blink { | 18 namespace blink { |
17 | 19 |
18 class TestPerformanceBase : public PerformanceBase { | 20 class TestPerformanceBase : public PerformanceBase { |
19 public: | 21 public: |
20 TestPerformanceBase() : PerformanceBase(0) {} | 22 TestPerformanceBase() : PerformanceBase(0) {} |
21 ~TestPerformanceBase() {} | 23 ~TestPerformanceBase() {} |
22 | 24 |
23 ExecutionContext* getExecutionContext() const override { return nullptr; } | 25 ExecutionContext* getExecutionContext() const override { return nullptr; } |
(...skipping 15 matching lines...) Expand all Loading... |
39 v8::Local<v8::Function> callback = | 41 v8::Local<v8::Function> callback = |
40 v8::Function::New(scriptState->context(), nullptr).ToLocalChecked(); | 42 v8::Function::New(scriptState->context(), nullptr).ToLocalChecked(); |
41 m_base = new TestPerformanceBase(); | 43 m_base = new TestPerformanceBase(); |
42 m_cb = PerformanceObserverCallback::create(scriptState, callback); | 44 m_cb = PerformanceObserverCallback::create(scriptState, callback); |
43 m_observer = PerformanceObserver::create(scriptState->getExecutionContext(), | 45 m_observer = PerformanceObserver::create(scriptState->getExecutionContext(), |
44 m_base, m_cb); | 46 m_base, m_cb); |
45 } | 47 } |
46 | 48 |
47 void SetUp() override { | 49 void SetUp() override { |
48 m_pageHolder = DummyPageHolder::create(IntSize(800, 600)); | 50 m_pageHolder = DummyPageHolder::create(IntSize(800, 600)); |
| 51 m_executionContext = new NullExecutionContext(); |
49 } | 52 } |
50 | 53 |
| 54 ExecutionContext* getExecutionContext() { return m_executionContext.get(); } |
| 55 |
51 int numPerformanceEntriesInObserver() { | 56 int numPerformanceEntriesInObserver() { |
52 return m_observer->m_performanceEntries.size(); | 57 return m_observer->m_performanceEntries.size(); |
53 } | 58 } |
54 | 59 |
55 PerformanceNavigationTiming::NavigationType getNavigationType( | 60 PerformanceNavigationTiming::NavigationType getNavigationType( |
56 NavigationType type, | 61 NavigationType type, |
57 Document* document) { | 62 Document* document) { |
58 return PerformanceBase::getNavigationType(type, document); | 63 return PerformanceBase::getNavigationType(type, document); |
59 } | 64 } |
60 | 65 |
| 66 static bool allowsTimingRedirect( |
| 67 const Vector<ResourceResponse>& redirectChain, |
| 68 const ResourceResponse& finalResponse, |
| 69 const SecurityOrigin& initiatorSecurityOrigin, |
| 70 ExecutionContext* context) { |
| 71 return PerformanceBase::allowsTimingRedirect( |
| 72 redirectChain, finalResponse, initiatorSecurityOrigin, context); |
| 73 } |
| 74 |
61 Persistent<TestPerformanceBase> m_base; | 75 Persistent<TestPerformanceBase> m_base; |
| 76 Persistent<ExecutionContext> m_executionContext; |
62 Persistent<PerformanceObserver> m_observer; | 77 Persistent<PerformanceObserver> m_observer; |
63 std::unique_ptr<DummyPageHolder> m_pageHolder; | 78 std::unique_ptr<DummyPageHolder> m_pageHolder; |
64 Persistent<PerformanceObserverCallback> m_cb; | 79 Persistent<PerformanceObserverCallback> m_cb; |
65 }; | 80 }; |
66 | 81 |
67 TEST_F(PerformanceBaseTest, Register) { | 82 TEST_F(PerformanceBaseTest, Register) { |
68 V8TestingScope scope; | 83 V8TestingScope scope; |
69 initialize(scope.getScriptState()); | 84 initialize(scope.getScriptState()); |
70 | 85 |
71 EXPECT_EQ(0, m_base->numObservers()); | 86 EXPECT_EQ(0, m_base->numObservers()); |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
137 getNavigationType(NavigationTypeBackForward, &m_pageHolder->document()); | 152 getNavigationType(NavigationTypeBackForward, &m_pageHolder->document()); |
138 EXPECT_EQ(returnedType, | 153 EXPECT_EQ(returnedType, |
139 PerformanceNavigationTiming::NavigationType::BackForward); | 154 PerformanceNavigationTiming::NavigationType::BackForward); |
140 | 155 |
141 m_pageHolder->page().setVisibilityState(PageVisibilityStateVisible, false); | 156 m_pageHolder->page().setVisibilityState(PageVisibilityStateVisible, false); |
142 returnedType = getNavigationType(NavigationTypeFormResubmitted, | 157 returnedType = getNavigationType(NavigationTypeFormResubmitted, |
143 &m_pageHolder->document()); | 158 &m_pageHolder->document()); |
144 EXPECT_EQ(returnedType, | 159 EXPECT_EQ(returnedType, |
145 PerformanceNavigationTiming::NavigationType::Navigate); | 160 PerformanceNavigationTiming::NavigationType::Navigate); |
146 } | 161 } |
| 162 |
| 163 TEST_F(PerformanceBaseTest, AllowsTimingRedirect) { |
| 164 // When there are no cross-origin redirects. |
| 165 AtomicString originDomain = "http://127.0.0.1:8000"; |
| 166 Vector<ResourceResponse> redirectChain; |
| 167 KURL url(ParsedURLString, originDomain + "/foo.html"); |
| 168 ResourceResponse finalResponse; |
| 169 finalResponse.setURL(url); |
| 170 ResourceResponse redirectResponse1; |
| 171 redirectResponse1.setURL(url); |
| 172 ResourceResponse redirectResponse2; |
| 173 redirectResponse2.setURL(url); |
| 174 redirectChain.append(redirectResponse1); |
| 175 redirectChain.append(redirectResponse2); |
| 176 RefPtr<SecurityOrigin> securityOrigin = SecurityOrigin::create(url); |
| 177 EXPECT_TRUE(allowsTimingRedirect(redirectChain, finalResponse, |
| 178 *securityOrigin.get(), |
| 179 getExecutionContext())); |
| 180 // When there exist cross-origin redirects. |
| 181 AtomicString crossOriginDomain = "http://126.0.0.1:8000"; |
| 182 KURL redirectUrl(ParsedURLString, crossOriginDomain + "/bar.html"); |
| 183 ResourceResponse redirectResponse3; |
| 184 redirectResponse3.setURL(redirectUrl); |
| 185 redirectChain.append(redirectResponse3); |
| 186 EXPECT_FALSE(allowsTimingRedirect(redirectChain, finalResponse, |
| 187 *securityOrigin.get(), |
| 188 getExecutionContext())); |
| 189 |
| 190 // When cross-origin redirect opts in. |
| 191 redirectChain.back().setHTTPHeaderField(HTTPNames::Timing_Allow_Origin, |
| 192 originDomain); |
| 193 EXPECT_TRUE(allowsTimingRedirect(redirectChain, finalResponse, |
| 194 *securityOrigin.get(), |
| 195 getExecutionContext())); |
| 196 } |
| 197 |
147 } // namespace blink | 198 } // namespace blink |
OLD | NEW |