Flutter Engine
fl_test.cc
Go to the documentation of this file.
1 // Copyright 2013 The Flutter 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.
4 
5 #include "gtest/gtest.h"
6 
7 #include "flutter/shell/platform/linux/testing/fl_test.h"
8 
9 #include "flutter/shell/platform/linux/fl_engine_private.h"
10 #include "flutter/shell/platform/linux/testing/mock_renderer.h"
11 
12 static uint8_t hex_digit_to_int(char value) {
13  if (value >= '0' && value <= '9') {
14  return value - '0';
15  } else if (value >= 'a' && value <= 'f') {
16  return value - 'a' + 10;
17  } else if (value >= 'F' && value <= 'F') {
18  return value - 'A' + 10;
19  } else {
20  return 0;
21  }
22 }
23 
24 static uint8_t parse_hex8(const gchar* hex_string) {
25  if (hex_string[0] == '\0') {
26  return 0x00;
27  }
28  return hex_digit_to_int(hex_string[0]) << 4 | hex_digit_to_int(hex_string[1]);
29 }
30 
31 GBytes* hex_string_to_bytes(const gchar* hex_string) {
32  GByteArray* buffer = g_byte_array_new();
33  for (int i = 0; hex_string[i] != '\0' && hex_string[i + 1] != '\0'; i += 2) {
34  uint8_t value = parse_hex8(hex_string + i);
35  g_byte_array_append(buffer, &value, 1);
36  }
37  return g_byte_array_free_to_bytes(buffer);
38 }
39 
40 gchar* bytes_to_hex_string(GBytes* bytes) {
41  GString* hex_string = g_string_new("");
42  size_t data_length;
43  const uint8_t* data =
44  static_cast<const uint8_t*>(g_bytes_get_data(bytes, &data_length));
45  for (size_t i = 0; i < data_length; i++) {
46  g_string_append_printf(hex_string, "%02x", data[i]);
47  }
48  return g_string_free(hex_string, FALSE);
49 }
50 
51 FlEngine* make_mock_engine() {
52  g_autoptr(FlDartProject) project = fl_dart_project_new();
53  return make_mock_engine_with_project(project);
54 }
55 
56 FlEngine* make_mock_engine_with_project(FlDartProject* project) {
57  g_autoptr(FlMockRenderer) renderer = fl_mock_renderer_new();
58  g_autoptr(FlEngine) engine = fl_engine_new(project, FL_RENDERER(renderer));
59  g_autoptr(GError) engine_error = nullptr;
60  EXPECT_TRUE(fl_engine_start(engine, &engine_error));
61  EXPECT_EQ(engine_error, nullptr);
62 
63  return static_cast<FlEngine*>(g_object_ref(engine));
64 }
FlMockRenderer * fl_mock_renderer_new()
gchar * bytes_to_hex_string(GBytes *bytes)
Definition: fl_test.cc:40
FlEngine * make_mock_engine()
Definition: fl_test.cc:51
uint8_t value
gboolean fl_engine_start(FlEngine *self, GError **error)
Definition: fl_engine.cc:403
static uint8_t parse_hex8(const gchar *hex_string)
Definition: fl_test.cc:24
GBytes * hex_string_to_bytes(const gchar *hex_string)
Definition: fl_test.cc:31
static uint8_t hex_digit_to_int(char value)
Definition: fl_test.cc:12
FlEngine * fl_engine_new(FlDartProject *project, FlRenderer *renderer)
Definition: fl_engine.cc:388
static const uint8_t buffer[]
G_MODULE_EXPORT FlDartProject * fl_dart_project_new()
FlEngine * make_mock_engine_with_project(FlDartProject *project)
Definition: fl_test.cc:56
return FALSE