| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/android/webapk/webapk_icon_hasher.h" | 5 #include "chrome/browser/android/webapk/webapk_icon_hasher.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/callback_forward.h" | 10 #include "base/callback_forward.h" |
| (...skipping 19 matching lines...) Expand all Loading... |
| 30 | 30 |
| 31 // Runs WebApkIconHasher and blocks till the murmur2 hash is computed. | 31 // Runs WebApkIconHasher and blocks till the murmur2 hash is computed. |
| 32 class WebApkIconHasherRunner { | 32 class WebApkIconHasherRunner { |
| 33 public: | 33 public: |
| 34 WebApkIconHasherRunner() | 34 WebApkIconHasherRunner() |
| 35 : url_request_context_getter_(new net::TestURLRequestContextGetter( | 35 : url_request_context_getter_(new net::TestURLRequestContextGetter( |
| 36 base::ThreadTaskRunnerHandle::Get())) {} | 36 base::ThreadTaskRunnerHandle::Get())) {} |
| 37 ~WebApkIconHasherRunner() {} | 37 ~WebApkIconHasherRunner() {} |
| 38 | 38 |
| 39 void Run(const GURL& icon_url) { | 39 void Run(const GURL& icon_url) { |
| 40 WebApkIconHasher hasher; | 40 WebApkIconHasher hasher(url_request_context_getter_.get(), icon_url, |
| 41 hasher.DownloadAndComputeMurmur2Hash( | |
| 42 url_request_context_getter_.get(), icon_url, | |
| 43 base::Bind(&WebApkIconHasherRunner::OnCompleted, | 41 base::Bind(&WebApkIconHasherRunner::OnCompleted, |
| 44 base::Unretained(this))); | 42 base::Unretained(this))); |
| 43 hasher.DownloadAndComputeMurmur2Hash(); |
| 45 | 44 |
| 46 base::RunLoop run_loop; | 45 base::RunLoop run_loop; |
| 47 on_completed_callback_ = run_loop.QuitClosure(); | 46 on_completed_callback_ = run_loop.QuitClosure(); |
| 48 run_loop.Run(); | 47 run_loop.Run(); |
| 49 } | 48 } |
| 50 | 49 |
| 51 const std::string& murmur2_hash() { return murmur2_hash_; } | 50 const std::string& murmur2_hash() { return murmur2_hash_; } |
| 52 | 51 |
| 53 private: | 52 private: |
| 54 void OnCompleted(const std::string& murmur2_hash) { | 53 void OnCompleted(const std::string& murmur2_hash) { |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 106 EXPECT_EQ("536500236142107998", runner.murmur2_hash()); | 105 EXPECT_EQ("536500236142107998", runner.murmur2_hash()); |
| 107 } | 106 } |
| 108 | 107 |
| 109 TEST_F(WebApkIconHasherTest, DataUriInvalid) { | 108 TEST_F(WebApkIconHasherTest, DataUriInvalid) { |
| 110 GURL icon_url("data:image/png;base64"); | 109 GURL icon_url("data:image/png;base64"); |
| 111 WebApkIconHasherRunner runner; | 110 WebApkIconHasherRunner runner; |
| 112 runner.Run(icon_url); | 111 runner.Run(icon_url); |
| 113 EXPECT_EQ("", runner.murmur2_hash()); | 112 EXPECT_EQ("", runner.murmur2_hash()); |
| 114 } | 113 } |
| 115 | 114 |
| 115 TEST_F(WebApkIconHasherTest, InvalidUrl) { |
| 116 GURL icon_url("http::google.com"); |
| 117 WebApkIconHasherRunner runner; |
| 118 runner.Run(icon_url); |
| 119 EXPECT_EQ("", runner.murmur2_hash()); |
| 120 } |
| 121 |
| 122 TEST_F(WebApkIconHasherTest, DownloadTimedOut) { |
| 123 GURL icon_url = test_server()->GetURL("/slow?100"); |
| 124 WebApkIconHasherRunner runner; |
| 125 runner.Run(icon_url); |
| 126 EXPECT_EQ("", runner.murmur2_hash()); |
| 127 } |
| 128 |
| 116 // Test that the hash callback is called with an empty string if an HTTP error | 129 // Test that the hash callback is called with an empty string if an HTTP error |
| 117 // prevents the icon URL from being fetched. | 130 // prevents the icon URL from being fetched. |
| 118 TEST_F(WebApkIconHasherTest, HTTPError) { | 131 TEST_F(WebApkIconHasherTest, HTTPError) { |
| 119 GURL icon_url = test_server()->GetURL("/nocontent"); | 132 GURL icon_url = test_server()->GetURL("/nocontent"); |
| 120 WebApkIconHasherRunner runner; | 133 WebApkIconHasherRunner runner; |
| 121 runner.Run(icon_url); | 134 runner.Run(icon_url); |
| 122 EXPECT_EQ("", runner.murmur2_hash()); | 135 EXPECT_EQ("", runner.murmur2_hash()); |
| 123 } | 136 } |
| OLD | NEW |