Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(309)

Side by Side Diff: chrome/renderer/chrome_render_frame_observer_browsertest.cc

Issue 2143383002: [Translate] Migrate IPCs to Mojo interfaces. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix trybots Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 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 #include "chrome/renderer/chrome_render_frame_observer.h" 5 #include "chrome/renderer/chrome_render_frame_observer.h"
6 6
7 #include <tuple> 7 #include <tuple>
8 8
9 #include "base/test/histogram_tester.h" 9 #include "base/test/histogram_tester.h"
10 #include "chrome/test/base/chrome_render_view_test.h" 10 #include "chrome/test/base/chrome_render_view_test.h"
11 #include "components/translate/content/common/translate_messages.h" 11 #include "components/translate/content/common/translate.mojom.h"
12 #include "components/translate/content/renderer/translate_helper.h" 12 #include "components/translate/content/renderer/translate_helper.h"
13 #include "components/translate/core/common/translate_constants.h" 13 #include "components/translate/core/common/translate_constants.h"
14 #include "content/public/renderer/render_frame.h" 14 #include "content/public/renderer/render_frame.h"
15 #include "content/public/renderer/render_view.h" 15 #include "content/public/renderer/render_view.h"
16 #include "mojo/public/cpp/bindings/binding_set.h"
17 #include "services/shell/public/cpp/interface_provider.h"
16 #include "third_party/WebKit/public/web/WebView.h" 18 #include "third_party/WebKit/public/web/WebView.h"
17 19
20 namespace {
21
22 class FakeContentTranslateDriver
23 : public translate::mojom::ContentTranslateDriver {
24 public:
25 FakeContentTranslateDriver()
26 : called_new_page_(false), page_needs_translation_(false) {}
27 ~FakeContentTranslateDriver() override {}
28
29 void BindHandle(mojo::ScopedMessagePipeHandle handle) {
30 bindings_.AddBinding(
31 this, mojo::MakeRequest<translate::mojom::ContentTranslateDriver>(
32 std::move(handle)));
33 }
34
35 // translate::mojom::ContentTranslateDriver implementation.
36 void RegisterPage(translate::mojom::PagePtr page,
37 const translate::LanguageDetectionDetails& details,
38 bool page_needs_translation) override {
39 called_new_page_ = true;
40 page_needs_translation_ = page_needs_translation;
41 }
42
43 bool called_new_page_;
44 bool page_needs_translation_;
45
46 private:
47 mojo::BindingSet<translate::mojom::ContentTranslateDriver> bindings_;
48 };
49
50 } // namespace
51
18 // Constants for UMA statistic collection. 52 // Constants for UMA statistic collection.
19 static const char kTranslateCaptureText[] = "Translate.CaptureText"; 53 static const char kTranslateCaptureText[] = "Translate.CaptureText";
20 54
21 class ChromeRenderFrameObserverTest : public ChromeRenderViewTest {}; 55 class ChromeRenderFrameObserverTest : public ChromeRenderViewTest {
56 protected:
57 void SetUp() override {
58 ChromeRenderViewTest::SetUp();
59
60 shell::InterfaceProvider* remote_interfaces =
61 view_->GetMainRenderFrame()->GetRemoteInterfaces();
62 shell::InterfaceProvider::TestApi test_api(remote_interfaces);
63 test_api.SetBinderForName(
64 translate::mojom::ContentTranslateDriver::Name_,
65 base::Bind(&FakeContentTranslateDriver::BindHandle,
66 base::Unretained(&fake_translate_driver_)));
67 }
68
69 FakeContentTranslateDriver fake_translate_driver_;
70 };
22 71
23 TEST_F(ChromeRenderFrameObserverTest, SkipCapturingSubFrames) { 72 TEST_F(ChromeRenderFrameObserverTest, SkipCapturingSubFrames) {
24 base::HistogramTester histogram_tester; 73 base::HistogramTester histogram_tester;
25 LoadHTML( 74 LoadHTML(
26 "<!DOCTYPE html><body>" 75 "<!DOCTYPE html><body>"
27 "This is a main document" 76 "This is a main document"
28 "<iframe srcdoc=\"This a document in an iframe.\">" 77 "<iframe srcdoc=\"This a document in an iframe.\">"
29 "</body>"); 78 "</body>");
30 view_->GetWebView()->updateAllLifecyclePhases(); 79 view_->GetWebView()->updateAllLifecyclePhases();
31 const IPC::Message* message = render_thread_->sink().GetUniqueMessageMatching( 80
32 ChromeFrameHostMsg_TranslateLanguageDetermined::ID); 81 base::RunLoop().RunUntilIdle();
33 ASSERT_NE(static_cast<IPC::Message*>(NULL), message); 82 ASSERT_TRUE(fake_translate_driver_.called_new_page_);
34 ChromeFrameHostMsg_TranslateLanguageDetermined::Param params; 83 EXPECT_TRUE(fake_translate_driver_.page_needs_translation_)
35 ChromeFrameHostMsg_TranslateLanguageDetermined::Read(message, &params); 84 << "Page should be translatable.";
36 EXPECT_TRUE(std::get<1>(params)) << "Page should be translatable.";
37 // Should have 2 samples: one for preliminary capture, one for final capture. 85 // Should have 2 samples: one for preliminary capture, one for final capture.
38 // If there are more, then subframes are being captured more than once. 86 // If there are more, then subframes are being captured more than once.
39 histogram_tester.ExpectTotalCount(kTranslateCaptureText, 2); 87 histogram_tester.ExpectTotalCount(kTranslateCaptureText, 2);
40 } 88 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698