1 // Copyright (c) 2012 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.
6 #include "base/files/file_path.h"
7 #include "base/files/file_util.h"
8 #include "base/files/scoped_temp_dir.h"
9 #include "base/run_loop.h"
10 #include "content/public/browser/web_contents.h"
11 #include "content/public/test/content_browser_test.h"
12 #include "content/public/test/content_browser_test_utils.h"
13 #include "content/public/test/test_utils.h"
14 #include "content/shell/browser/shell.h"
15 #include "net/test/embedded_test_server/embedded_test_server.h"
16 #include "testing/gtest/include/gtest/gtest.h"
20 class MHTMLGenerationTest
: public ContentBrowserTest
{
22 MHTMLGenerationTest() : mhtml_generated_(false), file_size_(0) {}
24 void MHTMLGenerated(int64 size
) {
25 mhtml_generated_
= true;
27 base::MessageLoopForUI::current()->Quit();
31 void SetUp() override
{
32 ASSERT_TRUE(temp_dir_
.CreateUniqueTempDir());
33 ContentBrowserTest::SetUp();
36 bool mhtml_generated() const { return mhtml_generated_
; }
37 int64
file_size() const { return file_size_
; }
39 base::ScopedTempDir temp_dir_
;
42 bool mhtml_generated_
;
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
50 IN_PROC_BROWSER_TEST_F(MHTMLGenerationTest
, GenerateMHTML
) {
51 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
53 base::FilePath
path(temp_dir_
.path());
54 path
= path
.Append(FILE_PATH_LITERAL("test.mht"));
56 NavigateToURL(shell(), embedded_test_server()->GetURL("/simple_page.html"));
58 shell()->web_contents()->GenerateMHTML(
59 path
, base::Bind(&MHTMLGenerationTest::MHTMLGenerated
, this));
61 // Block until the MHTML is generated.
64 EXPECT_TRUE(mhtml_generated());
65 EXPECT_GT(file_size(), 0);
67 // Make sure the actual generated file has some contents.
69 ASSERT_TRUE(base::GetFileSize(path
, &file_size
));
70 EXPECT_GT(file_size
, 100);
73 } // namespace content