File: boarding_pass_detector_unittest.cc

package info (click to toggle)
chromium 138.0.7204.157-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,864 kB
  • sloc: cpp: 34,936,859; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,967; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (117 lines) | stat: -rw-r--r-- 3,865 bytes parent folder | download | duplicates (4)
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
// Copyright 2023 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/wallet/android/boarding_pass_detector.h"
#include "chrome/common/wallet/boarding_pass_extractor.mojom.h"

#include "base/test/scoped_feature_list.h"
#include "base/test/task_environment.h"
#include "base/test/test_future.h"
#include "chrome/common/chrome_features.h"
#include "mojo/public/cpp/bindings/receiver.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace wallet {

class FakeBoardingPassExtractor : public mojom::BoardingPassExtractor {
 public:
  mojo::PendingRemote<mojom::BoardingPassExtractor> BindAndPassRemote() {
    return receiver_.BindNewPipeAndPassRemote();
  }

  // mojom::BoardingPassExtractor
  void ExtractBoardingPass(ExtractBoardingPassCallback callback) override {
    CHECK(!HasPendingRequest());
    callback_ = std::move(callback);
    if (!signal_.IsReady()) {
      signal_.SetValue();
    }
  }

  void WaitForRequest() {
    if (HasPendingRequest()) {
      return;
    }
    signal_.Clear();
    EXPECT_TRUE(signal_.Wait());
  }

  void AnswerRequest(const std::vector<std::string>& results) {
    CHECK(HasPendingRequest());
    std::move(callback_).Run(results);
  }

  void ResetReceiver() { receiver_.reset(); }

 private:
  bool HasPendingRequest() const { return bool(callback_); }
  base::test::TestFuture<void> signal_;

  ExtractBoardingPassCallback callback_;
  mojo::Receiver<mojom::BoardingPassExtractor> receiver_{this};
};

class BoardingPassDetectorTest : public ::testing::Test {
 public:
  void SetAllowList(const std::string& allowlist) {
    feature_list_.InitAndEnableFeatureWithParameters(
        features::kBoardingPassDetector,
        {{features::kBoardingPassDetectorUrlParam.name, allowlist}});
  }

 private:
  base::test::ScopedFeatureList feature_list_;
  base::test::TaskEnvironment task_env_;
};

TEST_F(BoardingPassDetectorTest, ShouldDetect) {
  SetAllowList("https://aa.com/adc, https://www.google.com/boarding");

  EXPECT_TRUE(BoardingPassDetector::ShouldDetect("https://aa.com/adc"));
  EXPECT_TRUE(BoardingPassDetector::ShouldDetect(
      "https://www.google.com/boarding/abc"));
}

TEST_F(BoardingPassDetectorTest, ShouldNotDetect) {
  SetAllowList("https://aa.com/adc, https://www.google.com/boarding");

  EXPECT_FALSE(BoardingPassDetector::ShouldDetect("https://aa.com/"));
  EXPECT_FALSE(
      BoardingPassDetector::ShouldDetect("https://www.google.com/abc"));
}

TEST_F(BoardingPassDetectorTest, DetectBoardingPassSuccess) {
  FakeBoardingPassExtractor boarding_pass_extractor;
  base::test::TestFuture<const std::vector<std::string>&> test_future;
  BoardingPassDetector* detector = new BoardingPassDetector();
  detector->DetectBoardingPassWithRemote(
      mojo::Remote<mojom::BoardingPassExtractor>(
          boarding_pass_extractor.BindAndPassRemote()),
      test_future.GetCallback());
  boarding_pass_extractor.WaitForRequest();

  std::vector<std::string> extractor_response;
  extractor_response.push_back("test1");
  boarding_pass_extractor.AnswerRequest(extractor_response);

  auto results = test_future.Get();
  EXPECT_EQ(results.size(), extractor_response.size());
  EXPECT_EQ(results[0], extractor_response[0]);
}

TEST_F(BoardingPassDetectorTest, DetectBoardingPassDisconnect) {
  FakeBoardingPassExtractor boarding_pass_extractor;
  base::test::TestFuture<const std::vector<std::string>&> test_future;
  BoardingPassDetector* detector = new BoardingPassDetector();
  detector->DetectBoardingPassWithRemote(
      mojo::Remote<mojom::BoardingPassExtractor>(
          boarding_pass_extractor.BindAndPassRemote()),
      test_future.GetCallback());
  boarding_pass_extractor.ResetReceiver();

  auto results = test_future.Get();
  EXPECT_EQ(results.size(), 0u);
}

}  // namespace wallet