OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 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/test/base/in_process_browser_test.h" | |
9 #include "content/public/browser/browser_thread.h" | |
10 #include "content/public/test/test_utils.h" | |
11 #include "net/url_request/test_url_fetcher_factory.h" | |
12 #include "net/url_request/url_fetcher.h" | |
13 #include "net/url_request/url_request_status.h" | |
14 #include "testing/gtest/include/gtest/gtest.h" | |
15 #include "third_party/skia/include/core/SKBitmap.h" | |
16 #include "ui/gfx/codec/png_codec.h" | |
17 #include "ui/gfx/size.h" | |
18 #include "ui/gfx/skia_util.h" | |
19 | |
20 namespace { | |
21 // FF0000FF is 100% alpha and max green.(A, R, B, G) | |
22 uint32 kMaxGreen = 0xFF0000FF; | |
23 const bool kAsyncCall = true; | |
24 const bool kSyncCall = false; | |
25 const bool kSuccess = true; | |
26 const bool kFailure = false; | |
27 } // namespace | |
28 | |
29 namespace notifier { | |
30 | |
31 // Class to catch events from the NotificationBitmapFetcher for testing. | |
32 class NotificationBitmapFetcherTestDelegate | |
33 : public NotificationBitmapFetcherDelegate { | |
34 public: | |
35 explicit NotificationBitmapFetcherTestDelegate(bool async) | |
36 : success_(false), async_(async) {} | |
37 | |
38 virtual ~NotificationBitmapFetcherTestDelegate() {} | |
39 | |
40 // Method inherited from NotificationBitmapFetcherDelegate | |
41 void OnFetchComplete(const SkBitmap* bitmap) OVERRIDE { | |
42 if (NULL != bitmap) { | |
43 success_ = true; | |
44 bitmap->deepCopyTo(&bitmap_, bitmap->getConfig()); | |
45 } | |
46 // For async calls, we need to quit the message loop so the test can | |
47 // continue. | |
48 if (async_) { | |
49 MessageLoop::current()->Quit(); | |
50 } | |
51 } | |
52 | |
53 bool success() const { return success_; } | |
54 const SkBitmap& bitmap() const { return bitmap_; } | |
55 | |
56 private: | |
57 bool success_; | |
58 bool async_; | |
59 SkBitmap bitmap_; | |
60 | |
61 DISALLOW_COPY_AND_ASSIGN(NotificationBitmapFetcherTestDelegate); | |
62 }; | |
63 | |
64 typedef InProcessBrowserTest NotificationBitmapFetcherBrowserTest; | |
65 | |
66 // WARNING: These tests work with --single_process, but not | |
67 // --single-process. The reason is that the sandbox does not get created | |
68 // for us by the test process if --single-process is used. | |
69 | |
70 IN_PROC_BROWSER_TEST_F(NotificationBitmapFetcherBrowserTest, | |
71 StartTest) { | |
72 GURL url("http://localhost"); | |
73 | |
74 // Put some realistic looking bitmap data into the url_fetcher. | |
75 SkBitmap image; | |
76 | |
77 // Put a real bitmap into "image". 2x2 bitmap of green 32 bit pixels. | |
78 image.setConfig(SkBitmap::kARGB_8888_Config, 2, 2); | |
79 image.allocPixels(); | |
80 SkColor c = kMaxGreen; | |
81 image.eraseColor(c); | |
82 | |
83 // Encode the bits as a PNG. | |
84 std::vector<unsigned char> compressed; | |
85 ASSERT_TRUE(gfx::PNGCodec::EncodeBGRASkBitmap(image, true, &compressed)); | |
86 | |
87 // Copy the bits into the string, and put them into the FakeURLFetcher. | |
88 std::string image_string(compressed.begin(), compressed.end()); | |
89 | |
90 // Set up a delegate to wait for the callback. | |
91 NotificationBitmapFetcherTestDelegate delegate(kAsyncCall); | |
92 | |
93 NotificationBitmapFetcher fetcher(url, &delegate); | |
94 | |
95 scoped_ptr<net::URLFetcher> url_fetcher(new net::FakeURLFetcher( | |
96 url, &fetcher, image_string, kSuccess)); | |
97 fetcher.SetURLFetcherForTest(url_fetcher.Pass()); | |
98 | |
99 // We expect that the image decoder will get called and return | |
100 // an image in a callback to OnImageDecoded(). | |
101 fetcher.Start(); | |
102 | |
103 // Blocks until test delegate is notified via a callback. | |
104 content::RunMessageLoop(); | |
105 | |
106 ASSERT_TRUE(delegate.success()); | |
107 | |
108 // Make sure we get back the bitmap we expect. | |
109 const SkBitmap& found_image = delegate.bitmap(); | |
110 EXPECT_TRUE(gfx::BitmapsAreEqual(image, found_image)); | |
111 } | |
112 | |
113 | |
114 IN_PROC_BROWSER_TEST_F(NotificationBitmapFetcherBrowserTest, | |
115 OnImageDecodedTest) { | |
116 GURL url("http://localhost"); | |
117 SkBitmap image; | |
118 | |
119 // Put a real bitmap into "image". 2x2 bitmap of green 16 bit pixels. | |
120 image.setConfig(SkBitmap::kARGB_8888_Config, 2, 2); | |
121 image.allocPixels(); | |
122 SkColor c = kMaxGreen; | |
123 image.eraseColor(c); | |
124 | |
125 NotificationBitmapFetcherTestDelegate delegate(kSyncCall); | |
126 | |
127 NotificationBitmapFetcher fetcher(url, &delegate); | |
128 | |
129 scoped_ptr<net::URLFetcher> url_fetcher( | |
130 new net::TestURLFetcher(1, url, &fetcher)); | |
131 fetcher.SetURLFetcherForTest(url_fetcher.Pass()); | |
dcheng
2013/05/31 02:34:52
If this test remains, note that url_fetcher is com
Pete Williamson
2013/05/31 16:42:20
Good point, removed.
| |
132 | |
133 fetcher.OnImageDecoded(NULL, image); | |
134 | |
135 // Ensure image is marked as succeeded. | |
136 EXPECT_TRUE(delegate.success()); | |
137 | |
138 // Test that the image is stored and ready. Pixel [0,0] should be green. | |
139 EXPECT_TRUE(gfx::BitmapsAreEqual(image, delegate.bitmap())); | |
140 | |
141 } | |
142 | |
143 IN_PROC_BROWSER_TEST_F(NotificationBitmapFetcherBrowserTest, | |
144 OnURLFetchFailureTest) { | |
145 GURL url("http://localhost"); | |
146 | |
147 // We intentionally put no data into the bitmap to simulate a failure. | |
148 | |
149 // Set up a delegate to wait for the callback. | |
150 NotificationBitmapFetcherTestDelegate delegate(kAsyncCall); | |
151 | |
152 // The fetcher controls the lifetime of url_fetcher, but we keep a | |
153 // pointer to it with stub_url_fetcher. | |
154 NotificationBitmapFetcher fetcher(url, &delegate); | |
155 | |
156 scoped_ptr<net::URLFetcher> url_fetcher(new net::FakeURLFetcher( | |
157 url, &fetcher, std::string(), kFailure)); | |
158 fetcher.SetURLFetcherForTest(url_fetcher.Pass()); | |
159 | |
160 fetcher.Start(); | |
161 | |
dcheng
2013/05/31 02:34:52
I think this test needs to run the message loop to
Pete Williamson
2013/05/31 16:42:20
Surprisingly, it passed without the message loop,
dcheng
2013/05/31 17:36:04
success() defaults to false, so that's why it pass
| |
162 EXPECT_FALSE(delegate.success()); | |
163 } | |
164 | |
165 IN_PROC_BROWSER_TEST_F(NotificationBitmapFetcherBrowserTest, | |
166 HandleImageFailedTest) { | |
167 GURL url("http://localhost"); | |
168 NotificationBitmapFetcherTestDelegate delegate(kAsyncCall); | |
169 NotificationBitmapFetcher fetcher(url, &delegate); | |
170 scoped_ptr<net::URLFetcher> url_fetcher(new net::FakeURLFetcher( | |
171 url, &fetcher, std::string("Not a real bitmap"), kSuccess)); | |
172 fetcher.SetURLFetcherForTest(url_fetcher.Pass()); | |
173 | |
174 fetcher.Start(); | |
175 | |
dcheng
2013/05/31 02:34:52
Ditto.
Pete Williamson
2013/05/31 16:42:20
Same response.
| |
176 EXPECT_FALSE(delegate.success()); | |
177 } | |
178 | |
179 } // namespace notifier | |
OLD | NEW |