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 "chrome/browser/android/webapk/webapk_icon_hasher.h" |
| 6 |
| 7 #include <string> |
| 8 |
| 9 #include "base/bind.h" |
| 10 #include "base/callback_forward.h" |
| 11 #include "base/files/file_path.h" |
| 12 #include "base/memory/ref_counted.h" |
| 13 #include "base/run_loop.h" |
| 14 #include "base/threading/thread_task_runner_handle.h" |
| 15 #include "content/public/test/test_browser_thread_bundle.h" |
| 16 #include "net/test/embedded_test_server/embedded_test_server.h" |
| 17 #include "net/url_request/url_request_test_util.h" |
| 18 #include "testing/gtest/include/gtest/gtest.h" |
| 19 #include "url/gurl.h" |
| 20 |
| 21 namespace { |
| 22 |
| 23 const base::FilePath::CharType kTestDataDir[] = |
| 24 FILE_PATH_LITERAL("chrome/test/data"); |
| 25 |
| 26 const char kIconUrl[] = "/android/google.png"; |
| 27 |
| 28 // Murmur2 hash for |kIconUrl|. |
| 29 const char kIconMurmur2Hash[] = "13321047016824288610"; |
| 30 |
| 31 // Runs WebApkIconHasher and blocks till the murmur2 hash is computed. |
| 32 class WebApkIconHasherRunner { |
| 33 public: |
| 34 WebApkIconHasherRunner() |
| 35 : url_request_context_getter_(new net::TestURLRequestContextGetter( |
| 36 base::ThreadTaskRunnerHandle::Get())) {} |
| 37 ~WebApkIconHasherRunner() {} |
| 38 |
| 39 void Run(const GURL& icon_url) { |
| 40 WebApkIconHasher hasher; |
| 41 hasher.DownloadAndComputeMurmur2Hash( |
| 42 url_request_context_getter_.get(), icon_url, |
| 43 base::Bind(&WebApkIconHasherRunner::OnCompleted, |
| 44 base::Unretained(this))); |
| 45 |
| 46 base::RunLoop run_loop; |
| 47 on_completed_callback_ = run_loop.QuitClosure(); |
| 48 run_loop.Run(); |
| 49 } |
| 50 |
| 51 const std::string& murmur2_hash() { return murmur2_hash_; } |
| 52 |
| 53 private: |
| 54 void OnCompleted(const std::string& murmur2_hash) { |
| 55 murmur2_hash_ = murmur2_hash; |
| 56 on_completed_callback_.Run(); |
| 57 } |
| 58 |
| 59 scoped_refptr<net::TestURLRequestContextGetter> |
| 60 url_request_context_getter_; |
| 61 |
| 62 // Called once the Murmur2 hash is taken. |
| 63 base::Closure on_completed_callback_; |
| 64 |
| 65 // Computed Murmur2 hash. |
| 66 std::string murmur2_hash_; |
| 67 |
| 68 DISALLOW_COPY_AND_ASSIGN(WebApkIconHasherRunner); |
| 69 }; |
| 70 |
| 71 } // anonymous namespace |
| 72 |
| 73 class WebApkIconHasherTest : public ::testing::Test { |
| 74 public: |
| 75 WebApkIconHasherTest() |
| 76 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP) {} |
| 77 ~WebApkIconHasherTest() override {} |
| 78 |
| 79 void SetUp() override { |
| 80 test_server_.AddDefaultHandlers(base::FilePath(kTestDataDir)); |
| 81 ASSERT_TRUE(test_server_.Start()); |
| 82 } |
| 83 |
| 84 net::test_server::EmbeddedTestServer* test_server() { return &test_server_; } |
| 85 |
| 86 private: |
| 87 content::TestBrowserThreadBundle thread_bundle_; |
| 88 net::EmbeddedTestServer test_server_; |
| 89 |
| 90 DISALLOW_COPY_AND_ASSIGN(WebApkIconHasherTest); |
| 91 }; |
| 92 |
| 93 TEST_F(WebApkIconHasherTest, Success) { |
| 94 GURL icon_url = test_server()->GetURL(kIconUrl); |
| 95 WebApkIconHasherRunner runner; |
| 96 runner.Run(icon_url); |
| 97 EXPECT_EQ(kIconMurmur2Hash, runner.murmur2_hash()); |
| 98 } |
| 99 |
| 100 // Test that the hash callback is called with an empty string if an HTTP error |
| 101 // prevents the icon URL from being fetched. |
| 102 TEST_F(WebApkIconHasherTest, HTTPError) { |
| 103 GURL icon_url = test_server()->GetURL("/nocontent"); |
| 104 WebApkIconHasherRunner runner; |
| 105 runner.Run(icon_url); |
| 106 EXPECT_EQ("", runner.murmur2_hash()); |
| 107 } |
OLD | NEW |