Index: ios/web/web_state/web_state_unittest.mm |
diff --git a/ios/web/web_state/web_state_unittest.mm b/ios/web/web_state/web_state_unittest.mm |
index 35c165c2e97920b0a1148b38be887504cdee26f9..de7f9e7a4e0101182bd8aad4ab4cbd20634a2e59 100644 |
--- a/ios/web/web_state/web_state_unittest.mm |
+++ b/ios/web/web_state/web_state_unittest.mm |
@@ -4,16 +4,43 @@ |
#import "ios/web/public/web_state/web_state.h" |
+#import <UIKit/UIKit.h> |
+ |
#include "base/mac/bind_objc_block.h" |
#include "base/strings/utf_string_conversions.h" |
#include "base/values.h" |
#import "ios/web/public/navigation_manager.h" |
#import "ios/web/public/test/web_test_with_web_state.h" |
+#import "testing/gtest_mac.h" |
+#include "ui/gfx/image/image.h" |
#if !defined(__has_feature) || !__has_feature(objc_arc) |
#error "This file requires ARC support." |
#endif |
+namespace { |
+// Returns the pixel color of the |point| in the |image|. |
+UIColor* colorAtPoint(UIImage* image, CGPoint point) { |
Eugene But (OOO till 7-30)
2017/06/21 22:11:29
Please follow C++ Style Guide for names (start fun
|
+ CGRect sourceRect = CGRectMake(point.x, point.y, 1.f, 1.f); |
+ CGImageRef imageRef = CGImageCreateWithImageInRect(image.CGImage, sourceRect); |
Eugene But (OOO till 7-30)
2017/06/21 22:11:29
Is there any gfx:Image API to get color at point?
edchin
2017/06/22 07:01:08
Yes, there are some gfx::test support functions. I
|
+ CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); |
+ unsigned char buffer[4] = {0, 0, 0, 0}; |
marq (ping after 24h)
2017/06/21 11:13:23
Add comments to explain how this all works. Also m
edchin
2017/06/22 07:01:07
Replaced with gfx::test support functions.
|
+ CGBitmapInfo bitmapInfo = |
+ kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big; |
+ CGContextRef context = |
+ CGBitmapContextCreate(buffer, 1, 1, 8, 4, colorSpace, bitmapInfo); |
+ CGColorSpaceRelease(colorSpace); |
+ CGContextDrawImage(context, CGRectMake(0.f, 0.f, 1.f, 1.f), imageRef); |
+ CGImageRelease(imageRef); |
+ CGContextRelease(context); |
+ CGFloat r = buffer[0] / 255.f; |
+ CGFloat g = buffer[1] / 255.f; |
+ CGFloat b = buffer[2] / 255.f; |
+ CGFloat a = buffer[3] / 255.f; |
+ return [UIColor colorWithRed:r green:g blue:b alpha:a]; |
+} |
+} |
+ |
namespace web { |
// Test fixture for web::WebTest class. |
@@ -112,4 +139,34 @@ TEST_F(WebStateTest, ReloadWithOriginalTypeWithEmptyNavigationManager) { |
ASSERT_FALSE(navigation_manager->GetLastCommittedItem()); |
} |
+// Tests that the snapshot method returns an image of a rendered html page. |
+TEST_F(WebStateTest, Snapshot) { |
+ LoadHtml( |
+ "<html><div style='background-color:#FF0000; width:100px; " |
+ "height:100px;'></div></html>"); |
+ __block bool execution_complete = false; |
Eugene But (OOO till 7-30)
2017/06/21 22:11:29
nit: s/execution_complete/snapshot_taken ? Sounds
edchin
2017/06/22 07:01:08
Done.
|
+ [[[UIApplication sharedApplication] keyWindow] |
+ addSubview:web_state()->GetView()]; |
+ dispatch_after( |
Eugene But (OOO till 7-30)
2017/06/21 22:11:30
This is just for the wait, right? If so, how about
edchin
2017/06/22 07:01:07
Done. Used MinDelay over MaxDelay.
|
+ dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), |
+ dispatch_get_main_queue(), ^{ |
+ web_state()->TakeSnapshot( |
+ base::BindBlockArc(^(const gfx::Image& snapshot) { |
+ ASSERT_FALSE(snapshot.IsEmpty()); |
+ CGSize viewSize = web_state()->GetView().bounds.size; |
Eugene But (OOO till 7-30)
2017/06/21 22:11:29
s/viewSize/view_size
Same comment for other vari
edchin
2017/06/22 07:01:07
Done.
|
+ UIImage* snapshotImage = snapshot.ToUIImage(); |
+ ASSERT_EQ(viewSize.width, snapshotImage.size.width); |
marq (ping after 24h)
2017/06/21 11:13:23
s/ASSERT/EXPECT for these.
edchin
2017/06/22 07:01:07
Done.
|
+ ASSERT_EQ(viewSize.height, snapshotImage.size.height); |
+ ASSERT_NSEQ([UIColor redColor], |
+ colorAtPoint(snapshotImage, CGPointMake(50, 50))); |
+ ASSERT_NSNE([UIColor redColor], |
+ colorAtPoint(snapshotImage, CGPointMake(200, 200))); |
+ execution_complete = true; |
+ })); |
+ }); |
+ WaitForCondition(^{ |
+ return execution_complete; |
+ }); |
+} |
+ |
} // namespace web |