| OLD | NEW | 
|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be | 
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. | 
| 4 | 4 | 
| 5 #include "base/base_switches.h" | 5 #include "base/base_switches.h" | 
| 6 #include "base/command_line.h" | 6 #include "base/command_line.h" | 
| 7 #include "base/threading/thread_restrictions.h" | 7 #include "base/threading/thread_restrictions.h" | 
| 8 #include "chrome/browser/extensions/api/page_capture/page_capture_api.h" | 8 #include "chrome/browser/extensions/api/page_capture/page_capture_api.h" | 
| 9 #include "chrome/browser/extensions/extension_apitest.h" | 9 #include "chrome/browser/extensions/extension_apitest.h" | 
| 10 #include "chrome/common/chrome_switches.h" | 10 #include "chrome/common/chrome_switches.h" | 
| 11 #include "chromeos/login/login_state.h" | 11 #include "chromeos/login/login_state.h" | 
| 12 #include "content/public/common/content_switches.h" | 12 #include "content/public/common/content_switches.h" | 
| 13 #include "content/public/test/test_utils.h" | 13 #include "content/public/test/test_utils.h" | 
| 14 #include "extensions/browser/extension_dialog_auto_confirm.h" | 14 #include "extensions/browser/extension_dialog_auto_confirm.h" | 
| 15 #include "net/dns/mock_host_resolver.h" | 15 #include "net/dns/mock_host_resolver.h" | 
| 16 | 16 | 
| 17 using extensions::PageCaptureSaveAsMHTMLFunction; | 17 using extensions::PageCaptureSaveAsMHTMLFunction; | 
| 18 using extensions::ScopedTestDialogAutoConfirm; | 18 using extensions::ScopedTestDialogAutoConfirm; | 
| 19 | 19 | 
| 20 class ExtensionPageCaptureApiTest : public ExtensionApiTest { | 20 class ExtensionPageCaptureApiTest : public ExtensionApiTest { | 
| 21  public: | 21  public: | 
| 22   void SetUpCommandLine(base::CommandLine* command_line) override { | 22   void SetUpCommandLine(base::CommandLine* command_line) override { | 
| 23     ExtensionApiTest::SetUpCommandLine(command_line); | 23     ExtensionApiTest::SetUpCommandLine(command_line); | 
| 24     command_line->AppendSwitchASCII(switches::kJavaScriptFlags, "--expose-gc"); | 24     command_line->AppendSwitchASCII(switches::kJavaScriptFlags, "--expose-gc"); | 
| 25   } | 25   } | 
|  | 26   void SetUpOnMainThread() override { | 
|  | 27     ExtensionApiTest::SetUpOnMainThread(); | 
|  | 28     host_resolver()->AddRule("*", "127.0.0.1"); | 
|  | 29   } | 
| 26 }; | 30 }; | 
| 27 | 31 | 
| 28 class PageCaptureSaveAsMHTMLDelegate | 32 class PageCaptureSaveAsMHTMLDelegate | 
| 29     : public PageCaptureSaveAsMHTMLFunction::TestDelegate { | 33     : public PageCaptureSaveAsMHTMLFunction::TestDelegate { | 
| 30  public: | 34  public: | 
| 31   PageCaptureSaveAsMHTMLDelegate() { | 35   PageCaptureSaveAsMHTMLDelegate() { | 
| 32     PageCaptureSaveAsMHTMLFunction::SetTestDelegate(this); | 36     PageCaptureSaveAsMHTMLFunction::SetTestDelegate(this); | 
| 33   } | 37   } | 
| 34 | 38 | 
| 35   virtual ~PageCaptureSaveAsMHTMLDelegate() { | 39   virtual ~PageCaptureSaveAsMHTMLDelegate() { | 
| 36     PageCaptureSaveAsMHTMLFunction::SetTestDelegate(NULL); | 40     PageCaptureSaveAsMHTMLFunction::SetTestDelegate(NULL); | 
| 37   } | 41   } | 
| 38 | 42 | 
| 39   void OnTemporaryFileCreated(const base::FilePath& temp_file) override { | 43   void OnTemporaryFileCreated(const base::FilePath& temp_file) override { | 
| 40     temp_file_ = temp_file; | 44     temp_file_ = temp_file; | 
| 41   } | 45   } | 
| 42 | 46 | 
| 43   base::FilePath temp_file_; | 47   base::FilePath temp_file_; | 
| 44 }; | 48 }; | 
| 45 | 49 | 
| 46 IN_PROC_BROWSER_TEST_F(ExtensionPageCaptureApiTest, SaveAsMHTML) { | 50 IN_PROC_BROWSER_TEST_F(ExtensionPageCaptureApiTest, SaveAsMHTML) { | 
| 47   host_resolver()->AddRule("www.a.com", "127.0.0.1"); |  | 
| 48   ASSERT_TRUE(StartEmbeddedTestServer()); | 51   ASSERT_TRUE(StartEmbeddedTestServer()); | 
| 49   PageCaptureSaveAsMHTMLDelegate delegate; | 52   PageCaptureSaveAsMHTMLDelegate delegate; | 
| 50   ASSERT_TRUE(RunExtensionTest("page_capture")) << message_; | 53   ASSERT_TRUE(RunExtensionTest("page_capture")) << message_; | 
| 51   // Make sure the MHTML data gets written to the temporary file. | 54   // Make sure the MHTML data gets written to the temporary file. | 
| 52   ASSERT_FALSE(delegate.temp_file_.empty()); | 55   ASSERT_FALSE(delegate.temp_file_.empty()); | 
| 53   // Flush the message loops to make sure the delete happens. | 56   // Flush the message loops to make sure the delete happens. | 
| 54   content::RunAllPendingInMessageLoop(content::BrowserThread::FILE); | 57   content::RunAllPendingInMessageLoop(content::BrowserThread::FILE); | 
| 55   content::RunAllPendingInMessageLoop(content::BrowserThread::IO); | 58   content::RunAllPendingInMessageLoop(content::BrowserThread::IO); | 
| 56   // Make sure the temporary file is destroyed once the javascript side reads | 59   // Make sure the temporary file is destroyed once the javascript side reads | 
| 57   // the contents. | 60   // the contents. | 
| 58   base::ThreadRestrictions::ScopedAllowIO allow_io; | 61   base::ThreadRestrictions::ScopedAllowIO allow_io; | 
| 59   ASSERT_FALSE(base::PathExists(delegate.temp_file_)); | 62   ASSERT_FALSE(base::PathExists(delegate.temp_file_)); | 
| 60 } | 63 } | 
| 61 | 64 | 
| 62 #if defined(OS_CHROMEOS) | 65 #if defined(OS_CHROMEOS) | 
| 63 IN_PROC_BROWSER_TEST_F(ExtensionPageCaptureApiTest, | 66 IN_PROC_BROWSER_TEST_F(ExtensionPageCaptureApiTest, | 
| 64                        PublicSessionRequestAllowed) { | 67                        PublicSessionRequestAllowed) { | 
| 65   host_resolver()->AddRule("www.a.com", "127.0.0.1"); |  | 
| 66   ASSERT_TRUE(StartEmbeddedTestServer()); | 68   ASSERT_TRUE(StartEmbeddedTestServer()); | 
| 67   PageCaptureSaveAsMHTMLDelegate delegate; | 69   PageCaptureSaveAsMHTMLDelegate delegate; | 
| 68   // Set Public Session state. | 70   // Set Public Session state. | 
| 69   chromeos::LoginState::Get()->SetLoggedInState( | 71   chromeos::LoginState::Get()->SetLoggedInState( | 
| 70       chromeos::LoginState::LOGGED_IN_ACTIVE, | 72       chromeos::LoginState::LOGGED_IN_ACTIVE, | 
| 71       chromeos::LoginState::LOGGED_IN_USER_PUBLIC_ACCOUNT); | 73       chromeos::LoginState::LOGGED_IN_USER_PUBLIC_ACCOUNT); | 
| 72   // Resolve Permission dialog with Allow. | 74   // Resolve Permission dialog with Allow. | 
| 73   ScopedTestDialogAutoConfirm auto_confirm(ScopedTestDialogAutoConfirm::ACCEPT); | 75   ScopedTestDialogAutoConfirm auto_confirm(ScopedTestDialogAutoConfirm::ACCEPT); | 
| 74   ASSERT_TRUE(RunExtensionTest("page_capture")) << message_; | 76   ASSERT_TRUE(RunExtensionTest("page_capture")) << message_; | 
| 75   ASSERT_FALSE(delegate.temp_file_.empty()); | 77   ASSERT_FALSE(delegate.temp_file_.empty()); | 
| 76   content::RunAllPendingInMessageLoop(content::BrowserThread::FILE); | 78   content::RunAllPendingInMessageLoop(content::BrowserThread::FILE); | 
| 77   content::RunAllPendingInMessageLoop(content::BrowserThread::IO); | 79   content::RunAllPendingInMessageLoop(content::BrowserThread::IO); | 
| 78   base::ThreadRestrictions::ScopedAllowIO allow_io; | 80   base::ThreadRestrictions::ScopedAllowIO allow_io; | 
| 79   ASSERT_FALSE(base::PathExists(delegate.temp_file_)); | 81   ASSERT_FALSE(base::PathExists(delegate.temp_file_)); | 
| 80 } | 82 } | 
| 81 | 83 | 
| 82 IN_PROC_BROWSER_TEST_F(ExtensionPageCaptureApiTest, | 84 IN_PROC_BROWSER_TEST_F(ExtensionPageCaptureApiTest, | 
| 83                        PublicSessionRequestDenied) { | 85                        PublicSessionRequestDenied) { | 
| 84   host_resolver()->AddRule("www.a.com", "127.0.0.1"); |  | 
| 85   ASSERT_TRUE(StartEmbeddedTestServer()); | 86   ASSERT_TRUE(StartEmbeddedTestServer()); | 
| 86   // Set Public Session state. | 87   // Set Public Session state. | 
| 87   chromeos::LoginState::Get()->SetLoggedInState( | 88   chromeos::LoginState::Get()->SetLoggedInState( | 
| 88       chromeos::LoginState::LOGGED_IN_ACTIVE, | 89       chromeos::LoginState::LOGGED_IN_ACTIVE, | 
| 89       chromeos::LoginState::LOGGED_IN_USER_PUBLIC_ACCOUNT); | 90       chromeos::LoginState::LOGGED_IN_USER_PUBLIC_ACCOUNT); | 
| 90   // Resolve Permission dialog with Deny. | 91   // Resolve Permission dialog with Deny. | 
| 91   ScopedTestDialogAutoConfirm auto_confirm(ScopedTestDialogAutoConfirm::CANCEL); | 92   ScopedTestDialogAutoConfirm auto_confirm(ScopedTestDialogAutoConfirm::CANCEL); | 
| 92   ASSERT_TRUE(RunExtensionTestWithArg("page_capture", "REQUEST_DENIED")) | 93   ASSERT_TRUE(RunExtensionTestWithArg("page_capture", "REQUEST_DENIED")) | 
| 93       << message_; | 94       << message_; | 
| 94 } | 95 } | 
| 95 #endif  // defined(OS_CHROMEOS) | 96 #endif  // defined(OS_CHROMEOS) | 
| OLD | NEW | 
|---|