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

Unified Diff: chrome/browser/extensions/api/document_scan/document_scan_api_unittest.cc

Issue 286933006: Implement a JavaScript API for document scanning (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Starting over. This used to be 286933006 Created 6 years, 2 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/document_scan/document_scan_api_unittest.cc
diff --git a/chrome/browser/extensions/api/document_scan/document_scan_api_unittest.cc b/chrome/browser/extensions/api/document_scan/document_scan_api_unittest.cc
new file mode 100644
index 0000000000000000000000000000000000000000..40faad210fa93e86d957f333e87f669622047d94
--- /dev/null
+++ b/chrome/browser/extensions/api/document_scan/document_scan_api_unittest.cc
@@ -0,0 +1,123 @@
+// Copyright 2014 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 "chrome/browser/extensions/api/document_scan/document_scan_api.h"
+
+#include <string>
+#include <vector>
+
+#include "chrome/browser/extensions/api/document_scan/mock_document_scan_interface.h"
+#include "chrome/browser/extensions/extension_api_unittest.h"
+#include "chrome/browser/extensions/extension_function_test_utils.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+using testing::_;
+
+namespace extensions {
+
+namespace api {
+
+// Tests of networking_private_crypto support for Networking Private API.
+class DocumentScanScanFunctionTest : public ExtensionApiUnittest {
+ public:
+ DocumentScanScanFunctionTest()
+ : function_(new DocumentScanScanFunction()),
+ document_scan_interface_(new MockDocumentScanInterface()) {}
+ virtual ~DocumentScanScanFunctionTest() {}
+
+ void SetUp() {
+ ExtensionApiUnittest::SetUp();
+ // Passes ownership.
+ function_->document_scan_interface_.reset(document_scan_interface_);
+ }
+
+ protected:
+ std::string RunFunctionAndReturnError(const std::string& args) {
+ function_->set_extension(extension());
+ std::string error =
+ extension_function_test_utils::RunFunctionAndReturnError(
+ function_, args, browser(),
+ extension_function_test_utils::NONE);
+ return error;
+ }
+
+ DocumentScanScanFunction* function_;
+ MockDocumentScanInterface* document_scan_interface_; // Owned by function_.
+};
+
+ACTION_P2(InvokeListScannersCallback, scanner_list, error) {
+ ::std::tr1::get<0>(args).Run(scanner_list, error);
+}
+
+ACTION_P3(InvokeScanCallback, data, mime_type, error) {
+ ::std::tr1::get<3>(args).Run(data, mime_type, error);
+}
+
+TEST_F(DocumentScanScanFunctionTest, GestureRequired) {
+ EXPECT_EQ("User gesture required to perform scan",
+ RunFunctionAndReturnError("[{}]"));
+}
+
+TEST_F(DocumentScanScanFunctionTest, NoScanners) {
+ function_->set_user_gesture(true);
+ EXPECT_CALL(*document_scan_interface_, ListScanners(_))
+ .WillOnce(InvokeListScannersCallback(
+ std::vector<DocumentScanInterface::ScannerDescription>(),
+ ""));
+ EXPECT_EQ("Scanner not available",
+ RunFunctionAndReturnError("[{}]"));
+}
+
+TEST_F(DocumentScanScanFunctionTest, NoMatchingScanners) {
+ function_->set_user_gesture(true);
+ std::vector<DocumentScanInterface::ScannerDescription> scanner_list;
+ DocumentScanInterface::ScannerDescription scanner;
+ scanner.image_mime_type = "img/fresco";
+ scanner_list.push_back(scanner);
+ EXPECT_CALL(*document_scan_interface_, ListScanners(_))
+ .WillOnce(InvokeListScannersCallback(scanner_list, ""));
+ EXPECT_EQ(
+ "Scanner not available",
+ RunFunctionAndReturnError("[{\"mimeTypes\": [\"img/silverpoint\"]}]"));
+}
+
+TEST_F(DocumentScanScanFunctionTest, ScanFailure) {
+ function_->set_user_gesture(true);
+ std::vector<DocumentScanInterface::ScannerDescription> scanner_list;
+ DocumentScanInterface::ScannerDescription scanner;
+ const char kMimeType[] = "img/tempera";
+ const char kScannerName[] = "Michelangelo";
+ scanner.name = kScannerName;
+ scanner.image_mime_type = kMimeType;
+ scanner_list.push_back(scanner);
+ EXPECT_CALL(*document_scan_interface_, ListScanners(_))
+ .WillOnce(InvokeListScannersCallback(scanner_list, ""));
+ const char kScanError[] = "Someone ate all the eggs";
+ EXPECT_CALL(*document_scan_interface_, Scan(kScannerName, _, _, _))
+ .WillOnce(InvokeScanCallback("", "", kScanError));
+ EXPECT_EQ(kScanError,
+ RunFunctionAndReturnError("[{\"mimeTypes\": [\"img/tempera\"]}]"));
+}
+
+TEST_F(DocumentScanScanFunctionTest, Success) {
+ std::vector<DocumentScanInterface::ScannerDescription> scanner_list;
+ scanner_list.push_back(DocumentScanInterface::ScannerDescription());
+ EXPECT_CALL(*document_scan_interface_, ListScanners(_))
+ .WillOnce(InvokeListScannersCallback(scanner_list, ""));
+ const char kScanData[] = "A beautiful picture";
+ const char kMimeType[] = "img/encaustic";
+ EXPECT_CALL(*document_scan_interface_, Scan(_, _, _, _))
+ .WillOnce(InvokeScanCallback(kScanData, kMimeType, ""));
+ function_->set_user_gesture(true);
+ scoped_ptr<base::DictionaryValue> result(RunFunctionAndReturnDictionary(
+ function_, "[{}]"));
+ ASSERT_NE(nullptr, result.get());
+ document_scan::ScanResults scan_results;
+ EXPECT_TRUE(document_scan::ScanResults::Populate(*result, &scan_results));
+ EXPECT_THAT(scan_results.data_urls, testing::ElementsAre(kScanData));
+ EXPECT_EQ(kMimeType, scan_results.mime_type);
+}
+
+} // namespace api
+
+} // namespace extensions

Powered by Google App Engine
This is Rietveld 408576698