OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Copyright (c) 2013, Opera Software ASA. All rights reserved. |
| 3 * |
| 4 * Redistribution and use in source and binary forms, with or without |
| 5 * modification, are permitted provided that the following conditions |
| 6 * are met: |
| 7 * 1. Redistributions of source code must retain the above copyright |
| 8 * notice, this list of conditions and the following disclaimer. |
| 9 * 2. Redistributions in binary form must reproduce the above copyright |
| 10 * notice, this list of conditions and the following disclaimer in the |
| 11 * documentation and/or other materials provided with the distribution. |
| 12 * 3. Neither the name of Opera Software ASA nor the names of its |
| 13 * contributors may be used to endorse or promote products derived |
| 14 * from this software without specific prior written permission. |
| 15 * |
| 16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
| 19 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
| 20 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, |
| 21 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 23 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 25 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED |
| 27 * OF THE POSSIBILITY OF SUCH DAMAGE. |
| 28 */ |
| 29 |
| 30 #include "config.h" |
| 31 #include "core/html/track/vtt/VTTScanner.h" |
| 32 |
| 33 #include "wtf/text/WTFString.h" |
| 34 |
| 35 #include <gtest/gtest.h> |
| 36 |
| 37 using WebCore::VTTScanner; |
| 38 |
| 39 namespace { |
| 40 |
| 41 TEST(VTTScanner, Constructor) |
| 42 { |
| 43 String data8("foo"); |
| 44 EXPECT_TRUE(data8.is8Bit()); |
| 45 VTTScanner scanner8(data8); |
| 46 EXPECT_FALSE(scanner8.isAtEnd()); |
| 47 |
| 48 String data16(data8); |
| 49 data16.ensure16Bit(); |
| 50 EXPECT_FALSE(data16.is8Bit()); |
| 51 VTTScanner scanner16(data16); |
| 52 EXPECT_FALSE(scanner16.isAtEnd()); |
| 53 |
| 54 VTTScanner scannerEmpty(emptyString()); |
| 55 EXPECT_TRUE(scannerEmpty.isAtEnd()); |
| 56 } |
| 57 |
| 58 void scanSequenceHelper1(const String& input) |
| 59 { |
| 60 VTTScanner scanner(input); |
| 61 EXPECT_FALSE(scanner.isAtEnd()); |
| 62 EXPECT_TRUE(scanner.match('f')); |
| 63 EXPECT_FALSE(scanner.match('o')); |
| 64 |
| 65 EXPECT_TRUE(scanner.scan('f')); |
| 66 EXPECT_FALSE(scanner.match('f')); |
| 67 EXPECT_TRUE(scanner.match('o')); |
| 68 |
| 69 EXPECT_FALSE(scanner.scan('e')); |
| 70 EXPECT_TRUE(scanner.scan('o')); |
| 71 |
| 72 EXPECT_TRUE(scanner.scan('e')); |
| 73 EXPECT_FALSE(scanner.match('e')); |
| 74 |
| 75 EXPECT_TRUE(scanner.isAtEnd()); |
| 76 } |
| 77 |
| 78 // Run TESTFUNC with DATA in Latin and then UTF-16. (Requires DATA being Latin.) |
| 79 #define TEST_WITH(TESTFUNC, DATA) do { \ |
| 80 String data8(DATA); \ |
| 81 EXPECT_TRUE(data8.is8Bit()); \ |
| 82 TESTFUNC(data8); \ |
| 83 \ |
| 84 String data16(data8); \ |
| 85 data16.ensure16Bit(); \ |
| 86 EXPECT_FALSE(data16.is8Bit()); \ |
| 87 TESTFUNC(data16); \ |
| 88 } while (false) |
| 89 |
| 90 |
| 91 // Exercises match(c) and scan(c). |
| 92 TEST(VTTScanner, BasicOperations1) |
| 93 { |
| 94 TEST_WITH(scanSequenceHelper1, "foe"); |
| 95 } |
| 96 |
| 97 void scanSequenceHelper2(const String& input) |
| 98 { |
| 99 VTTScanner scanner(input); |
| 100 EXPECT_FALSE(scanner.isAtEnd()); |
| 101 EXPECT_FALSE(scanner.scan("fe")); |
| 102 |
| 103 EXPECT_TRUE(scanner.scan("fo")); |
| 104 EXPECT_FALSE(scanner.isAtEnd()); |
| 105 |
| 106 EXPECT_FALSE(scanner.scan("ee")); |
| 107 |
| 108 EXPECT_TRUE(scanner.scan('e')); |
| 109 EXPECT_TRUE(scanner.isAtEnd()); |
| 110 } |
| 111 |
| 112 // Exercises scan(<literal>[, length]). |
| 113 TEST(VTTScanner, BasicOperations2) |
| 114 { |
| 115 TEST_WITH(scanSequenceHelper2, "foe"); |
| 116 } |
| 117 |
| 118 bool lowerCaseAlpha(UChar c) |
| 119 { |
| 120 return c >= 'a' && c <= 'z'; |
| 121 } |
| 122 |
| 123 void scanWithPredicate(const String& input) |
| 124 { |
| 125 VTTScanner scanner(input); |
| 126 EXPECT_FALSE(scanner.isAtEnd()); |
| 127 // Collect "bad". |
| 128 VTTScanner::Run lcRun = scanner.collectWhile<lowerCaseAlpha>(); |
| 129 // collectWhile doesn't move the scan position. |
| 130 EXPECT_TRUE(scanner.match('b')); |
| 131 // Consume "bad". |
| 132 scanner.skipWhile<lowerCaseAlpha>(); |
| 133 EXPECT_TRUE(scanner.match('A')); |
| 134 EXPECT_TRUE(scanner.isAt(lcRun.end())); |
| 135 |
| 136 // Consume "A". |
| 137 EXPECT_TRUE(scanner.scan('A')); |
| 138 |
| 139 // Collect "bing". |
| 140 lcRun = scanner.collectWhile<lowerCaseAlpha>(); |
| 141 // collectWhile doesn't move the scan position. |
| 142 EXPECT_FALSE(scanner.isAtEnd()); |
| 143 // Consume "bing". |
| 144 scanner.skipWhile<lowerCaseAlpha>(); |
| 145 EXPECT_TRUE(scanner.isAt(lcRun.end())); |
| 146 EXPECT_TRUE(scanner.isAtEnd()); |
| 147 } |
| 148 |
| 149 // Tests skipWhile() and collectWhile(). |
| 150 TEST(VTTScanner, PredicateScanning) |
| 151 { |
| 152 TEST_WITH(scanWithPredicate, "badAbing"); |
| 153 } |
| 154 |
| 155 void tailStringExtract(const String& input) |
| 156 { |
| 157 VTTScanner scanner(input); |
| 158 EXPECT_TRUE(scanner.scan("foo")); |
| 159 EXPECT_TRUE(scanner.scan(':')); |
| 160 String barSuffix = scanner.restOfInputAsString(); |
| 161 EXPECT_EQ(barSuffix, "bar"); |
| 162 |
| 163 EXPECT_TRUE(scanner.isAtEnd()); |
| 164 } |
| 165 |
| 166 // Tests restOfInputAsString(). |
| 167 TEST(VTTScanner, ExtractRestAsString) |
| 168 { |
| 169 TEST_WITH(tailStringExtract, "foo:bar"); |
| 170 } |
| 171 |
| 172 void scanDigits1(const String& input) |
| 173 { |
| 174 VTTScanner scanner(input); |
| 175 EXPECT_TRUE(scanner.scan("foo")); |
| 176 int number; |
| 177 EXPECT_EQ(scanner.scanDigits(number), 0u); |
| 178 EXPECT_EQ(number, 0); |
| 179 EXPECT_TRUE(scanner.scan(' ')); |
| 180 EXPECT_EQ(scanner.scanDigits(number), 3u); |
| 181 EXPECT_TRUE(scanner.match(' ')); |
| 182 EXPECT_EQ(number, 123); |
| 183 |
| 184 EXPECT_TRUE(scanner.scan(' ')); |
| 185 EXPECT_TRUE(scanner.scan("bar")); |
| 186 EXPECT_TRUE(scanner.scan(' ')); |
| 187 |
| 188 EXPECT_EQ(scanner.scanDigits(number), 5u); |
| 189 EXPECT_EQ(number, 45678); |
| 190 |
| 191 EXPECT_TRUE(scanner.isAtEnd()); |
| 192 } |
| 193 |
| 194 void scanDigits2(const String& input) |
| 195 { |
| 196 VTTScanner scanner(input); |
| 197 int number; |
| 198 EXPECT_EQ(scanner.scanDigits(number), 0u); |
| 199 EXPECT_EQ(number, 0); |
| 200 EXPECT_TRUE(scanner.scan('-')); |
| 201 EXPECT_EQ(scanner.scanDigits(number), 3u); |
| 202 EXPECT_EQ(number, 654); |
| 203 |
| 204 EXPECT_TRUE(scanner.scan(' ')); |
| 205 |
| 206 EXPECT_EQ(scanner.scanDigits(number), 19u); |
| 207 EXPECT_EQ(number, std::numeric_limits<int>::max()); |
| 208 |
| 209 EXPECT_TRUE(scanner.isAtEnd()); |
| 210 } |
| 211 |
| 212 // Tests scanDigits(). |
| 213 TEST(VTTScanner, ScanDigits) |
| 214 { |
| 215 TEST_WITH(scanDigits1, "foo 123 bar 45678"); |
| 216 TEST_WITH(scanDigits2, "-654 1000000000000000000"); |
| 217 } |
| 218 |
| 219 #undef TEST_WITH |
| 220 |
| 221 } // namespace |
OLD | NEW |