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