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 "chrome/common/chrome_notification_types.h" | |
8 #include "chrome/test/base/in_process_browser_test.h" | |
9 #include "content/public/browser/browser_thread.h" | |
10 #include "content/public/browser/notification_service.h" | |
11 #include "content/public/test/test_utils.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 | |
20 namespace { | |
21 // FF0000FF is 100% alpha and max green.(A, R, B, G) | |
22 uint32 kMaxGreen = 0xFF0000FF; | |
23 } // namespace | |
24 | |
25 namespace notifier { | |
26 | |
27 // Class to catch events from the NotificationBitmapFetcher for testing. | |
28 class NotificationBitmapFetcherTestDelegate | |
29 : public NotificationBitmapFetcherDelegate { | |
30 public: | |
31 NotificationBitmapFetcherTestDelegate() | |
32 : success_(false), bitmap_(NULL) {} | |
33 | |
34 ~NotificationBitmapFetcherTestDelegate() {} | |
dcheng
2013/05/28 20:11:34
The destructor should be explicitly marked virtual
Pete Williamson
2013/05/29 18:05:00
Done.
| |
35 | |
36 // Method inherited from NotificationBitmapFetcherDelegate | |
37 void OnFetchComplete(const bool success, SkBitmap* bitmap) { | |
dcheng
2013/05/28 20:11:34
Overridden methods should be marked virtual OVERRI
Pete Williamson
2013/05/29 18:05:00
Done.
| |
38 success_ = success; | |
39 bitmap_ = bitmap; | |
40 MessageLoop::current()->Quit(); | |
41 } | |
42 | |
43 bool success() { return success_; } | |
dcheng
2013/05/28 20:11:34
const.
Pete Williamson
2013/05/29 18:05:00
Done.
| |
44 SkBitmap* bitmap() { return bitmap_; } | |
dcheng
2013/05/28 20:11:34
Add const where appropriate.
Pete Williamson
2013/05/29 18:05:00
Done.
| |
45 | |
46 private: | |
47 bool success_; | |
48 SkBitmap* bitmap_; | |
dcheng
2013/05/28 20:11:34
It's probably safer to have this be SkBitmap bitma
Pete Williamson
2013/05/29 18:05:00
Done.
| |
49 | |
50 DISALLOW_COPY_AND_ASSIGN(NotificationBitmapFetcherTestDelegate); | |
51 }; | |
52 | |
53 typedef InProcessBrowserTest NotificationBitmapFetcherBrowserTest; | |
54 | |
55 // WARNING: These tests work with --single_process, but not | |
56 // --single-process. The reason is that the sandbox does not get created | |
57 // for us by the test process if --single-process is used. | |
58 | |
59 IN_PROC_BROWSER_TEST_F(NotificationBitmapFetcherBrowserTest, | |
60 OnURLFetchCompleteTest) { | |
61 GURL url("http://localhost"); | |
62 | |
63 // Put some realistic looking bitmap data into the url_fetcher. | |
64 SkBitmap image; | |
65 | |
66 // Put a real bitmap into "image". 2x2 bitmap of green 32 bit pixels. | |
67 image.setConfig(SkBitmap::kARGB_8888_Config, 2, 2); | |
68 image.allocPixels(); | |
69 SkColor c = kMaxGreen; | |
70 image.eraseColor(c); | |
71 // Test that the image is stored and ready. Pixel [0,0] should be green. | |
72 EXPECT_EQ(16, image.getSize()); | |
73 EXPECT_EQ(kMaxGreen, image.getColor(0, 0)); | |
dcheng
2013/05/28 20:11:34
These EXPECT statements seem out of place. I would
Pete Williamson
2013/05/29 18:05:00
Removed. My original thinking was that this was f
| |
74 | |
75 // Get vital stats from the bitmap for width, height, size, etc. | |
76 SkAutoLockPixels lock(image); | |
77 int width = image.width(); | |
78 int height = image.height(); | |
79 int row_length = static_cast<int>(image.rowBytes()); | |
80 size_t size = row_length * height; | |
81 // Actual bitmap data in arrays of RGBAs. | |
82 std::vector<unsigned char> data; | |
83 data.resize(size); | |
84 memcpy(&*data.begin(), image.getAddr(0, 0), size); | |
85 | |
86 // Encode the bits as a PNG. | |
87 std::vector<unsigned char> compressed; | |
88 ASSERT_TRUE(gfx::PNGCodec::Encode(&*data.begin(), | |
dcheng
2013/05/28 20:11:34
Instead of doing this, just use gfx::PNGCodec::Enc
Pete Williamson
2013/05/29 18:05:00
Done.
| |
89 gfx::PNGCodec::FORMAT_BGRA, | |
90 gfx::Size(width, height), | |
91 static_cast<int>(row_length), | |
92 true, | |
93 std::vector<gfx::PNGCodec::Comment>(), | |
94 &compressed)); | |
95 | |
96 // Copy the bits into the string, and put them into the StubURLFetcher. | |
97 std::string image_string; | |
98 image_string.resize(compressed.size()); | |
99 // TODO(petewil): It might be better to have STL to write this loop for us | |
100 // with for_each or a function. Make sure it handles embedded nulls. | |
101 for (size_t ii = 0; ii < compressed.size(); ++ii) { | |
102 image_string[ii] = compressed[ii]; | |
103 } | |
104 | |
105 // Set up a delegate to wait for the callback. | |
106 NotificationBitmapFetcherTestDelegate delegate; | |
107 | |
108 scoped_refptr<NotificationBitmapFetcher> fetcher = | |
109 new NotificationBitmapFetcher(url, &delegate); | |
110 | |
111 // The fetcher controls the lifetime of url_fetcher, but we keep a | |
112 // pointer to it with stub_url_fetcher. | |
113 net::TestURLFetcher* stub_url_fetcher = | |
dcheng
2013/05/28 20:11:34
Make this a scoped_ptr.
Pete Williamson
2013/05/29 18:05:00
If I do, won't it get double deleted, since url_fe
dcheng
2013/05/29 20:40:29
If the test uses Start() instead of OnURLFetchComp
Pete Williamson
2013/05/30 00:13:18
We now use Start, but it is still a problem. It i
| |
114 new net::TestURLFetcher(1, url, fetcher.get()); | |
115 scoped_ptr<net::URLFetcher> url_fetcher(stub_url_fetcher); | |
116 fetcher->SetURLFetcherForTest(url_fetcher); | |
117 stub_url_fetcher->SetResponseString(image_string); | |
118 | |
119 // We expect that the image decoder will get called and return | |
120 // an image in a callback to OnImageDecoded(). | |
121 fetcher->OnURLFetchComplete(stub_url_fetcher); | |
dcheng
2013/05/28 20:11:34
Maybe just invoke Start()
Pete Williamson
2013/05/29 18:05:00
This test is not set up to be async, and Start is
dcheng
2013/05/29 20:40:29
That means that Start() is never tested--and it mi
Pete Williamson
2013/05/30 00:13:18
Done.
| |
122 | |
123 // Blocks until test delegate is notified via a callback. | |
124 content::RunMessageLoop(); | |
125 | |
126 ASSERT_TRUE(delegate.success()); | |
127 | |
128 // Make sure we get back the bitmap we expect. | |
129 SkBitmap* found_image = delegate.bitmap(); | |
dcheng
2013/05/28 20:11:34
Consider just using BitmapsAreEqual from ui/gfx/sk
Pete Williamson
2013/05/29 18:05:00
Done.
| |
130 EXPECT_EQ(16, found_image->getSize()); | |
131 EXPECT_EQ(kMaxGreen, found_image->getColor(0, 0)); | |
132 } | |
133 | |
134 } // namespace notifier | |
OLD | NEW |