1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/ui/app_list/search/tokenized_string_char_iterator.h"
10 #include "base/strings/string_util.h"
11 #include "base/strings/stringprintf.h"
12 #include "base/strings/utf_string_conversions.h"
13 #include "testing/gtest/include/gtest/gtest.h"
20 // Returns a string represents the current state of |iter|. The state string
21 // has three fields. The first is the current char. The second is the offset of
22 // the current char in terms of the original text of the TokenizedString. The
23 // last one is optional and only shows up when IsFirstCharOfToken returns true.
24 std::string GetIterateState(const TokenizedStringCharIterator& iter) {
25 return base::StringPrintf("%s%d%s",
26 UTF16ToUTF8(string16(1, iter.Get())).c_str(),
28 iter.IsFirstCharOfToken() ? "!" : "");
31 void TestBeyondTheEnd(TokenizedStringCharIterator* iter) {
32 ASSERT_TRUE(iter->end());
33 ASSERT_FALSE(iter->NextChar());
34 ASSERT_FALSE(iter->NextToken());
36 // Don't care what it returns, but this shouldn't crash.
40 void TestEveryChar(const std::string& text, const std::string& expects) {
41 TokenizedString tokens(UTF8ToUTF16(text));
42 TokenizedStringCharIterator iter(tokens);
44 std::vector<std::string> results;
46 results.push_back(GetIterateState(iter));
50 EXPECT_EQ(expects, JoinString(results, ' '));
51 TestBeyondTheEnd(&iter);
54 void TestNextToken(const std::string& text, const std::string& expects) {
55 TokenizedString tokens(UTF8ToUTF16(text));
56 TokenizedStringCharIterator iter(tokens);
58 std::vector<std::string> results;
60 results.push_back(GetIterateState(iter));
64 EXPECT_EQ(expects, JoinString(results, ' '));
65 TestBeyondTheEnd(&iter);
68 void TestFirstTwoCharInEveryToken(const std::string& text,
69 const std::string& expects) {
70 TokenizedString tokens(UTF8ToUTF16(text));
71 TokenizedStringCharIterator iter(tokens);
73 std::vector<std::string> results;
75 results.push_back(GetIterateState(iter));
77 results.push_back(GetIterateState(iter));
82 EXPECT_EQ(expects, JoinString(results, ' '));
83 TestBeyondTheEnd(&iter);
88 TEST(TokenizedStringCharIteratorTest, NoTerms) {
92 TestEveryChar(text, "");
93 TestNextToken(text, "");
94 TestFirstTwoCharInEveryToken(text, "");
96 text = "!@#$%^&*()<<<**>>>";
97 TestEveryChar(text, "");
98 TestNextToken(text, "");
99 TestFirstTwoCharInEveryToken(text, "");
102 TEST(TokenizedStringCharIteratorTest, Basic) {
106 TestEveryChar(text, "c0!");
107 TestNextToken(text, "c0!");
108 TestFirstTwoCharInEveryToken(text, "c0!");
111 TestEveryChar(text, "s0! i1 m2 p3 l4 e5");
112 TestNextToken(text, "s0!");
113 TestFirstTwoCharInEveryToken(text, "s0! i1");
116 TestEveryChar(text, "s0! c1 r2 a3 t4 c5 h6 p7! a8 d9");
117 TestNextToken(text, "s0! p7!");
118 TestFirstTwoCharInEveryToken(text, "s0! c1 p7! a8");
121 TestEveryChar(text, "c0! h1 e2 s3 s4 25! .6 07");
122 TestNextToken(text, "c0! 25!");
123 TestFirstTwoCharInEveryToken(text, "c0! h1 25! .6");
125 text = "Cut the rope";
126 TestEveryChar(text, "c0! u1 t2 t4! h5 e6 r8! o9 p10 e11");
127 TestNextToken(text, "c0! t4! r8!");
128 TestFirstTwoCharInEveryToken(text, "c0! u1 t4! h5 r8! o9");
131 TestEveryChar(text, "a0! u1 t2 o3 c4! a5 d6 w8! s9");
132 TestNextToken(text, "a0! c4! w8!");
133 TestFirstTwoCharInEveryToken(text, "a0! u1 c4! a5 w8! s9");
135 text = "Great TweetDeck";
136 TestEveryChar(text, "g0! r1 e2 a3 t4 t6! w7 e8 e9 t10 d11! e12 c13 k14");
137 TestNextToken(text, "g0! t6! d11!");
138 TestFirstTwoCharInEveryToken(text, "g0! r1 t6! w7 d11! e12");
141 TestEveryChar(text, "d0! r1 a2 w3 i5! t6");
142 TestNextToken(text, "d0! i5!");
143 TestFirstTwoCharInEveryToken(text, "d0! r1 i5! t6");
145 text = "Faxing & Signing";
146 TestEveryChar(text, "f0! a1 x2 i3 n4 g5 s9! i10 g11 n12 i13 n14 g15");
147 TestNextToken(text, "f0! s9!");
148 TestFirstTwoCharInEveryToken(text, "f0! a1 s9! i10");
152 } // namespace app_list