| OLD | NEW |
| (Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "base/command_line.h" |
| 6 #include "base/files/file_util.h" |
| 7 #include "base/run_loop.h" |
| 8 #include "chrome/test/base/in_process_browser_test.h" |
| 9 #include "components/quirks_client/quirks_client_manager.h" |
| 10 #include "components/quirks_client/switches.h" |
| 11 #include "content/public/test/test_utils.h" |
| 12 #include "net/url_request/test_url_fetcher_factory.h" |
| 13 |
| 14 namespace quirks_client { |
| 15 |
| 16 namespace { |
| 17 |
| 18 base::Closure g_EndMessageLoop; // Callback to terminate msg loop. |
| 19 base::FilePath g_icc_path; // Path to icc file if found or downloaded. |
| 20 |
| 21 const char kFakeIccJson[] = "{\n \"icc\": \"AAAIkCAgICACEAAA\"\n}"; |
| 22 const char kFakeIccData[] = {0x00, 0x00, 0x08, 0x90, 0x20, 0x20, |
| 23 0x20, 0x20, 0x02, 0x10, 0x00, 0x00}; |
| 24 |
| 25 // Create FakeURLFetcher which returns fake icc file json. |
| 26 scoped_ptr<net::URLFetcher> CreateFakeURLFetcherSuccess( |
| 27 const GURL& url, |
| 28 net::URLFetcherDelegate* delegate) { |
| 29 net::URLFetcher* fetcher = |
| 30 new net::FakeURLFetcher(url, delegate, kFakeIccJson, net::HTTP_OK, |
| 31 net::URLRequestStatus::SUCCESS); |
| 32 return make_scoped_ptr(fetcher); |
| 33 } |
| 34 |
| 35 // Create FakeURLFetcher which replies with HTTP_NOT_FOUND. |
| 36 scoped_ptr<net::URLFetcher> CreateFakeURLFetcherFailure( |
| 37 const GURL& url, |
| 38 net::URLFetcherDelegate* delegate) { |
| 39 net::URLFetcher* fetcher = new net::FakeURLFetcher( |
| 40 url, delegate, "File not found", net::HTTP_NOT_FOUND, |
| 41 net::URLRequestStatus::FAILED); |
| 42 return make_scoped_ptr(fetcher); |
| 43 } |
| 44 |
| 45 // Full path to fake icc file in <tmp test directory>/display_profiles/. |
| 46 base::FilePath GetPathForIccFile(int64_t product_id) { |
| 47 return QuirksClientManager::Get() |
| 48 ->delegate() |
| 49 ->GetDisplayProfileDirectory() |
| 50 .Append(quirks_client::IdToHexString(product_id).append(".icc")); |
| 51 } |
| 52 |
| 53 void OnQuirksClientFinished(base::FilePath path) { |
| 54 g_icc_path = path; |
| 55 ASSERT_TRUE(!g_EndMessageLoop.is_null()); |
| 56 g_EndMessageLoop.Run(); |
| 57 } |
| 58 |
| 59 void RunQuirksClientOnBlockingPool(int64_t product_id, bool expect_exists) { |
| 60 base::FilePath icc_path = quirks_client::RequestIccProfilePath( |
| 61 product_id, base::Bind(&OnQuirksClientFinished)); |
| 62 bool exists = !icc_path.empty(); |
| 63 EXPECT_EQ(expect_exists, exists); |
| 64 |
| 65 // If path not empty, icc file already existed, and we're done waiting. |
| 66 if (exists) { |
| 67 g_icc_path = icc_path; |
| 68 ASSERT_TRUE(!g_EndMessageLoop.is_null()); |
| 69 QuirksClientManager::Get()->task_runner()->PostTask(FROM_HERE, |
| 70 g_EndMessageLoop); |
| 71 } |
| 72 } |
| 73 |
| 74 } // namespace |
| 75 |
| 76 class QuirksClientBrowserTest : public InProcessBrowserTest { |
| 77 public: |
| 78 QuirksClientBrowserTest() = default; |
| 79 |
| 80 protected: |
| 81 ~QuirksClientBrowserTest() override = default; |
| 82 |
| 83 void Initialize() { |
| 84 // NOTE: QuirksClientManager::Initialize() isn't necessary here, since it'll |
| 85 // be called in ChromeBrowserMainPartsChromeos::PreMainMessageLoopRun(). |
| 86 |
| 87 // Create display_profiles subdirectory under tmp profile dir. |
| 88 const base::FilePath path = |
| 89 QuirksClientManager::Get()->delegate()->GetDisplayProfileDirectory(); |
| 90 base::File::Error error = base::File::FILE_OK; |
| 91 bool created = base::CreateDirectoryAndGetError(path, &error); |
| 92 ASSERT_TRUE(created); |
| 93 } |
| 94 |
| 95 // Runs Quirks Client on file thread, then waits till it's done. |
| 96 // |find_fake_file| indicates that URLFetcher should respond with success. |
| 97 // |expect_exists| indicates that the file should have already been created. |
| 98 void TestQuirksClient(int64_t product_id, |
| 99 bool find_fake_file, |
| 100 bool expect_exists) { |
| 101 // Set up fake url getter |
| 102 QuirksClientManager::Get()->SetFakeQuirksFetcherCreator( |
| 103 base::Bind(find_fake_file ? &CreateFakeURLFetcherSuccess |
| 104 : &CreateFakeURLFetcherFailure)); |
| 105 |
| 106 base::RunLoop run_loop; |
| 107 g_EndMessageLoop = run_loop.QuitClosure(); |
| 108 |
| 109 g_icc_path.clear(); |
| 110 content::BrowserThread::GetBlockingPool()->PostTask( |
| 111 FROM_HERE, |
| 112 base::Bind(&RunQuirksClientOnBlockingPool, product_id, expect_exists)); |
| 113 |
| 114 run_loop.Run(); |
| 115 } |
| 116 |
| 117 // InProcessBrowserTest overrides. |
| 118 void SetUpCommandLine(base::CommandLine* command_line) override { |
| 119 command_line->AppendSwitch(switches::kEnableDisplayQuirksClient); |
| 120 } |
| 121 }; |
| 122 |
| 123 IN_PROC_BROWSER_TEST_F(QuirksClientBrowserTest, DownloadIccFile) { |
| 124 Initialize(); |
| 125 |
| 126 // Request a file from fake Quirks Server, verify that file is written with |
| 127 // correct location and data. |
| 128 TestQuirksClient(0x0000aaaa, true, false); |
| 129 base::FilePath path = GetPathForIccFile(0x0000aaaa); |
| 130 EXPECT_EQ(g_icc_path, path); |
| 131 EXPECT_TRUE(base::PathExists(path)); |
| 132 char data[32]; |
| 133 ReadFile(path, data, sizeof(data)); |
| 134 EXPECT_EQ(0, memcmp(data, kFakeIccData, sizeof(kFakeIccData))); |
| 135 |
| 136 // Retest same file, this time expect it to already exist. |
| 137 TestQuirksClient(0x0000aaaa, true, true); |
| 138 EXPECT_EQ(g_icc_path, path); |
| 139 |
| 140 // Finally, request a file that doesn't exist on fake Quirks Server. |
| 141 TestQuirksClient(0x1111bbbb, false, false); |
| 142 EXPECT_EQ(g_icc_path, base::FilePath()); |
| 143 EXPECT_FALSE(base::PathExists(GetPathForIccFile(0x1111bbbb))); |
| 144 } |
| 145 |
| 146 } // namespace quirks_client |
| OLD | NEW |