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