[email protected] | 3bbacc5b | 2012-04-17 17:46:15 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | e7f009d | 2011-06-14 19:35:10 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
3 | // found in the LICENSE file. | ||||
4 | |||||
[email protected] | 7477b271 | 2011-11-23 07:53:03 | [diff] [blame] | 5 | #include "base/bind.h" |
[email protected] | 5799981 | 2013-02-24 05:40:52 | [diff] [blame] | 6 | #include "base/files/file_path.h" |
thestig | b7aad54f | 2014-09-05 18:25:39 | [diff] [blame] | 7 | #include "base/files/file_util.h" |
[email protected] | ea1a3f6 | 2012-11-16 20:34:23 | [diff] [blame] | 8 | #include "base/files/scoped_temp_dir.h" |
[email protected] | 4cdc9a2 | 2012-07-26 03:39:31 | [diff] [blame] | 9 | #include "base/run_loop.h" |
[email protected] | 4ca1530 | 2012-01-03 05:53:20 | [diff] [blame] | 10 | #include "content/public/browser/web_contents.h" |
[email protected] | 6e9def1 | 2014-03-27 20:23:28 | [diff] [blame] | 11 | #include "content/public/test/content_browser_test.h" |
12 | #include "content/public/test/content_browser_test_utils.h" | ||||
[email protected] | 4cdc9a2 | 2012-07-26 03:39:31 | [diff] [blame] | 13 | #include "content/public/test/test_utils.h" |
[email protected] | de7d61ff | 2013-08-20 11:30:41 | [diff] [blame] | 14 | #include "content/shell/browser/shell.h" |
[email protected] | 0d31fbc | 2013-05-28 17:00:37 | [diff] [blame] | 15 | #include "net/test/embedded_test_server/embedded_test_server.h" |
[email protected] | e7f009d | 2011-06-14 19:35:10 | [diff] [blame] | 16 | #include "testing/gtest/include/gtest/gtest.h" |
17 | |||||
[email protected] | 4cdc9a2 | 2012-07-26 03:39:31 | [diff] [blame] | 18 | namespace content { |
[email protected] | 4ca1530 | 2012-01-03 05:53:20 | [diff] [blame] | 19 | |
[email protected] | 4cdc9a2 | 2012-07-26 03:39:31 | [diff] [blame] | 20 | class MHTMLGenerationTest : public ContentBrowserTest { |
[email protected] | e7f009d | 2011-06-14 19:35:10 | [diff] [blame] | 21 | public: |
[email protected] | 7477b271 | 2011-11-23 07:53:03 | [diff] [blame] | 22 | MHTMLGenerationTest() : mhtml_generated_(false), file_size_(0) {} |
23 | |||||
[email protected] | 18516cf9 | 2013-08-28 18:19:48 | [diff] [blame] | 24 | void MHTMLGenerated(int64 size) { |
[email protected] | 7477b271 | 2011-11-23 07:53:03 | [diff] [blame] | 25 | mhtml_generated_ = true; |
26 | file_size_ = size; | ||||
[email protected] | dd32b127 | 2013-05-04 14:17:11 | [diff] [blame] | 27 | base::MessageLoopForUI::current()->Quit(); |
[email protected] | 7477b271 | 2011-11-23 07:53:03 | [diff] [blame] | 28 | } |
[email protected] | e7f009d | 2011-06-14 19:35:10 | [diff] [blame] | 29 | |
30 | protected: | ||||
dcheng | fa85b15 | 2014-10-28 01:13:42 | [diff] [blame] | 31 | void SetUp() override { |
[email protected] | e7f009d | 2011-06-14 19:35:10 | [diff] [blame] | 32 | ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
[email protected] | 4cdc9a2 | 2012-07-26 03:39:31 | [diff] [blame] | 33 | ContentBrowserTest::SetUp(); |
[email protected] | e7f009d | 2011-06-14 19:35:10 | [diff] [blame] | 34 | } |
35 | |||||
[email protected] | 7477b271 | 2011-11-23 07:53:03 | [diff] [blame] | 36 | bool mhtml_generated() const { return mhtml_generated_; } |
37 | int64 file_size() const { return file_size_; } | ||||
38 | |||||
[email protected] | ea1a3f6 | 2012-11-16 20:34:23 | [diff] [blame] | 39 | base::ScopedTempDir temp_dir_; |
[email protected] | 7477b271 | 2011-11-23 07:53:03 | [diff] [blame] | 40 | |
41 | private: | ||||
42 | bool mhtml_generated_; | ||||
43 | int64 file_size_; | ||||
[email protected] | e7f009d | 2011-06-14 19:35:10 | [diff] [blame] | 44 | }; |
45 | |||||
46 | // Tests that generating a MHTML does create contents. | ||||
47 | // Note that the actual content of the file is not tested, the purpose of this | ||||
48 | // test is to ensure we were successfull in creating the MHTML data from the | ||||
49 | // renderer. | ||||
50 | IN_PROC_BROWSER_TEST_F(MHTMLGenerationTest, GenerateMHTML) { | ||||
[email protected] | 0d31fbc | 2013-05-28 17:00:37 | [diff] [blame] | 51 | ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady()); |
[email protected] | e7f009d | 2011-06-14 19:35:10 | [diff] [blame] | 52 | |
[email protected] | 2dec8ec | 2013-02-07 19:20:34 | [diff] [blame] | 53 | base::FilePath path(temp_dir_.path()); |
[email protected] | e7f009d | 2011-06-14 19:35:10 | [diff] [blame] | 54 | path = path.Append(FILE_PATH_LITERAL("test.mht")); |
55 | |||||
[email protected] | 0d31fbc | 2013-05-28 17:00:37 | [diff] [blame] | 56 | NavigateToURL(shell(), embedded_test_server()->GetURL("/simple_page.html")); |
[email protected] | e7f009d | 2011-06-14 19:35:10 | [diff] [blame] | 57 | |
[email protected] | 4cdc9a2 | 2012-07-26 03:39:31 | [diff] [blame] | 58 | shell()->web_contents()->GenerateMHTML( |
59 | path, base::Bind(&MHTMLGenerationTest::MHTMLGenerated, this)); | ||||
[email protected] | e7f009d | 2011-06-14 19:35:10 | [diff] [blame] | 60 | |
[email protected] | 7477b271 | 2011-11-23 07:53:03 | [diff] [blame] | 61 | // Block until the MHTML is generated. |
[email protected] | 3586962 | 2012-10-26 23:23:55 | [diff] [blame] | 62 | RunMessageLoop(); |
[email protected] | 7477b271 | 2011-11-23 07:53:03 | [diff] [blame] | 63 | |
64 | EXPECT_TRUE(mhtml_generated()); | ||||
65 | EXPECT_GT(file_size(), 0); | ||||
[email protected] | e7f009d | 2011-06-14 19:35:10 | [diff] [blame] | 66 | |
[email protected] | 3a605e8 | 2011-09-26 17:26:16 | [diff] [blame] | 67 | // Make sure the actual generated file has some contents. |
[email protected] | e7f009d | 2011-06-14 19:35:10 | [diff] [blame] | 68 | int64 file_size; |
[email protected] | 5628570 | 2013-12-04 18:22:49 | [diff] [blame] | 69 | ASSERT_TRUE(base::GetFileSize(path, &file_size)); |
[email protected] | e7f009d | 2011-06-14 19:35:10 | [diff] [blame] | 70 | EXPECT_GT(file_size, 100); |
71 | } | ||||
72 | |||||
[email protected] | 4cdc9a2 | 2012-07-26 03:39:31 | [diff] [blame] | 73 | } // namespace content |