| Index: chrome/browser/chromeos/arc/arc_opt_in_manager_unittest.cc
|
| diff --git a/chrome/browser/chromeos/arc/arc_opt_in_manager_unittest.cc b/chrome/browser/chromeos/arc/arc_opt_in_manager_unittest.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..31c15852e7db86dab7821d554bbf14d904618d54
|
| --- /dev/null
|
| +++ b/chrome/browser/chromeos/arc/arc_opt_in_manager_unittest.cc
|
| @@ -0,0 +1,158 @@
|
| +// Copyright 2016 The Chromium Authors. All rights reserved.
|
| +// Use of this source code is governed by a BSD-style license that can be
|
| +// found in the LICENSE file.
|
| +
|
| +#include <string>
|
| +#include <vector>
|
| +
|
| +#include "base/bind.h"
|
| +#include "base/files/file_path.h"
|
| +#include "base/files/file_util.h"
|
| +#include "base/files/scoped_temp_dir.h"
|
| +#include "base/macros.h"
|
| +#include "base/run_loop.h"
|
| +#include "chrome/browser/chromeos/arc/arc_opt_in_manager_impl.h"
|
| +#include "chrome/test/base/testing_profile.h"
|
| +#include "components/arc/arc_bridge_service.h"
|
| +#include "components/arc/test/fake_arc_bridge_service.h"
|
| +#include "content/public/browser/browser_thread.h"
|
| +#include "content/public/test/test_browser_thread_bundle.h"
|
| +#include "google_apis/gaia/gaia_constants.h"
|
| +#include "google_apis/gaia/gaia_urls.h"
|
| +#include "net/http/http_status_code.h"
|
| +#include "net/url_request/test_url_fetcher_factory.h"
|
| +#include "net/url_request/url_fetcher.h"
|
| +#include "testing/gtest/include/gtest/gtest.h"
|
| +
|
| +namespace arc {
|
| +
|
| +namespace {
|
| +
|
| +const int kThreadOptions = content::TestBrowserThreadBundle::IO_MAINLOOP;
|
| +const char kTestAuthToken[] = "4/Qa3CPIhh-WcMfWSf9HZaYcGUhEeax-F9sQK9CNRhZWs";
|
| +
|
| +scoped_ptr<TestingProfile> CreateProfile() {
|
| + base::ScopedTempDir temp_dir;
|
| + EXPECT_TRUE(temp_dir.CreateUniqueTempDir());
|
| + base::FilePath path = temp_dir.path().AppendASCII("TestArcProfile");
|
| + EXPECT_TRUE(base::DeleteFile(path, true));
|
| + EXPECT_TRUE(base::CreateDirectory(path));
|
| +
|
| + TestingProfile::Builder profile_builder;
|
| + profile_builder.SetPath(path);
|
| + return profile_builder.Build();
|
| +}
|
| +
|
| +} // namespace
|
| +
|
| +class ArcOptInManagerTest : public testing::Test {
|
| + public:
|
| + ArcOptInManagerTest()
|
| + : thread_bundle_(new content::TestBrowserThreadBundle(kThreadOptions)),
|
| + url_fetcher_factory_(
|
| + nullptr,
|
| + base::Bind(&ArcOptInManagerTest::FakeURLFetcherCreator,
|
| + base::Unretained(this))) {}
|
| + ~ArcOptInManagerTest() override {}
|
| +
|
| + void SetUp() override {
|
| + profile_ = CreateProfile();
|
| + bridge_service_.reset(new FakeArcBridgeService());
|
| + opt_in_manager_.reset(new ArcOptInManagerImpl());
|
| +
|
| + // Check initial conditions.
|
| + EXPECT_EQ(bridge_service_.get(), ArcBridgeService::Get());
|
| + EXPECT_EQ(true, !ArcBridgeService::Get()->available());
|
| + EXPECT_EQ(ArcBridgeService::State::STOPPED,
|
| + ArcBridgeService::Get()->state());
|
| + }
|
| +
|
| + void TearDown() override {}
|
| +
|
| + protected:
|
| + Profile* profile() { return profile_.get(); }
|
| + FakeArcBridgeService* bridge_service() { return bridge_service_.get(); }
|
| + ArcOptInManagerImpl* opt_in_manager() { return opt_in_manager_.get(); }
|
| + net::FakeURLFetcherFactory& url_fetcher_factory() {
|
| + return url_fetcher_factory_;
|
| + }
|
| + void set_cookie(const std::string& cookie) { rt_cookie_ = cookie; }
|
| +
|
| + private:
|
| + scoped_ptr<content::TestBrowserThreadBundle> thread_bundle_;
|
| + net::FakeURLFetcherFactory url_fetcher_factory_;
|
| + scoped_ptr<arc::FakeArcBridgeService> bridge_service_;
|
| + scoped_ptr<arc::ArcOptInManagerImpl> opt_in_manager_;
|
| + scoped_ptr<TestingProfile> profile_;
|
| + std::string rt_cookie_;
|
| +
|
| + scoped_ptr<net::FakeURLFetcher> FakeURLFetcherCreator(
|
| + const GURL& url,
|
| + net::URLFetcherDelegate* delegate,
|
| + const std::string& response_data,
|
| + net::HttpStatusCode response_code,
|
| + net::URLRequestStatus::Status status) {
|
| + scoped_ptr<net::FakeURLFetcher> fetcher(new net::FakeURLFetcher(
|
| + url, delegate, response_data, response_code, status));
|
| + // Use cookie only once.
|
| + if (!rt_cookie_.empty()) {
|
| + net::ResponseCookies cookies;
|
| + cookies.push_back(rt_cookie_);
|
| + fetcher->set_cookies(cookies);
|
| + rt_cookie_ = "";
|
| + }
|
| + return fetcher;
|
| + }
|
| +
|
| + DISALLOW_COPY_AND_ASSIGN(ArcOptInManagerTest);
|
| +};
|
| +
|
| +TEST_F(ArcOptInManagerTest, Workflow) {
|
| + ASSERT_EQ(ArcBridgeService::State::STOPPED, bridge_service()->state());
|
| + ASSERT_EQ(ArcOptInManager::State::DISABLE, opt_in_manager()->state());
|
| + ASSERT_EQ("", opt_in_manager()->GetAuthToken());
|
| +
|
| + const GURL gaia_gurl = ArcOptInManagerImpl::CreateURL(profile());
|
| + url_fetcher_factory().SetFakeResponse(gaia_gurl, "", net::HTTP_OK,
|
| + net::URLRequestStatus::SUCCESS);
|
| + std::string cookie = "oauth_code=";
|
| + cookie += kTestAuthToken;
|
| + cookie += "; Path=/o/oauth2/programmatic_auth; Secure; HttpOnly";
|
| + set_cookie(cookie);
|
| + opt_in_manager()->SetProfile(profile());
|
| +
|
| + // Setting profile initiates a token fetching process.
|
| + ASSERT_EQ(ArcOptInManager::State::FETCHING_TOKEN, opt_in_manager()->state());
|
| +
|
| + content::BrowserThread::GetBlockingPool()->FlushForTesting();
|
| + base::RunLoop().RunUntilIdle();
|
| +
|
| + ASSERT_EQ(ArcOptInManager::State::ENABLE, opt_in_manager()->state());
|
| + ASSERT_EQ(ArcBridgeService::State::READY, bridge_service()->state());
|
| + // Auth tolen valid only for one call.
|
| + ASSERT_EQ(kTestAuthToken, opt_in_manager()->GetAuthToken());
|
| + ASSERT_EQ("", opt_in_manager()->GetAuthToken());
|
| +
|
| + // Empty profile disables opt-in.
|
| + opt_in_manager()->SetProfile(nullptr);
|
| + ASSERT_EQ(ArcOptInManager::State::DISABLE, opt_in_manager()->state());
|
| + ASSERT_EQ(ArcBridgeService::State::STOPPED, bridge_service()->state());
|
| + ASSERT_EQ("", opt_in_manager()->GetAuthToken());
|
| +
|
| + // Send profile and don't provide a token.
|
| + opt_in_manager()->SetProfile(profile());
|
| +
|
| + // Setting profile initiates a token fetching process.
|
| + ASSERT_EQ(ArcOptInManager::State::FETCHING_TOKEN, opt_in_manager()->state());
|
| +
|
| + content::BrowserThread::GetBlockingPool()->FlushForTesting();
|
| + base::RunLoop().RunUntilIdle();
|
| +
|
| + ASSERT_EQ(ArcOptInManager::State::NO_TOKEN, opt_in_manager()->state());
|
| + // TODO(khmel). While UI for login is not ready we start bridge without
|
| + // token also.
|
| + // ASSERT_EQ(ArcBridgeService::State::STOPPED, bridge_service()->state());
|
| + ASSERT_EQ(ArcBridgeService::State::READY, bridge_service()->state());
|
| +}
|
| +
|
| +} // namespace arc
|
|
|