| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 <algorithm> | 5 #include <algorithm> |
| 6 #include <string> | 6 #include <string> |
| 7 #include <vector> | 7 #include <vector> |
| 8 | 8 |
| 9 #include "base/message_loop/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
| 10 #include "base/strings/stringprintf.h" |
| 10 #include "chrome/browser/signin/google_auto_login_helper.h" | 11 #include "chrome/browser/signin/google_auto_login_helper.h" |
| 11 #include "chrome/test/base/testing_profile.h" | 12 #include "chrome/test/base/testing_profile.h" |
| 13 #include "content/public/test/test_browser_thread_bundle.h" |
| 12 #include "testing/gmock/include/gmock/gmock.h" | 14 #include "testing/gmock/include/gmock/gmock.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
| 14 | 16 |
| 15 namespace { | 17 namespace { |
| 16 | 18 |
| 17 class MockUbertokenFetcher : public UbertokenFetcher { | |
| 18 public: | |
| 19 MockUbertokenFetcher(Profile* profile, UbertokenConsumer* consumer) : | |
| 20 UbertokenFetcher(profile, consumer), account_id("") {} | |
| 21 | |
| 22 void completePendingRequest() { | |
| 23 OnUberAuthTokenSuccess("mock token: " + account_id); | |
| 24 } | |
| 25 | |
| 26 virtual void StartFetchingToken(const std::string& id) OVERRIDE { | |
| 27 account_id = id; | |
| 28 } | |
| 29 | |
| 30 std::string account_id; | |
| 31 }; | |
| 32 | |
| 33 // Counts number of InstrumentedGoogleAutoLoginHelper created. | 19 // Counts number of InstrumentedGoogleAutoLoginHelper created. |
| 34 // We can EXPECT_* to be zero at the end of our unit tests | 20 // We can EXPECT_* to be zero at the end of our unit tests |
| 35 // to make sure everything is properly deleted. | 21 // to make sure everything is properly deleted. |
| 36 | 22 |
| 37 int total = 0; | 23 int total = 0; |
| 38 | 24 |
| 39 class InstrumentedGoogleAutoLoginHelper : public GoogleAutoLoginHelper { | 25 class InstrumentedGoogleAutoLoginHelper : public GoogleAutoLoginHelper { |
| 40 public: | 26 public: |
| 41 std::vector<MockUbertokenFetcher*>* mock_uber_fetchers_; | 27 explicit InstrumentedGoogleAutoLoginHelper(Profile* profile) : |
| 42 TestingProfile profile_; | 28 GoogleAutoLoginHelper(profile) { |
| 43 | |
| 44 InstrumentedGoogleAutoLoginHelper( | |
| 45 std::vector<MockUbertokenFetcher*>* fetchers) : | |
| 46 GoogleAutoLoginHelper(0), mock_uber_fetchers_(fetchers) { | |
| 47 total++; | 29 total++; |
| 48 } | 30 } |
| 49 | 31 |
| 50 virtual ~InstrumentedGoogleAutoLoginHelper() { | 32 virtual ~InstrumentedGoogleAutoLoginHelper() { |
| 51 total--; | 33 total--; |
| 52 } | 34 } |
| 53 | 35 |
| 54 virtual void OnUbertokenSuccess(const std::string& token) OVERRIDE { | 36 MOCK_METHOD0(StartFetching, void()); |
| 55 OnMergeSessionSuccess(token); | 37 MOCK_METHOD1(SignalComplete, |
| 56 } | 38 void(const GoogleServiceAuthError& error)); |
| 57 | 39 |
| 58 virtual UbertokenFetcher* CreateNewUbertokenFetcher() OVERRIDE { | 40 DISALLOW_COPY_AND_ASSIGN(InstrumentedGoogleAutoLoginHelper); |
| 59 MockUbertokenFetcher* m = new MockUbertokenFetcher(&profile_, this); | |
| 60 mock_uber_fetchers_->push_back(m); | |
| 61 return m; | |
| 62 } | |
| 63 | |
| 64 void completePendingFetchers() { | |
| 65 while (!mock_uber_fetchers_->empty()) { | |
| 66 MockUbertokenFetcher* fetcher = mock_uber_fetchers_->back(); | |
| 67 mock_uber_fetchers_->pop_back(); | |
| 68 fetcher->completePendingRequest(); | |
| 69 } | |
| 70 } | |
| 71 | |
| 72 MOCK_METHOD1(OnMergeSessionSuccess, void(const std::string& uber_token)); | |
| 73 | |
| 74 void OnMergeSessionSuccessConcrete(const std::string& uber_token) { | |
| 75 GoogleAutoLoginHelper::OnMergeSessionSuccess(uber_token); | |
| 76 } | |
| 77 }; | 41 }; |
| 78 | 42 |
| 79 class GoogleAutoLoginHelperTest : public testing::Test { | 43 class GoogleAutoLoginHelperTest : public testing::Test { |
| 80 public: | 44 public: |
| 81 std::vector<MockUbertokenFetcher*> mock_uber_fetchers_; | 45 GoogleAutoLoginHelperTest() |
| 82 base::MessageLoop message_loop_; | 46 : no_error_(GoogleServiceAuthError::NONE), |
| 47 error_(GoogleServiceAuthError::SERVICE_ERROR) {} |
| 48 |
| 49 TestingProfile* profile() { return &profile_; } |
| 50 |
| 51 void SimulateUbertokenFailure(UbertokenConsumer* consumer, |
| 52 const GoogleServiceAuthError& error) { |
| 53 consumer->OnUbertokenFailure(error); |
| 54 } |
| 55 |
| 56 void SimulateMergeSessionSuccess(GaiaAuthConsumer* consumer, |
| 57 const std::string& data) { |
| 58 consumer->OnMergeSessionSuccess(data); |
| 59 } |
| 60 |
| 61 void SimulateMergeSessionFailure(GaiaAuthConsumer* consumer, |
| 62 const GoogleServiceAuthError& error) { |
| 63 consumer->OnMergeSessionFailure(error); |
| 64 } |
| 65 |
| 66 const GoogleServiceAuthError& no_error() { return no_error_; } |
| 67 const GoogleServiceAuthError& error() { return error_; } |
| 68 |
| 69 private: |
| 70 content::TestBrowserThreadBundle thread_bundle_; |
| 71 TestingProfile profile_; |
| 72 GoogleServiceAuthError no_error_; |
| 73 GoogleServiceAuthError error_; |
| 83 }; | 74 }; |
| 84 | 75 |
| 85 } // namespace | 76 } // namespace |
| 86 | 77 |
| 87 using ::testing::Invoke; | 78 using ::testing::Invoke; |
| 88 using ::testing::_; | 79 using ::testing::_; |
| 89 | 80 |
| 90 TEST_F(GoogleAutoLoginHelperTest, AllRequestsInOneGo) { | 81 TEST_F(GoogleAutoLoginHelperTest, Success) { |
| 82 InstrumentedGoogleAutoLoginHelper helper(profile()); |
| 83 helper.disable_auto_delete_when_done(); |
| 84 |
| 85 EXPECT_CALL(helper, StartFetching()); |
| 86 EXPECT_CALL(helper, SignalComplete(no_error())); |
| 87 |
| 88 helper.LogIn("acc1@gmail.com"); |
| 89 SimulateMergeSessionSuccess(&helper, "token"); |
| 90 } |
| 91 |
| 92 TEST_F(GoogleAutoLoginHelperTest, FailedMergeSession) { |
| 93 InstrumentedGoogleAutoLoginHelper helper(profile()); |
| 94 helper.disable_auto_delete_when_done(); |
| 95 |
| 96 EXPECT_CALL(helper, StartFetching()); |
| 97 EXPECT_CALL(helper, SignalComplete(error())); |
| 98 |
| 99 helper.LogIn("acc1@gmail.com"); |
| 100 SimulateMergeSessionFailure(&helper, error()); |
| 101 } |
| 102 |
| 103 TEST_F(GoogleAutoLoginHelperTest, FailedUbertoken) { |
| 104 InstrumentedGoogleAutoLoginHelper helper(profile()); |
| 105 helper.disable_auto_delete_when_done(); |
| 106 |
| 107 EXPECT_CALL(helper, StartFetching()); |
| 108 EXPECT_CALL(helper, SignalComplete(error())); |
| 109 |
| 110 helper.LogIn("acc1@gmail.com"); |
| 111 SimulateUbertokenFailure(&helper, error()); |
| 112 } |
| 113 |
| 114 TEST_F(GoogleAutoLoginHelperTest, ContinueAfterSuccess) { |
| 115 InstrumentedGoogleAutoLoginHelper helper(profile()); |
| 116 helper.disable_auto_delete_when_done(); |
| 117 |
| 118 testing::InSequence sequence; |
| 119 EXPECT_CALL(helper, StartFetching()); |
| 120 EXPECT_CALL(helper, SignalComplete(no_error())); |
| 121 EXPECT_CALL(helper, StartFetching()); |
| 122 EXPECT_CALL(helper, SignalComplete(no_error())); |
| 123 |
| 124 helper.LogIn("acc1@gmail.com"); |
| 125 helper.LogIn("acc2@gmail.com"); |
| 126 SimulateMergeSessionSuccess(&helper, "data"); |
| 127 SimulateMergeSessionSuccess(&helper, "data"); |
| 128 } |
| 129 |
| 130 TEST_F(GoogleAutoLoginHelperTest, ContinueAfterFailure1) { |
| 131 InstrumentedGoogleAutoLoginHelper helper(profile()); |
| 132 helper.disable_auto_delete_when_done(); |
| 133 |
| 134 testing::InSequence sequence; |
| 135 EXPECT_CALL(helper, StartFetching()); |
| 136 EXPECT_CALL(helper, SignalComplete(error())); |
| 137 EXPECT_CALL(helper, StartFetching()); |
| 138 EXPECT_CALL(helper, SignalComplete(no_error())); |
| 139 |
| 140 helper.LogIn("acc1@gmail.com"); |
| 141 helper.LogIn("acc2@gmail.com"); |
| 142 SimulateMergeSessionFailure(&helper, error()); |
| 143 SimulateMergeSessionSuccess(&helper, "data"); |
| 144 } |
| 145 |
| 146 TEST_F(GoogleAutoLoginHelperTest, ContinueAfterFailure2) { |
| 147 InstrumentedGoogleAutoLoginHelper helper(profile()); |
| 148 helper.disable_auto_delete_when_done(); |
| 149 |
| 150 testing::InSequence sequence; |
| 151 EXPECT_CALL(helper, StartFetching()); |
| 152 EXPECT_CALL(helper, SignalComplete(error())); |
| 153 EXPECT_CALL(helper, StartFetching()); |
| 154 EXPECT_CALL(helper, SignalComplete(no_error())); |
| 155 |
| 156 helper.LogIn("acc1@gmail.com"); |
| 157 helper.LogIn("acc2@gmail.com"); |
| 158 SimulateUbertokenFailure(&helper, error()); |
| 159 SimulateMergeSessionSuccess(&helper, "data"); |
| 160 } |
| 161 |
| 162 TEST_F(GoogleAutoLoginHelperTest, AllRequestsInMultipleGoes) { |
| 163 InstrumentedGoogleAutoLoginHelper helper(profile()); |
| 164 helper.disable_auto_delete_when_done(); |
| 165 |
| 166 const int kNumAccounts = 4; |
| 167 |
| 168 testing::InSequence sequence; |
| 169 for (int i = 0; i < kNumAccounts; ++i) { |
| 170 EXPECT_CALL(helper, StartFetching()); |
| 171 EXPECT_CALL(helper, SignalComplete(no_error())); |
| 172 } |
| 173 |
| 174 helper.LogIn("acc1@gmail.com"); |
| 175 helper.LogIn("acc2@gmail.com"); |
| 176 |
| 177 SimulateMergeSessionSuccess(&helper, "token1"); |
| 178 |
| 179 helper.LogIn("acc3@gmail.com"); |
| 180 |
| 181 SimulateMergeSessionSuccess(&helper, "token2"); |
| 182 SimulateMergeSessionSuccess(&helper, "token3"); |
| 183 |
| 184 helper.LogIn("acc4@gmail.com"); |
| 185 |
| 186 SimulateMergeSessionSuccess(&helper, "token4"); |
| 187 } |
| 188 |
| 189 TEST_F(GoogleAutoLoginHelperTest, AutoDelete) { |
| 91 total = 0; | 190 total = 0; |
| 92 | 191 |
| 93 InstrumentedGoogleAutoLoginHelper* helper = | 192 // In auto delete mode, the helper deletes itself when done. So the same |
| 94 new InstrumentedGoogleAutoLoginHelper(&mock_uber_fetchers_); | 193 // variable is used with all new instances. The code checks at the end that |
| 95 EXPECT_CALL(*helper, OnMergeSessionSuccess("mock token: acc1@gmail.com")); | 194 // all instances have been destroyed. |
| 96 EXPECT_CALL(*helper, OnMergeSessionSuccess("mock token: acc2@gmail.com")); | 195 typedef testing::NiceMock<InstrumentedGoogleAutoLoginHelper> NiceHelper; |
| 97 EXPECT_CALL(*helper, OnMergeSessionSuccess("mock token: acc3@gmail.com")); | 196 InstrumentedGoogleAutoLoginHelper* helper = NULL; |
| 98 EXPECT_CALL(*helper, OnMergeSessionSuccess("mock token: acc4@gmail.com")); | |
| 99 EXPECT_CALL(*helper, OnMergeSessionSuccess("mock token: acc5@gmail.com")); | |
| 100 | 197 |
| 101 // Don't completely mock out OnMergeSessionSuccess, let GoogleAutoLoginHelper | 198 helper = new NiceHelper(profile()); |
| 102 // actually call OnMergeSessionSuccess to clean up it's work queue because | |
| 103 // it'll delete itself once the work queue becomes empty. | |
| 104 ON_CALL(*helper, OnMergeSessionSuccess(_)).WillByDefault(Invoke(helper, | |
| 105 &InstrumentedGoogleAutoLoginHelper::OnMergeSessionSuccessConcrete)); | |
| 106 helper->LogIn("acc1@gmail.com"); | 199 helper->LogIn("acc1@gmail.com"); |
| 200 SimulateMergeSessionSuccess(helper, "token1"); |
| 201 |
| 202 helper = new NiceHelper(profile()); |
| 107 helper->LogIn("acc2@gmail.com"); | 203 helper->LogIn("acc2@gmail.com"); |
| 204 SimulateMergeSessionFailure(helper, error()); |
| 205 |
| 206 helper = new NiceHelper(profile()); |
| 108 helper->LogIn("acc3@gmail.com"); | 207 helper->LogIn("acc3@gmail.com"); |
| 109 helper->LogIn("acc4@gmail.com"); | 208 SimulateUbertokenFailure(helper, error()); |
| 110 helper->LogIn("acc5@gmail.com"); | |
| 111 helper->completePendingFetchers(); | |
| 112 | 209 |
| 113 // Make sure GoogleAutoLoginHelper cleans itself up after everything is done. | 210 ASSERT_EQ(3, total); |
| 114 message_loop_.RunUntilIdle(); | 211 base::MessageLoop::current()->RunUntilIdle(); |
| 115 EXPECT_EQ(0, total); | 212 ASSERT_EQ(0, total); |
| 116 } | 213 } |
| OLD | NEW |