File: arc_ui_availability_reporter_unittest.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (144 lines) | stat: -rw-r--r-- 5,643 bytes parent folder | download | duplicates (6)
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/ash/arc/arc_ui_availability_reporter.h"

#include "base/command_line.h"
#include "base/metrics/histogram_samples.h"
#include "base/metrics/statistics_recorder.h"
#include "chrome/browser/ash/app_list/arc/arc_app_list_prefs.h"
#include "chrome/browser/ash/app_list/arc/arc_app_test.h"
#include "chrome/test/base/testing_profile.h"
#include "chromeos/ash/experiences/arc/session/arc_bridge_service.h"
#include "chromeos/ash/experiences/arc/session/arc_service_manager.h"
#include "chromeos/ash/experiences/arc/test/arc_util_test_support.h"
#include "chromeos/ash/experiences/arc/test/fake_app_instance.h"
#include "chromeos/ash/experiences/arc/test/fake_intent_helper_host.h"
#include "chromeos/ash/experiences/arc/test/fake_intent_helper_instance.h"
#include "content/public/test/browser_task_environment.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace arc {

namespace {

using Mode = ArcUiAvailabilityReporter::Mode;

const base::HistogramBase* GetHistogram(Mode mode) {
  return base::StatisticsRecorder::FindHistogram(
      "Arc.UiAvailable." +
      ArcUiAvailabilityReporter::GetHistogramNameForMode(mode) +
      ".TimeDelta.Unmanaged");
}

int64_t ReadSingleStatistics(Mode mode) {
  const base::HistogramBase* histogram = GetHistogram(mode);
  DCHECK(histogram);

  std::unique_ptr<base::HistogramSamples> samples =
      histogram->SnapshotFinalDelta();
  DCHECK(samples.get());
  // Expected reported only once.
  DCHECK_EQ(1, samples->TotalCount());
  return samples->sum();
}

}  // namespace

class ArcUiAvailabilityReporterTest : public testing::Test {
 public:
  ArcUiAvailabilityReporterTest() = default;

  ArcUiAvailabilityReporterTest(const ArcUiAvailabilityReporterTest&) = delete;
  ArcUiAvailabilityReporterTest& operator=(
      const ArcUiAvailabilityReporterTest&) = delete;

  ~ArcUiAvailabilityReporterTest() override = default;

  void SetUp() override {
    testing::Test::SetUp();
    arc::SetArcAvailableCommandLineForTesting(
        base::CommandLine::ForCurrentProcess());
    // arc_service_manager_ = std::make_unique<arc::ArcServiceManager>();
    profile_ = TestingProfile::Builder().Build();

    // Use ArcAppTest to initialize infrastructure.
    arc_app_test_.set_activate_arc_on_start(false);
    arc_app_test_.SetUp(profile());
    app_instance_ = std::make_unique<FakeAppInstance>(
        arc_app_test_.arc_app_list_prefs() /* app_host */);
    intent_helper_instance_ = std::make_unique<FakeIntentHelperInstance>();
    intent_helper_host_ = std::make_unique<FakeIntentHelperHost>(
        arc_bridge_service()->intent_helper());
  }

  void TearDown() override {
    intent_helper_host_.reset();
    intent_helper_instance_.reset();
    app_instance_.reset();
    arc_app_test_.TearDown();
    profile_.reset();
    // arc_service_manager_.reset();
    testing::Test::TearDown();
  }

  TestingProfile* profile() { return profile_.get(); }

  ArcBridgeService* arc_bridge_service() {
    return ArcServiceManager::Get()->arc_bridge_service();
  }

  FakeAppInstance* app_instance() { return app_instance_.get(); }

  FakeIntentHelperInstance* intent_helper_instance() {
    return intent_helper_instance_.get();
  }

 private:
  content::BrowserTaskEnvironment task_environment_;
  // std::unique_ptr<arc::ArcServiceManager> arc_service_manager_;
  std::unique_ptr<TestingProfile> profile_;
  ArcAppTest arc_app_test_;
  std::unique_ptr<FakeIntentHelperHost> intent_helper_host_;
  std::unique_ptr<FakeAppInstance> app_instance_;
  std::unique_ptr<FakeIntentHelperInstance> intent_helper_instance_;
};

// Reporting is triggered in case both instances are connected.
TEST_F(ArcUiAvailabilityReporterTest, Basic) {
  ArcUiAvailabilityReporter reporter(profile(), Mode::kOobeProvisioning);
  // No reporting at this time.
  EXPECT_FALSE(GetHistogram(Mode::kOobeProvisioning));
  arc_bridge_service()->app()->SetInstance(app_instance());
  EXPECT_FALSE(GetHistogram(Mode::kOobeProvisioning));
  arc_bridge_service()->intent_helper()->SetInstance(intent_helper_instance());
  EXPECT_GE(ReadSingleStatistics(Mode::kOobeProvisioning), 0);
  EXPECT_FALSE(GetHistogram(Mode::kInSessionProvisioning));
  EXPECT_FALSE(GetHistogram(Mode::kAlreadyProvisioned));
}

// Reporting is triggered only once, even if instances are reconnected.
TEST_F(ArcUiAvailabilityReporterTest, TriggeredOnce) {
  ArcUiAvailabilityReporter reporter(profile(), Mode::kAlreadyProvisioned);
  arc_bridge_service()->app()->SetInstance(app_instance());
  arc_bridge_service()->intent_helper()->SetInstance(intent_helper_instance());
  arc_bridge_service()->app()->CloseInstance(app_instance());
  arc_bridge_service()->intent_helper()->CloseInstance(
      intent_helper_instance());
  arc_bridge_service()->app()->SetInstance(app_instance());
  arc_bridge_service()->intent_helper()->SetInstance(intent_helper_instance());
  EXPECT_GE(ReadSingleStatistics(Mode::kAlreadyProvisioned), 0);
}

// Reporting is triggered even if instance restart happened.
TEST_F(ArcUiAvailabilityReporterTest, InstanceRestartAllowed) {
  ArcUiAvailabilityReporter reporter(profile(), Mode::kInSessionProvisioning);
  arc_bridge_service()->app()->SetInstance(app_instance());
  arc_bridge_service()->app()->CloseInstance(app_instance());
  arc_bridge_service()->intent_helper()->SetInstance(intent_helper_instance());
  arc_bridge_service()->app()->SetInstance(app_instance());
  EXPECT_GE(ReadSingleStatistics(Mode::kInSessionProvisioning), 0);
}

}  // namespace arc