aboutsummaryrefslogtreecommitdiff
path: root/test/background/mock/MockTabPresenter.ts
blob: 22fb947e66d89f676fe4deb6794b281ab0d44195 (plain) (blame)
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import TabPresenter from "../../../src/background/presenters/TabPresenter";

export default class MockTabPresenter implements TabPresenter {
  private readonly tabs: browser.tabs.Tab[] = [];
  private readonly zooms: number[] = [];
  private nextid = 0;

  private readonly lastSelectedId: number | undefined;

  private static defaultTabOptions = {
    hidden: false,
    highlighted: false,
    incognito: false,
    isArticle: false,
    isInReaderMode: false,
    lastAccessed: 0,
    pinned: false,
    selected: false,
    windowId: 0,
  };

  create(
    url: string,
    opts?: {
      active?: boolean;
      cookieStoreId?: string;
      index?: number;
      openerTabId?: number;
      pinned?: boolean;
      windowId?: number;
    }
  ): Promise<browser.tabs.Tab> {
    const tab = {
      ...MockTabPresenter.defaultTabOptions,
      ...opts,
      id: this.nextid++,
      active: false,
      title: "welcome, world",
      url,
      index: this.tabs.length,
    };
    if (opts?.active || this.tabs.length === 0) {
      this.tabs.forEach((t) => (t.active = false));
      tab.active = true;
    }
    this.tabs.push(tab);
    this.zooms.push(1);
    return Promise.resolve(tab);
  }

  duplicate(id: number): Promise<browser.tabs.Tab> {
    const src = this.tabs.find((t) => t.id === id);
    if (!src) {
      throw new Error(`tab ${id} not found`);
    }
    this.tabs.forEach((t) => (t.active = false));
    const tab = { ...src, id: this.nextid++, active: true };
    this.tabs.push(tab);
    this.zooms.push(1);

    return Promise.resolve(tab);
  }

  getAll(): Promise<browser.tabs.Tab[]> {
    return Promise.resolve([...this.tabs]);
  }

  getByKeyword(
    keyword: string,
    excludePinned: boolean
  ): Promise<browser.tabs.Tab[]> {
    const tabs = this.tabs

      .filter((t) => {
        return (
          (t.url && t.url.toLowerCase().includes(keyword.toLowerCase())) ||
          (t.title && t.title.toLowerCase().includes(keyword.toLowerCase()))
        );
      })
      .filter((t) => {
        return !(excludePinned && t.pinned);
      });
    return Promise.resolve(tabs);
  }

  getCurrent(): Promise<browser.tabs.Tab> {
    const tab = this.tabs.find((t) => t.active);
    if (!tab) {
      throw new Error("active tab not found");
    }
    return Promise.resolve(tab);
  }

  getLastSelectedId(): Promise<number | undefined> {
    return Promise.resolve(this.lastSelectedId);
  }

  getZoom(tabId: number): Promise<number> {
    const index = this.tabs.findIndex((t) => t.id === tabId);
    if (index === -1) {
      throw new Error(`tab ${tabId} not found`);
    }
    return Promise.resolve(this.zooms[index]);
  }

  onSelected(
    _listener: (arg: { tabId: number; windowId: number }) => void
  ): void {
    throw new Error("not implemented");
  }

  open(url: string, tabId?: number): Promise<browser.tabs.Tab> {
    let tab = this.tabs.find((t) => t.active);
    if (!tab) {
      throw new Error(`active tab not found`);
    }
    if (tabId !== undefined) {
      tab = this.tabs.find((t) => t.id === tabId);
    }
    if (!tab) {
      throw new Error(`tab ${tabId} not found`);
    }
    tab.url = url;
    return Promise.resolve(tab);
  }

  reload(_tabId: number, _cache: boolean): Promise<void> {
    throw new Error("not implemented");
  }

  remove(ids: number[]): Promise<void> {
    for (const id of ids) {
      const index = this.tabs.findIndex((t) => t.id === id);
      if (index === -1) {
        throw new Error(`tab ${id} not found`);
      }
      const tab = this.tabs[index];
      this.tabs.splice(index, 1);
      this.zooms.splice(index, 1);
      if (tab.active) {
        this.tabs[Math.min(index, this.tabs.length - 1)].active = true;
      }
    }

    return Promise.resolve(undefined);
  }

  reopen(): Promise<void> {
    throw new Error("not implemented");
  }

  select(tabId: number): Promise<void> {
    const tab = this.tabs.find((t) => t.id === tabId);
    if (!tab) {
      throw new Error(`tab ${tabId} not found`);
    }
    this.tabs.forEach((t) => (t.active = false));
    tab.active = true;
    return Promise.resolve(undefined);
  }

  setPinned(tabId: number, pinned: boolean): Promise<void> {
    const tab = this.tabs.find((t) => t.id === tabId);
    if (!tab) {
      throw new Error(`tab ${tabId} not found`);
    }
    tab.pinned = pinned;
    return Promise.resolve();
  }

  setZoom(tabId: number, factor: number): Promise<void> {
    const index = this.tabs.findIndex((t) => t.id === tabId);
    if (index === -1) {
      throw new Error(`tab ${tabId} not found`);
    }
    this.zooms[index] = factor;
    return Promise.resolve();
  }
}