| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/bitmap_fetcher/bitmap_fetcher.h" | 5 #include "chrome/browser/bitmap_fetcher/bitmap_fetcher.h" |
| 6 | 6 |
| 7 #include "base/compiler_specific.h" | 7 #include "base/compiler_specific.h" |
| 8 #include "base/macros.h" | 8 #include "base/macros.h" |
| 9 #include "chrome/browser/profiles/profile.h" | 9 #include "chrome/browser/profiles/profile.h" |
| 10 #include "chrome/browser/ui/browser.h" | 10 #include "chrome/browser/ui/browser.h" |
| 11 #include "chrome/test/base/in_process_browser_test.h" | 11 #include "chrome/test/base/in_process_browser_test.h" |
| 12 #include "content/public/test/test_utils.h" | 12 #include "content/public/test/test_utils.h" |
| 13 #include "net/base/load_flags.h" | 13 #include "net/base/load_flags.h" |
| 14 #include "net/http/http_status_code.h" | 14 #include "net/http/http_status_code.h" |
| 15 #include "net/traffic_annotation/network_traffic_annotation_test_helper.h" |
| 15 #include "net/url_request/test_url_fetcher_factory.h" | 16 #include "net/url_request/test_url_fetcher_factory.h" |
| 16 #include "net/url_request/url_fetcher.h" | 17 #include "net/url_request/url_fetcher.h" |
| 17 #include "net/url_request/url_request_status.h" | 18 #include "net/url_request/url_request_status.h" |
| 18 #include "testing/gtest/include/gtest/gtest.h" | 19 #include "testing/gtest/include/gtest/gtest.h" |
| 19 #include "third_party/skia/include/core/SkBitmap.h" | 20 #include "third_party/skia/include/core/SkBitmap.h" |
| 20 #include "ui/gfx/codec/png_codec.h" | 21 #include "ui/gfx/codec/png_codec.h" |
| 21 #include "ui/gfx/geometry/size.h" | 22 #include "ui/gfx/geometry/size.h" |
| 22 #include "ui/gfx/skia_util.h" | 23 #include "ui/gfx/skia_util.h" |
| 23 | 24 |
| 24 const bool kAsyncCall = true; | 25 const bool kAsyncCall = true; |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 100 // Encode the bits as a PNG. | 101 // Encode the bits as a PNG. |
| 101 std::vector<unsigned char> compressed; | 102 std::vector<unsigned char> compressed; |
| 102 ASSERT_TRUE(gfx::PNGCodec::EncodeBGRASkBitmap(image, true, &compressed)); | 103 ASSERT_TRUE(gfx::PNGCodec::EncodeBGRASkBitmap(image, true, &compressed)); |
| 103 | 104 |
| 104 // Copy the bits into the string, and put them into the FakeURLFetcher. | 105 // Copy the bits into the string, and put them into the FakeURLFetcher. |
| 105 std::string image_string(compressed.begin(), compressed.end()); | 106 std::string image_string(compressed.begin(), compressed.end()); |
| 106 | 107 |
| 107 // Set up a delegate to wait for the callback. | 108 // Set up a delegate to wait for the callback. |
| 108 BitmapFetcherTestDelegate delegate(kAsyncCall); | 109 BitmapFetcherTestDelegate delegate(kAsyncCall); |
| 109 | 110 |
| 110 BitmapFetcher fetcher(url, &delegate); | 111 BitmapFetcher fetcher(url, &delegate, TRAFFIC_ANNOTATION_FOR_TESTS); |
| 111 | 112 |
| 112 url_fetcher_factory_->SetFakeResponse( | 113 url_fetcher_factory_->SetFakeResponse( |
| 113 url, image_string, net::HTTP_OK, net::URLRequestStatus::SUCCESS); | 114 url, image_string, net::HTTP_OK, net::URLRequestStatus::SUCCESS); |
| 114 | 115 |
| 115 // We expect that the image decoder will get called and return | 116 // We expect that the image decoder will get called and return |
| 116 // an image in a callback to OnImageDecoded(). | 117 // an image in a callback to OnImageDecoded(). |
| 117 fetcher.Init( | 118 fetcher.Init( |
| 118 browser()->profile()->GetRequestContext(), | 119 browser()->profile()->GetRequestContext(), |
| 119 std::string(), | 120 std::string(), |
| 120 net::URLRequest::CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE, | 121 net::URLRequest::CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE, |
| (...skipping 13 matching lines...) Expand all Loading... |
| 134 IN_PROC_BROWSER_TEST_F(BitmapFetcherBrowserTest, OnImageDecodedTest) { | 135 IN_PROC_BROWSER_TEST_F(BitmapFetcherBrowserTest, OnImageDecodedTest) { |
| 135 GURL url("http://example.com/this-should-work-as-well"); | 136 GURL url("http://example.com/this-should-work-as-well"); |
| 136 SkBitmap image; | 137 SkBitmap image; |
| 137 | 138 |
| 138 // Put a real bitmap into "image". 2x2 bitmap of green 16 bit pixels. | 139 // Put a real bitmap into "image". 2x2 bitmap of green 16 bit pixels. |
| 139 image.allocN32Pixels(2, 2); | 140 image.allocN32Pixels(2, 2); |
| 140 image.eraseColor(SK_ColorGREEN); | 141 image.eraseColor(SK_ColorGREEN); |
| 141 | 142 |
| 142 BitmapFetcherTestDelegate delegate(kSyncCall); | 143 BitmapFetcherTestDelegate delegate(kSyncCall); |
| 143 | 144 |
| 144 BitmapFetcher fetcher(url, &delegate); | 145 BitmapFetcher fetcher(url, &delegate, TRAFFIC_ANNOTATION_FOR_TESTS); |
| 145 | 146 |
| 146 fetcher.OnImageDecoded(image); | 147 fetcher.OnImageDecoded(image); |
| 147 | 148 |
| 148 // Ensure image is marked as succeeded. | 149 // Ensure image is marked as succeeded. |
| 149 EXPECT_TRUE(delegate.success()); | 150 EXPECT_TRUE(delegate.success()); |
| 150 | 151 |
| 151 // Test that the image is what we expect. | 152 // Test that the image is what we expect. |
| 152 EXPECT_TRUE(gfx::BitmapsAreEqual(image, delegate.bitmap())); | 153 EXPECT_TRUE(gfx::BitmapsAreEqual(image, delegate.bitmap())); |
| 153 } | 154 } |
| 154 | 155 |
| 155 IN_PROC_BROWSER_TEST_F(BitmapFetcherBrowserTest, OnURLFetchFailureTest) { | 156 IN_PROC_BROWSER_TEST_F(BitmapFetcherBrowserTest, OnURLFetchFailureTest) { |
| 156 GURL url("http://example.com/this-should-be-fetch-failure"); | 157 GURL url("http://example.com/this-should-be-fetch-failure"); |
| 157 | 158 |
| 158 // We intentionally put no data into the bitmap to simulate a failure. | 159 // We intentionally put no data into the bitmap to simulate a failure. |
| 159 | 160 |
| 160 // Set up a delegate to wait for the callback. | 161 // Set up a delegate to wait for the callback. |
| 161 BitmapFetcherTestDelegate delegate(kAsyncCall); | 162 BitmapFetcherTestDelegate delegate(kAsyncCall); |
| 162 | 163 |
| 163 BitmapFetcher fetcher(url, &delegate); | 164 BitmapFetcher fetcher(url, &delegate, TRAFFIC_ANNOTATION_FOR_TESTS); |
| 164 | 165 |
| 165 url_fetcher_factory_->SetFakeResponse(url, | 166 url_fetcher_factory_->SetFakeResponse(url, |
| 166 std::string(), | 167 std::string(), |
| 167 net::HTTP_INTERNAL_SERVER_ERROR, | 168 net::HTTP_INTERNAL_SERVER_ERROR, |
| 168 net::URLRequestStatus::FAILED); | 169 net::URLRequestStatus::FAILED); |
| 169 | 170 |
| 170 fetcher.Init( | 171 fetcher.Init( |
| 171 browser()->profile()->GetRequestContext(), | 172 browser()->profile()->GetRequestContext(), |
| 172 std::string(), | 173 std::string(), |
| 173 net::URLRequest::CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE, | 174 net::URLRequest::CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE, |
| 174 net::LOAD_NORMAL); | 175 net::LOAD_NORMAL); |
| 175 fetcher.Start(); | 176 fetcher.Start(); |
| 176 | 177 |
| 177 // Blocks until test delegate is notified via a callback. | 178 // Blocks until test delegate is notified via a callback. |
| 178 delegate.Wait(); | 179 delegate.Wait(); |
| 179 | 180 |
| 180 EXPECT_FALSE(delegate.success()); | 181 EXPECT_FALSE(delegate.success()); |
| 181 } | 182 } |
| 182 | 183 |
| 183 IN_PROC_BROWSER_TEST_F(BitmapFetcherBrowserTest, HandleImageFailedTest) { | 184 IN_PROC_BROWSER_TEST_F(BitmapFetcherBrowserTest, HandleImageFailedTest) { |
| 184 GURL url("http://example.com/this-should-be-a-decode-failure"); | 185 GURL url("http://example.com/this-should-be-a-decode-failure"); |
| 185 BitmapFetcherTestDelegate delegate(kAsyncCall); | 186 BitmapFetcherTestDelegate delegate(kAsyncCall); |
| 186 BitmapFetcher fetcher(url, &delegate); | 187 BitmapFetcher fetcher(url, &delegate, TRAFFIC_ANNOTATION_FOR_TESTS); |
| 187 url_fetcher_factory_->SetFakeResponse(url, | 188 url_fetcher_factory_->SetFakeResponse(url, |
| 188 std::string("Not a real bitmap"), | 189 std::string("Not a real bitmap"), |
| 189 net::HTTP_OK, | 190 net::HTTP_OK, |
| 190 net::URLRequestStatus::SUCCESS); | 191 net::URLRequestStatus::SUCCESS); |
| 191 | 192 |
| 192 fetcher.Init( | 193 fetcher.Init( |
| 193 browser()->profile()->GetRequestContext(), | 194 browser()->profile()->GetRequestContext(), |
| 194 std::string(), | 195 std::string(), |
| 195 net::URLRequest::CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE, | 196 net::URLRequest::CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE, |
| 196 net::LOAD_NORMAL); | 197 net::LOAD_NORMAL); |
| 197 fetcher.Start(); | 198 fetcher.Start(); |
| 198 | 199 |
| 199 // Blocks until test delegate is notified via a callback. | 200 // Blocks until test delegate is notified via a callback. |
| 200 delegate.Wait(); | 201 delegate.Wait(); |
| 201 | 202 |
| 202 EXPECT_FALSE(delegate.success()); | 203 EXPECT_FALSE(delegate.success()); |
| 203 } | 204 } |
| 204 | 205 |
| 205 } // namespace chrome | 206 } // namespace chrome |
| OLD | NEW |