Commit 92b8ceff authored by Alexey Baskakov's avatar Alexey Baskakov Committed by Commit Bot

WebApp: Support iteration over all apps in WebAppRegistrar.

Expose Registry as read-only collection.

Users can write:
  for (auto& kv : registrar->registry()) {
    const WebApp* web_app = kv.second.get();
    ...
  }

Add IsEmpty and UnregisterAll methods.

Bug: 891172
Change-Id: I17ad8a5b569eeffb614adeec5575dc6804ce5251
Reviewed-on: https://chromium-review.googlesource.com/c/1295611
Commit-Queue: Alexey Baskakov <loyso@chromium.org>
Reviewed-by: default avatarBen Wells <benwells@chromium.org>
Cr-Commit-Position: refs/heads/master@{#601855}
parent 5490b51f
...@@ -37,4 +37,8 @@ WebApp* WebAppRegistrar::GetAppById(const AppId& app_id) { ...@@ -37,4 +37,8 @@ WebApp* WebAppRegistrar::GetAppById(const AppId& app_id) {
return kv == registry_.end() ? nullptr : kv->second.get(); return kv == registry_.end() ? nullptr : kv->second.get();
} }
void WebAppRegistrar::UnregisterAll() {
registry_.clear();
}
} // namespace web_app } // namespace web_app
...@@ -25,8 +25,16 @@ class WebAppRegistrar { ...@@ -25,8 +25,16 @@ class WebAppRegistrar {
WebApp* GetAppById(const AppId& app_id); WebApp* GetAppById(const AppId& app_id);
using Registry = std::map<AppId, std::unique_ptr<WebApp>>;
const Registry& registry() const { return registry_; }
bool is_empty() const { return registry_.empty(); }
// Clears registry.
void UnregisterAll();
private: private:
std::map<AppId, std::unique_ptr<WebApp>> registry_; Registry registry_;
DISALLOW_COPY_AND_ASSIGN(WebAppRegistrar); DISALLOW_COPY_AND_ASSIGN(WebAppRegistrar);
}; };
......
...@@ -4,6 +4,9 @@ ...@@ -4,6 +4,9 @@
#include "chrome/browser/web_applications/web_app_registrar.h" #include "chrome/browser/web_applications/web_app_registrar.h"
#include <set>
#include "base/strings/string_number_conversions.h"
#include "chrome/browser/web_applications/components/web_app_helpers.h" #include "chrome/browser/web_applications/components/web_app_helpers.h"
#include "chrome/browser/web_applications/web_app.h" #include "chrome/browser/web_applications/web_app.h"
#include "testing/gtest/include/gtest/gtest.h" #include "testing/gtest/include/gtest/gtest.h"
...@@ -11,6 +14,27 @@ ...@@ -11,6 +14,27 @@
namespace web_app { namespace web_app {
namespace {
std::set<AppId> RegisterAppsForTesting(const std::string& base_url,
int num_apps,
WebAppRegistrar* registrar) {
std::set<AppId> ids;
for (int i = 0; i < num_apps; ++i) {
const auto url = base_url + base::IntToString(i);
const AppId app_id = GenerateAppIdFromURL(GURL(url));
auto web_app = std::make_unique<WebApp>(app_id);
registrar->RegisterApp(std::move(web_app));
ids.insert(app_id);
}
return ids;
}
} // namespace
TEST(WebAppRegistrar, CreateRegisterUnregister) { TEST(WebAppRegistrar, CreateRegisterUnregister) {
auto registrar = std::make_unique<WebAppRegistrar>(); auto registrar = std::make_unique<WebAppRegistrar>();
EXPECT_EQ(nullptr, registrar->GetAppById(AppId())); EXPECT_EQ(nullptr, registrar->GetAppById(AppId()));
...@@ -32,6 +56,7 @@ TEST(WebAppRegistrar, CreateRegisterUnregister) { ...@@ -32,6 +56,7 @@ TEST(WebAppRegistrar, CreateRegisterUnregister) {
EXPECT_EQ(nullptr, registrar->GetAppById(app_id)); EXPECT_EQ(nullptr, registrar->GetAppById(app_id));
EXPECT_EQ(nullptr, registrar->GetAppById(app_id2)); EXPECT_EQ(nullptr, registrar->GetAppById(app_id2));
EXPECT_TRUE(registrar->is_empty());
registrar->RegisterApp(std::move(web_app)); registrar->RegisterApp(std::move(web_app));
WebApp* app = registrar->GetAppById(app_id); WebApp* app = registrar->GetAppById(app_id);
...@@ -42,13 +67,16 @@ TEST(WebAppRegistrar, CreateRegisterUnregister) { ...@@ -42,13 +67,16 @@ TEST(WebAppRegistrar, CreateRegisterUnregister) {
EXPECT_EQ(launch_url.spec(), app->launch_url()); EXPECT_EQ(launch_url.spec(), app->launch_url());
EXPECT_EQ(nullptr, registrar->GetAppById(app_id2)); EXPECT_EQ(nullptr, registrar->GetAppById(app_id2));
EXPECT_FALSE(registrar->is_empty());
registrar->RegisterApp(std::move(web_app2)); registrar->RegisterApp(std::move(web_app2));
WebApp* app2 = registrar->GetAppById(app_id2); WebApp* app2 = registrar->GetAppById(app_id2);
EXPECT_EQ(app_id2, app2->app_id()); EXPECT_EQ(app_id2, app2->app_id());
EXPECT_FALSE(registrar->is_empty());
registrar->UnregisterApp(app_id); registrar->UnregisterApp(app_id);
EXPECT_EQ(nullptr, registrar->GetAppById(app_id)); EXPECT_EQ(nullptr, registrar->GetAppById(app_id));
EXPECT_FALSE(registrar->is_empty());
// Check that app2 is still registered. // Check that app2 is still registered.
app2 = registrar->GetAppById(app_id2); app2 = registrar->GetAppById(app_id2);
...@@ -56,6 +84,7 @@ TEST(WebAppRegistrar, CreateRegisterUnregister) { ...@@ -56,6 +84,7 @@ TEST(WebAppRegistrar, CreateRegisterUnregister) {
registrar->UnregisterApp(app_id2); registrar->UnregisterApp(app_id2);
EXPECT_EQ(nullptr, registrar->GetAppById(app_id2)); EXPECT_EQ(nullptr, registrar->GetAppById(app_id2));
EXPECT_TRUE(registrar->is_empty());
} }
TEST(WebAppRegistrar, DestroyRegistrarOwningRegisteredApps) { TEST(WebAppRegistrar, DestroyRegistrarOwningRegisteredApps) {
...@@ -73,4 +102,23 @@ TEST(WebAppRegistrar, DestroyRegistrarOwningRegisteredApps) { ...@@ -73,4 +102,23 @@ TEST(WebAppRegistrar, DestroyRegistrarOwningRegisteredApps) {
registrar.reset(); registrar.reset();
} }
TEST(WebAppRegistrar, ForEachAndUnregisterAll) {
auto registrar = std::make_unique<WebAppRegistrar>();
auto ids =
RegisterAppsForTesting("https://example.com/path", 100, registrar.get());
EXPECT_EQ(100UL, ids.size());
for (auto& kv : registrar->registry()) {
const WebApp* web_app = kv.second.get();
const size_t num_removed = ids.erase(web_app->app_id());
EXPECT_EQ(1U, num_removed);
}
EXPECT_TRUE(ids.empty());
EXPECT_FALSE(registrar->is_empty());
registrar->UnregisterAll();
EXPECT_TRUE(registrar->is_empty());
}
} // namespace web_app } // namespace web_app
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment