Roll src/third_party/WebKit d9c6159:8139f33 (svn 201974:201975)
[chromium-blink-merge.git] / extensions / browser / api / mime_handler_private / mime_handler_private_unittest.cc
blob7f36a3995808e185d9b4165a07a0c2fc8f1f3767
1 // Copyright 2015 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 "base/memory/scoped_ptr.h"
6 #include "base/message_loop/message_loop.h"
7 #include "content/public/browser/stream_handle.h"
8 #include "content/public/browser/stream_info.h"
9 #include "extensions/browser/api/mime_handler_private/mime_handler_private.h"
10 #include "extensions/browser/guest_view/mime_handler_view/mime_handler_view_guest.h"
11 #include "testing/gtest/include/gtest/gtest.h"
13 namespace extensions {
15 class TestStreamHandle : public content::StreamHandle {
16 public:
17 TestStreamHandle() : url_("stream://url") {}
19 ~TestStreamHandle() override {
20 while (!close_callbacks_.empty()) {
21 close_callbacks_.front().Run();
22 close_callbacks_.pop();
26 const GURL& GetURL() override { return url_; }
28 void AddCloseListener(const base::Closure& callback) override {
29 close_callbacks_.push(callback);
32 private:
33 GURL url_;
34 std::queue<base::Closure> close_callbacks_;
37 class MimeHandlerServiceImplTest : public testing::Test {
38 public:
39 void SetUp() override {
40 scoped_ptr<content::StreamInfo> stream_info(new content::StreamInfo);
41 stream_info->handle = make_scoped_ptr(new TestStreamHandle);
42 stream_info->mime_type = "test/unit";
43 stream_info->original_url = GURL("test://extensions_unittests");
44 stream_container_.reset(
45 new StreamContainer(stream_info.Pass(), 1, true, GURL(), ""));
46 service_.reset(new MimeHandlerServiceImpl(stream_container_->GetWeakPtr(),
47 mojo::GetProxy(&service_ptr_)));
49 void TearDown() override {
50 service_.reset();
51 stream_container_.reset();
54 void AbortCallback() { abort_called_ = true; }
55 void GetStreamInfoCallback(mime_handler::StreamInfoPtr stream_info) {
56 stream_info_ = stream_info.Pass();
59 base::MessageLoop message_loop_;
60 scoped_ptr<StreamContainer> stream_container_;
61 mime_handler::MimeHandlerServicePtr service_ptr_;
62 scoped_ptr<mime_handler::MimeHandlerService> service_;
63 bool abort_called_ = false;
64 mime_handler::StreamInfoPtr stream_info_;
67 TEST_F(MimeHandlerServiceImplTest, Abort) {
68 service_->AbortStream(base::Bind(&MimeHandlerServiceImplTest::AbortCallback,
69 base::Unretained(this)));
70 EXPECT_TRUE(abort_called_);
72 abort_called_ = false;
73 service_->AbortStream(base::Bind(&MimeHandlerServiceImplTest::AbortCallback,
74 base::Unretained(this)));
75 EXPECT_TRUE(abort_called_);
77 stream_container_.reset();
78 abort_called_ = false;
79 service_->AbortStream(base::Bind(&MimeHandlerServiceImplTest::AbortCallback,
80 base::Unretained(this)));
81 EXPECT_TRUE(abort_called_);
84 TEST_F(MimeHandlerServiceImplTest, GetStreamInfo) {
85 service_->GetStreamInfo(
86 base::Bind(&MimeHandlerServiceImplTest::GetStreamInfoCallback,
87 base::Unretained(this)));
88 ASSERT_TRUE(stream_info_);
89 EXPECT_TRUE(stream_info_->embedded);
90 EXPECT_EQ(1, stream_info_->tab_id);
91 EXPECT_EQ("test/unit", stream_info_->mime_type);
92 EXPECT_EQ("test://extensions_unittests", stream_info_->original_url);
93 EXPECT_EQ("stream://url", stream_info_->stream_url);
95 service_->AbortStream(base::Bind(&MimeHandlerServiceImplTest::AbortCallback,
96 base::Unretained(this)));
97 EXPECT_TRUE(abort_called_);
98 service_->GetStreamInfo(
99 base::Bind(&MimeHandlerServiceImplTest::GetStreamInfoCallback,
100 base::Unretained(this)));
101 ASSERT_FALSE(stream_info_);
103 stream_container_.reset();
104 service_->GetStreamInfo(
105 base::Bind(&MimeHandlerServiceImplTest::GetStreamInfoCallback,
106 base::Unretained(this)));
107 ASSERT_FALSE(stream_info_);
110 } // namespace extensions