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

Unified Diff: components/resource_provider/resource_provider_apptest.cc

Issue 1108403008: Adds resource_provider::ResourceProvider (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 8 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
Index: components/resource_provider/resource_provider_apptest.cc
diff --git a/components/resource_provider/resource_provider_apptest.cc b/components/resource_provider/resource_provider_apptest.cc
new file mode 100644
index 0000000000000000000000000000000000000000..802707331a917c7a08cc38bcabaf829f8fa09e36
--- /dev/null
+++ b/components/resource_provider/resource_provider_apptest.cc
@@ -0,0 +1,120 @@
+// Copyright 2015 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include <stdint.h>
+
+#include "base/bind.h"
+#include "base/containers/scoped_ptr_hash_map.h"
+#include "base/run_loop.h"
+#include "components/resource_provider/public/interfaces/resource_provider.mojom.h"
+#include "mojo/application/application_test_base_chromium.h"
+#include "mojo/common/common_type_converters.h"
+#include "third_party/mojo/src/mojo/public/cpp/application/application_delegate.h"
+#include "third_party/mojo/src/mojo/public/cpp/application/application_impl.h"
+#include "third_party/mojo/src/mojo/public/cpp/application/service_provider_impl.h"
+#include "third_party/mojo/src/mojo/public/cpp/bindings/array.h"
+#include "third_party/mojo/src/mojo/public/cpp/system/macros.h"
+
+namespace resource_provider {
+namespace {
+
+// ResourceFetcher fetches resources from a ResourceProvider, storing the
+// results as well as running
+// a message loop until the resource has been obtained.
+class ResourceFetcher {
+ public:
+ using ResourceMap =
+ base::ScopedPtrHashMap<std::string, scoped_ptr<mojo::Array<uint8_t>>>;
+
+ explicit ResourceFetcher(ResourceProvider* provider)
+ : provider_(provider), waiting_count_(0u) {}
+ ~ResourceFetcher() {}
+
+ ResourceMap* resources() { return &resources_; }
+
+ void WaitForResults() {
+ ASSERT_NE(0u, waiting_count_);
+ run_loop_.Run();
+ }
+
+ void GetResource(const std::string& path) {
+ waiting_count_++;
+ provider_->GetResource(path, base::Bind(&ResourceFetcher::OnGotResource,
+ base::Unretained(this), path));
+ }
+
+ private:
+ // Callback when a resource has been fetched.
+ void OnGotResource(const std::string& path, mojo::Array<uint8_t> data) {
+ scoped_ptr<mojo::Array<uint8_t>> resource;
+ EXPECT_FALSE(resources_.contains(path));
+ if (!data.is_null())
+ resource.reset(new mojo::Array<uint8_t>(data.Pass()));
+ resources_.add(path, resource.Pass());
+
+ if (waiting_count_ > 0) {
+ waiting_count_--;
+ if (waiting_count_ == 0)
+ run_loop_.Quit();
+ }
+ }
+
+ ResourceProvider* provider_;
+ ResourceMap resources_;
+ // Number of resources we're waiting on.
+ size_t waiting_count_;
+ base::RunLoop run_loop_;
+
+ DISALLOW_COPY_AND_ASSIGN(ResourceFetcher);
+};
+
+class ResourceProviderApplicationTest : public mojo::test::ApplicationTestBase {
+ public:
+ ResourceProviderApplicationTest() {}
+ ~ResourceProviderApplicationTest() override {}
+
+ protected:
+ // ApplicationTestBase:
+ void SetUp() override {
+ ApplicationTestBase::SetUp();
+ application_impl()->ConnectToService("mojo:resource_provider",
+ &resource_provider_);
+ }
+
+ ResourceProviderPtr resource_provider_;
+
+ private:
+ MOJO_DISALLOW_COPY_AND_ASSIGN(ResourceProviderApplicationTest);
+};
+
+TEST_F(ResourceProviderApplicationTest, FetchOneResource) {
+ ResourceFetcher fetcher(resource_provider_.get());
+ fetcher.GetResource("sample");
+ fetcher.WaitForResults();
+ ASSERT_TRUE(fetcher.resources()->contains("sample"));
+ ASSERT_FALSE(fetcher.resources()->get("sample")->is_null());
+ EXPECT_EQ("test data\n",
+ fetcher.resources()->get("sample")->To<std::string>());
+}
+
+TEST_F(ResourceProviderApplicationTest, FetchTwoResources) {
+ ResourceFetcher fetcher(resource_provider_.get());
+ fetcher.GetResource("sample");
+ fetcher.GetResource("dir/sample2");
+ fetcher.WaitForResults();
+ ASSERT_TRUE(fetcher.resources()->contains("sample"));
+ ASSERT_TRUE(fetcher.resources()->get("sample"));
+ ASSERT_FALSE(fetcher.resources()->get("sample")->is_null());
+ EXPECT_EQ("test data\n",
+ fetcher.resources()->get("sample")->To<std::string>());
+
+ ASSERT_TRUE(fetcher.resources()->contains("dir/sample2"));
+ ASSERT_TRUE(fetcher.resources()->get("dir/sample2"));
+ ASSERT_FALSE(fetcher.resources()->get("dir/sample2")->is_null());
+ EXPECT_EQ("xxyy\n",
+ fetcher.resources()->get("dir/sample2")->To<std::string>());
+}
+
+} // namespace
+} // namespace resource_provider

Powered by Google App Engine
This is Rietveld 408576698