OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "components/gcm_driver/instance_id/instance_id_driver.h" | 5 #include "components/gcm_driver/instance_id/instance_id_driver.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
9 #include <cmath> | 9 #include <cmath> |
10 | 10 |
11 #include "base/bind.h" | 11 #include "base/bind.h" |
12 #include "base/macros.h" | 12 #include "base/macros.h" |
13 #include "base/run_loop.h" | 13 #include "base/run_loop.h" |
14 #include "base/strings/string_util.h" | 14 #include "base/strings/string_util.h" |
15 #include "base/test/scoped_task_environment.h" | 15 #include "base/test/scoped_task_environment.h" |
16 #include "components/gcm_driver/gcm_build_features.h" | 16 #include "components/gcm_driver/gcm_build_features.h" |
17 #include "components/gcm_driver/instance_id/fake_gcm_driver_for_instance_id.h" | 17 #include "components/gcm_driver/instance_id/fake_gcm_driver_for_instance_id.h" |
18 #include "components/gcm_driver/instance_id/instance_id.h" | 18 #include "components/gcm_driver/instance_id/instance_id.h" |
| 19 #include "components/gcm_driver/instance_id/instance_id_impl_factory.h" |
19 #include "testing/gtest/include/gtest/gtest.h" | 20 #include "testing/gtest/include/gtest/gtest.h" |
20 | 21 |
21 #if BUILDFLAG(USE_GCM_FROM_PLATFORM) | 22 #if BUILDFLAG(USE_GCM_FROM_PLATFORM) |
22 #include "components/gcm_driver/instance_id/instance_id_android.h" | 23 #include "components/gcm_driver/instance_id/instance_id_android.h" |
23 #include "components/gcm_driver/instance_id/scoped_use_fake_instance_id_android.
h" | 24 #include "components/gcm_driver/instance_id/scoped_use_fake_instance_id_android.
h" |
24 #endif // BUILDFLAG(USE_GCM_FROM_PLATFORM) | 25 #endif // BUILDFLAG(USE_GCM_FROM_PLATFORM) |
25 | 26 |
26 namespace instance_id { | 27 namespace instance_id { |
27 | 28 |
28 namespace { | 29 namespace { |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
83 | 84 |
84 private: | 85 private: |
85 void GetIDCompleted(const std::string& id); | 86 void GetIDCompleted(const std::string& id); |
86 void GetCreationTimeCompleted(const base::Time& creation_time); | 87 void GetCreationTimeCompleted(const base::Time& creation_time); |
87 void DeleteIDCompleted(InstanceID::Result result); | 88 void DeleteIDCompleted(InstanceID::Result result); |
88 void GetTokenCompleted(const std::string& token, InstanceID::Result result); | 89 void GetTokenCompleted(const std::string& token, InstanceID::Result result); |
89 void DeleteTokenCompleted(InstanceID::Result result); | 90 void DeleteTokenCompleted(InstanceID::Result result); |
90 | 91 |
91 base::test::ScopedTaskEnvironment scoped_task_environment_; | 92 base::test::ScopedTaskEnvironment scoped_task_environment_; |
92 std::unique_ptr<FakeGCMDriverForInstanceID> gcm_driver_; | 93 std::unique_ptr<FakeGCMDriverForInstanceID> gcm_driver_; |
| 94 std::unique_ptr<InstanceIDImplFactory> instance_id_factory_; |
93 std::unique_ptr<InstanceIDDriver> driver_; | 95 std::unique_ptr<InstanceIDDriver> driver_; |
94 | 96 |
95 #if BUILDFLAG(USE_GCM_FROM_PLATFORM) | 97 #if BUILDFLAG(USE_GCM_FROM_PLATFORM) |
96 InstanceIDAndroid::ScopedBlockOnAsyncTasksForTesting block_async_; | 98 InstanceIDAndroid::ScopedBlockOnAsyncTasksForTesting block_async_; |
97 ScopedUseFakeInstanceIDAndroid use_fake_; | 99 ScopedUseFakeInstanceIDAndroid use_fake_; |
98 #endif // BUILDFLAG(USE_GCM_FROM_PLATFORM) | 100 #endif // BUILDFLAG(USE_GCM_FROM_PLATFORM) |
99 | 101 |
100 std::string id_; | 102 std::string id_; |
101 base::Time creation_time_; | 103 base::Time creation_time_; |
102 std::string token_; | 104 std::string token_; |
103 InstanceID::Result result_; | 105 InstanceID::Result result_; |
104 | 106 |
105 bool async_operation_completed_; | 107 bool async_operation_completed_; |
106 base::Closure async_operation_completed_callback_; | 108 base::Closure async_operation_completed_callback_; |
107 | 109 |
108 DISALLOW_COPY_AND_ASSIGN(InstanceIDDriverTest); | 110 DISALLOW_COPY_AND_ASSIGN(InstanceIDDriverTest); |
109 }; | 111 }; |
110 | 112 |
111 InstanceIDDriverTest::InstanceIDDriverTest() | 113 InstanceIDDriverTest::InstanceIDDriverTest() |
112 : scoped_task_environment_( | 114 : scoped_task_environment_( |
113 base::test::ScopedTaskEnvironment::MainThreadType::UI), | 115 base::test::ScopedTaskEnvironment::MainThreadType::UI), |
114 result_(InstanceID::UNKNOWN_ERROR), | 116 result_(InstanceID::UNKNOWN_ERROR), |
115 async_operation_completed_(false) {} | 117 async_operation_completed_(false) {} |
116 | 118 |
117 InstanceIDDriverTest::~InstanceIDDriverTest() { | 119 InstanceIDDriverTest::~InstanceIDDriverTest() { |
118 } | 120 } |
119 | 121 |
120 void InstanceIDDriverTest::SetUp() { | 122 void InstanceIDDriverTest::SetUp() { |
121 gcm_driver_.reset(new FakeGCMDriverForInstanceID); | 123 gcm_driver_.reset(new FakeGCMDriverForInstanceID); |
| 124 instance_id_factory_.reset(new InstanceIDImplFactory); |
122 RecreateInstanceIDDriver(); | 125 RecreateInstanceIDDriver(); |
123 } | 126 } |
124 | 127 |
125 void InstanceIDDriverTest::TearDown() { | 128 void InstanceIDDriverTest::TearDown() { |
126 driver_.reset(); | 129 driver_.reset(); |
| 130 instance_id_factory_.reset(); |
127 gcm_driver_.reset(); | 131 gcm_driver_.reset(); |
128 // |gcm_driver_| owns a GCMKeyStore that owns a ProtoDatabaseImpl whose | 132 // |gcm_driver_| owns a GCMKeyStore that owns a ProtoDatabaseImpl whose |
129 // destructor deletes the underlying LevelDB on the task runner. | 133 // destructor deletes the underlying LevelDB on the task runner. |
130 base::RunLoop().RunUntilIdle(); | 134 base::RunLoop().RunUntilIdle(); |
131 } | 135 } |
132 | 136 |
133 void InstanceIDDriverTest::RecreateInstanceIDDriver() { | 137 void InstanceIDDriverTest::RecreateInstanceIDDriver() { |
134 driver_.reset(new InstanceIDDriver(gcm_driver_.get())); | 138 driver_.reset( |
| 139 new InstanceIDDriver(gcm_driver_.get(), instance_id_factory_.get())); |
135 } | 140 } |
136 | 141 |
137 void InstanceIDDriverTest::WaitForAsyncOperation() { | 142 void InstanceIDDriverTest::WaitForAsyncOperation() { |
138 // No need to wait if async operation is not needed. | 143 // No need to wait if async operation is not needed. |
139 if (async_operation_completed_) | 144 if (async_operation_completed_) |
140 return; | 145 return; |
141 base::RunLoop run_loop; | 146 base::RunLoop run_loop; |
142 async_operation_completed_callback_ = run_loop.QuitClosure(); | 147 async_operation_completed_callback_ = run_loop.QuitClosure(); |
143 run_loop.Run(); | 148 run_loop.Run(); |
144 } | 149 } |
(...skipping 227 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
372 GetToken(instance_id, kAuthorizedEntity1, kScope2, options); | 377 GetToken(instance_id, kAuthorizedEntity1, kScope2, options); |
373 EXPECT_FALSE(new_token1.empty()); | 378 EXPECT_FALSE(new_token1.empty()); |
374 EXPECT_NE(token1, new_token1); | 379 EXPECT_NE(token1, new_token1); |
375 | 380 |
376 // The other token is not affected by the deletion. | 381 // The other token is not affected by the deletion. |
377 EXPECT_EQ(token2, | 382 EXPECT_EQ(token2, |
378 GetToken(instance_id, kAuthorizedEntity2, kScope1, options)); | 383 GetToken(instance_id, kAuthorizedEntity2, kScope1, options)); |
379 } | 384 } |
380 | 385 |
381 } // namespace instance_id | 386 } // namespace instance_id |
OLD | NEW |