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