assistant_query_history_unittest.cc 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. // Copyright 2018 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. #include "ash/assistant/model/assistant_query_history.h"
  5. #include "testing/gtest/include/gtest/gtest.h"
  6. namespace ash {
  7. // Assert that iterator Prev() or Next() does not crash on an empty history.
  8. TEST(AssistantQueryHistory, Empty) {
  9. AssistantQueryHistory history(10);
  10. auto it = history.GetIterator();
  11. EXPECT_EQ(absl::nullopt, it->Next());
  12. EXPECT_EQ(absl::nullopt, it->Prev());
  13. EXPECT_EQ(absl::nullopt, it->Next());
  14. EXPECT_EQ(absl::nullopt, it->Next());
  15. EXPECT_EQ(absl::nullopt, it->Next());
  16. EXPECT_EQ(absl::nullopt, it->Prev());
  17. }
  18. TEST(AssistantQueryHistory, Full) {
  19. int size = 3;
  20. AssistantQueryHistory history(size);
  21. // Make more queries than history limit.
  22. for (int i = 0; i <= size; i++)
  23. history.Add(std::to_string(i));
  24. auto it = history.GetIterator();
  25. // Assert history only contains last 3 queries.
  26. for (int i = size; i > 0; i--)
  27. EXPECT_EQ(std::to_string(i), it->Prev().value());
  28. EXPECT_EQ(std::to_string(1), it->Prev().value());
  29. // Assert that iterate does not pass first query.
  30. EXPECT_EQ(std::to_string(1), it->Prev().value());
  31. // Make more queries than history limit again.
  32. for (int i = 0; i <= size; i++)
  33. history.Add(std::to_string(i + 4));
  34. it->ResetToLast();
  35. // Assert that history only contains last 3 queries.
  36. for (int i = size; i > 0; i--)
  37. EXPECT_EQ(std::to_string(i + 4), it->Prev());
  38. EXPECT_EQ(std::to_string(5), it->Prev().value());
  39. // Assert that iterate does not pass first query.
  40. EXPECT_EQ(std::to_string(5), it->Prev().value());
  41. }
  42. TEST(AssistantQueryHistory, Add) {
  43. AssistantQueryHistory history(10);
  44. auto it = history.GetIterator();
  45. EXPECT_EQ(absl::nullopt, it->Next());
  46. EXPECT_EQ(absl::nullopt, it->Next());
  47. EXPECT_EQ(absl::nullopt, it->Next());
  48. EXPECT_EQ(absl::nullopt, it->Next());
  49. history.Add("Query01");
  50. history.Add("Query02");
  51. it = history.GetIterator();
  52. EXPECT_EQ(absl::nullopt, it->Next());
  53. EXPECT_EQ("Query02", it->Prev().value());
  54. EXPECT_EQ("Query01", it->Prev().value());
  55. EXPECT_EQ("Query01", it->Prev().value());
  56. EXPECT_EQ("Query02", it->Next().value());
  57. EXPECT_EQ(absl::nullopt, it->Next());
  58. EXPECT_EQ(absl::nullopt, it->Next());
  59. }
  60. } // namespace ash