OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <utility> | 5 #include <utility> |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/files/file_util.h" | 8 #include "base/files/file_util.h" |
9 #include "base/files/scoped_temp_dir.h" | 9 #include "base/files/scoped_temp_dir.h" |
10 #include "base/hash.h" | 10 #include "base/hash.h" |
11 #include "base/logging.h" | 11 #include "base/logging.h" |
12 #include "base/memory/ptr_util.h" | 12 #include "base/memory/ptr_util.h" |
13 #include "base/message_loop/message_loop.h" | 13 #include "base/message_loop/message_loop.h" |
14 #include "base/run_loop.h" | 14 #include "base/run_loop.h" |
15 #include "base/single_thread_task_runner.h" | 15 #include "base/single_thread_task_runner.h" |
| 16 #include "base/test/scoped_task_environment.h" |
16 #include "base/threading/thread_task_runner_handle.h" | 17 #include "base/threading/thread_task_runner_handle.h" |
17 #include "chromeos/printing/ppd_cache.h" | 18 #include "chromeos/printing/ppd_cache.h" |
18 #include "net/url_request/test_url_request_interceptor.h" | 19 #include "net/url_request/test_url_request_interceptor.h" |
19 #include "net/url_request/url_request_test_util.h" | 20 #include "net/url_request/url_request_test_util.h" |
20 #include "testing/gtest/include/gtest/gtest.h" | 21 #include "testing/gtest/include/gtest/gtest.h" |
21 | 22 |
22 namespace chromeos { | 23 namespace chromeos { |
23 namespace printing { | 24 namespace printing { |
24 namespace { | 25 namespace { |
25 | 26 |
26 // This fixture just points the cache at a temporary directory for the life of | 27 // This fixture just points the cache at a temporary directory for the life of |
27 // the test. | 28 // the test. |
28 class PpdCacheTest : public ::testing::Test { | 29 class PpdCacheTest : public ::testing::Test { |
29 public: | 30 public: |
30 PpdCacheTest() : loop_(base::MessageLoop::TYPE_IO) {} | 31 PpdCacheTest() |
| 32 : scoped_task_environment_( |
| 33 base::test::ScopedTaskEnvironment::MainThreadType::IO) {} |
| 34 |
31 void SetUp() override { | 35 void SetUp() override { |
32 ASSERT_TRUE(ppd_cache_temp_dir_.CreateUniqueTempDir()); | 36 ASSERT_TRUE(ppd_cache_temp_dir_.CreateUniqueTempDir()); |
33 } | 37 } |
34 | 38 |
35 // Make and return a cache for the test that uses a temporary directory | 39 // Make and return a cache for the test that uses a temporary directory |
36 // which is cleaned up at the end of the test. Note that we pass | 40 // which is cleaned up at the end of the test. Note that we pass |
37 // a (nonexistant) subdirectory of temp_dir_ to the cache to exercise | 41 // a (nonexistant) subdirectory of temp_dir_ to the cache to exercise |
38 // the lazy-creation-of-the-cache-directory code. | 42 // the lazy-creation-of-the-cache-directory code. |
39 scoped_refptr<PpdCache> CreateTestCache() { | 43 scoped_refptr<PpdCache> CreateTestCache() { |
40 return PpdCache::Create(ppd_cache_temp_dir_.GetPath().Append("Cache"), | 44 return PpdCache::Create(ppd_cache_temp_dir_.GetPath().Append("Cache")); |
41 loop_.task_runner().get()); | |
42 } | 45 } |
43 | 46 |
44 void CaptureFindResult(const PpdCache::FindResult& result) { | 47 void CaptureFindResult(const PpdCache::FindResult& result) { |
45 ++captured_find_results_; | 48 ++captured_find_results_; |
46 find_result_ = result; | 49 find_result_ = result; |
47 } | 50 } |
48 | 51 |
49 void CaptureStoreResult() { ++captured_store_results_; } | 52 void CaptureStoreResult() { ++captured_store_results_; } |
50 | 53 |
51 protected: | 54 protected: |
| 55 // Environment for task schedulers. |
| 56 base::test::ScopedTaskEnvironment scoped_task_environment_; |
| 57 |
52 // Number of find results we've captured. | 58 // Number of find results we've captured. |
53 int captured_find_results_ = 0; | 59 int captured_find_results_ = 0; |
54 | 60 |
55 // Most recent captured result. | 61 // Most recent captured result. |
56 PpdCache::FindResult find_result_; | 62 PpdCache::FindResult find_result_; |
57 | 63 |
58 // Number of store callbacks we've seen. | 64 // Number of store callbacks we've seen. |
59 int captured_store_results_ = 0; | 65 int captured_store_results_ = 0; |
60 | 66 |
61 // Overrider for DIR_CHROMEOS_PPD_CACHE that points it at a temporary | 67 // Overrider for DIR_CHROMEOS_PPD_CACHE that points it at a temporary |
62 // directory for the life of the test. | 68 // directory for the life of the test. |
63 base::ScopedTempDir ppd_cache_temp_dir_; | 69 base::ScopedTempDir ppd_cache_temp_dir_; |
64 base::MessageLoop loop_; | |
65 }; | 70 }; |
66 | 71 |
67 | 72 |
68 // Test that we miss on an empty cache. | 73 // Test that we miss on an empty cache. |
69 TEST_F(PpdCacheTest, SimpleMiss) { | 74 TEST_F(PpdCacheTest, SimpleMiss) { |
70 auto cache = CreateTestCache(); | 75 auto cache = CreateTestCache(); |
71 cache->Find("foo", base::Bind(&PpdCacheTest::CaptureFindResult, | 76 cache->Find("foo", base::Bind(&PpdCacheTest::CaptureFindResult, |
72 base::Unretained(this))); | 77 base::Unretained(this))); |
73 base::RunLoop().RunUntilIdle(); | 78 scoped_task_environment_.RunUntilIdle(); |
74 EXPECT_EQ(captured_find_results_, 1); | 79 EXPECT_EQ(captured_find_results_, 1); |
75 EXPECT_FALSE(find_result_.success); | 80 EXPECT_FALSE(find_result_.success); |
76 } | 81 } |
77 | 82 |
78 TEST_F(PpdCacheTest, MissThenHit) { | 83 TEST_F(PpdCacheTest, MissThenHit) { |
79 auto cache = CreateTestCache(); | 84 auto cache = CreateTestCache(); |
80 const char kTestKey[] = "My totally awesome key"; | 85 const char kTestKey[] = "My totally awesome key"; |
81 const char kTestKey2[] = "A different key"; | 86 const char kTestKey2[] = "A different key"; |
82 const char kTestContents[] = "Like, totally awesome contents"; | 87 const char kTestContents[] = "Like, totally awesome contents"; |
83 | 88 |
84 cache->Find(kTestKey, base::Bind(&PpdCacheTest::CaptureFindResult, | 89 cache->Find(kTestKey, base::Bind(&PpdCacheTest::CaptureFindResult, |
85 base::Unretained(this))); | 90 base::Unretained(this))); |
86 base::RunLoop().RunUntilIdle(); | 91 scoped_task_environment_.RunUntilIdle(); |
87 EXPECT_EQ(captured_find_results_, 1); | 92 EXPECT_EQ(captured_find_results_, 1); |
88 EXPECT_FALSE(find_result_.success); | 93 EXPECT_FALSE(find_result_.success); |
89 | 94 |
90 cache->Store( | 95 cache->Store( |
91 kTestKey, kTestContents, | 96 kTestKey, kTestContents, |
92 base::Bind(&PpdCacheTest::CaptureStoreResult, base::Unretained(this))); | 97 base::Bind(&PpdCacheTest::CaptureStoreResult, base::Unretained(this))); |
93 base::RunLoop().RunUntilIdle(); | 98 scoped_task_environment_.RunUntilIdle(); |
94 EXPECT_EQ(captured_store_results_, 1); | 99 EXPECT_EQ(captured_store_results_, 1); |
95 | 100 |
96 cache->Find(kTestKey, base::Bind(&PpdCacheTest::CaptureFindResult, | 101 cache->Find(kTestKey, base::Bind(&PpdCacheTest::CaptureFindResult, |
97 base::Unretained(this))); | 102 base::Unretained(this))); |
98 base::RunLoop().RunUntilIdle(); | 103 scoped_task_environment_.RunUntilIdle(); |
99 EXPECT_EQ(captured_find_results_, 2); | 104 EXPECT_EQ(captured_find_results_, 2); |
100 EXPECT_TRUE(find_result_.success); | 105 EXPECT_TRUE(find_result_.success); |
101 EXPECT_EQ(find_result_.contents, kTestContents); | 106 EXPECT_EQ(find_result_.contents, kTestContents); |
102 EXPECT_LT(find_result_.age, base::TimeDelta::FromMinutes(5)); | 107 EXPECT_LT(find_result_.age, base::TimeDelta::FromMinutes(5)); |
103 | 108 |
104 cache->Find(kTestKey2, base::Bind(&PpdCacheTest::CaptureFindResult, | 109 cache->Find(kTestKey2, base::Bind(&PpdCacheTest::CaptureFindResult, |
105 base::Unretained(this))); | 110 base::Unretained(this))); |
106 base::RunLoop().RunUntilIdle(); | 111 scoped_task_environment_.RunUntilIdle(); |
107 EXPECT_EQ(captured_find_results_, 3); | 112 EXPECT_EQ(captured_find_results_, 3); |
108 EXPECT_FALSE(find_result_.success); | 113 EXPECT_FALSE(find_result_.success); |
109 } | 114 } |
110 | 115 |
111 } // namespace | 116 } // namespace |
112 } // namespace printing | 117 } // namespace printing |
113 } // namespace chromeos | 118 } // namespace chromeos |
OLD | NEW |