| 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/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/strings/string_util.h" | 15 #include "base/strings/string_util.h" |
| 16 #include "components/gcm_driver/instance_id/fake_gcm_driver_for_instance_id.h" | 16 #include "components/gcm_driver/instance_id/fake_gcm_driver_for_instance_id.h" |
| 17 #include "components/gcm_driver/instance_id/instance_id.h" | 17 #include "components/gcm_driver/instance_id/instance_id.h" |
| 18 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
| 19 | 19 |
| 20 #if defined(OS_ANDROID) |
| 21 #include "components/gcm_driver/instance_id/instance_id_android.h" |
| 22 #include "components/gcm_driver/instance_id/scoped_use_fake_instance_id_android.
h" |
| 23 #endif // OS_ANDROID |
| 24 |
| 20 namespace instance_id { | 25 namespace instance_id { |
| 21 | 26 |
| 22 namespace { | 27 namespace { |
| 23 | 28 |
| 24 const char kTestAppID1[] = "TestApp1"; | 29 const char kTestAppID1[] = "TestApp1"; |
| 25 const char kTestAppID2[] = "TestApp2"; | 30 const char kTestAppID2[] = "TestApp2"; |
| 26 const char kAuthorizedEntity1[] = "Sender 1"; | 31 const char kAuthorizedEntity1[] = "Sender 1"; |
| 27 const char kAuthorizedEntity2[] = "Sender 2"; | 32 const char kAuthorizedEntity2[] = "Sender 2"; |
| 28 const char kScope1[] = "GCM1"; | 33 const char kScope1[] = "GCM1"; |
| 29 const char kScope2[] = "FooBar"; | 34 const char kScope2[] = "FooBar"; |
| (...skipping 15 matching lines...) Expand all Loading... |
| 45 | 50 |
| 46 } // namespace | 51 } // namespace |
| 47 | 52 |
| 48 class InstanceIDDriverTest : public testing::Test { | 53 class InstanceIDDriverTest : public testing::Test { |
| 49 public: | 54 public: |
| 50 InstanceIDDriverTest(); | 55 InstanceIDDriverTest(); |
| 51 ~InstanceIDDriverTest() override; | 56 ~InstanceIDDriverTest() override; |
| 52 | 57 |
| 53 // testing::Test: | 58 // testing::Test: |
| 54 void SetUp() override; | 59 void SetUp() override; |
| 60 void TearDown() override; |
| 55 | 61 |
| 56 void WaitForAsyncOperation(); | 62 void WaitForAsyncOperation(); |
| 57 | 63 |
| 58 // Recreates InstanceIDDriver to simulate restart. | 64 // Recreates InstanceIDDriver to simulate restart. |
| 59 void RecreateInstanceIDDriver(); | 65 void RecreateInstanceIDDriver(); |
| 60 | 66 |
| 61 // Sync wrappers for async version. | 67 // Sync wrappers for async version. |
| 62 std::string GetID(InstanceID* instance_id); | 68 std::string GetID(InstanceID* instance_id); |
| 63 base::Time GetCreationTime(InstanceID* instance_id); | 69 base::Time GetCreationTime(InstanceID* instance_id); |
| 64 InstanceID::Result DeleteID(InstanceID* instance_id); | 70 InstanceID::Result DeleteID(InstanceID* instance_id); |
| (...skipping 13 matching lines...) Expand all Loading... |
| 78 void GetIDCompleted(const std::string& id); | 84 void GetIDCompleted(const std::string& id); |
| 79 void GetCreationTimeCompleted(const base::Time& creation_time); | 85 void GetCreationTimeCompleted(const base::Time& creation_time); |
| 80 void DeleteIDCompleted(InstanceID::Result result); | 86 void DeleteIDCompleted(InstanceID::Result result); |
| 81 void GetTokenCompleted(const std::string& token, InstanceID::Result result); | 87 void GetTokenCompleted(const std::string& token, InstanceID::Result result); |
| 82 void DeleteTokenCompleted(InstanceID::Result result); | 88 void DeleteTokenCompleted(InstanceID::Result result); |
| 83 | 89 |
| 84 base::MessageLoopForUI message_loop_; | 90 base::MessageLoopForUI message_loop_; |
| 85 scoped_ptr<FakeGCMDriverForInstanceID> gcm_driver_; | 91 scoped_ptr<FakeGCMDriverForInstanceID> gcm_driver_; |
| 86 scoped_ptr<InstanceIDDriver> driver_; | 92 scoped_ptr<InstanceIDDriver> driver_; |
| 87 | 93 |
| 94 #if defined(OS_ANDROID) |
| 95 InstanceIDAndroid::ScopedBlockOnAsyncTasksForTesting block_async_; |
| 96 ScopedUseFakeInstanceIDAndroid use_fake_; |
| 97 #endif // OS_ANDROID |
| 98 |
| 88 std::string id_; | 99 std::string id_; |
| 89 base::Time creation_time_; | 100 base::Time creation_time_; |
| 90 std::string token_; | 101 std::string token_; |
| 91 InstanceID::Result result_; | 102 InstanceID::Result result_; |
| 92 | 103 |
| 93 bool async_operation_completed_; | 104 bool async_operation_completed_; |
| 94 base::Closure async_operation_completed_callback_; | 105 base::Closure async_operation_completed_callback_; |
| 95 | 106 |
| 96 DISALLOW_COPY_AND_ASSIGN(InstanceIDDriverTest); | 107 DISALLOW_COPY_AND_ASSIGN(InstanceIDDriverTest); |
| 97 }; | 108 }; |
| 98 | 109 |
| 99 InstanceIDDriverTest::InstanceIDDriverTest() | 110 InstanceIDDriverTest::InstanceIDDriverTest() |
| 100 : result_(InstanceID::UNKNOWN_ERROR), | 111 : result_(InstanceID::UNKNOWN_ERROR), |
| 101 async_operation_completed_(false) { | 112 async_operation_completed_(false) { |
| 102 } | 113 } |
| 103 | 114 |
| 104 InstanceIDDriverTest::~InstanceIDDriverTest() { | 115 InstanceIDDriverTest::~InstanceIDDriverTest() { |
| 105 } | 116 } |
| 106 | 117 |
| 107 void InstanceIDDriverTest::SetUp() { | 118 void InstanceIDDriverTest::SetUp() { |
| 108 gcm_driver_.reset(new FakeGCMDriverForInstanceID); | 119 gcm_driver_.reset(new FakeGCMDriverForInstanceID); |
| 109 RecreateInstanceIDDriver(); | 120 RecreateInstanceIDDriver(); |
| 110 } | 121 } |
| 111 | 122 |
| 123 void InstanceIDDriverTest::TearDown() {} |
| 124 |
| 112 void InstanceIDDriverTest::RecreateInstanceIDDriver() { | 125 void InstanceIDDriverTest::RecreateInstanceIDDriver() { |
| 113 driver_.reset(new InstanceIDDriver(gcm_driver_.get())); | 126 driver_.reset(new InstanceIDDriver(gcm_driver_.get())); |
| 114 } | 127 } |
| 115 | 128 |
| 116 void InstanceIDDriverTest::WaitForAsyncOperation() { | 129 void InstanceIDDriverTest::WaitForAsyncOperation() { |
| 117 // No need to wait if async operation is not needed. | 130 // No need to wait if async operation is not needed. |
| 118 if (async_operation_completed_) | 131 if (async_operation_completed_) |
| 119 return; | 132 return; |
| 120 base::RunLoop run_loop; | 133 base::RunLoop run_loop; |
| 121 async_operation_completed_callback_ = run_loop.QuitClosure(); | 134 async_operation_completed_callback_ = run_loop.QuitClosure(); |
| (...skipping 229 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 351 GetToken(instance_id, kAuthorizedEntity1, kScope2, options); | 364 GetToken(instance_id, kAuthorizedEntity1, kScope2, options); |
| 352 EXPECT_FALSE(new_token1.empty()); | 365 EXPECT_FALSE(new_token1.empty()); |
| 353 EXPECT_NE(token1, new_token1); | 366 EXPECT_NE(token1, new_token1); |
| 354 | 367 |
| 355 // The other token is not affected by the deletion. | 368 // The other token is not affected by the deletion. |
| 356 EXPECT_EQ(token2, | 369 EXPECT_EQ(token2, |
| 357 GetToken(instance_id, kAuthorizedEntity2, kScope1, options)); | 370 GetToken(instance_id, kAuthorizedEntity2, kScope1, options)); |
| 358 } | 371 } |
| 359 | 372 |
| 360 } // namespace instance_id | 373 } // namespace instance_id |
| OLD | NEW |