Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(77)

Side by Side Diff: chrome/browser/ui/app_list/arc/arc_app_unittest.cc

Issue 1413153007: arc-app-launcher: Minimal support for ARC app launcher. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Merged FakeArcBridgeService, comments addressed. Created 5 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2015 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.
4
5 #include <algorithm>
6 #include <map>
7 #include <string>
8 #include <vector>
9
10 #include "base/files/file_path.h"
11 #include "base/files/file_util.h"
12 #include "base/memory/scoped_vector.h"
13 #include "base/run_loop.h"
14 #include "base/strings/string_util.h"
15 #include "base/task_runner_util.h"
16 #include "chrome/browser/ui/app_list/app_list_test_util.h"
17 #include "chrome/browser/ui/app_list/arc/arc_app_icon.h"
18 #include "chrome/browser/ui/app_list/arc/arc_app_item.h"
19 #include "chrome/browser/ui/app_list/arc/arc_app_list_prefs.h"
20 #include "chrome/browser/ui/app_list/arc/arc_app_model_builder.h"
21 #include "chrome/browser/ui/app_list/test/test_app_list_controller_delegate.h"
22 #include "chrome/test/base/testing_profile.h"
23 #include "components/arc/arc_bridge_service.h"
24 #include "components/arc/test/fake_arc_bridge_service.h"
25 #include "content/public/browser/browser_thread.h"
26 #include "testing/gtest/include/gtest/gtest.h"
27 #include "ui/app_list/app_list_model.h"
28 #include "ui/gfx/image/image_skia.h"
29
30 namespace {
31
32 std::string GetAppId(const arc::AppInfo& app_info) {
33 return ArcAppListPrefs::GetAppId(app_info.package, app_info.activity);
34 }
35
36 } // namespace
37
38 class ArcAppModelBuilderTest : public AppListTestBase {
39 public:
40 ArcAppModelBuilderTest() {}
41 ~ArcAppModelBuilderTest() override {
42 // Release profile file in order to keep right sequence.
43 profile_.reset();
44 }
45
46 void SetUp() override {
47 AppListTestBase::SetUp();
48
49 // Make sure we have enough data for test.
50 for (int i = 0; i < 3; ++i) {
51 arc::AppInfo app;
52 char buffer[16];
53 base::snprintf(buffer, arraysize(buffer), "Fake App %d", i);
54 app.name = buffer;
55 base::snprintf(buffer, arraysize(buffer), "fake.app.%d", i);
56 app.package = buffer;
57 base::snprintf(buffer, arraysize(buffer), "fake.app.%d.activity", i);
58 app.activity = buffer;
59 fake_apps_.push_back(app);
60 }
61
62 bridge_service_.reset(new arc::FakeArcBridgeService());
63
64 // Check initial conditions.
65 EXPECT_EQ(bridge_service_.get(), arc::ArcBridgeService::Get());
66 EXPECT_EQ(true, !arc::ArcBridgeService::Get()->available());
67 EXPECT_EQ(arc::ArcBridgeService::State::STOPPED,
68 arc::ArcBridgeService::Get()->state());
69
70 CreateBuilder();
71
72 // At this point we should have ArcAppListPrefs as observer of service.
73 EXPECT_EQ(
74 true,
75 bridge_service()->HasObserver(ArcAppListPrefs::Get(profile_.get())));
76 }
77
78 void TearDown() override { ResetBuilder(); }
79
80 protected:
81 // Creates a new builder, destroying any existing one.
82 void CreateBuilder() {
83 ResetBuilder(); // Destroy any existing builder in the correct order.
84
85 model_.reset(new app_list::AppListModel);
86 controller_.reset(new test::TestAppListControllerDelegate);
87 builder_.reset(new ArcAppModelBuilder(controller_.get()));
88 builder_->InitializeWithProfile(profile_.get(), model_.get());
89 }
90
91 void ResetBuilder() {
92 builder_.reset();
93 controller_.reset();
94 model_.reset();
95 }
96
97 size_t GetArcItemCount() const {
98 size_t arc_count = 0;
99 const size_t count = model_->top_level_item_list()->item_count();
100 for (size_t i = 0; i < count; ++i) {
101 app_list::AppListItem* item = model_->top_level_item_list()->item_at(i);
102 if (item->GetItemType() == ArcAppItem::kItemType) {
103 ++arc_count;
104 }
105 }
106 return arc_count;
107 }
108
109 ArcAppItem* GetArcItem(size_t index) const {
110 size_t arc_count = 0;
111 const size_t count = model_->top_level_item_list()->item_count();
112 ArcAppItem* arc_item = nullptr;
113 for (size_t i = 0; i < count; ++i) {
114 app_list::AppListItem* item = model_->top_level_item_list()->item_at(i);
115 if (item->GetItemType() == ArcAppItem::kItemType) {
116 if (arc_count++ == index) {
117 arc_item = reinterpret_cast<ArcAppItem*>(item);
118 break;
119 }
120 }
121 }
122 EXPECT_NE(nullptr, arc_item);
123 return arc_item;
124 }
125
126 ArcAppItem* FindArcItem(const std::string& id) const {
127 const size_t count = GetArcItemCount();
128 for (size_t i = 0; i < count; ++i) {
129 ArcAppItem* item = GetArcItem(i);
130 if (item && item->id() == id) {
131 return item;
132 }
133 }
134 return nullptr;
135 }
136
137 // Validate that prefs and model have right content.
138 void ValidateHaveApps(const std::vector<arc::AppInfo> apps) {
139 ArcAppListPrefs* prefs = ArcAppListPrefs::Get(profile_.get());
140 const std::vector<std::string> ids = prefs->GetAppIds();
141 ASSERT_EQ(apps.size(), ids.size());
142 ASSERT_EQ(apps.size(), GetArcItemCount());
143 // In principle, order of items is not defined.
144 for (auto& app : apps) {
145 const std::string id = GetAppId(app);
146 EXPECT_NE(std::find(ids.begin(), ids.end(), id), ids.end());
147 scoped_ptr<ArcAppListPrefs::AppInfo> app_info = prefs->GetApp(id);
148 ASSERT_NE(nullptr, app_info.get());
149 EXPECT_EQ(app.name, app_info->name);
150 EXPECT_EQ(app.package, app_info->package);
151 EXPECT_EQ(app.activity, app_info->activity);
152
153 const ArcAppItem* app_item = FindArcItem(id);
154 ASSERT_NE(nullptr, app_item);
155 EXPECT_EQ(app.name, app_item->GetDisplayName());
156 }
157 }
158
159 // Validate that requested apps have required ready state and other apps have
160 // opposite state.
161 void ValidateAppReadyState(const std::vector<arc::AppInfo> apps, bool ready) {
162 ArcAppListPrefs* prefs = ArcAppListPrefs::Get(profile_.get());
163 ASSERT_NE(nullptr, prefs);
164
165 std::vector<std::string> ids = prefs->GetAppIds();
166 EXPECT_EQ(ids.size(), GetArcItemCount());
167
168 // Process requested apps.
169 for (auto& app : apps) {
170 const std::string id = GetAppId(app);
171 std::vector<std::string>::iterator it_id = std::find(ids.begin(),
172 ids.end(),
173 id);
174 ASSERT_NE(it_id, ids.end());
175 ids.erase(it_id);
176
177 scoped_ptr<ArcAppListPrefs::AppInfo> app_info = prefs->GetApp(id);
178 ASSERT_NE(nullptr, app_info.get());
179 EXPECT_EQ(ready, app_info->ready);
180 const ArcAppItem* app_item = FindArcItem(id);
181 ASSERT_NE(nullptr, app_item);
182 EXPECT_EQ(ready, app_item->ready());
183 }
184
185 // Process the rest of the apps.
186 for (auto& id : ids) {
187 scoped_ptr<ArcAppListPrefs::AppInfo> app_info = prefs->GetApp(id);
188 ASSERT_NE(nullptr, app_info.get());
189 EXPECT_NE(ready, app_info->ready);
190 const ArcAppItem* app_item = FindArcItem(id);
191 ASSERT_NE(nullptr, app_item);
192 EXPECT_NE(ready, app_item->ready());
193 }
194
195 }
196
197 arc::FakeArcBridgeService* bridge_service() { return bridge_service_.get(); }
198
199 const std::vector<arc::AppInfo>& fake_apps() const { return fake_apps_; }
200
201 private:
202 scoped_ptr<app_list::AppListModel> model_;
203 scoped_ptr<test::TestAppListControllerDelegate> controller_;
204 scoped_ptr<ArcAppModelBuilder> builder_;
205 scoped_ptr<arc::FakeArcBridgeService> bridge_service_;
206 std::vector<arc::AppInfo> fake_apps_;
207
208 DISALLOW_COPY_AND_ASSIGN(ArcAppModelBuilderTest);
209 };
210
211 TEST_F(ArcAppModelBuilderTest, RefreshAllOnReady) {
212 EXPECT_EQ(0, bridge_service()->refresh_app_list_count());
213 bridge_service()->SetReady();
214 EXPECT_EQ(1, bridge_service()->refresh_app_list_count());
215 }
216
217 TEST_F(ArcAppModelBuilderTest, RefreshAllFillsContent) {
218 ValidateHaveApps(std::vector<arc::AppInfo>());
219 bridge_service()->SetReady();
220 bridge_service()->SendRefreshAppList(fake_apps());
221 ValidateHaveApps(fake_apps());
222 }
223
224 TEST_F(ArcAppModelBuilderTest, MultipleRefreshAll) {
225 ValidateHaveApps(std::vector<arc::AppInfo>());
226 bridge_service()->SetReady();
227 // Send info about all fake apps except last.
228 std::vector<arc::AppInfo> apps1(fake_apps().begin(), fake_apps().end() - 1);
229 bridge_service()->SendRefreshAppList(apps1);
230 // At this point all apps (except last) should exist and be ready.
231 ValidateHaveApps(apps1);
232 ValidateAppReadyState(apps1, true);
233
234 // Send info about all fake apps except first.
235 std::vector<arc::AppInfo> apps2(fake_apps().begin() + 1, fake_apps().end());
236 bridge_service()->SendRefreshAppList(apps2);
237 // At this point all apps should exist but first one should be non-ready.
238 ValidateHaveApps(fake_apps());
239 ValidateAppReadyState(apps2, true);
240
241 // Send info about all fake apps.
242 bridge_service()->SendRefreshAppList(fake_apps());
243 // At this point all apps should exist and be ready.
244 ValidateHaveApps(fake_apps());
245 ValidateAppReadyState(fake_apps(), true);
246
247 // Send info no app available.
248 bridge_service()->SendRefreshAppList(std::vector<arc::AppInfo>());
249 // At this point all apps should exist and be non-ready.
250 ValidateHaveApps(fake_apps());
251 ValidateAppReadyState(fake_apps(), false);
252 }
253
254 TEST_F(ArcAppModelBuilderTest, StopServiceDisablesApps) {
255 ArcAppListPrefs* prefs = ArcAppListPrefs::Get(profile_.get());
256 ASSERT_NE(nullptr, prefs);
257
258 bridge_service()->SetReady();
259 EXPECT_EQ(static_cast<size_t>(0), GetArcItemCount());
260 EXPECT_EQ(static_cast<size_t>(0), prefs->GetAppIds().size());
261
262 bridge_service()->SendRefreshAppList(fake_apps());
263 std::vector<std::string> ids = prefs->GetAppIds();
264 EXPECT_EQ(fake_apps().size(), ids.size());
265 ValidateAppReadyState(fake_apps(), true);
266
267 // Stopping service does not delete items. It makes them non-ready.
268 bridge_service()->SetStopped();
269 // Ids should be the same.
270 EXPECT_EQ(ids, prefs->GetAppIds());
271 ValidateAppReadyState(fake_apps(), false);
272 }
273
274 TEST_F(ArcAppModelBuilderTest, LaunchApps) {
275 bridge_service()->SetReady();
276 bridge_service()->SendRefreshAppList(fake_apps());
277
278 // Simulate item activate.
279 const arc::AppInfo& app_first = fake_apps()[0];
280 const arc::AppInfo& app_last = fake_apps()[0];
281 ArcAppItem* item_first = FindArcItem(GetAppId(app_first));
282 ArcAppItem* item_last = FindArcItem(GetAppId(app_last));
283 ASSERT_NE(nullptr, item_first);
284 ASSERT_NE(nullptr, item_last);
285 item_first->Activate(0);
286 item_last->Activate(0);
287 item_first->Activate(0);
288
289 const ScopedVector<arc::FakeArcBridgeService::Request>& launch_requests =
290 bridge_service()->launch_requests();
291 EXPECT_EQ(static_cast<size_t>(3), launch_requests.size());
292 EXPECT_EQ(true, launch_requests[0]->IsForApp(app_first));
293 EXPECT_EQ(true, launch_requests[1]->IsForApp(app_last));
294 EXPECT_EQ(true, launch_requests[2]->IsForApp(app_first));
295 }
296
297 TEST_F(ArcAppModelBuilderTest, RequestIcons) {
298 // Make sure we are on UI thread.
299 ASSERT_EQ(true,
300 content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
301
302 bridge_service()->SetReady();
303 bridge_service()->SendRefreshAppList(fake_apps());
304
305 // Validate that no icon exists at the beginning and request icon for
306 // each supported scale factor. This will start asynchronous loading.
307 uint32_t expected_mask = 0;
308 const std::vector<ui::ScaleFactor>& scale_factors =
309 ui::GetSupportedScaleFactors();
310 for (auto& scale_factor : scale_factors) {
311 expected_mask |= 1 << scale_factor;
312 for (auto& app : fake_apps()) {
313 ArcAppItem* app_item = FindArcItem(GetAppId(app));
314 ASSERT_NE(nullptr, app_item);
315 const float scale = ui::GetScaleForScaleFactor(scale_factor);
316 app_item->icon().GetRepresentation(scale);
317 }
318 }
319
320 // Process pending tasks.
321 content::BrowserThread::GetBlockingPool()->FlushForTesting();
322 base::RunLoop().RunUntilIdle();
323
324 // At this moment we should receive all requests for icon loading.
325 const ScopedVector<arc::FakeArcBridgeService::IconRequest>& icon_requests =
326 bridge_service()->icon_requests();
327 EXPECT_EQ(scale_factors.size() * fake_apps().size(), icon_requests.size());
328 std::map<std::string, uint32_t> app_masks;
329 for (size_t i = 0; i < icon_requests.size(); ++i) {
330 const arc::FakeArcBridgeService::IconRequest* icon_request =
331 icon_requests[i];
332 const std::string id = ArcAppListPrefs::GetAppId(icon_request->package(),
333 icon_request->activity());
334 // Make sure no double requests.
335 EXPECT_NE(app_masks[id],
336 app_masks[id] | (1 << icon_request->scale_factor()));
337 app_masks[id] |= (1 << icon_request->scale_factor());
338 }
339
340 // Validate that we have a request for each icon for each supported scale
341 // factor.
342 EXPECT_EQ(fake_apps().size(), app_masks.size());
343 for (auto& app : fake_apps()) {
344 const std::string id = GetAppId(app);
345 ASSERT_NE(app_masks.find(id), app_masks.end());
346 EXPECT_EQ(app_masks[id], expected_mask);
347 }
348 }
349
350 TEST_F(ArcAppModelBuilderTest, InstallIcon) {
351 // Make sure we are on UI thread.
352 ASSERT_EQ(true,
353 content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
354
355
356 bridge_service()->SetReady();
357 bridge_service()->SendRefreshAppList(std::vector<arc::AppInfo>(
358 fake_apps().begin(), fake_apps().begin() + 1));
359 const arc::AppInfo& app = fake_apps()[0];
360
361 ArcAppListPrefs* prefs = ArcAppListPrefs::Get(profile_.get());
362 ASSERT_NE(nullptr, prefs);
363
364 const ui::ScaleFactor scale_factor = ui::GetSupportedScaleFactors()[0];
365 const float scale = ui::GetScaleForScaleFactor(scale_factor);
366 const base::FilePath icon_path = prefs->GetIconPath(GetAppId(app),
367 scale_factor);
368 EXPECT_EQ(true, !base::PathExists(icon_path));
369
370 const ArcAppItem* app_item = FindArcItem(GetAppId(app));
371 EXPECT_NE(nullptr, app_item);
372 // This initiates async loading.
373 app_item->icon().GetRepresentation(scale);
374
375 // Process pending tasks.
376 content::BrowserThread::GetBlockingPool()->FlushForTesting();
377 base::RunLoop().RunUntilIdle();
378
379 // Validating decoded content does not fit well for unit tests.
380 ArcAppIcon::DisableDecodingForTesting();
381
382 // Now send generated icon for the app.
383 std::string png_data;
384 EXPECT_EQ(true, bridge_service()->GenerateAndSendIcon(
385 app,
386 static_cast<arc::ScaleFactor>(scale_factor),
387 &png_data));
388
389 // Process pending tasks.
390 content::BrowserThread::GetBlockingPool()->FlushForTesting();
391 base::RunLoop().RunUntilIdle();
392
393 // Validate that icons are installed, have right content and icon is
394 // refreshed for ARC app item.
395 EXPECT_EQ(true, base::PathExists(icon_path));
396
397 std::string icon_data;
398 // Read the file from disk and compare with reference data.
399 EXPECT_EQ(true, base::ReadFileToString(icon_path, &icon_data));
400 ASSERT_EQ(icon_data, png_data);
401 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698