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 "content/public/test/nested_message_pump_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 14 matching lines...) Expand all Loading... | |
44 } | 49 } |
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: |
59 void TearDown() override; | |
54 void SetUp() override; | 60 void SetUp() 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); |
(...skipping 10 matching lines...) Expand all Loading... | |
74 | 80 |
75 InstanceIDDriver* driver() const { return driver_.get(); } | 81 InstanceIDDriver* driver() const { return driver_.get(); } |
76 | 82 |
77 private: | 83 private: |
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 scoped_ptr<base::MessageLoop> 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 |
88 std::string id_; | 94 std::string id_; |
89 base::Time creation_time_; | 95 base::Time creation_time_; |
90 std::string token_; | 96 std::string token_; |
91 InstanceID::Result result_; | 97 InstanceID::Result result_; |
92 | 98 |
93 bool async_operation_completed_; | 99 bool async_operation_completed_; |
94 base::Closure async_operation_completed_callback_; | 100 base::Closure async_operation_completed_callback_; |
95 | 101 |
96 DISALLOW_COPY_AND_ASSIGN(InstanceIDDriverTest); | 102 DISALLOW_COPY_AND_ASSIGN(InstanceIDDriverTest); |
97 }; | 103 }; |
98 | 104 |
99 InstanceIDDriverTest::InstanceIDDriverTest() | 105 InstanceIDDriverTest::InstanceIDDriverTest() |
100 : result_(InstanceID::UNKNOWN_ERROR), | 106 : result_(InstanceID::UNKNOWN_ERROR), |
101 async_operation_completed_(false) { | 107 async_operation_completed_(false) { |
108 #if defined(OS_ANDROID) | |
109 // Need a NestedMessagePumpAndroid so WaitForAsyncOperation's RunLoop will run | |
110 // InstanceIDWithSubtype.java's AsyncTask onPostExecute methods. | |
111 message_loop_.reset(new base::MessageLoop( | |
112 scoped_ptr<base::MessagePump>(new content::NestedMessagePumpAndroid()))); | |
113 static_cast<base::MessageLoopForUI*>(message_loop_.get())->Start(); | |
Peter Beverloo
2016/04/11 14:57:05
You're casting an instance of base::MessageLoop to
johnme
2016/04/14 18:32:42
Bizarrely, this seems to be what you're supposed t
| |
114 #else | |
115 message_loop_.reset(new base::MessageLoopForUI()); | |
116 #endif // OS_ANDROID | |
102 } | 117 } |
103 | 118 |
104 InstanceIDDriverTest::~InstanceIDDriverTest() { | 119 InstanceIDDriverTest::~InstanceIDDriverTest() { |
105 } | 120 } |
106 | 121 |
107 void InstanceIDDriverTest::SetUp() { | 122 void InstanceIDDriverTest::SetUp() { |
123 #if defined(OS_ANDROID) | |
124 InstanceIDAndroid::ClearDataAndSetUseFakeForTesting(true); | |
125 #endif // OS_ANDROID | |
108 gcm_driver_.reset(new FakeGCMDriverForInstanceID); | 126 gcm_driver_.reset(new FakeGCMDriverForInstanceID); |
109 RecreateInstanceIDDriver(); | 127 RecreateInstanceIDDriver(); |
110 } | 128 } |
111 | 129 |
130 void InstanceIDDriverTest::TearDown() { | |
131 #if defined(OS_ANDROID) | |
132 InstanceIDAndroid::ClearDataAndSetUseFakeForTesting(false); | |
133 #endif // OS_ANDROID | |
134 } | |
135 | |
112 void InstanceIDDriverTest::RecreateInstanceIDDriver() { | 136 void InstanceIDDriverTest::RecreateInstanceIDDriver() { |
113 driver_.reset(new InstanceIDDriver(gcm_driver_.get())); | 137 driver_.reset(new InstanceIDDriver(gcm_driver_.get())); |
114 } | 138 } |
115 | 139 |
116 void InstanceIDDriverTest::WaitForAsyncOperation() { | 140 void InstanceIDDriverTest::WaitForAsyncOperation() { |
117 // No need to wait if async operation is not needed. | 141 // No need to wait if async operation is not needed. |
118 if (async_operation_completed_) | 142 if (async_operation_completed_) |
119 return; | 143 return; |
120 base::RunLoop run_loop; | 144 base::RunLoop run_loop; |
121 async_operation_completed_callback_ = run_loop.QuitClosure(); | 145 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); | 375 GetToken(instance_id, kAuthorizedEntity1, kScope2, options); |
352 EXPECT_FALSE(new_token1.empty()); | 376 EXPECT_FALSE(new_token1.empty()); |
353 EXPECT_NE(token1, new_token1); | 377 EXPECT_NE(token1, new_token1); |
354 | 378 |
355 // The other token is not affected by the deletion. | 379 // The other token is not affected by the deletion. |
356 EXPECT_EQ(token2, | 380 EXPECT_EQ(token2, |
357 GetToken(instance_id, kAuthorizedEntity2, kScope1, options)); | 381 GetToken(instance_id, kAuthorizedEntity2, kScope1, options)); |
358 } | 382 } |
359 | 383 |
360 } // namespace instance_id | 384 } // namespace instance_id |
OLD | NEW |