- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / app_list / search / tokenized_string_char_iterator_unittest.cc
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.
4
5 #include "chrome/browser/ui/app_list/search/tokenized_string_char_iterator.h"
6
7 #include <string>
8 #include <vector>
9
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"
14
15 namespace app_list {
16 namespace test {
17
18 namespace {
19
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(),
27                             iter.GetArrayPos(),
28                             iter.IsFirstCharOfToken() ? "!" : "");
29 }
30
31 void TestBeyondTheEnd(TokenizedStringCharIterator* iter) {
32   ASSERT_TRUE(iter->end());
33   ASSERT_FALSE(iter->NextChar());
34   ASSERT_FALSE(iter->NextToken());
35
36   // Don't care what it returns, but this shouldn't crash.
37   iter->Get();
38 }
39
40 void TestEveryChar(const std::string& text, const std::string& expects) {
41   TokenizedString tokens(UTF8ToUTF16(text));
42   TokenizedStringCharIterator iter(tokens);
43
44   std::vector<std::string> results;
45   while (!iter.end()) {
46     results.push_back(GetIterateState(iter));
47     iter.NextChar();
48   }
49
50   EXPECT_EQ(expects, JoinString(results, ' '));
51   TestBeyondTheEnd(&iter);
52 }
53
54 void TestNextToken(const std::string& text, const std::string& expects) {
55   TokenizedString tokens(UTF8ToUTF16(text));
56   TokenizedStringCharIterator iter(tokens);
57
58   std::vector<std::string> results;
59   while (!iter.end()) {
60     results.push_back(GetIterateState(iter));
61     iter.NextToken();
62   }
63
64   EXPECT_EQ(expects, JoinString(results, ' '));
65   TestBeyondTheEnd(&iter);
66 }
67
68 void TestFirstTwoCharInEveryToken(const std::string& text,
69                                   const std::string& expects) {
70   TokenizedString tokens(UTF8ToUTF16(text));
71   TokenizedStringCharIterator iter(tokens);
72
73   std::vector<std::string> results;
74   while (!iter.end()) {
75     results.push_back(GetIterateState(iter));
76     if (iter.NextChar())
77       results.push_back(GetIterateState(iter));
78
79     iter.NextToken();
80   }
81
82   EXPECT_EQ(expects, JoinString(results, ' '));
83   TestBeyondTheEnd(&iter);
84 }
85
86 }  // namespace
87
88 TEST(TokenizedStringCharIteratorTest, NoTerms) {
89   const char* text;
90
91   text = "";
92   TestEveryChar(text, "");
93   TestNextToken(text, "");
94   TestFirstTwoCharInEveryToken(text, "");
95
96   text = "!@#$%^&*()<<<**>>>";
97   TestEveryChar(text, "");
98   TestNextToken(text, "");
99   TestFirstTwoCharInEveryToken(text, "");
100 }
101
102 TEST(TokenizedStringCharIteratorTest, Basic) {
103   const char* text;
104
105   text = "c";
106   TestEveryChar(text, "c0!");
107   TestNextToken(text, "c0!");
108   TestFirstTwoCharInEveryToken(text, "c0!");
109
110   text = "Simple";
111   TestEveryChar(text, "s0! i1 m2 p3 l4 e5");
112   TestNextToken(text, "s0!");
113   TestFirstTwoCharInEveryToken(text, "s0! i1");
114
115   text = "ScratchPad";
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");
119
120   text = "Chess2.0";
121   TestEveryChar(text, "c0! h1 e2 s3 s4 25! .6 07");
122   TestNextToken(text, "c0! 25!");
123   TestFirstTwoCharInEveryToken(text, "c0! h1 25! .6");
124
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");
129
130   text = "AutoCAD WS";
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");
134
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");
139
140   text = "Draw-It!";
141   TestEveryChar(text, "d0! r1 a2 w3 i5! t6");
142   TestNextToken(text, "d0! i5!");
143   TestFirstTwoCharInEveryToken(text, "d0! r1 i5! t6");
144
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");
149 }
150
151 }  // namespace test
152 }  // namespace app_list