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/quirks_manager.h" |
| 10 #include "components/quirks/switches.h" |
| 11 #include "content/public/test/test_utils.h" |
| 12 #include "net/url_request/test_url_fetcher_factory.h" |
| 13 |
| 14 namespace quirks { |
| 15 |
| 16 namespace { |
| 17 |
| 18 const char kFakeIccJson[] = "{\n \"icc\": \"AAAIkCAgICACEAAA\"\n}"; |
| 19 const char kFakeIccData[] = {0x00, 0x00, 0x08, 0x90, 0x20, 0x20, |
| 20 0x20, 0x20, 0x02, 0x10, 0x00, 0x00}; |
| 21 |
| 22 // Create FakeURLFetcher which returns fake icc file json. |
| 23 scoped_ptr<net::URLFetcher> CreateFakeURLFetcherSuccess( |
| 24 const GURL& url, |
| 25 net::URLFetcherDelegate* delegate) { |
| 26 net::URLFetcher* fetcher = |
| 27 new net::FakeURLFetcher(url, delegate, kFakeIccJson, net::HTTP_OK, |
| 28 net::URLRequestStatus::SUCCESS); |
| 29 return make_scoped_ptr(fetcher); |
| 30 } |
| 31 |
| 32 // Create FakeURLFetcher which replies with HTTP_NOT_FOUND. |
| 33 scoped_ptr<net::URLFetcher> CreateFakeURLFetcherFailure( |
| 34 const GURL& url, |
| 35 net::URLFetcherDelegate* delegate) { |
| 36 net::URLFetcher* fetcher = new net::FakeURLFetcher( |
| 37 url, delegate, "File not found", net::HTTP_NOT_FOUND, |
| 38 net::URLRequestStatus::FAILED); |
| 39 return make_scoped_ptr(fetcher); |
| 40 } |
| 41 |
| 42 // Full path to fake icc file in <tmp test directory>/display_profiles/. |
| 43 base::FilePath GetPathForIccFile(int64_t product_id) { |
| 44 return QuirksManager::Get() |
| 45 ->delegate() |
| 46 ->GetDownloadDisplayProfileDirectory() |
| 47 .Append(quirks::IdToFileName(product_id)); |
| 48 } |
| 49 |
| 50 } // namespace |
| 51 |
| 52 class QuirksBrowserTest : public InProcessBrowserTest { |
| 53 public: |
| 54 QuirksBrowserTest() : file_existed_(false) {} |
| 55 |
| 56 protected: |
| 57 ~QuirksBrowserTest() override = default; |
| 58 |
| 59 void Initialize() { |
| 60 // NOTE: QuirksManager::Initialize() isn't necessary here, since it'll be |
| 61 // called in ChromeBrowserMainPartsChromeos::PreMainMessageLoopRun(). |
| 62 |
| 63 // Create display_profiles subdirectory under temp profile directory. |
| 64 const base::FilePath path = |
| 65 QuirksManager::Get()->delegate()->GetDownloadDisplayProfileDirectory(); |
| 66 base::File::Error error = base::File::FILE_OK; |
| 67 bool created = base::CreateDirectoryAndGetError(path, &error); |
| 68 ASSERT_TRUE(created); |
| 69 |
| 70 // Quirks clients can't run until after login. |
| 71 quirks::QuirksManager::Get()->OnLoginCompleted(); |
| 72 } |
| 73 |
| 74 // Query QuirksManager for icc file, then run msg loop to wait for callback. |
| 75 // |find_fake_file| indicates that URLFetcher should respond with success. |
| 76 void TestQuirksClient(int64_t product_id, bool find_fake_file) { |
| 77 // Set up fake url getter. |
| 78 QuirksManager::Get()->SetFakeQuirksFetcherCreatorForTests( |
| 79 base::Bind(find_fake_file ? &CreateFakeURLFetcherSuccess |
| 80 : &CreateFakeURLFetcherFailure)); |
| 81 |
| 82 base::RunLoop run_loop; |
| 83 end_message_loop_ = run_loop.QuitClosure(); |
| 84 |
| 85 quirks::QuirksManager::Get()->RequestIccProfilePath( |
| 86 product_id, base::Bind(&QuirksBrowserTest::OnQuirksClientFinished, |
| 87 base::Unretained(this))); |
| 88 |
| 89 run_loop.Run(); |
| 90 } |
| 91 |
| 92 // Callback from RequestIccProfilePath(). |
| 93 void OnQuirksClientFinished(const base::FilePath& path, bool downloaded) { |
| 94 icc_path_ = path; |
| 95 file_existed_ = !downloaded; |
| 96 ASSERT_TRUE(!end_message_loop_.is_null()); |
| 97 end_message_loop_.Run(); |
| 98 } |
| 99 |
| 100 // InProcessBrowserTest overrides. |
| 101 void SetUpCommandLine(base::CommandLine* command_line) override { |
| 102 command_line->AppendSwitch(switches::kEnableQuirksClient); |
| 103 } |
| 104 |
| 105 base::Closure end_message_loop_; // Callback to terminate message loop. |
| 106 base::FilePath icc_path_; // Path to icc file if found or downloaded. |
| 107 bool file_existed_; // File was previously downloaded. |
| 108 }; |
| 109 |
| 110 IN_PROC_BROWSER_TEST_F(QuirksBrowserTest, DownloadIccFile) { |
| 111 Initialize(); |
| 112 |
| 113 // Request a file from fake Quirks Server, verify that file is written with |
| 114 // correct location and data. |
| 115 TestQuirksClient(0x0000aaaa, true); |
| 116 base::FilePath path = GetPathForIccFile(0x0000aaaa); |
| 117 EXPECT_EQ(icc_path_, path); |
| 118 EXPECT_EQ(file_existed_, false); |
| 119 EXPECT_TRUE(base::PathExists(path)); |
| 120 char data[32]; |
| 121 ReadFile(path, data, sizeof(data)); |
| 122 EXPECT_EQ(0, memcmp(data, kFakeIccData, sizeof(kFakeIccData))); |
| 123 |
| 124 // Retest same file, this time expect it to already exist. |
| 125 TestQuirksClient(0x0000aaaa, true); |
| 126 EXPECT_EQ(icc_path_, path); |
| 127 EXPECT_EQ(file_existed_, true); |
| 128 |
| 129 // Finally, request a file that doesn't exist on fake Quirks Server. |
| 130 TestQuirksClient(0x1111bbbb, false); |
| 131 EXPECT_EQ(icc_path_, base::FilePath()); |
| 132 EXPECT_EQ(file_existed_, false); |
| 133 EXPECT_FALSE(base::PathExists(GetPathForIccFile(0x1111bbbb))); |
| 134 } |
| 135 |
| 136 } // namespace quirks |
OLD | NEW |