Re-subimission of https://codereview.chromium.org/1041213003/
[chromium-blink-merge.git] / extensions / browser / api / document_scan / document_scan_api_unittest.cc
blob1c8f5971d3e66ee5e44691e9a10f418f57fb5b88
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "extensions/browser/api/document_scan/document_scan_api.h"
7 #include <string>
8 #include <vector>
10 #include "extensions/browser/api/document_scan/mock_document_scan_interface.h"
11 #include "extensions/browser/api_test_utils.h"
12 #include "extensions/browser/api_unittest.h"
13 #include "testing/gtest/include/gtest/gtest.h"
15 using testing::_;
17 namespace extensions {
19 namespace core_api {
21 // Tests of networking_private_crypto support for Networking Private API.
22 class DocumentScanScanFunctionTest : public ApiUnitTest {
23 public:
24 DocumentScanScanFunctionTest()
25 : function_(new DocumentScanScanFunction()),
26 document_scan_interface_(new MockDocumentScanInterface()) {}
27 ~DocumentScanScanFunctionTest() override {}
29 void SetUp() override {
30 ApiUnitTest::SetUp();
31 // Passes ownership.
32 function_->document_scan_interface_.reset(document_scan_interface_);
35 protected:
36 std::string RunFunctionAndReturnError(const std::string& args) {
37 function_->set_extension(extension());
38 std::string error = api_test_utils::RunFunctionAndReturnError(
39 function_, args, browser_context(), api_test_utils::NONE);
40 return error;
43 DocumentScanScanFunction* function_;
44 MockDocumentScanInterface* document_scan_interface_; // Owned by function_.
47 ACTION_P2(InvokeListScannersCallback, scanner_list, error) {
48 ::std::tr1::get<0>(args).Run(scanner_list, error);
51 ACTION_P3(InvokeScanCallback, data, mime_type, error) {
52 ::std::tr1::get<3>(args).Run(data, mime_type, error);
55 TEST_F(DocumentScanScanFunctionTest, GestureRequired) {
56 EXPECT_EQ("User gesture required to perform scan",
57 RunFunctionAndReturnError("[{}]"));
60 TEST_F(DocumentScanScanFunctionTest, NoScanners) {
61 function_->set_user_gesture(true);
62 EXPECT_CALL(*document_scan_interface_, ListScanners(_))
63 .WillOnce(InvokeListScannersCallback(
64 std::vector<DocumentScanInterface::ScannerDescription>(), ""));
65 EXPECT_EQ("Scanner not available", RunFunctionAndReturnError("[{}]"));
68 TEST_F(DocumentScanScanFunctionTest, NoMatchingScanners) {
69 function_->set_user_gesture(true);
70 std::vector<DocumentScanInterface::ScannerDescription> scanner_list;
71 DocumentScanInterface::ScannerDescription scanner;
72 scanner.image_mime_type = "img/fresco";
73 scanner_list.push_back(scanner);
74 EXPECT_CALL(*document_scan_interface_, ListScanners(_))
75 .WillOnce(InvokeListScannersCallback(scanner_list, ""));
76 EXPECT_EQ(
77 "Scanner not available",
78 RunFunctionAndReturnError("[{\"mimeTypes\": [\"img/silverpoint\"]}]"));
81 TEST_F(DocumentScanScanFunctionTest, ScanFailure) {
82 function_->set_user_gesture(true);
83 std::vector<DocumentScanInterface::ScannerDescription> scanner_list;
84 DocumentScanInterface::ScannerDescription scanner;
85 const char kMimeType[] = "img/tempera";
86 const char kScannerName[] = "Michelangelo";
87 scanner.name = kScannerName;
88 scanner.image_mime_type = kMimeType;
89 scanner_list.push_back(scanner);
90 EXPECT_CALL(*document_scan_interface_, ListScanners(_))
91 .WillOnce(InvokeListScannersCallback(scanner_list, ""));
92 const char kScanError[] = "Someone ate all the eggs";
93 EXPECT_CALL(*document_scan_interface_, Scan(kScannerName, _, _, _))
94 .WillOnce(InvokeScanCallback("", "", kScanError));
95 EXPECT_EQ(kScanError,
96 RunFunctionAndReturnError("[{\"mimeTypes\": [\"img/tempera\"]}]"));
99 TEST_F(DocumentScanScanFunctionTest, Success) {
100 std::vector<DocumentScanInterface::ScannerDescription> scanner_list;
101 scanner_list.push_back(DocumentScanInterface::ScannerDescription());
102 EXPECT_CALL(*document_scan_interface_, ListScanners(_))
103 .WillOnce(InvokeListScannersCallback(scanner_list, ""));
104 const char kScanData[] = "A beautiful picture";
105 const char kMimeType[] = "img/encaustic";
106 EXPECT_CALL(*document_scan_interface_, Scan(_, _, _, _))
107 .WillOnce(InvokeScanCallback(kScanData, kMimeType, ""));
108 function_->set_user_gesture(true);
109 scoped_ptr<base::DictionaryValue> result(
110 RunFunctionAndReturnDictionary(function_, "[{}]"));
111 ASSERT_NE(nullptr, result.get());
112 document_scan::ScanResults scan_results;
113 EXPECT_TRUE(document_scan::ScanResults::Populate(*result, &scan_results));
114 EXPECT_THAT(scan_results.data_urls, testing::ElementsAre(kScanData));
115 EXPECT_EQ(kMimeType, scan_results.mime_type);
118 } // namespace core_api
120 } // namespace extensions