1 // Copyright 2014 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 "components/omnibox/answers_cache.h"
7 #include "base/strings/utf_string_conversions.h"
8 #include "testing/gtest/include/gtest/gtest.h"
10 TEST(AnswersCacheTest
, CacheStartsEmpty
) {
11 AnswersCache
cache(1);
12 EXPECT_TRUE(cache
.empty());
15 TEST(AnswersCacheTest
, UpdatePopulatesCache
) {
16 AnswersCache
cache(1);
17 cache
.UpdateRecentAnswers(base::ASCIIToUTF16("weather los angeles"),
18 base::ASCIIToUTF16("2334"));
19 EXPECT_FALSE(cache
.empty());
22 TEST(AnswersCacheTest
, GetWillRetrieveMatchingInfo
) {
23 AnswersCache
cache(1);
25 base::string16 full_query_text
= base::ASCIIToUTF16("weather los angeles");
26 base::string16 query_type
= base::ASCIIToUTF16("2334");
27 cache
.UpdateRecentAnswers(full_query_text
, query_type
);
29 // Partial prefixes retrieve info.
30 AnswersQueryData data
= cache
.GetTopAnswerEntry(full_query_text
.substr(0, 8));
31 EXPECT_EQ(full_query_text
, data
.full_query_text
);
32 EXPECT_EQ(query_type
, data
.query_type
);
34 // Mismatched prefix retrieves empty data.
35 data
= cache
.GetTopAnswerEntry(full_query_text
.substr(1, 8));
36 EXPECT_TRUE(data
.full_query_text
.empty() && data
.query_type
.empty());
38 // Full match retrieves info.
39 data
= cache
.GetTopAnswerEntry(full_query_text
);
40 EXPECT_EQ(full_query_text
, data
.full_query_text
);
41 EXPECT_EQ(query_type
, data
.query_type
);
44 TEST(AnswersCacheTest
, MatchMostRecent
) {
45 AnswersCache
cache(2);
47 base::string16 query_weather_la
= base::ASCIIToUTF16("weather los angeles");
48 base::string16 query_weather_lv
= base::ASCIIToUTF16("weather las vegas");
49 base::string16 query_type
= base::ASCIIToUTF16("2334");
51 cache
.UpdateRecentAnswers(query_weather_lv
, query_type
);
52 cache
.UpdateRecentAnswers(query_weather_la
, query_type
);
54 // "weather los angeles" is most recent match to "weather l".
55 AnswersQueryData data
=
56 cache
.GetTopAnswerEntry(base::ASCIIToUTF16("weather l"));
57 EXPECT_EQ(query_weather_la
, data
.full_query_text
);
59 // Update recency for "weather las vegas".
60 cache
.GetTopAnswerEntry(query_weather_lv
);
62 // "weather las vegas" should now be the most recent match to "weather l".
63 data
= cache
.GetTopAnswerEntry(base::ASCIIToUTF16("weather l"));
64 EXPECT_EQ(query_weather_lv
, data
.full_query_text
);
67 TEST(AnswersCacheTest
, LeastRecentItemIsEvicted
) {
68 AnswersCache
cache(2);
70 base::string16 query_weather_la
= base::ASCIIToUTF16("weather los angeles");
71 base::string16 query_weather_lv
= base::ASCIIToUTF16("weather las vegas");
72 base::string16 query_weather_lb
= base::ASCIIToUTF16("weather long beach");
73 base::string16 query_type
= base::ASCIIToUTF16("2334");
75 cache
.UpdateRecentAnswers(query_weather_lb
, query_type
);
76 cache
.UpdateRecentAnswers(query_weather_lv
, query_type
);
78 cache
.GetTopAnswerEntry(query_weather_lb
).full_query_text
.empty());
80 cache
.GetTopAnswerEntry(query_weather_lv
).full_query_text
.empty());
82 cache
.GetTopAnswerEntry(query_weather_la
).full_query_text
.empty());
84 cache
.UpdateRecentAnswers(query_weather_la
, query_type
);
86 cache
.GetTopAnswerEntry(query_weather_lb
).full_query_text
.empty());
88 cache
.GetTopAnswerEntry(query_weather_lv
).full_query_text
.empty());
90 cache
.GetTopAnswerEntry(query_weather_la
).full_query_text
.empty());
93 TEST(AnswersCacheTest
, DuplicateEntries
) {
94 AnswersCache
cache(2);
96 base::string16 query_weather_lv
= base::ASCIIToUTF16("weather las vegas");
97 base::string16 query_weather_lb
= base::ASCIIToUTF16("weather long beach");
98 base::string16 query_weather_l
= base::ASCIIToUTF16("weather l");
99 base::string16 query_type
= base::ASCIIToUTF16("2334");
101 cache
.UpdateRecentAnswers(query_weather_lb
, query_type
);
102 cache
.UpdateRecentAnswers(query_weather_lv
, query_type
);
104 // Test that duplicate entries update recency.
105 cache
.UpdateRecentAnswers(query_weather_lb
, query_type
);
106 EXPECT_EQ(query_weather_lb
,
107 cache
.GetTopAnswerEntry(query_weather_l
).full_query_text
);
109 // Test duplicate do not evict other entries. LV should still be in cache.
110 cache
.UpdateRecentAnswers(query_weather_lb
, query_type
);
112 cache
.GetTopAnswerEntry(query_weather_lv
).full_query_text
.empty());