OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 5 #include "chrome/browser/autocomplete/answers_cache.h" |
| 6 |
| 7 #include "base/strings/utf_string_conversions.h" |
| 8 #include "testing/gtest/include/gtest/gtest.h" |
| 9 |
| 10 TEST(AnswersCacheTest, CacheStartsEmpty) { |
| 11 AnswersCache cache(1); |
| 12 EXPECT_TRUE(cache.empty()); |
| 13 } |
| 14 |
| 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()); |
| 20 } |
| 21 |
| 22 TEST(AnswersCacheTest, GetWillRetrieveMatchingInfo) { |
| 23 AnswersCache cache(1); |
| 24 |
| 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); |
| 28 |
| 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); |
| 33 |
| 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()); |
| 37 |
| 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); |
| 42 } |
| 43 |
| 44 TEST(AnswersCacheTest, MatchMostRecent) { |
| 45 AnswersCache cache(2); |
| 46 |
| 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"); |
| 50 |
| 51 cache.UpdateRecentAnswers(query_weather_lv, query_type); |
| 52 cache.UpdateRecentAnswers(query_weather_la, query_type); |
| 53 |
| 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); |
| 58 |
| 59 // Update recency for "weather las vegas". |
| 60 cache.GetTopAnswerEntry(query_weather_lv); |
| 61 |
| 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); |
| 65 } |
| 66 |
| 67 TEST(AnswersCacheTest, LeastRecentItemIsEvicted) { |
| 68 AnswersCache cache(2); |
| 69 |
| 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"); |
| 74 |
| 75 cache.UpdateRecentAnswers(query_weather_lb, query_type); |
| 76 cache.UpdateRecentAnswers(query_weather_lv, query_type); |
| 77 EXPECT_FALSE( |
| 78 cache.GetTopAnswerEntry(query_weather_lb).full_query_text.empty()); |
| 79 EXPECT_FALSE( |
| 80 cache.GetTopAnswerEntry(query_weather_lv).full_query_text.empty()); |
| 81 EXPECT_TRUE( |
| 82 cache.GetTopAnswerEntry(query_weather_la).full_query_text.empty()); |
| 83 |
| 84 cache.UpdateRecentAnswers(query_weather_la, query_type); |
| 85 EXPECT_TRUE( |
| 86 cache.GetTopAnswerEntry(query_weather_lb).full_query_text.empty()); |
| 87 EXPECT_FALSE( |
| 88 cache.GetTopAnswerEntry(query_weather_lv).full_query_text.empty()); |
| 89 EXPECT_FALSE( |
| 90 cache.GetTopAnswerEntry(query_weather_la).full_query_text.empty()); |
| 91 } |
| 92 |
| 93 TEST(AnswersCacheTest, DuplicateEntries) { |
| 94 AnswersCache cache(2); |
| 95 |
| 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"); |
| 100 |
| 101 cache.UpdateRecentAnswers(query_weather_lb, query_type); |
| 102 cache.UpdateRecentAnswers(query_weather_lv, query_type); |
| 103 |
| 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); |
| 108 |
| 109 // Test duplicate do not evict other entries. LV should still be in cache. |
| 110 cache.UpdateRecentAnswers(query_weather_lb, query_type); |
| 111 EXPECT_FALSE( |
| 112 cache.GetTopAnswerEntry(query_weather_lv).full_query_text.empty()); |
| 113 } |
OLD | NEW |