ppd_cache_unittest.cc 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. // Copyright 2016 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 <utility>
  5. #include "base/bind.h"
  6. #include "base/files/file_util.h"
  7. #include "base/files/scoped_temp_dir.h"
  8. #include "base/hash/hash.h"
  9. #include "base/memory/ptr_util.h"
  10. #include "base/run_loop.h"
  11. #include "base/task/single_thread_task_runner.h"
  12. #include "base/test/task_environment.h"
  13. #include "base/threading/thread_task_runner_handle.h"
  14. #include "chromeos/printing/ppd_cache.h"
  15. #include "testing/gtest/include/gtest/gtest.h"
  16. namespace chromeos {
  17. namespace {
  18. // This fixture just points the cache at a temporary directory for the life of
  19. // the test.
  20. class PpdCacheTest : public ::testing::Test {
  21. public:
  22. PpdCacheTest()
  23. : task_environment_(base::test::TaskEnvironment::MainThreadType::IO) {}
  24. void SetUp() override {
  25. ASSERT_TRUE(ppd_cache_temp_dir_.CreateUniqueTempDir());
  26. }
  27. // Make and return a cache for the test that uses a temporary directory
  28. // which is cleaned up at the end of the test. Note that we pass
  29. // a (nonexistant) subdirectory of temp_dir_ to the cache to exercise
  30. // the lazy-creation-of-the-cache-directory code.
  31. scoped_refptr<PpdCache> CreateTestCache() {
  32. return PpdCache::CreateForTesting(
  33. ppd_cache_temp_dir_.GetPath().Append("Cache"),
  34. task_environment_.GetMainThreadTaskRunner());
  35. }
  36. void CaptureFindResult(const PpdCache::FindResult& result) {
  37. ++captured_find_results_;
  38. find_result_ = result;
  39. }
  40. protected:
  41. // Environment for task schedulers.
  42. base::test::TaskEnvironment task_environment_;
  43. // Number of find results we've captured.
  44. int captured_find_results_ = 0;
  45. // Most recent captured result.
  46. PpdCache::FindResult find_result_;
  47. // Overrider for DIR_CHROMEOS_PPD_CACHE that points it at a temporary
  48. // directory for the life of the test.
  49. base::ScopedTempDir ppd_cache_temp_dir_;
  50. };
  51. // Test that we miss on an empty cache.
  52. TEST_F(PpdCacheTest, SimpleMiss) {
  53. auto cache = CreateTestCache();
  54. cache->Find("foo", base::BindOnce(&PpdCacheTest::CaptureFindResult,
  55. base::Unretained(this)));
  56. task_environment_.RunUntilIdle();
  57. EXPECT_EQ(captured_find_results_, 1);
  58. EXPECT_FALSE(find_result_.success);
  59. }
  60. TEST_F(PpdCacheTest, MissThenHit) {
  61. auto cache = CreateTestCache();
  62. const char kTestKey[] = "My totally awesome key";
  63. const char kTestKey2[] = "A different key";
  64. const char kTestContents[] = "Like, totally awesome contents";
  65. cache->Find(kTestKey, base::BindOnce(&PpdCacheTest::CaptureFindResult,
  66. base::Unretained(this)));
  67. task_environment_.RunUntilIdle();
  68. EXPECT_EQ(captured_find_results_, 1);
  69. EXPECT_FALSE(find_result_.success);
  70. cache->Store(kTestKey, kTestContents);
  71. task_environment_.RunUntilIdle();
  72. cache->Find(kTestKey, base::BindOnce(&PpdCacheTest::CaptureFindResult,
  73. base::Unretained(this)));
  74. task_environment_.RunUntilIdle();
  75. EXPECT_EQ(captured_find_results_, 2);
  76. EXPECT_TRUE(find_result_.success);
  77. EXPECT_EQ(find_result_.contents, kTestContents);
  78. EXPECT_LT(find_result_.age, base::Minutes(5));
  79. cache->Find(kTestKey2, base::BindOnce(&PpdCacheTest::CaptureFindResult,
  80. base::Unretained(this)));
  81. task_environment_.RunUntilIdle();
  82. EXPECT_EQ(captured_find_results_, 3);
  83. EXPECT_FALSE(find_result_.success);
  84. }
  85. // Test that we fill in the age field with something plausible.
  86. TEST_F(PpdCacheTest, HitAge) {
  87. auto cache = CreateTestCache();
  88. const char kTestKey[] = "My totally awesome key";
  89. const char kTestContents[] = "Like, totally awesome contents";
  90. cache->Store(kTestKey, kTestContents);
  91. task_environment_.RunUntilIdle();
  92. cache->Find(kTestKey, base::BindOnce(&PpdCacheTest::CaptureFindResult,
  93. base::Unretained(this)));
  94. task_environment_.RunUntilIdle();
  95. EXPECT_EQ(captured_find_results_, 1);
  96. // The age should be well under a second, but accept anything under an hour.
  97. EXPECT_LT(find_result_.age, base::Hours(1));
  98. }
  99. } // namespace
  100. } // namespace chromeos