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

Unified Diff: chrome/browser/extensions/api/dial/device_description_fetcher_unittest.cc

Issue 2583853004: [chrome.dial] Adds chrome.dial.fetchDeviceDecription API. (Closed)
Patch Set: Respond to imcheng@ comments Created 3 years, 12 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: chrome/browser/extensions/api/dial/device_description_fetcher_unittest.cc
diff --git a/chrome/browser/extensions/api/dial/device_description_fetcher_unittest.cc b/chrome/browser/extensions/api/dial/device_description_fetcher_unittest.cc
new file mode 100644
index 0000000000000000000000000000000000000000..7f6b17824d75d1cd877a60437d5856253429f80a
--- /dev/null
+++ b/chrome/browser/extensions/api/dial/device_description_fetcher_unittest.cc
@@ -0,0 +1,174 @@
+// Copyright (c) 2016 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 <memory>
+#include <string>
+
+#include "base/callback.h"
+#include "base/memory/ptr_util.h"
+#include "base/memory/ref_counted.h"
+#include "chrome/browser/extensions/api/dial/device_description_fetcher.h"
+#include "chrome/browser/extensions/api/dial/dial_device_data.h"
+#include "chrome/test/base/testing_profile.h"
+#include "content/public/test/test_browser_thread_bundle.h"
+#include "net/http/http_response_headers.h"
+#include "net/http/http_status_code.h"
+#include "net/url_request/test_url_fetcher_factory.h"
+#include "net/url_request/url_fetcher.h"
+#include "testing/gtest/include/gtest/gtest.h"
+#include "url/gurl.h"
+
+namespace extensions {
+
+class DeviceDescriptionFetcherTest : public testing::Test {
+ public:
+ DeviceDescriptionFetcherTest() : url_("http://127.0.0.1/description.xml") {}
+
+ void TearDown() override { EXPECT_TRUE(callback_was_run_); }
+
+ void ExpectSuccess(const GURL& expected_app_url,
+ const std::string& expected_description) {
+ success_cb_ = base::BindOnce(&DeviceDescriptionFetcherTest::AssertSuccess,
+ base::Unretained(this), expected_app_url,
+ expected_description);
+ }
+
+ void ExpectFailure(const std::string& expected_message) {
+ expect_success_ = false;
+ error_cb_ = base::BindOnce(&DeviceDescriptionFetcherTest::AssertFailure,
+ base::Unretained(this), expected_message);
+ }
+
+ net::TestURLFetcher* StartRequest() {
+ fetcher_ = base::MakeUnique<DeviceDescriptionFetcher>(
+ url_, &profile_, std::move(success_cb_), std::move(error_cb_));
+ fetcher_->Start();
+ return factory_.GetFetcherByID(DeviceDescriptionFetcher::kURLFetcherID);
+ }
+
+ protected:
+ const content::TestBrowserThreadBundle thread_bundle_;
+ TestingProfile profile_;
+ const net::TestURLFetcherFactory factory_;
+ const GURL url_;
+ base::OnceCallback<void(const DialDeviceDescription&)> success_cb_;
+ base::OnceCallback<void(const std::string&)> error_cb_;
+ std::unique_ptr<DeviceDescriptionFetcher> fetcher_;
+ bool callback_was_run_ = false;
+ bool expect_success_ = true;
+
+ private:
+ void AssertSuccess(const GURL& expected_app_url,
+ const std::string& expected_description,
+ const DialDeviceDescription& description) {
+ EXPECT_TRUE(expect_success_);
+ callback_was_run_ = true;
+ EXPECT_EQ(expected_app_url, description.app_url);
+ EXPECT_EQ(expected_description, description.device_description);
+ }
+
+ void AssertFailure(const std::string& expected_message,
+ const std::string& message) {
+ EXPECT_FALSE(expect_success_);
+ callback_was_run_ = true;
+ EXPECT_TRUE(message.find(expected_message) == 0);
+ }
+};
+
+TEST_F(DeviceDescriptionFetcherTest, TestFetchSuccessful) {
+ ExpectSuccess(GURL("http://127.0.0.1/apps"), "<xml>description</xml>");
+ net::TestURLFetcher* test_fetcher = StartRequest();
+
+ test_fetcher->set_response_code(net::HTTP_OK);
+ scoped_refptr<net::HttpResponseHeaders> headers =
+ new net::HttpResponseHeaders("");
+ headers->AddHeader("Application-URL: http://127.0.0.1/apps");
+ test_fetcher->set_response_headers(headers);
+ test_fetcher->SetResponseString("<xml>description</xml>");
+ test_fetcher->delegate()->OnURLFetchComplete(test_fetcher);
+}
+
+TEST_F(DeviceDescriptionFetcherTest, TestFetchFailsOnHttp404) {
+ ExpectFailure("HTTP 404:");
+ net::TestURLFetcher* test_fetcher = StartRequest();
+
+ test_fetcher->set_response_code(net::HTTP_NOT_FOUND);
+ test_fetcher->delegate()->OnURLFetchComplete(test_fetcher);
+}
+
+TEST_F(DeviceDescriptionFetcherTest, TestFetchFailsOnMissingAppUrl) {
+ ExpectFailure("Missing or empty Application-URL:");
+ net::TestURLFetcher* test_fetcher = StartRequest();
+
+ test_fetcher->set_response_code(net::HTTP_OK);
+ scoped_refptr<net::HttpResponseHeaders> headers =
+ new net::HttpResponseHeaders("");
+ test_fetcher->set_response_headers(headers);
+ test_fetcher->delegate()->OnURLFetchComplete(test_fetcher);
+}
+
+TEST_F(DeviceDescriptionFetcherTest, TestFetchFailsOnEmptyAppUrl) {
+ ExpectFailure("Missing or empty Application-URL:");
+ net::TestURLFetcher* test_fetcher = StartRequest();
+
+ test_fetcher->set_response_code(net::HTTP_OK);
+ scoped_refptr<net::HttpResponseHeaders> headers =
+ new net::HttpResponseHeaders("");
+ headers->AddHeader("Application-URL:");
+ test_fetcher->set_response_headers(headers);
+ test_fetcher->delegate()->OnURLFetchComplete(test_fetcher);
+}
+
+TEST_F(DeviceDescriptionFetcherTest, TestFetchFailsOnInvalidAppUrl) {
+ ExpectFailure("Invalid Application-URL:");
+ net::TestURLFetcher* test_fetcher = StartRequest();
+
+ test_fetcher->set_response_code(net::HTTP_OK);
+ scoped_refptr<net::HttpResponseHeaders> headers =
+ new net::HttpResponseHeaders("");
+ headers->AddHeader("Application-URL: http://www.example.com");
+ test_fetcher->set_response_headers(headers);
+ test_fetcher->delegate()->OnURLFetchComplete(test_fetcher);
+}
+
+TEST_F(DeviceDescriptionFetcherTest, TestFetchFailsOnEmptyDescription) {
+ ExpectFailure("Missing or empty response");
+ net::TestURLFetcher* test_fetcher = StartRequest();
+
+ test_fetcher->set_response_code(net::HTTP_OK);
+ scoped_refptr<net::HttpResponseHeaders> headers =
+ new net::HttpResponseHeaders("");
+ headers->AddHeader("Application-URL: http://127.0.0.1/apps");
+ test_fetcher->set_response_headers(headers);
+ test_fetcher->SetResponseString("");
+ test_fetcher->delegate()->OnURLFetchComplete(test_fetcher);
+}
+
+TEST_F(DeviceDescriptionFetcherTest, TestFetchFailsOnBadDescription) {
+ ExpectFailure("Invalid response encoding");
+ net::TestURLFetcher* test_fetcher = StartRequest();
+
+ test_fetcher->set_response_code(net::HTTP_OK);
+ scoped_refptr<net::HttpResponseHeaders> headers =
+ new net::HttpResponseHeaders("");
+ headers->AddHeader("Application-URL: http://127.0.0.1/apps");
+ test_fetcher->set_response_headers(headers);
+ test_fetcher->SetResponseString("\xfc\x9c\xbf\x80\xbf\x80");
+ test_fetcher->delegate()->OnURLFetchComplete(test_fetcher);
+}
+
+TEST_F(DeviceDescriptionFetcherTest, TestFetchFailsOnResponseTooLarge) {
+ ExpectFailure("Response too large");
+ net::TestURLFetcher* test_fetcher = StartRequest();
+
+ test_fetcher->set_response_code(net::HTTP_OK);
+ scoped_refptr<net::HttpResponseHeaders> headers =
+ new net::HttpResponseHeaders("");
+ headers->AddHeader("Application-URL: http://127.0.0.1/apps");
+ test_fetcher->set_response_headers(headers);
+ test_fetcher->SetResponseString(std::string(262145, 'd'));
+ test_fetcher->delegate()->OnURLFetchComplete(test_fetcher);
+}
+
+} // namespace extensions

Powered by Google App Engine
This is Rietveld 408576698