Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #import "ios/web/public/web_state/web_state.h" | 5 #import "ios/web/public/web_state/web_state.h" |
| 6 | 6 |
| 7 #import <UIKit/UIKit.h> | |
| 8 | |
| 7 #include "base/mac/bind_objc_block.h" | 9 #include "base/mac/bind_objc_block.h" |
| 8 #include "base/strings/utf_string_conversions.h" | 10 #include "base/strings/utf_string_conversions.h" |
| 9 #include "base/values.h" | 11 #include "base/values.h" |
| 10 #import "ios/web/public/navigation_manager.h" | 12 #import "ios/web/public/navigation_manager.h" |
| 11 #import "ios/web/public/test/web_test_with_web_state.h" | 13 #import "ios/web/public/test/web_test_with_web_state.h" |
| 14 #import "testing/gtest_mac.h" | |
| 15 #include "ui/gfx/image/image.h" | |
| 12 | 16 |
| 13 #if !defined(__has_feature) || !__has_feature(objc_arc) | 17 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 14 #error "This file requires ARC support." | 18 #error "This file requires ARC support." |
| 15 #endif | 19 #endif |
| 16 | 20 |
| 21 namespace { | |
| 22 // Returns the pixel color of the |point| in the |image|. | |
| 23 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
| |
| 24 CGRect sourceRect = CGRectMake(point.x, point.y, 1.f, 1.f); | |
| 25 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
| |
| 26 CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); | |
| 27 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.
| |
| 28 CGBitmapInfo bitmapInfo = | |
| 29 kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big; | |
| 30 CGContextRef context = | |
| 31 CGBitmapContextCreate(buffer, 1, 1, 8, 4, colorSpace, bitmapInfo); | |
| 32 CGColorSpaceRelease(colorSpace); | |
| 33 CGContextDrawImage(context, CGRectMake(0.f, 0.f, 1.f, 1.f), imageRef); | |
| 34 CGImageRelease(imageRef); | |
| 35 CGContextRelease(context); | |
| 36 CGFloat r = buffer[0] / 255.f; | |
| 37 CGFloat g = buffer[1] / 255.f; | |
| 38 CGFloat b = buffer[2] / 255.f; | |
| 39 CGFloat a = buffer[3] / 255.f; | |
| 40 return [UIColor colorWithRed:r green:g blue:b alpha:a]; | |
| 41 } | |
| 42 } | |
| 43 | |
| 17 namespace web { | 44 namespace web { |
| 18 | 45 |
| 19 // Test fixture for web::WebTest class. | 46 // Test fixture for web::WebTest class. |
| 20 typedef web::WebTestWithWebState WebStateTest; | 47 typedef web::WebTestWithWebState WebStateTest; |
| 21 | 48 |
| 22 // Tests script execution with and without callback. | 49 // Tests script execution with and without callback. |
| 23 TEST_F(WebStateTest, ScriptExecution) { | 50 TEST_F(WebStateTest, ScriptExecution) { |
| 24 LoadHtml("<html></html>"); | 51 LoadHtml("<html></html>"); |
| 25 | 52 |
| 26 // Execute script without callback. | 53 // Execute script without callback. |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 105 ASSERT_FALSE(navigation_manager->GetLastCommittedItem()); | 132 ASSERT_FALSE(navigation_manager->GetLastCommittedItem()); |
| 106 | 133 |
| 107 navigation_manager->Reload(web::ReloadType::ORIGINAL_REQUEST_URL, | 134 navigation_manager->Reload(web::ReloadType::ORIGINAL_REQUEST_URL, |
| 108 false /* check_for_repost */); | 135 false /* check_for_repost */); |
| 109 | 136 |
| 110 ASSERT_FALSE(navigation_manager->GetTransientItem()); | 137 ASSERT_FALSE(navigation_manager->GetTransientItem()); |
| 111 ASSERT_FALSE(navigation_manager->GetPendingItem()); | 138 ASSERT_FALSE(navigation_manager->GetPendingItem()); |
| 112 ASSERT_FALSE(navigation_manager->GetLastCommittedItem()); | 139 ASSERT_FALSE(navigation_manager->GetLastCommittedItem()); |
| 113 } | 140 } |
| 114 | 141 |
| 142 // Tests that the snapshot method returns an image of a rendered html page. | |
| 143 TEST_F(WebStateTest, Snapshot) { | |
| 144 LoadHtml( | |
| 145 "<html><div style='background-color:#FF0000; width:100px; " | |
| 146 "height:100px;'></div></html>"); | |
| 147 __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.
| |
| 148 [[[UIApplication sharedApplication] keyWindow] | |
| 149 addSubview:web_state()->GetView()]; | |
| 150 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.
| |
| 151 dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), | |
| 152 dispatch_get_main_queue(), ^{ | |
| 153 web_state()->TakeSnapshot( | |
| 154 base::BindBlockArc(^(const gfx::Image& snapshot) { | |
| 155 ASSERT_FALSE(snapshot.IsEmpty()); | |
| 156 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.
| |
| 157 UIImage* snapshotImage = snapshot.ToUIImage(); | |
| 158 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.
| |
| 159 ASSERT_EQ(viewSize.height, snapshotImage.size.height); | |
| 160 ASSERT_NSEQ([UIColor redColor], | |
| 161 colorAtPoint(snapshotImage, CGPointMake(50, 50))); | |
| 162 ASSERT_NSNE([UIColor redColor], | |
| 163 colorAtPoint(snapshotImage, CGPointMake(200, 200))); | |
| 164 execution_complete = true; | |
| 165 })); | |
| 166 }); | |
| 167 WaitForCondition(^{ | |
| 168 return execution_complete; | |
| 169 }); | |
| 170 } | |
| 171 | |
| 115 } // namespace web | 172 } // namespace web |
| OLD | NEW |