| Index: chrome/browser/chromeos/display/quirks_client_browsertest.cc
|
| diff --git a/chrome/browser/chromeos/display/quirks_client_browsertest.cc b/chrome/browser/chromeos/display/quirks_client_browsertest.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..bdc1e152afcc2ab4d7190c00827feda3b4ce4c59
|
| --- /dev/null
|
| +++ b/chrome/browser/chromeos/display/quirks_client_browsertest.cc
|
| @@ -0,0 +1,146 @@
|
| +// 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 "base/command_line.h"
|
| +#include "base/files/file_util.h"
|
| +#include "chrome/test/base/in_process_browser_test.h"
|
| +#include "components/quirks_client/quirks_client_manager.h"
|
| +#include "components/quirks_client/switches.h"
|
| +#include "content/public/test/test_utils.h"
|
| +#include "net/url_request/test_url_fetcher_factory.h"
|
| +
|
| +namespace quirks_client {
|
| +
|
| +namespace {
|
| +
|
| +base::Closure g_EndMessageLoop; // Callback to terminate msg loop.
|
| +base::FilePath g_icc_path; // Path to icc file if found or downloaded.
|
| +
|
| +const char kFakeIccJson[] = "{\n \"icc\": \"AAAIkCAgICACEAAA\"\n}";
|
| +const char kFakeIccData[] = {0x00, 0x00, 0x08, 0x90, 0x20, 0x20,
|
| + 0x20, 0x20, 0x02, 0x10, 0x00, 0x00};
|
| +
|
| +// Create FakeURLFetcher which returns fake icc file json.
|
| +scoped_ptr<net::URLFetcher> CreateFakeURLFetcherSuccess(
|
| + const GURL& url,
|
| + net::URLFetcherDelegate* delegate) {
|
| + net::URLFetcher* fetcher =
|
| + new net::FakeURLFetcher(url, delegate, kFakeIccJson, net::HTTP_OK,
|
| + net::URLRequestStatus::SUCCESS);
|
| + return make_scoped_ptr(fetcher);
|
| +}
|
| +
|
| +// Create FakeURLFetcher which replies with HTTP_NOT_FOUND.
|
| +scoped_ptr<net::URLFetcher> CreateFakeURLFetcherFailure(
|
| + const GURL& url,
|
| + net::URLFetcherDelegate* delegate) {
|
| + net::URLFetcher* fetcher = new net::FakeURLFetcher(
|
| + url, delegate, "File not found", net::HTTP_NOT_FOUND,
|
| + net::URLRequestStatus::FAILED);
|
| + return make_scoped_ptr(fetcher);
|
| +}
|
| +
|
| +// Full path to fake icc file in <tmp test directory>/display_profiles/.
|
| +base::FilePath GetPathForIccFile(int64_t product_id) {
|
| + return QuirksClientManager::Get()
|
| + ->delegate()
|
| + ->GetDisplayProfileDirectory()
|
| + .Append(quirks_client::QuirksClient::IdToHexString(product_id)
|
| + .append(".icc"));
|
| +}
|
| +
|
| +void OnQuirksClientFinished(base::FilePath path) {
|
| + g_icc_path = path;
|
| + ASSERT_TRUE(!g_EndMessageLoop.is_null());
|
| + g_EndMessageLoop.Run();
|
| +}
|
| +
|
| +void RunQuirksClientOnIoThread(int64_t product_id, bool expect_exists) {
|
| + g_icc_path = quirks_client::QuirksClient::RequestIccProfilePath(
|
| + product_id, base::Bind(&OnQuirksClientFinished));
|
| + bool exists = !g_icc_path.empty();
|
| + EXPECT_EQ(expect_exists, exists);
|
| +
|
| + // If path not empty, icc file already existed, and we're done waiting.
|
| + if (exists) {
|
| + ASSERT_TRUE(!g_EndMessageLoop.is_null());
|
| + QuirksClientManager::Get()->message_loop_ui()->PostTask(FROM_HERE,
|
| + g_EndMessageLoop);
|
| + }
|
| +}
|
| +
|
| +} // namespace
|
| +
|
| +class QuirksClientBrowserTest : public InProcessBrowserTest {
|
| + public:
|
| + QuirksClientBrowserTest() {}
|
| +
|
| + protected:
|
| + ~QuirksClientBrowserTest() override {}
|
| +
|
| + void Initialize() {
|
| + // NOTE: QuirksClientManager::Initialize() isn't necessary here, since it'll
|
| + // be called in ChromeBrowserMainPartsChromeos::PreMainMessageLoopRun().
|
| +
|
| + // Create display_profiles subdirectory under tmp profile dir.
|
| + const base::FilePath path =
|
| + QuirksClientManager::Get()->delegate()->GetDisplayProfileDirectory();
|
| + base::File::Error error = base::File::FILE_OK;
|
| + bool created = base::CreateDirectoryAndGetError(path, &error);
|
| + ASSERT_TRUE(created);
|
| + }
|
| +
|
| + // Runs Quirks Client on io thread, then waits till it's done.
|
| + // |find_fake_file| indicates that URLFetcher should respond with success.
|
| + // |expect_exists| indicates that the file should have already been created.
|
| + void TestQuirksClient(int64_t product_id,
|
| + bool find_fake_file,
|
| + bool expect_exists) {
|
| + // Set up fake url getter
|
| + QuirksClientManager::Get()->SetFakeQuirksFetcherCreator(
|
| + base::Bind(find_fake_file ? &CreateFakeURLFetcherSuccess
|
| + : &CreateFakeURLFetcherFailure));
|
| +
|
| + scoped_refptr<content::MessageLoopRunner> message_loop_runner =
|
| + new content::MessageLoopRunner;
|
| + g_EndMessageLoop = message_loop_runner->QuitClosure();
|
| +
|
| + g_icc_path.clear();
|
| + content::BrowserThread::GetBlockingPool()->PostTask(
|
| + FROM_HERE,
|
| + base::Bind(&RunQuirksClientOnIoThread, product_id, expect_exists));
|
| +
|
| + message_loop_runner->Run();
|
| + }
|
| +
|
| + // InProcessBrowserTest overrides.
|
| + void SetUpCommandLine(base::CommandLine* command_line) override {
|
| + command_line->AppendSwitch(switches::kEnableDisplayQuirksClient);
|
| + }
|
| +};
|
| +
|
| +IN_PROC_BROWSER_TEST_F(QuirksClientBrowserTest, DownloadIccFile) {
|
| + Initialize();
|
| +
|
| + // Request a file from fake Quirks Server, verify that file is written with
|
| + // correct location and data.
|
| + TestQuirksClient(0x0000aaaa, true, false);
|
| + base::FilePath path = GetPathForIccFile(0x0000aaaa);
|
| + EXPECT_EQ(g_icc_path, path);
|
| + EXPECT_TRUE(base::PathExists(path));
|
| + char data[32];
|
| + ReadFile(path, data, 32);
|
| + EXPECT_EQ(0, memcmp(data, kFakeIccData, sizeof(kFakeIccData)));
|
| +
|
| + // Retest same file, this time expect it to already exist.
|
| + TestQuirksClient(0x0000aaaa, true, true);
|
| + EXPECT_EQ(g_icc_path, path);
|
| +
|
| + // Finally, request a file that doesn't exist on fake Quirks Server.
|
| + TestQuirksClient(0x1111bbbb, false, false);
|
| + EXPECT_EQ(g_icc_path, base::FilePath());
|
| + EXPECT_FALSE(base::PathExists(GetPathForIccFile(0x1111bbbb)));
|
| +}
|
| +
|
| +} // namespace quirks_client
|
|
|