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

Side by Side Diff: content/browser/utility_process_mojo_client_browsertest.cc

Issue 2049303002: Add the UtilityProcessMojoClient class and convert SafeJsonParser (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 6 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
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "content/public/browser/utility_process_mojo_client.h"
6
7 #include <memory>
8
9 #include "base/bind.h"
10 #include "base/run_loop.h"
11 #include "content/public/browser/browser_thread.h"
12 #include "content/public/test/content_browser_test.h"
13 #include "content/public/test/test_mojo_service.mojom.h"
14
15 namespace content {
16
17 enum class TestCase {
18 // Value of result_test_case_ before assignment.
19 NONE,
20 // Successful call through the Mojo service with response back.
21 CALL_SERVICE,
22 // Call the Mojo service but the utility process terminates before getting
23 // the result back.
24 CONNECTION_ERROR,
25 // Call a function that fails because the sandbox is still enabled.
26 SANDBOX_FAILURE,
27 // Call a function that succeeds only when the sandbox is disabled.
28 SANDBOX_SUCCESS,
29 };
30
31 // Test fixture used to make different Mojo calls to the utility process.
32 // Depending on the test parameter, a different function on the Mojo service
33 // will be invoked.
34 class UtilityProcessMojoClientBrowserTest
35 : public ContentBrowserTest,
36 public ::testing::WithParamInterface<TestCase> {
37 public:
38 void StartTestOnIOThread() {
Anand Mistry (off Chromium) 2016/06/10 11:13:54 Part of the point of the helper is that it doesn't
Patrick Monette 2016/06/10 18:13:23 Done. It even simplified the test.
39 mojo_client_.reset(new UtilityProcessMojoClient<mojom::TestMojoService>(
40 L"TestMojoProcess"));
41 mojo_client_->set_error_handler(
42 base::Bind(&UtilityProcessMojoClientBrowserTest::EndTestOnIoThread,
43 base::Unretained(this), TestCase::CONNECTION_ERROR));
44
45 TestCase test_case = GetParam();
46 // This test case needs to have the sandbox disabled.
47 if (test_case == TestCase::SANDBOX_SUCCESS)
48 mojo_client_->set_disable_sandbox();
49 mojo_client_->Start();
50
51 switch (test_case) {
52 case TestCase::CALL_SERVICE:
53 mojo_client_->service()->DoSomething(
54 base::Bind(&UtilityProcessMojoClientBrowserTest::EndTestOnIoThread,
55 base::Unretained(this), TestCase::CALL_SERVICE));
56 break;
57 case TestCase::CONNECTION_ERROR:
58 mojo_client_->service()->DoTerminateProcess(
59 base::Bind(&UtilityProcessMojoClientBrowserTest::EndTestOnIoThread,
60 base::Unretained(this), TestCase::CALL_SERVICE));
61 break;
62 case TestCase::SANDBOX_FAILURE:
63 case TestCase::SANDBOX_SUCCESS:
64 mojo_client_->service()->CreateFolder(base::Bind(
65 &UtilityProcessMojoClientBrowserTest::OnCreateFolderFinished,
66 base::Unretained(this)));
67 break;
68 case TestCase::NONE:
69 FAIL();
70 break;
71 }
72 }
73
74 protected:
75 void EndTestOnIoThread(TestCase result_test_case) {
76 result_test_case_ = result_test_case;
77 mojo_client_.reset();
78 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, done_closure_);
79 }
80
81 void OnCreateFolderFinished(bool succeeded) {
82 EndTestOnIoThread(succeeded ? TestCase::SANDBOX_SUCCESS
83 : TestCase::SANDBOX_FAILURE);
84 }
85
86 std::unique_ptr<UtilityProcessMojoClient<mojom::TestMojoService>>
87 mojo_client_;
88 base::Closure done_closure_;
89
90 // The test result that is compared to the current test case.
91 TestCase result_test_case_ = TestCase::NONE;
92 };
93
94 IN_PROC_BROWSER_TEST_P(UtilityProcessMojoClientBrowserTest, UtilityProcess) {
95 base::RunLoop run_loop;
96 done_closure_ = run_loop.QuitClosure();
97 BrowserThread::PostTask(
98 BrowserThread::IO, FROM_HERE,
99 base::Bind(&UtilityProcessMojoClientBrowserTest::StartTestOnIOThread,
100 base::Unretained(this)));
101 run_loop.Run();
102 EXPECT_EQ(GetParam(), result_test_case_);
103 }
104
105 INSTANTIATE_TEST_CASE_P(TestCases,
106 UtilityProcessMojoClientBrowserTest,
107 ::testing::Values(TestCase::CALL_SERVICE,
108 TestCase::CONNECTION_ERROR,
109 TestCase::SANDBOX_FAILURE,
110 TestCase::SANDBOX_SUCCESS));
111
112 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698