File: intent_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 (136 lines) | stat: -rw-r--r-- 5,515 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
// Copyright 2021 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/app_list/arc/intent.h"

#include <string>

#include "chrome/browser/ash/app_list/arc/arc_app_utils.h"
#include "chrome/browser/ui/ash/shelf/arc_app_shelf_id.h"
#include "chromeos/ash/experiences/arc/app/arc_app_constants.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace arc {

namespace {

constexpr char kTestPackage[] = "com.test.app";
constexpr char kTestActivity[] = "com.test.app.some.activity";
constexpr char kTestShelfGroupId[] = "some_shelf_group";
constexpr char kIntentWithShelfGroupId[] =
    "#Intent;launchFlags=0x18001000;"
    "component=com.test.app/com.test.app.some.activity;"
    "S.org.chromium.arc.shelf_group_id=some_shelf_group;end";

std::string GetPlayStoreInitialLaunchIntent() {
  return GetLaunchIntent(kPlayStorePackage, kPlayStoreActivity,
                         {kInitialStartParam});
}

}  // namespace

using ArcIntentTest = testing::Test;

TEST_F(ArcIntentTest, LaunchIntent) {
  const std::string launch_intent = GetPlayStoreInitialLaunchIntent();

  auto intent1 = Intent::Get(launch_intent);
  ASSERT_TRUE(intent1);
  EXPECT_EQ(intent1->action(), "android.intent.action.MAIN");
  EXPECT_EQ(intent1->category(), "android.intent.category.LAUNCHER");
  EXPECT_EQ(intent1->package_name(), kPlayStorePackage);
  EXPECT_EQ(intent1->activity(), kPlayStoreActivity);
  EXPECT_EQ(intent1->launch_flags(),
            Intent::FLAG_ACTIVITY_NEW_TASK |
                Intent::FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
  ASSERT_EQ(intent1->extra_params().size(), 1U);
  EXPECT_TRUE(intent1->HasExtraParam(kInitialStartParam));

  auto intent2 = Intent::Get(kIntentWithShelfGroupId);
  ASSERT_TRUE(intent2);
  EXPECT_EQ(intent2->action(), "");
  EXPECT_EQ(intent2->category(), "");
  EXPECT_EQ(intent2->package_name(), kTestPackage);
  EXPECT_EQ(intent2->activity(), kTestActivity);
  EXPECT_EQ(intent2->launch_flags(), Intent::FLAG_ACTIVITY_NEW_TASK |
                                         Intent::FLAG_RECEIVER_NO_ABORT |
                                         Intent::FLAG_ACTIVITY_LAUNCH_ADJACENT);
  ASSERT_EQ(intent2->extra_params().size(), 1U);
  EXPECT_TRUE(intent2->HasExtraParam(
      "S.org.chromium.arc.shelf_group_id=some_shelf_group"));
}

TEST_F(ArcIntentTest, AppendLaunchIntent) {
  const std::string param1 =
      CreateIntentTicksExtraParam(kRequestDeferredStartTimeParamKey,
                                  base::TimeTicks() + base::Milliseconds(100));
  const std::string param2 = CreateIntentTicksExtraParam(
      kRequestStartTimeParamKey, base::TimeTicks() + base::Milliseconds(200));
  const std::string launch_intent =
      AppendLaunchIntent(GetPlayStoreInitialLaunchIntent(), {param1, param2});

  auto intent = Intent::Get(launch_intent);
  ASSERT_TRUE(intent);
  EXPECT_EQ(intent->action(), "android.intent.action.MAIN");
  EXPECT_EQ(intent->category(), "android.intent.category.LAUNCHER");
  EXPECT_EQ(intent->package_name(), kPlayStorePackage);
  EXPECT_EQ(intent->activity(), kPlayStoreActivity);
  EXPECT_EQ(intent->launch_flags(),
            Intent::FLAG_ACTIVITY_NEW_TASK |
                Intent::FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
  ASSERT_EQ(intent->extra_params().size(), 3U);
  EXPECT_TRUE(intent->HasExtraParam(kInitialStartParam));
  std::string deferred_time;
  EXPECT_TRUE(intent->GetExtraParamValue(kRequestDeferredStartTimeParamKey,
                                         &deferred_time));
  EXPECT_EQ("100", deferred_time);
  std::string start_time;
  EXPECT_TRUE(
      intent->GetExtraParamValue(kRequestStartTimeParamKey, &start_time));
  EXPECT_EQ("200", start_time);
}

TEST_F(ArcIntentTest, CreateIntentTicksExtraParam) {
  EXPECT_EQ("S.org.chromium.arc.request.deferred.start=12345",
            CreateIntentTicksExtraParam(
                "S.org.chromium.arc.request.deferred.start",
                base::TimeTicks() + base::Milliseconds(12345)));
}

TEST_F(ArcIntentTest, ShelfGroupId) {
  const std::string intent_with_shelf_group_id(kIntentWithShelfGroupId);
  const std::string shelf_app_id =
      ArcAppListPrefs::GetAppId(kTestPackage, kTestActivity);
  ArcAppShelfId shelf_id1 = ArcAppShelfId::FromIntentAndAppId(
      intent_with_shelf_group_id, shelf_app_id);
  EXPECT_TRUE(shelf_id1.has_shelf_group_id());
  EXPECT_EQ(shelf_id1.shelf_group_id(), kTestShelfGroupId);
  EXPECT_EQ(shelf_id1.app_id(), shelf_app_id);

  ArcAppShelfId shelf_id2 = ArcAppShelfId::FromIntentAndAppId(
      GetPlayStoreInitialLaunchIntent(), kPlayStoreAppId);
  EXPECT_FALSE(shelf_id2.has_shelf_group_id());
  EXPECT_EQ(shelf_id2.app_id(), kPlayStoreAppId);
}

TEST_F(ArcIntentTest, EmptyExtraParams) {
  auto intent =
      Intent::Get(GetLaunchIntent(kPlayStorePackage, kPlayStoreActivity, {}));
  ASSERT_TRUE(intent);
  EXPECT_TRUE(intent->extra_params().empty());
}

TEST_F(ArcIntentTest, WrongIntents) {
  // ;; is bad part here.
  EXPECT_FALSE(
      Intent::Get("#Intent;action=android.intent.action.MAIN;category=android."
                  "intent.category.LAUNCHER;launchFlags=0x10200000;component="
                  "com.android.vending/.AssetBrowserActivity;;end"));
  EXPECT_TRUE(
      Intent::Get("#Intent;action=android.intent.action.MAIN;category=android."
                  "intent.category.LAUNCHER;launchFlags=0x10200000;component="
                  "com.android.vending/.AssetBrowserActivity;end"));
}

}  // namespace arc