summaryrefslogtreecommitdiffstats
path: root/components/arc/test/fake_app_instance.cc
blob: f837a7ba3909a0d5cd97f229cc8a2795f98c99e4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
// Copyright 2015 The Chromium 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 "components/arc/test/fake_app_instance.h"

#include <stdint.h>

#include <string>
#include <vector>

#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/path_service.h"
#include "base/run_loop.h"
#include "mojo/common/common_type_converters.h"

namespace mojo {

template <>
struct TypeConverter<arc::AppInfoPtr, arc::AppInfo> {
  static arc::AppInfoPtr Convert(const arc::AppInfo& app_info) {
    return app_info.Clone();
  }
};

}  // namespace mojo

namespace arc {

FakeAppInstance::FakeAppInstance(AppHost* app_host)
    : binding_(this), app_host_(app_host) {}
FakeAppInstance::~FakeAppInstance() {}

void FakeAppInstance::RefreshAppList() {
  ++refresh_app_list_count_;
}

void FakeAppInstance::LaunchApp(const mojo::String& package_name,
                                const mojo::String& activity) {
  launch_requests_.push_back(new Request(package_name, activity));
}

void FakeAppInstance::RequestAppIcon(const mojo::String& package_name,
                                     const mojo::String& activity,
                                     ScaleFactor scale_factor) {
  icon_requests_.push_back(
      new IconRequest(package_name, activity, scale_factor));
}

void FakeAppInstance::SendRefreshAppList(const std::vector<AppInfo>& apps) {
  app_host_->OnAppListRefreshed(mojo::Array<AppInfoPtr>::From(apps));
}

bool FakeAppInstance::GenerateAndSendIcon(const AppInfo& app,
                                          ScaleFactor scale_factor,
                                          std::string* png_data_as_string) {
  CHECK(png_data_as_string != nullptr);
  std::string icon_file_name;
  switch (scale_factor) {
    case ScaleFactor::SCALE_FACTOR_100P:
      icon_file_name = "icon_100p.png";
      break;
    case ScaleFactor::SCALE_FACTOR_125P:
      icon_file_name = "icon_125p.png";
      break;
    case ScaleFactor::SCALE_FACTOR_133P:
      icon_file_name = "icon_133p.png";
      break;
    case ScaleFactor::SCALE_FACTOR_140P:
      icon_file_name = "icon_140p.png";
      break;
    case ScaleFactor::SCALE_FACTOR_150P:
      icon_file_name = "icon_150p.png";
      break;
    case ScaleFactor::SCALE_FACTOR_180P:
      icon_file_name = "icon_180p.png";
      break;
    case ScaleFactor::SCALE_FACTOR_200P:
      icon_file_name = "icon_200p.png";
      break;
    case ScaleFactor::SCALE_FACTOR_250P:
      icon_file_name = "icon_250p.png";
      break;
    case ScaleFactor::SCALE_FACTOR_300P:
      icon_file_name = "icon_300p.png";
      break;
    default:
      NOTREACHED();
      return false;
  }

  base::FilePath base_path;
  CHECK(PathService::Get(base::DIR_SOURCE_ROOT, &base_path));
  base::FilePath icon_file_path = base_path.AppendASCII("components")
                                      .AppendASCII("test")
                                      .AppendASCII("data")
                                      .AppendASCII("arc")
                                      .AppendASCII(icon_file_name);
  CHECK(base::PathExists(icon_file_path));
  CHECK(base::ReadFileToString(icon_file_path, png_data_as_string));

  app_host_->OnAppIcon(app.package_name, app.activity, scale_factor,
                       mojo::Array<uint8_t>::From(*png_data_as_string));

  return true;
}

void FakeAppInstance::WaitForIncomingMethodCall() {
  binding_.WaitForIncomingMethodCall();
}

void FakeAppInstance::WaitForOnAppInstanceReady() {
  // Several messages are sent back and forth when OnAppInstanceReady() is
  // called. Normally, it would be preferred to use a single
  // WaitForIncomingMethodCall() to wait for each method individually, but
  // QueryVersion() does require processing on the I/O thread, so
  // RunUntilIdle() is required to correctly dispatch it. On slower machines
  // (and when running under Valgrind), the two thread hops needed to send and
  // dispatch each Mojo message might not be picked up by a single
  // RunUntilIdle(), so keep pumping the message loop until all expected
  // messages are.
  while (refresh_app_list_count_ != 1) {
    base::RunLoop().RunUntilIdle();
  }
}

}  // namespace arc