| 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/page/FocusController.h" | 5 #include "core/page/FocusController.h" |
| 6 | 6 |
| 7 #include <memory> |
| 8 #include "bindings/core/v8/V8Binding.h" |
| 7 #include "core/dom/shadow/ShadowRootInit.h" | 9 #include "core/dom/shadow/ShadowRootInit.h" |
| 8 #include "core/html/HTMLElement.h" | 10 #include "core/html/HTMLElement.h" |
| 9 #include "core/testing/DummyPageHolder.h" | 11 #include "core/testing/DummyPageHolder.h" |
| 10 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
| 11 #include <memory> | |
| 12 | 13 |
| 13 namespace blink { | 14 namespace blink { |
| 14 | 15 |
| 15 class FocusControllerTest : public testing::Test { | 16 class FocusControllerTest : public testing::Test { |
| 16 public: | 17 public: |
| 17 Document& document() const { return m_pageHolder->document(); } | 18 Document& document() const { return m_pageHolder->document(); } |
| 18 FocusController& focusController() const { | 19 FocusController& focusController() const { |
| 19 return document().page()->focusController(); | 20 return document().page()->focusController(); |
| 20 } | 21 } |
| 21 DummyPageHolder* pageHolder() const { return m_pageHolder.get(); } | 22 DummyPageHolder* pageHolder() const { return m_pageHolder.get(); } |
| (...skipping 17 matching lines...) Expand all Loading... |
| 39 } | 40 } |
| 40 | 41 |
| 41 TEST_F(FocusControllerTest, DoNotCrash1) { | 42 TEST_F(FocusControllerTest, DoNotCrash1) { |
| 42 document().body()->setInnerHTML( | 43 document().body()->setInnerHTML( |
| 43 "<div id='host'></div>This test is for crbug.com/609012<p id='target' " | 44 "<div id='host'></div>This test is for crbug.com/609012<p id='target' " |
| 44 "tabindex='0'></p>"); | 45 "tabindex='0'></p>"); |
| 45 // <div> with shadow root | 46 // <div> with shadow root |
| 46 Element* host = toElement(document().body()->firstChild()); | 47 Element* host = toElement(document().body()->firstChild()); |
| 47 ShadowRootInit init; | 48 ShadowRootInit init; |
| 48 init.setMode("open"); | 49 init.setMode("open"); |
| 49 host->attachShadow(ScriptState::forMainWorld(document().frame()), init, | 50 host->attachShadow(toScriptStateForMainWorld(document().frame()), init, |
| 50 ASSERT_NO_EXCEPTION); | 51 ASSERT_NO_EXCEPTION); |
| 51 // "This test is for crbug.com/609012" | 52 // "This test is for crbug.com/609012" |
| 52 Node* text = host->nextSibling(); | 53 Node* text = host->nextSibling(); |
| 53 // <p> | 54 // <p> |
| 54 Element* target = toElement(text->nextSibling()); | 55 Element* target = toElement(text->nextSibling()); |
| 55 | 56 |
| 56 // Set sequential focus navigation point at text node. | 57 // Set sequential focus navigation point at text node. |
| 57 document().setSequentialFocusNavigationStartingPoint(text); | 58 document().setSequentialFocusNavigationStartingPoint(text); |
| 58 | 59 |
| 59 focusController().advanceFocus(WebFocusTypeForward); | 60 focusController().advanceFocus(WebFocusTypeForward); |
| 60 EXPECT_EQ(target, document().focusedElement()) | 61 EXPECT_EQ(target, document().focusedElement()) |
| 61 << "This should not hit assertion and finish properly."; | 62 << "This should not hit assertion and finish properly."; |
| 62 } | 63 } |
| 63 | 64 |
| 64 TEST_F(FocusControllerTest, DoNotCrash2) { | 65 TEST_F(FocusControllerTest, DoNotCrash2) { |
| 65 document().body()->setInnerHTML( | 66 document().body()->setInnerHTML( |
| 66 "<p id='target' tabindex='0'></p>This test is for crbug.com/609012<div " | 67 "<p id='target' tabindex='0'></p>This test is for crbug.com/609012<div " |
| 67 "id='host'></div>"); | 68 "id='host'></div>"); |
| 68 // <p> | 69 // <p> |
| 69 Element* target = toElement(document().body()->firstChild()); | 70 Element* target = toElement(document().body()->firstChild()); |
| 70 // "This test is for crbug.com/609012" | 71 // "This test is for crbug.com/609012" |
| 71 Node* text = target->nextSibling(); | 72 Node* text = target->nextSibling(); |
| 72 // <div> with shadow root | 73 // <div> with shadow root |
| 73 Element* host = toElement(text->nextSibling()); | 74 Element* host = toElement(text->nextSibling()); |
| 74 ShadowRootInit init; | 75 ShadowRootInit init; |
| 75 init.setMode("open"); | 76 init.setMode("open"); |
| 76 host->attachShadow(ScriptState::forMainWorld(document().frame()), init, | 77 host->attachShadow(toScriptStateForMainWorld(document().frame()), init, |
| 77 ASSERT_NO_EXCEPTION); | 78 ASSERT_NO_EXCEPTION); |
| 78 | 79 |
| 79 // Set sequential focus navigation point at text node. | 80 // Set sequential focus navigation point at text node. |
| 80 document().setSequentialFocusNavigationStartingPoint(text); | 81 document().setSequentialFocusNavigationStartingPoint(text); |
| 81 | 82 |
| 82 focusController().advanceFocus(WebFocusTypeBackward); | 83 focusController().advanceFocus(WebFocusTypeBackward); |
| 83 EXPECT_EQ(target, document().focusedElement()) | 84 EXPECT_EQ(target, document().focusedElement()) |
| 84 << "This should not hit assertion and finish properly."; | 85 << "This should not hit assertion and finish properly."; |
| 85 } | 86 } |
| 86 | 87 |
| 87 TEST_F(FocusControllerTest, SetActiveOnInactiveDocument) { | 88 TEST_F(FocusControllerTest, SetActiveOnInactiveDocument) { |
| 88 // Test for crbug.com/700334 | 89 // Test for crbug.com/700334 |
| 89 document().shutdown(); | 90 document().shutdown(); |
| 90 // Document::shutdown() detaches document from its frame, and thus | 91 // Document::shutdown() detaches document from its frame, and thus |
| 91 // document().page() becomes nullptr. | 92 // document().page() becomes nullptr. |
| 92 // Use DummyPageHolder's page to retrieve FocusController. | 93 // Use DummyPageHolder's page to retrieve FocusController. |
| 93 pageHolder()->page().focusController().setActive(true); | 94 pageHolder()->page().focusController().setActive(true); |
| 94 } | 95 } |
| 95 | 96 |
| 96 } // namespace blink | 97 } // namespace blink |
| OLD | NEW |