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

Unified Diff: mojo/shell/service_connector_unittest.cc

Issue 137623017: Cleanup Service<> and ServiceFactory<> when clients go away. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Address review nits Created 6 years, 10 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « mojo/shell/service_connector.cc ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: mojo/shell/service_connector_unittest.cc
diff --git a/mojo/shell/service_connector_unittest.cc b/mojo/shell/service_connector_unittest.cc
index 6ec7f07a3c88f6cf52ea31cab2b5e3d8722a2102..2af4f10d2d8a1e03f97db204c0eac26b317e389a 100644
--- a/mojo/shell/service_connector_unittest.cc
+++ b/mojo/shell/service_connector_unittest.cc
@@ -2,10 +2,11 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
-#include "base/message_loop/message_loop.h"
#include "mojo/public/bindings/allocation_scope.h"
#include "mojo/public/bindings/remote_ptr.h"
+#include "mojo/public/environment/environment.h"
#include "mojo/public/shell/service.h"
+#include "mojo/public/utility/run_loop.h"
#include "mojo/shell/service_connector.h"
#include "mojom/shell.h"
#include "mojom/test.h"
@@ -15,16 +16,34 @@ namespace mojo {
namespace shell {
namespace {
-struct Context {
+const char kTestURLString[] = "test:testService";
+
+struct TestContext {
+ TestContext() : num_impls(0) {}
std::string last_test_string;
+ int num_impls;
};
-class TestServiceImpl : public Service<TestService, TestServiceImpl, Context> {
+class TestServiceImpl :
+ public Service<TestService, TestServiceImpl, TestContext> {
public:
+ TestServiceImpl() {}
+
+ virtual ~TestServiceImpl() {
+ --context()->num_impls;
+ }
+
virtual void Test(const mojo::String& test_string) OVERRIDE {
context()->last_test_string = test_string.To<std::string>();
client()->AckTest();
}
+
+ void Initialize(ServiceFactory<TestServiceImpl, TestContext>* service_factory,
+ ScopedMessagePipeHandle client_handle) {
+ Service<TestService, TestServiceImpl, TestContext>::Initialize(
+ service_factory, client_handle.Pass());
+ ++context()->num_impls;
+ }
};
class TestClientImpl : public TestClient {
@@ -33,32 +52,36 @@ class TestClientImpl : public TestClient {
: service_(service_handle.Pass(), this),
quit_after_ack_(false) {
}
- virtual ~TestClientImpl() {
- }
+
+ virtual ~TestClientImpl() {}
+
virtual void AckTest() OVERRIDE {
if (quit_after_ack_)
- base::MessageLoop::current()->QuitNow();
+ mojo::RunLoop::current()->Quit();
}
+
void Test(std::string test_string) {
AllocationScope scope;
quit_after_ack_ = true;
service_->Test(mojo::String(test_string));
}
+
+ private:
RemotePtr<TestService> service_;
bool quit_after_ack_;
+ DISALLOW_COPY_AND_ASSIGN(TestClientImpl);
};
+} // namespace
class ServiceConnectorTest : public testing::Test,
public ServiceConnector::Loader {
public:
- ServiceConnectorTest() {
- }
+ ServiceConnectorTest() {}
- virtual ~ServiceConnectorTest() {
- }
+ virtual ~ServiceConnectorTest() {}
virtual void SetUp() OVERRIDE {
- GURL test_url("test:testService");
+ GURL test_url(kTestURLString);
service_connector_.reset(new ServiceConnector);
service_connector_->SetLoaderForURL(this, test_url);
MessagePipe pipe;
@@ -74,14 +97,20 @@ class ServiceConnectorTest : public testing::Test,
virtual void Load(const GURL& url,
ScopedMessagePipeHandle shell_handle) OVERRIDE {
- test_app_.reset(new ServiceFactory<TestServiceImpl, Context>(
+ test_app_.reset(new ServiceFactory<TestServiceImpl, TestContext>(
shell_handle.Pass(), &context_));
}
+ bool HasFactoryForTestURL() {
+ ServiceConnector::TestAPI connector_test_api(service_connector_.get());
+ return connector_test_api.HasFactoryForURL(GURL(kTestURLString));
+ }
+
protected:
- base::MessageLoop loop_;
- Context context_;
- scoped_ptr<ServiceFactory<TestServiceImpl, Context> > test_app_;
+ mojo::Environment env_;
+ mojo::RunLoop loop_;
+ TestContext context_;
+ scoped_ptr<ServiceFactory<TestServiceImpl, TestContext> > test_app_;
scoped_ptr<TestClientImpl> test_client_;
scoped_ptr<ServiceConnector> service_connector_;
DISALLOW_COPY_AND_ASSIGN(ServiceConnectorTest);
@@ -93,6 +122,15 @@ TEST_F(ServiceConnectorTest, Basic) {
EXPECT_EQ(std::string("test"), context_.last_test_string);
}
-} // namespace
+TEST_F(ServiceConnectorTest, ClientError) {
+ test_client_->Test("test");
+ EXPECT_TRUE(HasFactoryForTestURL());
+ loop_.Run();
+ EXPECT_EQ(1, context_.num_impls);
+ test_client_.reset(NULL);
+ loop_.Run();
+ EXPECT_EQ(0, context_.num_impls);
+ EXPECT_FALSE(HasFactoryForTestURL());
+}
} // namespace shell
} // namespace mojo
« no previous file with comments | « mojo/shell/service_connector.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698