David Ghandehari | 6017030 | 2017-03-10 21:18:13 -0800 | [diff] [blame] | 1 | // Copyright 2015 Google Inc. All Rights Reserved. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 14 | |
| 15 | #include "base/memory/ref_counted.h" |
| 16 | #include "base/memory/scoped_ptr.h" |
| 17 | #include "cobalt/base/clock.h" |
| 18 | #include "cobalt/css_parser/parser.h" |
| 19 | #include "cobalt/dom/document.h" |
| 20 | #include "cobalt/dom/dom_parser.h" |
| 21 | #include "cobalt/dom/dom_stat_tracker.h" |
| 22 | #include "cobalt/dom/html_body_element.h" |
| 23 | #include "cobalt/dom/html_br_element.h" |
| 24 | #include "cobalt/dom/html_div_element.h" |
| 25 | #include "cobalt/dom/html_element_context.h" |
| 26 | #include "cobalt/dom/html_head_element.h" |
| 27 | #include "cobalt/dom/html_html_element.h" |
| 28 | #include "cobalt/dom/html_paragraph_element.h" |
| 29 | #include "cobalt/dom/text.h" |
| 30 | #include "cobalt/webdriver/algorithms.h" |
| 31 | #include "testing/gmock/include/gmock/gmock.h" |
| 32 | #include "testing/gtest/include/gtest/gtest.h" |
| 33 | |
| 34 | namespace cobalt { |
| 35 | namespace webdriver { |
| 36 | namespace { |
| 37 | const char kBlankDocument[] = "<html><head/><body/><html>"; |
| 38 | class GetElementTextTest : public ::testing::Test { |
| 39 | protected: |
| 40 | GetElementTextTest() |
| 41 | : css_parser_(css_parser::Parser::Create()), |
| 42 | dom_stat_tracker_(new dom::DomStatTracker("GetElementTextTest")), |
| 43 | html_element_context_(NULL, css_parser_.get(), NULL, NULL, NULL, NULL, |
David Ghandehari | 00be30f | 2017-04-27 21:30:48 -0700 | [diff] [blame] | 44 | NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, |
David Ghandehari | da5c4f0 | 2017-07-10 14:31:27 -0700 | [diff] [blame] | 45 | dom_stat_tracker_.get(), "", |
| 46 | base::kApplicationStateStarted) {} |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 47 | |
Andrew Top | 200ce4b | 2018-01-29 13:43:50 -0800 | [diff] [blame] | 48 | void SetUp() override { |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 49 | dom::Document::Options options; |
| 50 | options.viewport_size = math::Size(1920, 1080); |
| 51 | options.navigation_start_clock = new base::SystemMonotonicClock(); |
| 52 | document_ = new dom::Document(&html_element_context_, options); |
| 53 | document_->AppendChild(new dom::HTMLHtmlElement(document_.get())); |
| 54 | document_->html()->AppendChild(new dom::HTMLHeadElement(document_.get())); |
| 55 | document_->html()->AppendChild(new dom::HTMLBodyElement(document_.get())); |
| 56 | div_ = new dom::HTMLDivElement(document_.get()); |
| 57 | document_->body()->AppendChild(div_); |
| 58 | |
| 59 | document_->SampleTimelineTime(); |
| 60 | } |
| 61 | |
| 62 | void AppendText(const char* text_content) { |
| 63 | div_->AppendChild(new dom::Text(document_.get(), text_content)); |
| 64 | } |
| 65 | |
| 66 | void AppendBR() { |
| 67 | div_->AppendChild(new dom::HTMLBRElement(document_.get())); |
| 68 | } |
| 69 | |
| 70 | void AppendParagraphWithDisplayStyle(const char* display_style) { |
| 71 | scoped_refptr<dom::HTMLParagraphElement> p( |
| 72 | new dom::HTMLParagraphElement(document_.get())); |
| 73 | p->style()->set_display(display_style, NULL); |
| 74 | div_->AppendChild(p); |
| 75 | } |
| 76 | |
| 77 | scoped_ptr<css_parser::Parser> css_parser_; |
| 78 | scoped_ptr<dom::DomStatTracker> dom_stat_tracker_; |
| 79 | dom::HTMLElementContext html_element_context_; |
| 80 | scoped_refptr<dom::Document> document_; |
| 81 | scoped_refptr<dom::HTMLDivElement> div_; |
| 82 | }; |
| 83 | } // namespace |
| 84 | |
| 85 | TEST_F(GetElementTextTest, ZeroSpaceWidthIsRemoved) { |
Mike Fleming | 3933d92 | 2018-04-02 10:53:08 -0700 | [diff] [blame] | 86 | AppendText(u8"a\u200bb\u200ec\u200f\u200bd"); |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 87 | EXPECT_STREQ("abcd", algorithms::GetElementText(div_.get()).c_str()); |
| 88 | } |
| 89 | |
| 90 | TEST_F(GetElementTextTest, NewLinesAreConvertedToSpaces) { |
| 91 | AppendText("a\r\nb\rc\nd"); |
| 92 | EXPECT_STREQ("a b c d", algorithms::GetElementText(div_.get()).c_str()); |
| 93 | } |
| 94 | |
| 95 | TEST_F(GetElementTextTest, NoWrapStyle) { |
| 96 | div_->style()->set_white_space("nowrap", NULL); |
Mike Fleming | 3933d92 | 2018-04-02 10:53:08 -0700 | [diff] [blame] | 97 | AppendText(u8"a\n\nb\nc\td\u2028e\u2029f\u00a0g"); |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 98 | EXPECT_STREQ("a b c d e f g", algorithms::GetElementText(div_.get()).c_str()); |
| 99 | } |
| 100 | |
| 101 | TEST_F(GetElementTextTest, Uppercase) { |
| 102 | div_->style()->set_text_transform("uppercase", NULL); |
| 103 | AppendText("AbCdE fGhIj kl-mn-Op"); |
| 104 | EXPECT_STREQ("ABCDE FGHIJ KL-MN-OP", |
| 105 | algorithms::GetElementText(div_.get()).c_str()); |
| 106 | } |
| 107 | |
| 108 | TEST_F(GetElementTextTest, BrElementBecomesNewLine) { |
| 109 | AppendText("a"); |
| 110 | AppendBR(); |
| 111 | AppendText("b"); |
| 112 | AppendBR(); |
| 113 | AppendBR(); |
| 114 | AppendText("c"); |
| 115 | |
| 116 | EXPECT_STREQ("a\nb\n\nc", algorithms::GetElementText(div_.get()).c_str()); |
| 117 | } |
| 118 | |
| 119 | TEST_F(GetElementTextTest, Blocks) { |
| 120 | AppendText("a"); |
| 121 | AppendParagraphWithDisplayStyle("block"); |
| 122 | AppendText("b"); |
| 123 | AppendParagraphWithDisplayStyle("block"); |
| 124 | AppendParagraphWithDisplayStyle("block"); |
| 125 | AppendText("c"); |
| 126 | |
| 127 | EXPECT_STREQ("a\nb\nc", algorithms::GetElementText(div_.get()).c_str()); |
| 128 | } |
| 129 | |
Mike Fleming | 3933d92 | 2018-04-02 10:53:08 -0700 | [diff] [blame] | 130 | TEST_F(GetElementTextTest, LinesAreTrimmed) { |
| 131 | AppendText(" a \n"); |
| 132 | AppendBR(); |
| 133 | AppendText(" b \n\n"); |
| 134 | AppendBR(); |
| 135 | AppendText("\nc "); |
| 136 | AppendBR(); |
| 137 | AppendText(" "); |
| 138 | |
| 139 | EXPECT_STREQ("a\nb\nc", algorithms::GetElementText(div_.get()).c_str()); |
| 140 | } |
| 141 | |
| 142 | TEST_F(GetElementTextTest, WholeCodePointsAreProcessed) { |
| 143 | AppendText(u8"a\u200b\u2020\u2029\u2022\U0002070Eb"); |
| 144 | EXPECT_STREQ(u8"a\u2020 \u2022\U0002070Eb", |
| 145 | algorithms::GetElementText(div_.get()).c_str()); |
| 146 | } |
| 147 | |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 148 | } // namespace webdriver |
| 149 | } // namespace cobalt |