| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 <string> | 5 #include <string> |
| 6 #include <vector> | 6 #include <vector> |
| 7 | 7 |
| 8 #include "base/bind.h" | 8 #include "base/bind.h" |
| 9 #include "base/files/file_path.h" | 9 #include "base/files/file_path.h" |
| 10 #include "base/files/file_util.h" | 10 #include "base/files/file_util.h" |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 64 EXPECT_EQ(ArcBridgeService::State::STOPPED, | 64 EXPECT_EQ(ArcBridgeService::State::STOPPED, |
| 65 ArcBridgeService::Get()->state()); | 65 ArcBridgeService::Get()->state()); |
| 66 } | 66 } |
| 67 | 67 |
| 68 void TearDown() override {} | 68 void TearDown() override {} |
| 69 | 69 |
| 70 protected: | 70 protected: |
| 71 Profile* profile() { return profile_.get(); } | 71 Profile* profile() { return profile_.get(); } |
| 72 FakeArcBridgeService* bridge_service() { return bridge_service_.get(); } | 72 FakeArcBridgeService* bridge_service() { return bridge_service_.get(); } |
| 73 ArcAuthService* auth_service() { return auth_service_.get(); } | 73 ArcAuthService* auth_service() { return auth_service_.get(); } |
| 74 net::FakeURLFetcherFactory& url_fetcher_factory() { | 74 |
| 75 return url_fetcher_factory_; | 75 void PrepareURLResponse(net::HttpStatusCode code, bool enable_auth_code) { |
| 76 const GURL gaia_gurl = ArcAuthFetcher::CreateURL(); |
| 77 url_fetcher_factory_.SetFakeResponse(gaia_gurl, std::string(), code, |
| 78 net::URLRequestStatus::SUCCESS); |
| 79 if (enable_auth_code) { |
| 80 std::string cookie = "oauth_code="; |
| 81 cookie += kTestAuthCode; |
| 82 cookie += "; Path=/o/oauth2/programmatic_auth; Secure; HttpOnly"; |
| 83 rt_cookie_ = cookie; |
| 84 } |
| 76 } | 85 } |
| 77 void set_cookie(const std::string& cookie) { rt_cookie_ = cookie; } | |
| 78 | 86 |
| 79 private: | 87 private: |
| 80 scoped_ptr<net::FakeURLFetcher> FakeURLFetcherCreator( | 88 scoped_ptr<net::FakeURLFetcher> FakeURLFetcherCreator( |
| 81 const GURL& url, | 89 const GURL& url, |
| 82 net::URLFetcherDelegate* delegate, | 90 net::URLFetcherDelegate* delegate, |
| 83 const std::string& response_data, | 91 const std::string& response_data, |
| 84 net::HttpStatusCode response_code, | 92 net::HttpStatusCode response_code, |
| 85 net::URLRequestStatus::Status status) { | 93 net::URLRequestStatus::Status status) { |
| 86 scoped_ptr<net::FakeURLFetcher> fetcher(new net::FakeURLFetcher( | 94 scoped_ptr<net::FakeURLFetcher> fetcher(new net::FakeURLFetcher( |
| 87 url, delegate, response_data, response_code, status)); | 95 url, delegate, response_data, response_code, status)); |
| (...skipping 20 matching lines...) Expand all Loading... |
| 108 | 116 |
| 109 TEST_F(ArcAuthServiceTest, PrefChangeTriggersService) { | 117 TEST_F(ArcAuthServiceTest, PrefChangeTriggersService) { |
| 110 ASSERT_EQ(ArcAuthService::State::DISABLE, auth_service()->state()); | 118 ASSERT_EQ(ArcAuthService::State::DISABLE, auth_service()->state()); |
| 111 | 119 |
| 112 PrefService* pref = profile()->GetPrefs(); | 120 PrefService* pref = profile()->GetPrefs(); |
| 113 DCHECK_EQ(false, pref->GetBoolean(prefs::kArcEnabled)); | 121 DCHECK_EQ(false, pref->GetBoolean(prefs::kArcEnabled)); |
| 114 | 122 |
| 115 auth_service()->OnPrimaryUserProfilePrepared(profile()); | 123 auth_service()->OnPrimaryUserProfilePrepared(profile()); |
| 116 ASSERT_EQ(ArcAuthService::State::DISABLE, auth_service()->state()); | 124 ASSERT_EQ(ArcAuthService::State::DISABLE, auth_service()->state()); |
| 117 | 125 |
| 118 // Need to initialize URLFetcher for test framework. | 126 PrepareURLResponse(net::HTTP_OK, false); |
| 119 const GURL gaia_gurl = ArcAuthFetcher::CreateURL(); | |
| 120 url_fetcher_factory().SetFakeResponse(gaia_gurl, std::string(), net::HTTP_OK, | |
| 121 net::URLRequestStatus::SUCCESS); | |
| 122 | |
| 123 pref->SetBoolean(prefs::kArcEnabled, true); | 127 pref->SetBoolean(prefs::kArcEnabled, true); |
| 124 ASSERT_EQ(ArcAuthService::State::FETCHING_CODE, auth_service()->state()); | 128 ASSERT_EQ(ArcAuthService::State::FETCHING_CODE, auth_service()->state()); |
| 125 | 129 |
| 126 pref->SetBoolean(prefs::kArcEnabled, false); | 130 pref->SetBoolean(prefs::kArcEnabled, false); |
| 127 ASSERT_EQ(ArcAuthService::State::DISABLE, auth_service()->state()); | 131 ASSERT_EQ(ArcAuthService::State::DISABLE, auth_service()->state()); |
| 128 | 132 |
| 129 // Correctly stop service. | 133 // Correctly stop service. |
| 130 auth_service()->Shutdown(); | 134 auth_service()->Shutdown(); |
| 131 } | 135 } |
| 132 | 136 |
| 133 TEST_F(ArcAuthServiceTest, BaseWorkflow) { | 137 TEST_F(ArcAuthServiceTest, BaseWorkflow) { |
| 134 ASSERT_EQ(ArcBridgeService::State::STOPPED, bridge_service()->state()); | 138 ASSERT_EQ(ArcBridgeService::State::STOPPED, bridge_service()->state()); |
| 135 ASSERT_EQ(ArcAuthService::State::DISABLE, auth_service()->state()); | 139 ASSERT_EQ(ArcAuthService::State::DISABLE, auth_service()->state()); |
| 136 ASSERT_EQ(std::string(), auth_service()->GetAndResetAuthCode()); | 140 ASSERT_EQ(std::string(), auth_service()->GetAndResetAuthCode()); |
| 137 | 141 |
| 138 const GURL gaia_gurl = ArcAuthFetcher::CreateURL(); | 142 PrepareURLResponse(net::HTTP_OK, true); |
| 139 url_fetcher_factory().SetFakeResponse(gaia_gurl, std::string(), net::HTTP_OK, | |
| 140 net::URLRequestStatus::SUCCESS); | |
| 141 std::string cookie = "oauth_code="; | |
| 142 cookie += kTestAuthCode; | |
| 143 cookie += "; Path=/o/oauth2/programmatic_auth; Secure; HttpOnly"; | |
| 144 set_cookie(cookie); | |
| 145 auth_service()->OnPrimaryUserProfilePrepared(profile()); | 143 auth_service()->OnPrimaryUserProfilePrepared(profile()); |
| 146 | 144 |
| 147 // By default ARC is not enabled. | 145 // By default ARC is not enabled. |
| 148 ASSERT_EQ(ArcAuthService::State::DISABLE, auth_service()->state()); | 146 ASSERT_EQ(ArcAuthService::State::DISABLE, auth_service()->state()); |
| 149 | 147 |
| 150 profile()->GetPrefs()->SetBoolean(prefs::kArcEnabled, true); | 148 profile()->GetPrefs()->SetBoolean(prefs::kArcEnabled, true); |
| 151 | 149 |
| 152 // Setting profile and pref initiates a code fetching process. | 150 // Setting profile and pref initiates a code fetching process. |
| 153 ASSERT_EQ(ArcAuthService::State::FETCHING_CODE, auth_service()->state()); | 151 ASSERT_EQ(ArcAuthService::State::FETCHING_CODE, auth_service()->state()); |
| 154 | 152 |
| (...skipping 17 matching lines...) Expand all Loading... |
| 172 // Setting profile initiates a code fetching process. | 170 // Setting profile initiates a code fetching process. |
| 173 ASSERT_EQ(ArcAuthService::State::FETCHING_CODE, auth_service()->state()); | 171 ASSERT_EQ(ArcAuthService::State::FETCHING_CODE, auth_service()->state()); |
| 174 | 172 |
| 175 content::BrowserThread::GetBlockingPool()->FlushForTesting(); | 173 content::BrowserThread::GetBlockingPool()->FlushForTesting(); |
| 176 base::RunLoop().RunUntilIdle(); | 174 base::RunLoop().RunUntilIdle(); |
| 177 | 175 |
| 178 // UI is disabled in unit tests and this code is unchanged. | 176 // UI is disabled in unit tests and this code is unchanged. |
| 179 ASSERT_EQ(ArcAuthService::State::FETCHING_CODE, auth_service()->state()); | 177 ASSERT_EQ(ArcAuthService::State::FETCHING_CODE, auth_service()->state()); |
| 180 | 178 |
| 181 // Send error response. | 179 // Send error response. |
| 182 url_fetcher_factory().SetFakeResponse(gaia_gurl, std::string(), | 180 PrepareURLResponse(net::HTTP_BAD_REQUEST, false); |
| 183 net::HTTP_BAD_REQUEST, | |
| 184 net::URLRequestStatus::SUCCESS); | |
| 185 auth_service()->Shutdown(); | 181 auth_service()->Shutdown(); |
| 186 ASSERT_EQ(ArcAuthService::State::DISABLE, auth_service()->state()); | 182 ASSERT_EQ(ArcAuthService::State::DISABLE, auth_service()->state()); |
| 187 auth_service()->OnPrimaryUserProfilePrepared(profile()); | 183 auth_service()->OnPrimaryUserProfilePrepared(profile()); |
| 188 | 184 |
| 189 ASSERT_EQ(ArcAuthService::State::FETCHING_CODE, auth_service()->state()); | 185 ASSERT_EQ(ArcAuthService::State::FETCHING_CODE, auth_service()->state()); |
| 190 content::BrowserThread::GetBlockingPool()->FlushForTesting(); | 186 content::BrowserThread::GetBlockingPool()->FlushForTesting(); |
| 191 base::RunLoop().RunUntilIdle(); | 187 base::RunLoop().RunUntilIdle(); |
| 192 | 188 |
| 193 ASSERT_EQ(ArcAuthService::State::NO_CODE, auth_service()->state()); | 189 ASSERT_EQ(ArcAuthService::State::NO_CODE, auth_service()->state()); |
| 194 | 190 |
| 195 // Correctly stop service. | 191 // Correctly stop service. |
| 196 auth_service()->Shutdown(); | 192 auth_service()->Shutdown(); |
| 197 } | 193 } |
| 198 | 194 |
| 195 TEST_F(ArcAuthServiceTest, CancelFetchingDisablesArc) { |
| 196 PrepareURLResponse(net::HTTP_OK, false); |
| 197 PrefService* pref = profile()->GetPrefs(); |
| 198 |
| 199 auth_service()->OnPrimaryUserProfilePrepared(profile()); |
| 200 pref->SetBoolean(prefs::kArcEnabled, true); |
| 201 ASSERT_EQ(ArcAuthService::State::FETCHING_CODE, auth_service()->state()); |
| 202 |
| 203 auth_service()->CancelAuthCode(); |
| 204 ASSERT_EQ(ArcAuthService::State::DISABLE, auth_service()->state()); |
| 205 ASSERT_EQ(false, pref->GetBoolean(prefs::kArcEnabled)); |
| 206 |
| 207 // Correctly stop service. |
| 208 auth_service()->Shutdown(); |
| 209 } |
| 210 |
| 211 TEST_F(ArcAuthServiceTest, CloseUIKeepsArcEnabled) { |
| 212 PrepareURLResponse(net::HTTP_OK, true); |
| 213 PrefService* pref = profile()->GetPrefs(); |
| 214 |
| 215 auth_service()->OnPrimaryUserProfilePrepared(profile()); |
| 216 pref->SetBoolean(prefs::kArcEnabled, true); |
| 217 |
| 218 content::BrowserThread::GetBlockingPool()->FlushForTesting(); |
| 219 base::RunLoop().RunUntilIdle(); |
| 220 |
| 221 ASSERT_EQ(ArcAuthService::State::ENABLE, auth_service()->state()); |
| 222 |
| 223 auth_service()->CancelAuthCode(); |
| 224 ASSERT_EQ(ArcAuthService::State::ENABLE, auth_service()->state()); |
| 225 ASSERT_EQ(true, pref->GetBoolean(prefs::kArcEnabled)); |
| 226 |
| 227 // Correctly stop service. |
| 228 auth_service()->Shutdown(); |
| 229 } |
| 230 |
| 199 } // namespace arc | 231 } // namespace arc |
| OLD | NEW |