OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/notifications/sync_notifier/notification_bitmap_fetcher
.h" | 5 #include "chrome/browser/bitmap_fetcher.h" |
6 | 6 |
7 #include "base/compiler_specific.h" | 7 #include "base/compiler_specific.h" |
8 #include "chrome/browser/ui/browser.h" | 8 #include "chrome/browser/ui/browser.h" |
9 #include "chrome/test/base/in_process_browser_test.h" | 9 #include "chrome/test/base/in_process_browser_test.h" |
10 #include "content/public/browser/browser_thread.h" | 10 #include "content/public/browser/browser_thread.h" |
11 #include "content/public/test/test_utils.h" | 11 #include "content/public/test/test_utils.h" |
12 #include "net/http/http_status_code.h" | 12 #include "net/http/http_status_code.h" |
13 #include "net/url_request/test_url_fetcher_factory.h" | 13 #include "net/url_request/test_url_fetcher_factory.h" |
14 #include "net/url_request/url_fetcher.h" | 14 #include "net/url_request/url_fetcher.h" |
15 #include "net/url_request/url_request_status.h" | 15 #include "net/url_request/url_request_status.h" |
16 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
17 #include "third_party/skia/include/core/SkBitmap.h" | 17 #include "third_party/skia/include/core/SkBitmap.h" |
18 #include "ui/gfx/codec/png_codec.h" | 18 #include "ui/gfx/codec/png_codec.h" |
19 #include "ui/gfx/size.h" | 19 #include "ui/gfx/size.h" |
20 #include "ui/gfx/skia_util.h" | 20 #include "ui/gfx/skia_util.h" |
21 | 21 |
22 namespace { | 22 namespace { |
23 const bool kAsyncCall = true; | 23 const bool kAsyncCall = true; |
24 const bool kSyncCall = false; | 24 const bool kSyncCall = false; |
25 } // namespace | 25 } // namespace |
26 | 26 |
27 namespace notifier { | 27 namespace chrome { |
28 | 28 |
29 // Class to catch events from the NotificationBitmapFetcher for testing. | 29 // Class to catch events from the BitmapFetcher for testing. |
30 class NotificationBitmapFetcherTestDelegate | 30 class BitmapFetcherTestDelegate : public BitmapFetcherDelegate { |
31 : public NotificationBitmapFetcherDelegate { | |
32 public: | 31 public: |
33 explicit NotificationBitmapFetcherTestDelegate(bool async) | 32 explicit BitmapFetcherTestDelegate(bool async) |
34 : called_(false), success_(false), async_(async) {} | 33 : called_(false), success_(false), async_(async) {} |
35 | 34 |
36 virtual ~NotificationBitmapFetcherTestDelegate() { | 35 virtual ~BitmapFetcherTestDelegate() { EXPECT_TRUE(called_); } |
37 EXPECT_TRUE(called_); | |
38 } | |
39 | 36 |
40 // Method inherited from NotificationBitmapFetcherDelegate. | 37 // Method inherited from BitmapFetcherDelegate. |
41 virtual void OnFetchComplete(const GURL url, | 38 virtual void OnFetchComplete(const GURL url, |
42 const SkBitmap* bitmap) OVERRIDE { | 39 const SkBitmap* bitmap) OVERRIDE { |
43 called_ = true; | 40 called_ = true; |
44 url_ = url; | 41 url_ = url; |
45 if (NULL != bitmap) { | 42 if (NULL != bitmap) { |
46 success_ = true; | 43 success_ = true; |
47 bitmap->deepCopyTo(&bitmap_, bitmap->getConfig()); | 44 bitmap->deepCopyTo(&bitmap_, bitmap->getConfig()); |
48 } | 45 } |
49 // For async calls, we need to quit the message loop so the test can | 46 // For async calls, we need to quit the message loop so the test can |
50 // continue. | 47 // continue. |
51 if (async_) { | 48 if (async_) { |
52 base::MessageLoop::current()->Quit(); | 49 base::MessageLoop::current()->Quit(); |
53 } | 50 } |
54 } | 51 } |
55 | 52 |
56 GURL url() const { return url_; } | 53 GURL url() const { return url_; } |
57 bool success() const { return success_; } | 54 bool success() const { return success_; } |
58 const SkBitmap& bitmap() const { return bitmap_; } | 55 const SkBitmap& bitmap() const { return bitmap_; } |
59 | 56 |
60 private: | 57 private: |
61 bool called_; | 58 bool called_; |
62 GURL url_; | 59 GURL url_; |
63 bool success_; | 60 bool success_; |
64 bool async_; | 61 bool async_; |
65 SkBitmap bitmap_; | 62 SkBitmap bitmap_; |
66 | 63 |
67 DISALLOW_COPY_AND_ASSIGN(NotificationBitmapFetcherTestDelegate); | 64 DISALLOW_COPY_AND_ASSIGN(BitmapFetcherTestDelegate); |
68 }; | 65 }; |
69 | 66 |
70 class NotificationBitmapFetcherBrowserTest : public InProcessBrowserTest { | 67 class BitmapFetcherBrowserTest : public InProcessBrowserTest { |
71 public: | 68 public: |
72 virtual void SetUp() OVERRIDE { | 69 virtual void SetUp() OVERRIDE { |
73 url_fetcher_factory_.reset(new net::FakeURLFetcherFactory(NULL)); | 70 url_fetcher_factory_.reset(new net::FakeURLFetcherFactory(NULL)); |
74 InProcessBrowserTest::SetUp(); | 71 InProcessBrowserTest::SetUp(); |
75 } | 72 } |
76 | 73 |
77 protected: | 74 protected: |
78 scoped_ptr<net::FakeURLFetcherFactory> url_fetcher_factory_; | 75 scoped_ptr<net::FakeURLFetcherFactory> url_fetcher_factory_; |
79 }; | 76 }; |
80 | 77 |
81 #if defined(OS_WIN) | 78 #if defined(OS_WIN) |
82 #define MAYBE_StartTest DISABLED_StartTest | 79 #define MAYBE_StartTest DISABLED_StartTest |
83 #else | 80 #else |
84 #define MAYBE_StartTest StartTest | 81 #define MAYBE_StartTest StartTest |
85 #endif | 82 #endif |
86 | 83 |
87 // WARNING: These tests work with --single_process, but not | 84 // WARNING: These tests work with --single_process, but not |
88 // --single-process. The reason is that the sandbox does not get created | 85 // --single-process. The reason is that the sandbox does not get created |
89 // for us by the test process if --single-process is used. | 86 // for us by the test process if --single-process is used. |
90 | 87 |
91 IN_PROC_BROWSER_TEST_F(NotificationBitmapFetcherBrowserTest, | 88 IN_PROC_BROWSER_TEST_F(BitmapFetcherBrowserTest, MAYBE_StartTest) { |
92 MAYBE_StartTest) { | |
93 GURL url("http://example.com/this-should-work"); | 89 GURL url("http://example.com/this-should-work"); |
94 | 90 |
95 // Put some realistic looking bitmap data into the url_fetcher. | 91 // Put some realistic looking bitmap data into the url_fetcher. |
96 SkBitmap image; | 92 SkBitmap image; |
97 | 93 |
98 // Put a real bitmap into "image". 2x2 bitmap of green 32 bit pixels. | 94 // Put a real bitmap into "image". 2x2 bitmap of green 32 bit pixels. |
99 image.setConfig(SkBitmap::kARGB_8888_Config, 2, 2); | 95 image.setConfig(SkBitmap::kARGB_8888_Config, 2, 2); |
100 image.allocPixels(); | 96 image.allocPixels(); |
101 image.eraseColor(SK_ColorGREEN); | 97 image.eraseColor(SK_ColorGREEN); |
102 | 98 |
103 // Encode the bits as a PNG. | 99 // Encode the bits as a PNG. |
104 std::vector<unsigned char> compressed; | 100 std::vector<unsigned char> compressed; |
105 ASSERT_TRUE(gfx::PNGCodec::EncodeBGRASkBitmap(image, true, &compressed)); | 101 ASSERT_TRUE(gfx::PNGCodec::EncodeBGRASkBitmap(image, true, &compressed)); |
106 | 102 |
107 // Copy the bits into the string, and put them into the FakeURLFetcher. | 103 // Copy the bits into the string, and put them into the FakeURLFetcher. |
108 std::string image_string(compressed.begin(), compressed.end()); | 104 std::string image_string(compressed.begin(), compressed.end()); |
109 | 105 |
110 // Set up a delegate to wait for the callback. | 106 // Set up a delegate to wait for the callback. |
111 NotificationBitmapFetcherTestDelegate delegate(kAsyncCall); | 107 BitmapFetcherTestDelegate delegate(kAsyncCall); |
112 | 108 |
113 NotificationBitmapFetcher fetcher(url, &delegate); | 109 BitmapFetcher fetcher(url, &delegate); |
114 | 110 |
115 url_fetcher_factory_->SetFakeResponse(url, image_string, net::HTTP_OK, | 111 url_fetcher_factory_->SetFakeResponse( |
116 net::URLRequestStatus::SUCCESS); | 112 url, image_string, net::HTTP_OK, net::URLRequestStatus::SUCCESS); |
117 | 113 |
118 // We expect that the image decoder will get called and return | 114 // We expect that the image decoder will get called and return |
119 // an image in a callback to OnImageDecoded(). | 115 // an image in a callback to OnImageDecoded(). |
120 fetcher.Start(browser()->profile()); | 116 fetcher.Start(browser()->profile()); |
121 | 117 |
122 // Blocks until test delegate is notified via a callback. | 118 // Blocks until test delegate is notified via a callback. |
123 content::RunMessageLoop(); | 119 content::RunMessageLoop(); |
124 | 120 |
125 ASSERT_TRUE(delegate.success()); | 121 ASSERT_TRUE(delegate.success()); |
126 | 122 |
127 // Make sure we get back the bitmap we expect. | 123 // Make sure we get back the bitmap we expect. |
128 const SkBitmap& found_image = delegate.bitmap(); | 124 const SkBitmap& found_image = delegate.bitmap(); |
129 EXPECT_TRUE(gfx::BitmapsAreEqual(image, found_image)); | 125 EXPECT_TRUE(gfx::BitmapsAreEqual(image, found_image)); |
130 } | 126 } |
131 | 127 |
132 IN_PROC_BROWSER_TEST_F(NotificationBitmapFetcherBrowserTest, | 128 IN_PROC_BROWSER_TEST_F(BitmapFetcherBrowserTest, OnImageDecodedTest) { |
133 OnImageDecodedTest) { | |
134 GURL url("http://example.com/this-should-work-as-well"); | 129 GURL url("http://example.com/this-should-work-as-well"); |
135 SkBitmap image; | 130 SkBitmap image; |
136 | 131 |
137 // Put a real bitmap into "image". 2x2 bitmap of green 16 bit pixels. | 132 // Put a real bitmap into "image". 2x2 bitmap of green 16 bit pixels. |
138 image.setConfig(SkBitmap::kARGB_8888_Config, 2, 2); | 133 image.setConfig(SkBitmap::kARGB_8888_Config, 2, 2); |
139 image.allocPixels(); | 134 image.allocPixels(); |
140 image.eraseColor(SK_ColorGREEN); | 135 image.eraseColor(SK_ColorGREEN); |
141 | 136 |
142 NotificationBitmapFetcherTestDelegate delegate(kSyncCall); | 137 BitmapFetcherTestDelegate delegate(kSyncCall); |
143 | 138 |
144 NotificationBitmapFetcher fetcher(url, &delegate); | 139 BitmapFetcher fetcher(url, &delegate); |
145 | 140 |
146 fetcher.OnImageDecoded(NULL, image); | 141 fetcher.OnImageDecoded(NULL, image); |
147 | 142 |
148 // Ensure image is marked as succeeded. | 143 // Ensure image is marked as succeeded. |
149 EXPECT_TRUE(delegate.success()); | 144 EXPECT_TRUE(delegate.success()); |
150 | 145 |
151 // Test that the image is what we expect. | 146 // Test that the image is what we expect. |
152 EXPECT_TRUE(gfx::BitmapsAreEqual(image, delegate.bitmap())); | 147 EXPECT_TRUE(gfx::BitmapsAreEqual(image, delegate.bitmap())); |
153 } | 148 } |
154 | 149 |
155 IN_PROC_BROWSER_TEST_F(NotificationBitmapFetcherBrowserTest, | 150 IN_PROC_BROWSER_TEST_F(BitmapFetcherBrowserTest, OnURLFetchFailureTest) { |
156 OnURLFetchFailureTest) { | |
157 GURL url("http://example.com/this-should-be-fetch-failure"); | 151 GURL url("http://example.com/this-should-be-fetch-failure"); |
158 | 152 |
159 // We intentionally put no data into the bitmap to simulate a failure. | 153 // We intentionally put no data into the bitmap to simulate a failure. |
160 | 154 |
161 // Set up a delegate to wait for the callback. | 155 // Set up a delegate to wait for the callback. |
162 NotificationBitmapFetcherTestDelegate delegate(kAsyncCall); | 156 BitmapFetcherTestDelegate delegate(kAsyncCall); |
163 | 157 |
164 NotificationBitmapFetcher fetcher(url, &delegate); | 158 BitmapFetcher fetcher(url, &delegate); |
165 | 159 |
166 url_fetcher_factory_->SetFakeResponse(url, | 160 url_fetcher_factory_->SetFakeResponse(url, |
167 std::string(), | 161 std::string(), |
168 net::HTTP_INTERNAL_SERVER_ERROR, | 162 net::HTTP_INTERNAL_SERVER_ERROR, |
169 net::URLRequestStatus::FAILED); | 163 net::URLRequestStatus::FAILED); |
170 | 164 |
171 fetcher.Start(browser()->profile()); | 165 fetcher.Start(browser()->profile()); |
172 | 166 |
173 // Blocks until test delegate is notified via a callback. | 167 // Blocks until test delegate is notified via a callback. |
174 content::RunMessageLoop(); | 168 content::RunMessageLoop(); |
175 | 169 |
176 EXPECT_FALSE(delegate.success()); | 170 EXPECT_FALSE(delegate.success()); |
177 } | 171 } |
178 | 172 |
179 // Flaky on Win XP Debug: crbug.com/316488 | 173 // Flaky on Win XP Debug: crbug.com/316488 |
180 #if defined(OS_WIN) && !defined(NDEBUG) | 174 #if defined(OS_WIN) && !defined(NDEBUG) |
181 #define MAYBE_HandleImageFailedTest DISABLED_HandleImageFailedTest | 175 #define MAYBE_HandleImageFailedTest DISABLED_HandleImageFailedTest |
182 #else | 176 #else |
183 #define MAYBE_HandleImageFailedTest HandleImageFailedTest | 177 #define MAYBE_HandleImageFailedTest HandleImageFailedTest |
184 #endif | 178 #endif |
185 | 179 |
186 IN_PROC_BROWSER_TEST_F(NotificationBitmapFetcherBrowserTest, | 180 IN_PROC_BROWSER_TEST_F(BitmapFetcherBrowserTest, MAYBE_HandleImageFailedTest) { |
187 MAYBE_HandleImageFailedTest) { | |
188 GURL url("http://example.com/this-should-be-a-decode-failure"); | 181 GURL url("http://example.com/this-should-be-a-decode-failure"); |
189 NotificationBitmapFetcherTestDelegate delegate(kAsyncCall); | 182 BitmapFetcherTestDelegate delegate(kAsyncCall); |
190 NotificationBitmapFetcher fetcher(url, &delegate); | 183 BitmapFetcher fetcher(url, &delegate); |
191 url_fetcher_factory_->SetFakeResponse( | 184 url_fetcher_factory_->SetFakeResponse(url, |
192 url, std::string("Not a real bitmap"), | 185 std::string("Not a real bitmap"), |
193 net::HTTP_OK, net::URLRequestStatus::SUCCESS); | 186 net::HTTP_OK, |
| 187 net::URLRequestStatus::SUCCESS); |
194 | 188 |
195 fetcher.Start(browser()->profile()); | 189 fetcher.Start(browser()->profile()); |
196 | 190 |
197 // Blocks until test delegate is notified via a callback. | 191 // Blocks until test delegate is notified via a callback. |
198 content::RunMessageLoop(); | 192 content::RunMessageLoop(); |
199 | 193 |
200 EXPECT_FALSE(delegate.success()); | 194 EXPECT_FALSE(delegate.success()); |
201 } | 195 } |
202 | 196 |
203 } // namespace notifier | 197 } // namespace chrome |
OLD | NEW |