OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include <algorithm> |
| 6 #include <string> |
| 7 #include <vector> |
| 8 |
| 9 #include "base/message_loop/message_loop.h" |
| 10 #include "chrome/browser/signin/google_auto_login_helper.h" |
| 11 #include "chrome/test/base/testing_profile.h" |
| 12 #include "testing/gmock/include/gmock/gmock.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" |
| 14 |
| 15 namespace { |
| 16 |
| 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. |
| 34 // We can EXPECT_* to be zero at the end of our unit tests |
| 35 // to make sure everything is properly deleted. |
| 36 |
| 37 int total = 0; |
| 38 |
| 39 class InstrumentedGoogleAutoLoginHelper : public GoogleAutoLoginHelper { |
| 40 public: |
| 41 std::vector<MockUbertokenFetcher*>* mock_uber_fetchers_; |
| 42 TestingProfile profile_; |
| 43 |
| 44 InstrumentedGoogleAutoLoginHelper( |
| 45 std::vector<MockUbertokenFetcher*>* fetchers) : |
| 46 GoogleAutoLoginHelper(0), mock_uber_fetchers_(fetchers) { |
| 47 total++; |
| 48 } |
| 49 |
| 50 virtual ~InstrumentedGoogleAutoLoginHelper() { |
| 51 total--; |
| 52 } |
| 53 |
| 54 virtual void OnUbertokenSuccess(const std::string& token) OVERRIDE { |
| 55 OnMergeSessionSuccess(token); |
| 56 } |
| 57 |
| 58 virtual UbertokenFetcher* CreateNewUbertokenFetcher() OVERRIDE { |
| 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 }; |
| 78 |
| 79 class GoogleAutoLoginHelperTest : public testing::Test { |
| 80 public: |
| 81 std::vector<MockUbertokenFetcher*> mock_uber_fetchers_; |
| 82 base::MessageLoop message_loop_; |
| 83 }; |
| 84 |
| 85 } // namespace |
| 86 |
| 87 using ::testing::Invoke; |
| 88 using ::testing::_; |
| 89 |
| 90 TEST_F(GoogleAutoLoginHelperTest, AllRequestsInOneGo) { |
| 91 total = 0; |
| 92 |
| 93 InstrumentedGoogleAutoLoginHelper* helper = |
| 94 new InstrumentedGoogleAutoLoginHelper(&mock_uber_fetchers_); |
| 95 EXPECT_CALL(*helper, OnMergeSessionSuccess("mock token: acc1@gmail.com")); |
| 96 EXPECT_CALL(*helper, OnMergeSessionSuccess("mock token: acc2@gmail.com")); |
| 97 EXPECT_CALL(*helper, OnMergeSessionSuccess("mock token: acc3@gmail.com")); |
| 98 EXPECT_CALL(*helper, OnMergeSessionSuccess("mock token: acc4@gmail.com")); |
| 99 EXPECT_CALL(*helper, OnMergeSessionSuccess("mock token: acc5@gmail.com")); |
| 100 |
| 101 // Don't completely mock out OnMergeSessionSuccess, let GoogleAutoLoginHelper |
| 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"); |
| 107 helper->LogIn("acc2@gmail.com"); |
| 108 helper->LogIn("acc3@gmail.com"); |
| 109 helper->LogIn("acc4@gmail.com"); |
| 110 helper->LogIn("acc5@gmail.com"); |
| 111 helper->completePendingFetchers(); |
| 112 |
| 113 // Make sure GoogleAutoLoginHelper cleans itself up after everything is done. |
| 114 message_loop_.RunUntilIdle(); |
| 115 EXPECT_EQ(0, total); |
| 116 } |
OLD | NEW |