mirror of
				https://bitbucket.org/chromiumembedded/cef
				synced 2025-06-05 21:39:12 +02:00 
			
		
		
		
	This change allows the client to directly send and receive DevTools protocol messages (send method calls, and receive method results and events) without requiring a DevTools front-end or remote-debugging session. This change includes additional supporting changes: - Add a new CefRequestHandler::OnDocumentAvailableInMainFrame callback (see issue #1454). - Add a CefParseJSON variant that accepts a UTF8-encoded buffer. - Add a `--devtools-protocol-log-file=<path>` command-line flag for logging protocol messages sent to/from the DevTools front-end while it is displayed. This is useful for understanding existing DevTools protocol usage. - Add a new "libcef_static_unittests" executable target to support light-weight unit tests of libcef_static internals (e.g. without requiring exposure via the CEF API). Files to be unittested are placed in the new "libcef_static_unittested" source_set which is then included by both the existing libcef_static library and the new unittests executable target. - Linux: Remove use_bundled_fontconfig=false, which is no longer required and causes unittest build errors (see issue #2424). This change also adds a cefclient demo for configuring offline mode using the DevTools protocol (fixes issue #245). This is controlled by the "Offline mode" context menu option and the `--offline` command-line switch which will launch cefclient in offline mode. When cefclient is offline all network requests will fail with ERR_INTERNET_DISCONNECTED and navigator.onLine will return false when called from JavaScript in any frame. This mode is per-browser so newly created browser windows will have the default mode. Note that configuring offline mode in this way will not update the Network tab UI ("Throtting" option) in a displayed DevTools front-end instance.
		
			
				
	
	
		
			205 lines
		
	
	
		
			6.5 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
			
		
		
	
	
			205 lines
		
	
	
		
			6.5 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
| // Copyright (c) 2020 The Chromium Embedded Framework Authors. All rights
 | |
| // reserved. Use of this source code is governed by a BSD-style license that
 | |
| // can be found in the LICENSE file.
 | |
| 
 | |
| #include "libcef/browser/devtools/devtools_util.h"
 | |
| 
 | |
| #include "testing/gtest/include/gtest/gtest.h"
 | |
| 
 | |
| using namespace devtools_util;
 | |
| 
 | |
| TEST(DevToolsUtil, ProtocolParser_IsValidMessage) {
 | |
|   // Empty dictionary is not valid.
 | |
|   EXPECT_FALSE(ProtocolParser::IsValidMessage(""));
 | |
|   EXPECT_FALSE(ProtocolParser::IsValidMessage("{}"));
 | |
| 
 | |
|   // Incorrectly formatted dictionary is not valid.
 | |
|   EXPECT_FALSE(ProtocolParser::IsValidMessage("{ ]"));
 | |
| 
 | |
|   // Everything else is valid (we don't verify JSON structure).
 | |
|   EXPECT_TRUE(ProtocolParser::IsValidMessage("{ }"));
 | |
|   EXPECT_TRUE(ProtocolParser::IsValidMessage("{blah blah}"));
 | |
|   EXPECT_TRUE(ProtocolParser::IsValidMessage("{method:\"foobar\"}"));
 | |
| }
 | |
| 
 | |
| TEST(DevToolsUtil, ProtocolParser_Initialize_IsFailure_Unknown) {
 | |
|   ProtocolParser parser;
 | |
|   EXPECT_FALSE(parser.IsInitialized());
 | |
| 
 | |
|   // Empty message is invalid.
 | |
|   EXPECT_TRUE(parser.Initialize(""));
 | |
|   EXPECT_TRUE(parser.IsInitialized());
 | |
|   EXPECT_TRUE(parser.IsFailure());
 | |
| 
 | |
|   parser.Reset();
 | |
|   EXPECT_FALSE(parser.IsInitialized());
 | |
| 
 | |
|   // Empty dictionary is invalid.
 | |
|   EXPECT_TRUE(parser.Initialize("{}"));
 | |
|   EXPECT_TRUE(parser.IsInitialized());
 | |
|   EXPECT_TRUE(parser.IsFailure());
 | |
| 
 | |
|   parser.Reset();
 | |
|   EXPECT_FALSE(parser.IsInitialized());
 | |
| 
 | |
|   // Unrecognized dictionary type is invalid.
 | |
|   EXPECT_TRUE(parser.Initialize("{blah blah}"));
 | |
|   EXPECT_TRUE(parser.IsInitialized());
 | |
|   EXPECT_TRUE(parser.IsFailure());
 | |
| }
 | |
| 
 | |
| TEST(DevToolsUtil, ProtocolParser_Initialize_IsFailure_EventMalformed) {
 | |
|   ProtocolParser parser;
 | |
|   EXPECT_FALSE(parser.IsInitialized());
 | |
| 
 | |
|   // Empty method is invalid.
 | |
|   EXPECT_TRUE(parser.Initialize("{\"method\":\"\"}"));
 | |
|   EXPECT_TRUE(parser.IsInitialized());
 | |
|   EXPECT_TRUE(parser.IsFailure());
 | |
| 
 | |
|   parser.Reset();
 | |
|   EXPECT_FALSE(parser.IsInitialized());
 | |
| 
 | |
|   // Unrecognized value is invalid.
 | |
|   EXPECT_TRUE(parser.Initialize("{\"method\":\"foo\",oops:false}"));
 | |
|   EXPECT_TRUE(parser.IsInitialized());
 | |
|   EXPECT_TRUE(parser.IsFailure());
 | |
| 
 | |
|   parser.Reset();
 | |
|   EXPECT_FALSE(parser.IsInitialized());
 | |
| 
 | |
|   // Params must be a dictionary.
 | |
|   EXPECT_TRUE(parser.Initialize("{\"method\":\",params:[]}"));
 | |
|   EXPECT_TRUE(parser.IsInitialized());
 | |
|   EXPECT_TRUE(parser.IsFailure());
 | |
| }
 | |
| 
 | |
| TEST(DevToolsUtil, ProtocolParser_Initialize_IsEvent) {
 | |
|   ProtocolParser parser;
 | |
|   EXPECT_FALSE(parser.IsInitialized());
 | |
| 
 | |
|   // Method without params is valid.
 | |
|   std::string message = "{\"method\":\"Test.myMethod\"}";
 | |
|   EXPECT_TRUE(parser.Initialize(message));
 | |
|   EXPECT_TRUE(parser.IsInitialized());
 | |
|   EXPECT_TRUE(parser.IsEvent());
 | |
|   EXPECT_STREQ("Test.myMethod", parser.method_.as_string().data());
 | |
|   EXPECT_TRUE(parser.params_.empty());
 | |
| 
 | |
|   parser.Reset();
 | |
|   EXPECT_FALSE(parser.IsInitialized());
 | |
| 
 | |
|   // Method with empty params dictionary is valid.
 | |
|   message = "{\"method\":\"Test.myMethod2\",\"params\":{}}";
 | |
|   EXPECT_TRUE(parser.Initialize(message));
 | |
|   EXPECT_TRUE(parser.IsInitialized());
 | |
|   EXPECT_TRUE(parser.IsEvent());
 | |
|   EXPECT_STREQ("Test.myMethod2", parser.method_.as_string().data());
 | |
|   EXPECT_STREQ("{}", parser.params_.as_string().data());
 | |
| 
 | |
|   parser.Reset();
 | |
|   EXPECT_FALSE(parser.IsInitialized());
 | |
| 
 | |
|   // Method with non-empty params dictionary is valid.
 | |
|   message = "{\"method\":\"Test.myMethod3\",\"params\":{\"foo\":\"bar\"}}";
 | |
|   EXPECT_TRUE(parser.Initialize(message));
 | |
|   EXPECT_TRUE(parser.IsInitialized());
 | |
|   EXPECT_TRUE(parser.IsEvent());
 | |
|   EXPECT_STREQ("Test.myMethod3", parser.method_.as_string().data());
 | |
|   EXPECT_STREQ("{\"foo\":\"bar\"}", parser.params_.as_string().data());
 | |
| }
 | |
| 
 | |
| TEST(DevToolsUtil, ProtocolParser_Initialize_IsFailure_ResultMalformed) {
 | |
|   ProtocolParser parser;
 | |
|   EXPECT_FALSE(parser.IsInitialized());
 | |
| 
 | |
|   // Empty ID is invalid.
 | |
|   EXPECT_TRUE(parser.Initialize("{\"id\":,result:{}}"));
 | |
|   EXPECT_TRUE(parser.IsInitialized());
 | |
|   EXPECT_TRUE(parser.IsFailure());
 | |
| 
 | |
|   parser.Reset();
 | |
|   EXPECT_FALSE(parser.IsInitialized());
 | |
| 
 | |
|   // Missing result or error value is invalid.
 | |
|   EXPECT_TRUE(parser.Initialize("{\"id\":1}"));
 | |
|   EXPECT_TRUE(parser.IsInitialized());
 | |
|   EXPECT_TRUE(parser.IsFailure());
 | |
| 
 | |
|   parser.Reset();
 | |
|   EXPECT_FALSE(parser.IsInitialized());
 | |
| 
 | |
|   // Unrecognized value is invalid.
 | |
|   EXPECT_TRUE(parser.Initialize("{\"id\":1,oops:false}"));
 | |
|   EXPECT_TRUE(parser.IsInitialized());
 | |
|   EXPECT_TRUE(parser.IsFailure());
 | |
| 
 | |
|   parser.Reset();
 | |
|   EXPECT_FALSE(parser.IsInitialized());
 | |
| 
 | |
|   // Result must be a dictionary.
 | |
|   EXPECT_TRUE(parser.Initialize("{\"id\":1,\"result\":[]}"));
 | |
|   EXPECT_TRUE(parser.IsInitialized());
 | |
|   EXPECT_TRUE(parser.IsFailure());
 | |
| 
 | |
|   parser.Reset();
 | |
|   EXPECT_FALSE(parser.IsInitialized());
 | |
| 
 | |
|   // Error must be a dictionary.
 | |
|   EXPECT_TRUE(parser.Initialize("{\"id\":1,\"error\":[]}"));
 | |
|   EXPECT_TRUE(parser.IsInitialized());
 | |
|   EXPECT_TRUE(parser.IsFailure());
 | |
| }
 | |
| 
 | |
| TEST(DevToolsUtil, ProtocolParser_Initialize_IsResult_Result) {
 | |
|   ProtocolParser parser;
 | |
|   EXPECT_FALSE(parser.IsInitialized());
 | |
| 
 | |
|   // Id with empty result dictionary is valid.
 | |
|   std::string message = "{\"id\":1,\"result\":{}}";
 | |
|   EXPECT_TRUE(parser.Initialize(message));
 | |
|   EXPECT_TRUE(parser.IsInitialized());
 | |
|   EXPECT_TRUE(parser.IsResult());
 | |
|   EXPECT_EQ(1, parser.message_id_);
 | |
|   EXPECT_TRUE(parser.success_);
 | |
|   EXPECT_STREQ("{}", parser.params_.as_string().data());
 | |
| 
 | |
|   parser.Reset();
 | |
|   EXPECT_FALSE(parser.IsInitialized());
 | |
| 
 | |
|   // Id with non-empty result dictionary is valid.
 | |
|   message = "{\"id\":2,\"result\":{\"foo\":\"bar\"}}";
 | |
|   EXPECT_TRUE(parser.Initialize(message));
 | |
|   EXPECT_TRUE(parser.IsInitialized());
 | |
|   EXPECT_TRUE(parser.IsResult());
 | |
|   EXPECT_EQ(2, parser.message_id_);
 | |
|   EXPECT_TRUE(parser.success_);
 | |
|   EXPECT_STREQ("{\"foo\":\"bar\"}", parser.params_.as_string().data());
 | |
| }
 | |
| 
 | |
| TEST(DevToolsUtil, ProtocolParser_Initialize_IsResult_Error) {
 | |
|   ProtocolParser parser;
 | |
|   EXPECT_FALSE(parser.IsInitialized());
 | |
| 
 | |
|   // Id with empty error dictionary is valid.
 | |
|   std::string message = "{\"id\":1,\"error\":{}}";
 | |
|   EXPECT_TRUE(parser.Initialize(message));
 | |
|   EXPECT_TRUE(parser.IsInitialized());
 | |
|   EXPECT_TRUE(parser.IsResult());
 | |
|   EXPECT_EQ(1, parser.message_id_);
 | |
|   EXPECT_FALSE(parser.success_);
 | |
|   EXPECT_STREQ("{}", parser.params_.as_string().data());
 | |
| 
 | |
|   parser.Reset();
 | |
|   EXPECT_FALSE(parser.IsInitialized());
 | |
| 
 | |
|   // Id with non-empty error dictionary is valid.
 | |
|   message = "{\"id\":2,\"error\":{\"foo\":\"bar\"}}";
 | |
|   EXPECT_TRUE(parser.Initialize(message));
 | |
|   EXPECT_TRUE(parser.IsInitialized());
 | |
|   EXPECT_TRUE(parser.IsResult());
 | |
|   EXPECT_EQ(2, parser.message_id_);
 | |
|   EXPECT_FALSE(parser.success_);
 | |
|   EXPECT_STREQ("{\"foo\":\"bar\"}", parser.params_.as_string().data());
 | |
| }
 |