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 "base/message_loop.h" | |
6 #include "chrome/browser/notifications/sync_notifier/notification_bitmap_fetcher .h" | |
7 #include "content/public/browser/browser_thread.h" | |
8 #include "content/public/test/test_browser_thread.h" | |
9 #include "net/base/host_port_pair.h" | |
10 #include "net/url_request/test_url_fetcher_factory.h" | |
11 #include "net/url_request/url_fetcher.h" | |
12 #include "net/url_request/url_request_status.h" | |
13 #include "testing/gtest/include/gtest/gtest.h" | |
14 #include "third_party/skia/include/core/SKBitmap.h" | |
15 | |
16 namespace { | |
17 // FF0000FF is 100% alpha and max green.(A, R, B, G) | |
18 uint32_t kMaxGreen = 0xFF0000FF; | |
19 | |
20 } // namespace | |
21 | |
22 namespace net { | |
23 class HttpRequestHeaders; | |
24 class HttpResponseHeaders; | |
25 class URLRequestContextGetter; | |
26 } | |
27 | |
28 namespace notifier { | |
29 | |
30 // Class to catch events from the NotificationBitmapFetcher for testing. | |
31 class NotificationBitmapFetcherTestDelegate | |
32 : public NotificationBitmapFetcherDelegate { | |
33 public: | |
34 NotificationBitmapFetcherTestDelegate() | |
35 : success_(false), bitmap_(NULL) {} | |
36 | |
37 ~NotificationBitmapFetcherTestDelegate() {} | |
38 | |
39 // Method inherited from NotificationBitmapFetcherDelegate | |
40 void OnFetchComplete(const bool success, SkBitmap* bitmap) { | |
41 success_ = success; | |
42 bitmap_ = bitmap; | |
43 } | |
44 | |
45 bool success() { return success_; } | |
46 SkBitmap* bitmap() { return bitmap_; } | |
47 | |
48 private: | |
49 bool success_; | |
50 SkBitmap* bitmap_; | |
51 | |
52 DISALLOW_COPY_AND_ASSIGN(NotificationBitmapFetcherTestDelegate); | |
53 }; | |
54 | |
55 class NotificationBitmapFetcherTest : public testing::Test { | |
56 public: | |
57 NotificationBitmapFetcherTest() | |
58 : ui_thread_(content::BrowserThread::UI, &message_loop_) {} | |
59 | |
60 private: | |
61 MessageLoopForIO message_loop_; | |
62 content::TestBrowserThread ui_thread_; | |
63 }; | |
64 | |
65 TEST_F(NotificationBitmapFetcherTest, OnImageDecodedTest) { | |
dcheng
2013/05/28 20:11:34
It feels strange to have the functional tests spli
Pete Williamson
2013/05/29 18:05:00
OK, I can easily make these browser tests. I'm no
dcheng
2013/05/29 20:40:29
I think there are two potential pitfalls with this
| |
66 GURL url("http://localhost"); | |
67 scoped_ptr<SkBitmap> image(new SkBitmap()); | |
68 | |
69 // Put a real bitmap into "image". 2x2 bitmap of green 16 bit pixels. | |
70 image->setConfig(SkBitmap::kRGB_565_Config, 2, 2); | |
71 image->allocPixels(); | |
72 SkColor c = kMaxGreen; | |
73 image->eraseColor(c); | |
74 // Test that the image is stored and ready. Pixel [0,0] should be green. | |
75 EXPECT_EQ(8, image->getSize()); | |
76 EXPECT_EQ(kMaxGreen, image->getColor(0, 0)); | |
77 | |
78 NotificationBitmapFetcherTestDelegate delegate; | |
79 | |
80 scoped_refptr<NotificationBitmapFetcher> fetcher = | |
81 new NotificationBitmapFetcher(url, &delegate); | |
82 | |
83 scoped_ptr<net::URLFetcher> url_fetcher( | |
84 new net::TestURLFetcher(1, url, fetcher.get())); | |
85 fetcher->SetURLFetcherForTest(url_fetcher); | |
86 | |
87 fetcher->OnImageDecoded(NULL, *image.get()); | |
88 | |
89 // Ensure image is marked as succeeded. | |
90 EXPECT_EQ(true, delegate.success()); | |
91 // Test that the image is stored and ready. Pixel [0,0] should be green. | |
92 EXPECT_EQ(8, delegate.bitmap()->getSize()); | |
93 EXPECT_EQ(2, delegate.bitmap()->width()); | |
94 EXPECT_EQ(2, delegate.bitmap()->height()); | |
95 EXPECT_TRUE(delegate.bitmap()->getPixels() != NULL); | |
96 EXPECT_EQ(kMaxGreen, delegate.bitmap()->getColor(0, 0)); | |
97 } | |
98 | |
99 TEST_F(NotificationBitmapFetcherTest, OnURLFetchFailureTest) { | |
100 GURL url("http://localhost"); | |
101 | |
102 // We intentionally put no data into the bitmap to simulate a failure. | |
103 | |
104 // Set up a delegate to wait for the callback. | |
105 NotificationBitmapFetcherTestDelegate delegate; | |
106 | |
107 // The fetcher controls the lifetime of url_fetcher, but we keep a | |
108 // pointer to it with stub_url_fetcher. | |
109 scoped_refptr<NotificationBitmapFetcher> fetcher = | |
110 new NotificationBitmapFetcher(url, &delegate); | |
111 | |
112 // The fetcher controls the lifetime of url_fetcher, but we keep a | |
113 // pointer to it with stub_url_fetcher. | |
114 net::TestURLFetcher* stub_url_fetcher = | |
115 new net::TestURLFetcher(1, url, fetcher.get()); | |
116 scoped_ptr<net::URLFetcher> url_fetcher(stub_url_fetcher); | |
117 fetcher->SetURLFetcherForTest(url_fetcher); | |
118 | |
119 // We expect that the fetch complete notification will be sent, but that the | |
120 // image will be marked as failed. | |
121 fetcher->OnURLFetchComplete(stub_url_fetcher); | |
122 | |
123 EXPECT_FALSE(delegate.success()); | |
124 EXPECT_EQ(NULL, delegate.bitmap()); | |
125 } | |
126 | |
127 TEST_F(NotificationBitmapFetcherTest, HandleImageFailedTest) { | |
128 GURL url("http://localhost"); | |
129 NotificationBitmapFetcherTestDelegate delegate; | |
130 scoped_refptr<NotificationBitmapFetcher> fetcher = | |
131 new NotificationBitmapFetcher(url, &delegate); | |
132 | |
133 scoped_ptr<net::URLFetcher> url_fetcher( | |
134 new net::TestURLFetcher(1, url, fetcher.get())); | |
135 fetcher->SetURLFetcherForTest(url_fetcher); | |
136 | |
137 EXPECT_EQ(false, delegate.success()); | |
138 | |
139 fetcher->OnDecodeImageFailed(NULL); | |
140 | |
141 EXPECT_EQ(NULL, delegate.bitmap()); | |
142 EXPECT_EQ(false, delegate.success()); | |
143 } | |
144 | |
145 } // namespace notifier | |
OLD | NEW |