From 9449152cafc88741f89c86c720837db390e8feec Mon Sep 17 00:00:00 2001 From: andyque Date: Tue, 26 Aug 2014 22:03:55 +0800 Subject: [PATCH] finish WebView Tests --- build/cocos2d_tests.xcodeproj/project.pbxproj | 4 + cocos/ui/WebView.h | 4 +- cocos/ui/WebViewImpl_iOS.h | 4 +- cocos/ui/WebViewImpl_iOS.mm | 5 +- .../UIWebViewTest/UIWebViewTest.cpp | 124 +++++++++++++++++- .../UIWebViewTest/UIWebViewTest.h | 4 +- tests/cpp-tests/Resources/Test.html | 5 + 7 files changed, 140 insertions(+), 10 deletions(-) create mode 100644 tests/cpp-tests/Resources/Test.html diff --git a/build/cocos2d_tests.xcodeproj/project.pbxproj b/build/cocos2d_tests.xcodeproj/project.pbxproj index a0dc2b00f6..2029a5d0ff 100644 --- a/build/cocos2d_tests.xcodeproj/project.pbxproj +++ b/build/cocos2d_tests.xcodeproj/project.pbxproj @@ -866,6 +866,7 @@ 295824591987415900F9746D /* UIScale9SpriteTest.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 295824571987415900F9746D /* UIScale9SpriteTest.cpp */; }; 2958245A1987415900F9746D /* UIScale9SpriteTest.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 295824571987415900F9746D /* UIScale9SpriteTest.cpp */; }; 298D7F6F19AC31F300FF096D /* UIWebViewTest.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 298D7F6D19AC31F300FF096D /* UIWebViewTest.cpp */; }; + 29AFEF6719ACCAA000F6B10A /* Test.html in Resources */ = {isa = PBXBuildFile; fileRef = 29AFEF6619ACCAA000F6B10A /* Test.html */; }; 29FBBBFE196A9ECD00E65826 /* CocostudioParserJsonTest.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 29FBBBFC196A9ECD00E65826 /* CocostudioParserJsonTest.cpp */; }; 29FBBBFF196A9ECD00E65826 /* CocostudioParserJsonTest.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 29FBBBFC196A9ECD00E65826 /* CocostudioParserJsonTest.cpp */; }; 38FA2E73194AEBE100FF2BE4 /* ActionTimelineTestScene.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 38FA2E71194AEBE100FF2BE4 /* ActionTimelineTestScene.cpp */; }; @@ -2889,6 +2890,7 @@ 295824581987415900F9746D /* UIScale9SpriteTest.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = UIScale9SpriteTest.h; sourceTree = ""; }; 298D7F6D19AC31F300FF096D /* UIWebViewTest.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = UIWebViewTest.cpp; path = UIWebViewTest/UIWebViewTest.cpp; sourceTree = ""; }; 298D7F6E19AC31F300FF096D /* UIWebViewTest.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = UIWebViewTest.h; path = UIWebViewTest/UIWebViewTest.h; sourceTree = ""; }; + 29AFEF6619ACCAA000F6B10A /* Test.html */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.html; name = Test.html; path = "../tests/cpp-tests/Resources/Test.html"; sourceTree = ""; }; 29FBBBFC196A9ECD00E65826 /* CocostudioParserJsonTest.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = CocostudioParserJsonTest.cpp; sourceTree = ""; }; 29FBBBFD196A9ECD00E65826 /* CocostudioParserJsonTest.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CocostudioParserJsonTest.h; sourceTree = ""; }; 38FA2E71194AEBE100FF2BE4 /* ActionTimelineTestScene.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = ActionTimelineTestScene.cpp; sourceTree = ""; }; @@ -6498,6 +6500,7 @@ 1AC35CB518CED84500F37B72 /* effect1.raw */, 1AC35CB618CED84500F37B72 /* effect1.wav */, 1AC35CC418CED84500F37B72 /* pew-pew-lei.wav */, + 29AFEF6619ACCAA000F6B10A /* Test.html */, ); name = Resources; sourceTree = ""; @@ -7829,6 +7832,7 @@ 1AC35D0918CED84500F37B72 /* TileMaps in Resources */, 1AC35CFF18CED84500F37B72 /* Particles in Resources */, 1AC35C8818CECF1400F37B72 /* Default-568h@2x.png in Resources */, + 29AFEF6719ACCAA000F6B10A /* Test.html in Resources */, 1AC35CD518CED84500F37B72 /* ccb in Resources */, 1AC35CE118CED84500F37B72 /* configs in Resources */, 1AC35CE918CED84500F37B72 /* extensions in Resources */, diff --git a/cocos/ui/WebView.h b/cocos/ui/WebView.h index e0977d2e85..37c67e3d91 100644 --- a/cocos/ui/WebView.h +++ b/cocos/ui/WebView.h @@ -167,9 +167,9 @@ private: WebViewImpl *_impl; }; - } // namespace cocos2d + } // namespace ui } // namespace experimental -}//namespace ui +}//namespace cocos2d #endif diff --git a/cocos/ui/WebViewImpl_iOS.h b/cocos/ui/WebViewImpl_iOS.h index fb371ed424..277ca59a58 100644 --- a/cocos/ui/WebViewImpl_iOS.h +++ b/cocos/ui/WebViewImpl_iOS.h @@ -84,8 +84,8 @@ private: WebView *_webView; }; - } // namespace cocos2d + } // namespace ui } // namespace experimental -}//namespace ui +}//namespace cocos2d #endif //__cocos2d_plugin_WebViewImpl_IOS_H_ diff --git a/cocos/ui/WebViewImpl_iOS.mm b/cocos/ui/WebViewImpl_iOS.mm index 22652b2e91..8ef646eef9 100644 --- a/cocos/ui/WebViewImpl_iOS.mm +++ b/cocos/ui/WebViewImpl_iOS.mm @@ -96,6 +96,7 @@ } - (void)dealloc { + self.uiWebView.delegate = nil; [self.uiWebView removeFromSuperview]; self.jsScheme = nil; [super dealloc]; @@ -343,8 +344,8 @@ void WebViewImpl::setVisible(bool visible){ [_uiWebViewWrapper setVisible:visible]; } - } // namespace cocos2d + } // namespace ui } // namespace experimental -} //namespace ui +} //namespace cocos2d #endif // CC_TARGET_PLATFORM == CC_PLATFORM_IOS diff --git a/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UIWebViewTest/UIWebViewTest.cpp b/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UIWebViewTest/UIWebViewTest.cpp index f7bec25f7d..a21c2afba4 100644 --- a/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UIWebViewTest/UIWebViewTest.cpp +++ b/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UIWebViewTest/UIWebViewTest.cpp @@ -37,14 +37,132 @@ WebViewTest::~WebViewTest() bool WebViewTest::init() { if (UIScene::init()) { + Size winSize = Director::getInstance()->getVisibleSize(); + + _webView = cocos2d::experimental::ui::WebView::create(); - _webView->setPosition(Director::getInstance()->getVisibleSize()/2); - _webView->setContentSize(Director::getInstance()->getVisibleSize() * 0.5); - _webView->loadUrl("http://www.baidu.com"); + _webView->setPosition(winSize/2); + _webView->setContentSize(winSize * 0.5); + _webView->loadUrl("http://www.google.com"); _webView->setScalesPageToFit(true); + + _webView->shouldStartLoading = CC_CALLBACK_2(WebViewTest::onWebViewShouldStartLoading, this); + _webView->didFinishLoading = CC_CALLBACK_2(WebViewTest::onWebViewDidFinishLoading, this); + _webView->didFailLoading = CC_CALLBACK_2(WebViewTest::onWebViewDidFailLoading, this); + this->addChild(_webView); + TextField *urlTextField = TextField::create("Input a URL here", "Arial", 20); + urlTextField->setPlaceHolderColor(Color3B::RED); + urlTextField->setPosition(Vec2(winSize/2) + Vec2(-80, _webView->getContentSize().height/2 + + urlTextField->getContentSize().height/2 + 10)); + this->addChild(urlTextField); + + Text *httpLabel = Text::create("http:// ", "Arial", 20); + httpLabel->setTextColor(Color4B::GREEN); + httpLabel->setAnchorPoint(Vec2(1.0,0.5)); + httpLabel->setPosition(urlTextField->getPosition() - Vec2(urlTextField->getContentSize().width/2,0)); + this->addChild(httpLabel); + + + Button *resetBtn = Button::create("cocosui/animationbuttonnormal.png", + "cocosui/animationbuttonpressed.png"); + resetBtn->setTitleText("Visit URL"); + resetBtn->setPosition(Vec2(winSize/2) + Vec2(50, _webView->getContentSize().height/2 + + resetBtn->getContentSize().height/2 + 10)); + resetBtn->addClickEventListener([=](Ref*){ + _webView->loadUrl(std::string("http://") + urlTextField->getStringValue()); + }); + this->addChild(resetBtn); + + + Button *reloadBtn = Button::create("cocosui/animationbuttonnormal.png", + "cocosui/animationbuttonpressed.png"); + reloadBtn->setTitleText("Reload"); + reloadBtn->setPosition(Vec2(winSize/2) + Vec2( _webView->getContentSize().width/2 + + reloadBtn->getContentSize().width/2 + 10,50 )); + reloadBtn->addClickEventListener([=](Ref*){ + _webView->reload(); + }); + this->addChild(reloadBtn); + + Button *forwardBtn = Button::create("cocosui/animationbuttonnormal.png", + "cocosui/animationbuttonpressed.png"); + forwardBtn->setTitleText("Forward"); + forwardBtn->setPosition(Vec2(winSize/2) + Vec2( _webView->getContentSize().width/2 + + forwardBtn->getContentSize().width/2 + 10,0 )); + forwardBtn->addClickEventListener([=](Ref*){ + _webView->goForward(); + }); + this->addChild(forwardBtn); + + + + Button *backBtn = Button::create("cocosui/animationbuttonnormal.png", + "cocosui/animationbuttonpressed.png"); + backBtn->setTitleText("Back"); + backBtn->setPosition(Vec2(winSize/2) + Vec2( _webView->getContentSize().width/2 + + backBtn->getContentSize().width/2 + 10,-50 )); + backBtn->addClickEventListener([=](Ref*){ + _webView->goBack(); + }); + this->addChild(backBtn); + + + Button *loadFileBtn = Button::create("cocosui/animationbuttonnormal.png", + "cocosui/animationbuttonpressed.png"); + loadFileBtn->setTitleText("Load FILE"); + loadFileBtn->setPosition(Vec2(winSize/2) - Vec2( _webView->getContentSize().width/2 + + loadFileBtn->getContentSize().width/2 + 10,50 )); + loadFileBtn->addClickEventListener([=](Ref*){ + _webView->loadFile("Test.html"); + }); + this->addChild(loadFileBtn); + + Button *loadHTMLBtn = Button::create("cocosui/animationbuttonnormal.png", + "cocosui/animationbuttonpressed.png"); + loadHTMLBtn->setTitleText("Load HTML"); + loadHTMLBtn->setPosition(Vec2(winSize/2) - Vec2( _webView->getContentSize().width/2 + + loadHTMLBtn->getContentSize().width/2 + 10,0 )); + loadHTMLBtn->addClickEventListener([=](Ref*){ + _webView->loadHTMLString("Hello World",""); + }); + this->addChild(loadHTMLBtn); + + + + + Button *evalJsBtn = Button::create("cocosui/animationbuttonnormal.png", + "cocosui/animationbuttonpressed.png"); + evalJsBtn->setTitleText("Evaluate JS"); + evalJsBtn->setPosition(Vec2(winSize/2) - Vec2( _webView->getContentSize().width/2 + + evalJsBtn->getContentSize().width/2 + 10,-50 )); + evalJsBtn->addClickEventListener([=](Ref*){ + _webView->evaluateJS("alert(\"hello\")"); + }); + this->addChild(evalJsBtn); + return true; } return false; } + +bool WebViewTest::onWebViewShouldStartLoading(experimental::ui::WebView *sender, std::string url) +{ + CCLOG("onWebViewShouldStartLoading, url is %s", url.c_str()); + + return true; +} + +void WebViewTest::onWebViewDidFinishLoading(experimental::ui::WebView *sender, std::string url) +{ + CCLOG("onWebViewDidFinishLoading, url is %s", url.c_str()); + +} + +void WebViewTest::onWebViewDidFailLoading(experimental::ui::WebView *sender, std::string url) +{ + CCLOG("onWebViewDidFailLoading, url is %s", url.c_str()); + +} + diff --git a/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UIWebViewTest/UIWebViewTest.h b/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UIWebViewTest/UIWebViewTest.h index 865ed9f02c..a7ae59e075 100644 --- a/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UIWebViewTest/UIWebViewTest.h +++ b/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UIWebViewTest/UIWebViewTest.h @@ -37,8 +37,10 @@ public: UI_SCENE_CREATE_FUNC(WebViewTest); virtual bool init(); + bool onWebViewShouldStartLoading(experimental::ui::WebView *sender, std::string url); + void onWebViewDidFinishLoading(experimental::ui::WebView *sender, std::string url); + void onWebViewDidFailLoading(experimental::ui::WebView *sender, std::string url); - private: cocos2d::experimental::ui::WebView *_webView; diff --git a/tests/cpp-tests/Resources/Test.html b/tests/cpp-tests/Resources/Test.html new file mode 100644 index 0000000000..1e90fb526f --- /dev/null +++ b/tests/cpp-tests/Resources/Test.html @@ -0,0 +1,5 @@ + + + Hello World + + \ No newline at end of file