| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 // Note that although this is not a "browser" test, it runs as part of | 5 // Note that although this is not a "browser" test, it runs as part of |
| 6 // browser_tests. This is because WebKit does not work properly if it is | 6 // browser_tests. This is because WebKit does not work properly if it is |
| 7 // shutdown and re-initialized. Since browser_tests runs each test in a | 7 // shutdown and re-initialized. Since browser_tests runs each test in a |
| 8 // new process, this avoids the problem. | 8 // new process, this avoids the problem. |
| 9 | 9 |
| 10 #include "chrome/renderer/safe_browsing/phishing_dom_feature_extractor.h" | 10 #include "chrome/renderer/safe_browsing/phishing_dom_feature_extractor.h" |
| (...skipping 26 matching lines...) Expand all Loading... |
| 37 #include "testing/gmock/include/gmock/gmock.h" | 37 #include "testing/gmock/include/gmock/gmock.h" |
| 38 #include "third_party/WebKit/public/platform/WebString.h" | 38 #include "third_party/WebKit/public/platform/WebString.h" |
| 39 #include "third_party/WebKit/public/web/WebFrame.h" | 39 #include "third_party/WebKit/public/web/WebFrame.h" |
| 40 #include "third_party/WebKit/public/web/WebScriptSource.h" | 40 #include "third_party/WebKit/public/web/WebScriptSource.h" |
| 41 #include "third_party/WebKit/public/web/WebView.h" | 41 #include "third_party/WebKit/public/web/WebView.h" |
| 42 | 42 |
| 43 using ::testing::DoAll; | 43 using ::testing::DoAll; |
| 44 using ::testing::Invoke; | 44 using ::testing::Invoke; |
| 45 using ::testing::Return; | 45 using ::testing::Return; |
| 46 | 46 |
| 47 namespace { |
| 48 |
| 49 // The first RenderFrame is routing ID 1, and the first RenderView is 2. |
| 50 const int kRenderViewRoutingId = 2; |
| 51 |
| 52 } |
| 53 |
| 47 namespace safe_browsing { | 54 namespace safe_browsing { |
| 48 | 55 |
| 49 class PhishingDOMFeatureExtractorTest : public InProcessBrowserTest { | 56 class PhishingDOMFeatureExtractorTest : public InProcessBrowserTest { |
| 50 public: | 57 public: |
| 51 content::WebContents* GetWebContents() { | 58 content::WebContents* GetWebContents() { |
| 52 return browser()->tab_strip_model()->GetActiveWebContents(); | 59 return browser()->tab_strip_model()->GetActiveWebContents(); |
| 53 } | 60 } |
| 54 | 61 |
| 55 // Helper for the SubframeRemoval test that posts a message to remove | 62 // Helper for the SubframeRemoval test that posts a message to remove |
| 56 // the iframe "frame1" from the document. | 63 // the iframe "frame1" from the document. |
| (...skipping 12 matching lines...) Expand all Loading... |
| 69 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE { | 76 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE { |
| 70 command_line->AppendSwitch(switches::kSingleProcess); | 77 command_line->AppendSwitch(switches::kSingleProcess); |
| 71 #if defined(OS_WIN) && defined(USE_AURA) | 78 #if defined(OS_WIN) && defined(USE_AURA) |
| 72 // Don't want to try to create a GPU process. | 79 // Don't want to try to create a GPU process. |
| 73 command_line->AppendSwitch(switches::kDisableAcceleratedCompositing); | 80 command_line->AppendSwitch(switches::kDisableAcceleratedCompositing); |
| 74 #endif | 81 #endif |
| 75 } | 82 } |
| 76 | 83 |
| 77 virtual void SetUpOnMainThread() OVERRIDE { | 84 virtual void SetUpOnMainThread() OVERRIDE { |
| 78 extractor_.reset(new PhishingDOMFeatureExtractor( | 85 extractor_.reset(new PhishingDOMFeatureExtractor( |
| 79 content::RenderView::FromRoutingID(1), &clock_)); | 86 content::RenderView::FromRoutingID(kRenderViewRoutingId), &clock_)); |
| 80 | 87 |
| 81 ASSERT_TRUE(StartTestServer()); | 88 ASSERT_TRUE(StartTestServer()); |
| 82 host_resolver()->AddRule("*", "127.0.0.1"); | 89 host_resolver()->AddRule("*", "127.0.0.1"); |
| 83 } | 90 } |
| 84 | 91 |
| 85 // Runs the DOMFeatureExtractor on the RenderView, waiting for the | 92 // Runs the DOMFeatureExtractor on the RenderView, waiting for the |
| 86 // completion callback. Returns the success boolean from the callback. | 93 // completion callback. Returns the success boolean from the callback. |
| 87 bool ExtractFeatures(FeatureMap* features) { | 94 bool ExtractFeatures(FeatureMap* features) { |
| 88 success_ = false; | 95 success_ = false; |
| 89 PostTaskToInProcessRendererAndWait( | 96 PostTaskToInProcessRendererAndWait( |
| (...skipping 16 matching lines...) Expand all Loading... |
| 106 | 113 |
| 107 // Completion callback for feature extraction. | 114 // Completion callback for feature extraction. |
| 108 void ExtractionDone(const base::Closure& quit_closure, | 115 void ExtractionDone(const base::Closure& quit_closure, |
| 109 bool success) { | 116 bool success) { |
| 110 success_ = success; | 117 success_ = success; |
| 111 quit_closure.Run(); | 118 quit_closure.Run(); |
| 112 } | 119 } |
| 113 | 120 |
| 114 // Does the actual work of removing the iframe "frame1" from the document. | 121 // Does the actual work of removing the iframe "frame1" from the document. |
| 115 void RemoveIframe() { | 122 void RemoveIframe() { |
| 116 blink::WebFrame* main_frame = | 123 content::RenderView* render_view = |
| 117 content::RenderView::FromRoutingID(1)->GetWebView()->mainFrame(); | 124 content::RenderView::FromRoutingID(kRenderViewRoutingId); |
| 125 blink::WebFrame* main_frame = render_view->GetWebView()->mainFrame(); |
| 118 ASSERT_TRUE(main_frame); | 126 ASSERT_TRUE(main_frame); |
| 119 main_frame->executeScript( | 127 main_frame->executeScript( |
| 120 blink::WebString( | 128 blink::WebString( |
| 121 "document.body.removeChild(document.getElementById('frame1'));")); | 129 "document.body.removeChild(document.getElementById('frame1'));")); |
| 122 } | 130 } |
| 123 | 131 |
| 124 bool StartTestServer() { | 132 bool StartTestServer() { |
| 125 CHECK(!embedded_test_server_); | 133 CHECK(!embedded_test_server_); |
| 126 embedded_test_server_.reset(new net::test_server::EmbeddedTestServer()); | 134 embedded_test_server_.reset(new net::test_server::EmbeddedTestServer()); |
| 127 embedded_test_server_->RegisterRequestHandler( | 135 embedded_test_server_->RegisterRequestHandler( |
| (...skipping 376 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 504 LoadHtml( | 512 LoadHtml( |
| 505 "host.com", | 513 "host.com", |
| 506 "<html><head></head><body>" | 514 "<html><head></head><body>" |
| 507 "<iframe src=\"frame.html\" id=\"frame1\"></iframe>" | 515 "<iframe src=\"frame.html\" id=\"frame1\"></iframe>" |
| 508 "<form></form></body></html>"); | 516 "<form></form></body></html>"); |
| 509 ASSERT_TRUE(ExtractFeatures(&features)); | 517 ASSERT_TRUE(ExtractFeatures(&features)); |
| 510 ExpectFeatureMapsAreEqual(features, expected_features); | 518 ExpectFeatureMapsAreEqual(features, expected_features); |
| 511 } | 519 } |
| 512 | 520 |
| 513 } // namespace safe_browsing | 521 } // namespace safe_browsing |
| OLD | NEW |